Skip to content

Commit fa0d114

Browse files
[SDK] Add retry logic to predictAccountAddress (#6707)
1 parent f618e2b commit fa0d114

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

.changeset/slow-mice-grin.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Add retry logic to predictAccountAddress

packages/thirdweb/src/extensions/erc1155/customDrop1155.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ describe.runIf(process.env.TW_SECRET_KEY)(
9999
const condition = await getClaimConditions({
100100
contract,
101101
tokenId: 0n,
102+
singlePhaseDrop: true,
102103
});
103104
expect(condition.length).to.eq(1);
104105
expect(condition[0]?.maxClaimableSupply).to.eq(10n);

packages/thirdweb/src/wallets/smart/lib/calls.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,35 @@ export async function predictAddress(args: {
9797
accountSalt && isHex(accountSalt)
9898
? accountSalt
9999
: stringToHex(accountSalt ?? "");
100-
return readContract({
101-
contract: factoryContract,
102-
method: "function getAddress(address, bytes) returns (address)",
103-
params: [adminAddress, saltHex],
104-
});
100+
let result: string | undefined;
101+
let retries = 0;
102+
const maxRetries = 3;
103+
104+
while (retries <= maxRetries) {
105+
try {
106+
result = await readContract({
107+
contract: factoryContract,
108+
method: "function getAddress(address, bytes) returns (address)",
109+
params: [adminAddress, saltHex],
110+
});
111+
break;
112+
} catch (error) {
113+
if (retries === maxRetries) {
114+
throw error;
115+
}
116+
117+
// Exponential backoff: 2^(retries + 1) * 100ms (200ms, 400ms, 800ms)
118+
const delay = 2 ** (retries + 1) * 100;
119+
await new Promise((resolve) => setTimeout(resolve, delay));
120+
retries++;
121+
}
122+
}
123+
if (!result) {
124+
throw new Error(
125+
`No smart account address found for admin address ${adminAddress} and salt ${accountSalt}`,
126+
);
127+
}
128+
return result;
105129
},
106130
{
107131
cacheKey: `${args.factoryContract.chain.id}-${args.factoryContract.address}-${args.adminAddress}-${args.accountSalt}`,

0 commit comments

Comments
 (0)