Skip to content

Commit

Permalink
fix(dw): mainnet rate limit
Browse files Browse the repository at this point in the history
  • Loading branch information
javadkh2 committed Jan 16, 2025
1 parent e958fad commit 49a983c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/apps/dev-wallet/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const config = {
ACCOUNTS: {
// this need to be more intelligent; for now, we just set it to 5 seconds
SYNC_INTERVAL: 5 * 1000, // 5 seconds
RATE_LIMIT: 3000, // 3 second
},
SESSION: {
TTL: 30 * 60 * 1000, // 30 minutes
Expand Down
11 changes: 11 additions & 0 deletions packages/apps/dev-wallet/src/modules/account/account.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import type { IKeyItem, IKeySource } from '../wallet/wallet.repository';

import { config } from '@/config';
import * as transactionService from '@/modules/transaction/transaction.service';
import { sleep } from '@/utils/helpers';
import {
composePactCommand,
execution,
Expand Down Expand Up @@ -151,6 +152,10 @@ export const accountDiscovery = (
await emit('key-retrieved')(key);
const principal = `k:${key.publicKey}`;
for (const network of networks) {
if (network.networkId === 'mainnet01') {
await sleep(config.ACCOUNTS.RATE_LIMIT);
}

const chainResult = await discoverAccount(
principal,
network.networkId,
Expand Down Expand Up @@ -307,6 +312,9 @@ export const syncAllAccounts = async (profileId: string, networkUUID: UUID) => {
profileId,
networkUUID,
);

const network = await networkRepository.getNetwork(networkUUID);

const watchedAccounts = await accountRepository.getWatchedAccountsByProfileId(
profileId,
networkUUID,
Expand All @@ -330,6 +338,9 @@ export const syncAllAccounts = async (profileId: string, networkUUID: UUID) => {

for (const account of accountsToSync) {
result.push(await syncAccount(account));
if (network.networkId === 'mainnet01') {
await sleep(config.ACCOUNTS.RATE_LIMIT);
}
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,11 @@ export function AccountDiscovery() {
isDisabled={selectedNetworks.length === 0}
onClick={async () => {
setDiscoveryStatus('discovering');

const ks = await Promise.all(
keySources.map(async (keySource) => ({
keySource,
accounts: await start(keySource),
})),
);
const ks = [];
for (const keySource of keySources) {
const accounts = await start(keySource);
ks.push({ keySource, accounts });
}
const mostUsedKs = ks.reduce((acc, data) =>
acc.accounts.length < data.accounts.length ? data : acc,
);
Expand Down Expand Up @@ -159,12 +157,16 @@ export function AccountDiscovery() {
: `(${key[keySource.source]?.index})`}
</Text>
<Stack gap={'sm'}>
{key && (
{key[keySource.source]?.publicKey ? (
<>
<Text color="emphasize" bold>
k:{key[keySource.source]?.publicKey}
</Text>
</>
) : (
<Text color="emphasize" bold>
Pending...
</Text>
)}
</Stack>
</Stack>
Expand Down

0 comments on commit 49a983c

Please sign in to comment.