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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ 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.
- `batchSize`: Amount of rpc request per batch
- `throttleTimeout`: timeout in millisecond between each request
- `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.
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
2 changes: 2 additions & 0 deletions src/config/configSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export const AppConfigSchema = v.object({
}),
sdk: v.object({
cacheTTL: v.number(),
batchSize: v.optional(v.number()),
throttleTimeout: v.optional(v.number()),
}),
defaultTokenPair: v.tuple([v.string(), v.string()]),
popularPairs: v.array(v.tuple([v.string(), v.string()])),
Expand Down
6 changes: 5 additions & 1 deletion src/hooks/useCarbonInit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ export const useCarbonInit = () => {
},
contractsConfig,
getTokenDecimalMap(),
cacheData
{
cache: cacheData,
batchSize: config.sdk.batchSize,
throttleTimeout: config.sdk.throttleTimeout,
}
),
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.sdk.batchSize,
wait: config.sdk.throttleTimeout,
},
}),
};
14 changes: 12 additions & 2 deletions src/workers/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ const init = async (
},
config: ContractsConfig,
decimalsMap?: Map<string, number>,
cachedData?: string
sdkConfig?: {
cache?: string;
batchSize?: number;
throttleTimeout?: number;
}
) => {
if (isInitialized || isInitializing) return;
isInitializing = true;
Expand All @@ -54,7 +58,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?.batchSize,
sdkConfig?.throttleTimeout,
sdkConfig?.batchSize
);
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/[email protected].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/[email protected].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