Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add batchSize & throttleTimeout for both wagmi & sdk #1668

Merged
merged 12 commits into from
Feb 10, 2025
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,11 @@ The file `common.ts` with type [`AppConfig`](src/config/types.ts) contains impor
- `defaultLimitedApproval`: Optional flag to set the default ERC-20 approval to limited approval. For chains where gas is low, it is recommended to set this flag to true.
- `gasToken`: Gas token name, symbol, decimals, address and logoURI. This parameter will take priority over the `tokenListOverride`.
- `blockExplorer`: The name and URL of the block explorer to be used in the notifications and when the network is added to the injected wallet.
- `rpc`: The RPC url and headers of the network, used to add the network to the injected wallet and to fetch data from the chain.
- `rpc`:
- `url`: RPC url of the network
- `headers`: Headers append to each request to the RPC network
- `batchSize`: The maximum number of JSON-RPC requests to send in a batch
- `wait`: The maximum number of milliseconds to wait before sending a batch
zavelevsky marked this conversation as resolved.
Show resolved Hide resolved
- `defaultTokenPair`: Default token pair to be used in the app when opening the trade, explore, and simulation pages.
- `popularPairs`: List of popular pairs to be used in the app when opening the token selection modal.
- `popularTokens`: List of popular tokens to be used in the app when opening the token selection modal.
Expand All @@ -276,6 +280,9 @@ The file `common.ts` with type [`AppConfig`](src/config/types.ts) contains impor
- `tokenLists`: List of token lists including the uri and the parser name to be used to parse the token list. Please update the tokenParserMap in the `src/config/utils.ts` file to include the parser name and the parser function.
- `sdk`:
- `cacheTTL`: When the app loads, it will ignore any cached data if it is older than the cacheTTL time in milliseconds. If set to 0, the app will always ignore the cache data and fetch new data on load.
- `pairBatchSize`: Amount of pairs batched together in the same multicall when calling strategiesByPair
- `blockRangeSize`: Max number of blocks to read logs from in a single call. Each time the SDK reads logs since last processed block it will split the total range into smaller ranges according to this configuration. This should be determined according to the RPC limitations.
- `refreshInterval`: interval in millisecond between each cycle of reading latest events from the chain.
- `tenderly`
- `faucetTokens`: List of address to get tokens from in the debug page
- `ui`
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"private": true,
"dependencies": {
"@babel/core": "^7.0.0-0",
"@bancor/carbon-sdk": "0.0.105-DEV",
"@bancor/carbon-sdk": "0.0.106-DEV",
"@cloudflare/workers-types": "^4.20230717.0",
"@ethersproject/abi": "^5.0.0",
"@ethersproject/bytes": "^5.0.0",
Expand Down
5 changes: 5 additions & 0 deletions src/config/configSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export const AppConfigSchema = v.object({
rpc: v.object({
url: v.string(),
headers: v.optional(v.record(v.string(), v.string())),
batchSize: v.optional(v.number()),
wait: v.optional(v.number()),
}),
blockExplorer: v.object({
name: v.string(),
Expand All @@ -42,6 +44,9 @@ export const AppConfigSchema = v.object({
}),
sdk: v.object({
cacheTTL: v.number(),
pairBatchSize: v.optional(v.number()),
blockRangeSize: v.optional(v.number()),
refreshInterval: v.optional(v.number()),
}),
defaultTokenPair: v.tuple([v.string(), v.string()]),
popularPairs: v.array(v.tuple([v.string(), v.string()])),
Expand Down
7 changes: 6 additions & 1 deletion src/hooks/useCarbonInit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,12 @@ export const useCarbonInit = () => {
},
contractsConfig,
getTokenDecimalMap(),
cacheData
{
cache: cacheData,
pairBatchSize: config.sdk.pairBatchSize,
blockRangeSize: config.sdk.blockRangeSize,
refreshInterval: config.sdk.refreshInterval,
}
),
carbonSDK.setOnChangeHandlers(
Comlink.proxy(onPairDataChangedCallback),
Expand Down
17 changes: 12 additions & 5 deletions src/libs/queries/sdk/pairs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,25 @@ export const useGetTradePairsData = () => {
markForMissing(pair[1]);
}

const missingTokens: Token[] = [];
const getMissing = Array.from(missing).map(async (address) => {
const token = await fetchTokenData(Token, address);
missingTokens.push(token);
tokens.set(address, token);
return token;
});
const missingTokens = await Promise.all(getMissing);
const responses = await Promise.allSettled(getMissing);
for (const response of responses) {
if (response.status === 'rejected') console.error(response.reason);
}
importTokens(missingTokens);

const result = pairs.map((pair) => ({
baseToken: tokens.get(pair[0])!,
quoteToken: tokens.get(pair[1])!,
}));
const result: { baseToken: Token; quoteToken: Token }[] = [];
for (const pair of pairs) {
const baseToken = tokens.get(pair[0]);
const quoteToken = tokens.get(pair[1]);
if (baseToken && quoteToken) result.push({ baseToken, quoteToken });
}

const pairsWithInverse = [
...result,
Expand Down
4 changes: 4 additions & 0 deletions src/libs/wagmi/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,9 @@ export const configTransports = {
fetchOptions: {
headers: RPC_HEADERS[CHAIN_ID],
},
batch: {
batchSize: config.network.rpc.batchSize,
wait: config.network.rpc.wait,
},
}),
};
15 changes: 13 additions & 2 deletions src/workers/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ const init = async (
},
config: ContractsConfig,
decimalsMap?: Map<string, number>,
cachedData?: string
sdkConfig?: {
cache?: string;
pairBatchSize?: number;
blockRangeSize?: number;
refreshInterval?: number;
}
) => {
if (isInitialized || isInitializing) return;
isInitializing = true;
Expand All @@ -54,7 +59,13 @@ const init = async (
chainId
);
api = new ContractsApi(provider, config);
const { cache, startDataSync } = initSyncedCache(api.reader, cachedData);
const { cache, startDataSync } = initSyncedCache(
api.reader,
sdkConfig?.cache,
sdkConfig?.pairBatchSize,
sdkConfig?.refreshInterval,
sdkConfig?.blockRangeSize
);
sdkCache = cache;
carbonSDK = new Toolkit(
api,
Expand Down
15 changes: 10 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1623,17 +1623,17 @@
"@babel/helper-string-parser" "^7.25.9"
"@babel/helper-validator-identifier" "^7.25.9"

"@bancor/carbon-sdk@0.0.105-DEV":
version "0.0.105-DEV"
resolved "https://registry.yarnpkg.com/@bancor/carbon-sdk/-/carbon-sdk-0.0.105-DEV.tgz#3001161f694b87ea92dcf5f1980ab0c3a04cb632"
integrity sha512-79gJnx8M5NRz67/gAkFFq1qBd8A5y3mwGbyGM2xsOh2bw2EYmfNdUM5KnCYOgo925oWJRyDuglptf4m5oC1J9Q==
"@bancor/carbon-sdk@0.0.106-DEV":
version "0.0.106-DEV"
resolved "https://registry.yarnpkg.com/@bancor/carbon-sdk/-/carbon-sdk-0.0.106-DEV.tgz#8fcc85a58f220bd7621e021a30078d71a402cb20"
integrity sha512-cErAvJwGmf82d2XnaqCCXuB7DrGHBo9Ktyl2d/eoArH+gkcUZlHwxuv8VryRShmbzQfFPcmXjyBAMEkTpFxOOA==
dependencies:
"@ethersproject/abi" "^5.7.0"
"@ethersproject/bignumber" "^5.7.0"
"@ethersproject/contracts" "^5.7.0"
"@ethersproject/providers" "^5.7.2"
"@ethersproject/units" "^5.7.0"
decimal.js "^10.4.3"
decimal.js "^10.5.0"
ethers "^5.7.2"
events "^3.3.0"

Expand Down Expand Up @@ -5786,6 +5786,11 @@ decimal.js@^10.4.2, decimal.js@^10.4.3:
resolved "https://registry.yarnpkg.com/decimal.js/-/decimal.js-10.4.3.tgz#1044092884d245d1b7f65725fa4ad4c6f781cc23"
integrity sha512-VBBaLc1MgL5XpzgIP7ny5Z6Nx3UrRkIViUkPUdtl9aya5amy3De1gsUUSB1g3+3sExYNjCAsAznmukyxCb1GRA==

decimal.js@^10.5.0:
version "10.5.0"
resolved "https://registry.yarnpkg.com/decimal.js/-/decimal.js-10.5.0.tgz#0f371c7cf6c4898ce0afb09836db73cd82010f22"
integrity sha512-8vDa8Qxvr/+d94hSh5P3IJwI5t8/c0KsMp+g8bNw9cY2icONa5aPfvKeieW1WlG0WQYwwhJ7mjui2xtiePQSXw==

decode-uri-component@^0.2.2:
version "0.2.2"
resolved "https://registry.yarnpkg.com/decode-uri-component/-/decode-uri-component-0.2.2.tgz#e69dbe25d37941171dd540e024c444cd5188e1e9"
Expand Down