Skip to content

Commit

Permalink
Re-try fetch from cbridge
Browse files Browse the repository at this point in the history
  • Loading branch information
bobo-k2 committed Sep 3, 2024
1 parent a726570 commit 131e369
Showing 1 changed file with 40 additions and 34 deletions.
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

0 comments on commit 131e369

Please sign in to comment.