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

Re-try fetch from cbridge #1382

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 40 additions & 34 deletions src/v2/repositories/implementations/EvmAssetsRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export class EvmAssetsRepository implements IEvmAssetsRepository {
isFetchUsd: boolean;
}): Promise<Erc20Token[]> {
Guard.ThrowIfUndefined('currentAccount', currentAccount);
const numberOfRetries = 2;

if (
String(srcChainId) === providerEndpoints[endpointKey.SHIBUYA].evmChainId ||
Expand All @@ -67,43 +68,48 @@ export class EvmAssetsRepository implements IEvmAssetsRepository {
return [];
}

const data = await getTransferConfigs(currentNetworkIdx);
if (!data || !data.tokens) {
throw Error('Cannot fetch from cBridge API');
}
const seen = new Set();
// Todo: use srcChain and destChainID to re-define token information for bridging (ex: PKEX)
for (let i = 0; i < numberOfRetries; i++) {
const data = await getTransferConfigs(currentNetworkIdx);
if (!data || !data.tokens) {
continue;
}

const tokens = (await Promise.all(
objToArray(data.tokens[srcChainId as EvmChain])
.flat()
.map(async (token: CbridgeToken) => {
const t = getSelectedToken({ srcChainId, token });
if (!t) return undefined;
const formattedToken = castCbridgeToErc20({ srcChainId, token: t });
const isDuplicated = seen.has(formattedToken.address);
seen.add(formattedToken.address);
// Memo: Remove the duplicated contract address (ex: PKEX)
if (isDuplicated) return undefined;
const seen = new Set();
// Todo: use srcChain and destChainID to re-define token information for bridging (ex: PKEX)

const { balUsd, userBalance } = await this.updateTokenBalanceHandler({
userAddress: currentAccount,
token: formattedToken,
isFetchUsd,
srcChainId,
});
const tokenWithBalance = {
...formattedToken,
userBalance,
userBalanceUsd: String(balUsd),
};
return castCbridgeTokenData(tokenWithBalance);
})
)) as Erc20Token[];
const tokens = (await Promise.all(
objToArray(data.tokens[srcChainId as EvmChain])
.flat()
.map(async (token: CbridgeToken) => {
const t = getSelectedToken({ srcChainId, token });
if (!t) return undefined;
const formattedToken = castCbridgeToErc20({ srcChainId, token: t });
const isDuplicated = seen.has(formattedToken.address);
seen.add(formattedToken.address);
// Memo: Remove the duplicated contract address (ex: PKEX)
if (isDuplicated) return undefined;

return tokens.filter((token) => {
return token !== undefined;
});
const { balUsd, userBalance } = await this.updateTokenBalanceHandler({
userAddress: currentAccount,
token: formattedToken,
isFetchUsd,
srcChainId,
});
const tokenWithBalance = {
...formattedToken,
userBalance,
userBalanceUsd: String(balUsd),
};
return castCbridgeTokenData(tokenWithBalance);
})
)) as Erc20Token[];

return tokens.filter((token) => {
return token !== undefined;
});
}

throw Error('Cannot fetch from cBridge API');
}

public async fetchRegisteredAssets({
Expand Down
Loading