Skip to content

Commit

Permalink
chore(retry): unify retry function
Browse files Browse the repository at this point in the history
  • Loading branch information
D4mph1r committed Sep 17, 2024
1 parent 31a6cb6 commit 6a0ff11
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export const prodBridgeConfig: IBridgeConfig = {
bridgeChains: [
{
chain: "BSC",
rpcURL: "https://bsc.blockpi.network/v1/rpc/public",
rpcURL: "https://bnb.rpc.subquery.network/public",
nativeCoinSymbol: "BNB",
intialFund: "50000000000000000",
contractAddress: "0x289FDdDce5119C41B82C969135212061D5E7Dce5",
Expand Down
2 changes: 1 addition & 1 deletion src/handler/evm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export function evmHandler({

nftData:
royaltyProxy !== undefined
? nftDataForHedera(provider, royaltyProxy)
? nftDataForHedera(provider, royaltyProxy, logger)
: nftData(provider, logger),
selfIsValidator: selfIsValidator(bc, signer),
signClaimData: signClaimData(chainIdent, txSigner, logger),
Expand Down
37 changes: 16 additions & 21 deletions src/handler/evm/utils/nftDataForHedera.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,44 @@
import type { JsonRpcProvider } from "ethers";
import { ERC721Royalty__factory } from "../../../contractsTypes/evm";
import { RoyaltyInfoProxy__factory } from "../../../contractsTypes/hedera/RoyaltyInfoProxy__factory";

const nftDataForHedera = (provider: JsonRpcProvider, royaltyProxy: string) => {
import type { LogInstance } from "../../types";
import { retry } from "../../utils";

const nftDataForHedera = (
provider: JsonRpcProvider,
royaltyProxy: string,
logger: LogInstance,
) => {
const proxy = RoyaltyInfoProxy__factory.connect(royaltyProxy, provider);
return async (tokenId: string, contract: string) => {
const nft = ERC721Royalty__factory.connect(contract, provider);

const name = await retryFn(
const name = await retry(
() => nft.name(),
`Trying to fetch name() for ${contract}`,
logger,
);

const symbol = await retryFn(
const symbol = await retry(
() => nft.symbol(),
`Trying to fetch symbol() for ${contract}`,
logger,
);

const tokenInfo = await retryFn(
const tokenInfo = await retry(
() => proxy.royaltyInfo.staticCall(contract, tokenId),
`Trying to fetch royaltyInfo() for ${contract}`,
logger,
);

const rinfo = tokenInfo?.[1].tokenInfo[7][0];
// If undefined, set royalty as zero.
const royalty = rinfo?.numerator ?? 0n;

const metadata = await retryFn(
const metadata = await retry(
() => nft.tokenURI(tokenId),
`Trying to fetch tokenURI() for ${contract}`,
logger,
);

return {
Expand All @@ -42,18 +52,3 @@ const nftDataForHedera = (provider: JsonRpcProvider, royaltyProxy: string) => {
};

export default nftDataForHedera;

export async function retryFn<T>(
func: () => Promise<T>,
ctx: string,
retries = 3,
) {
if (retries === 0) {
throw new Error(`Failed ${ctx}`);
}
try {
return await func();
} catch (e) {
return await retryFn(func, ctx, retries - 1);
}
}

0 comments on commit 6a0ff11

Please sign in to comment.