Skip to content

Commit 6cd681c

Browse files
committed
Fix resolve implementation (#8413)
<!-- ## title your PR with this format: "[SDK/Dashboard/Portal] Feature/Fix: Concise title for the changes" If you did not copy the branch name from Linear, paste the issue tag here (format is TEAM-0000): ## Notes for the reviewer Anything important to call out? Be sure to also clarify these in your comments. ## How to test Unit tests, playground, etc. --> <!-- start pr-codex --> --- ## PR-Codex overview This PR focuses on improving the `resolveImplementation` function in the `thirdweb` package by enhancing the implementation resolution process for proxy contracts. ### Detailed summary - Replaced the existing implementation resolution logic with a more robust approach using `Promise.all` to fetch implementations from both storage and contract calls. - Introduced a loop to handle multiple implementations, ensuring that valid addresses are checked before proceeding. - Updated the return logic to ensure correct addresses and bytecode are returned based on the resolved implementation. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex --> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved the reliability of contract implementation resolution to handle edge cases more robustly when detecting implementations across proxy contracts and various contract architecture types. Processing is now more efficient through concurrent implementation detection and optimized fallback handling, ensuring more consistent and faster results in complex contract scenarios. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 144bb33 commit 6cd681c

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

.changeset/afraid-regions-push.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+
Resolve implementation from contract call

packages/thirdweb/src/utils/bytecode/resolveImplementation.ts

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,42 +47,43 @@ export async function resolveImplementation(
4747
}
4848

4949
// check other proxy types
50-
let implementationAddress: string | undefined;
51-
5250
if (beacon && beacon !== AddressZero) {
5351
// In case of a BeaconProxy, it is setup as BeaconProxy --> Beacon --> Implementation
5452
// Hence we replace the proxy address with Beacon address, and continue further resolving below
5553
contract = getContract({
5654
...contract,
5755
address: beacon,
5856
});
59-
60-
implementationAddress = await getImplementationFromContractCall(contract);
61-
} else {
62-
implementationAddress = await getImplementationFromStorageSlot(contract);
6357
}
6458

65-
if (
66-
implementationAddress &&
67-
isAddress(implementationAddress) &&
68-
implementationAddress !== AddressZero
69-
) {
70-
const implementationBytecode = await getBytecode({
71-
...contract,
72-
address: implementationAddress,
73-
});
74-
// return the original contract bytecode if the implementation bytecode is empty
75-
if (implementationBytecode === "0x") {
59+
const implementations = await Promise.all([
60+
getImplementationFromStorageSlot(contract),
61+
getImplementationFromContractCall(contract),
62+
]);
63+
64+
for (const implementationAddress of implementations) {
65+
if (
66+
implementationAddress &&
67+
isAddress(implementationAddress) &&
68+
implementationAddress !== AddressZero
69+
) {
70+
const implementationBytecode = await getBytecode({
71+
...contract,
72+
address: implementationAddress,
73+
});
74+
// return the original contract bytecode if the implementation bytecode is empty
75+
if (implementationBytecode === "0x") {
76+
return {
77+
address: contract.address,
78+
bytecode: originalBytecode,
79+
};
80+
}
81+
7682
return {
77-
address: contract.address,
78-
bytecode: originalBytecode,
83+
address: implementationAddress,
84+
bytecode: implementationBytecode,
7985
};
8086
}
81-
82-
return {
83-
address: implementationAddress,
84-
bytecode: implementationBytecode,
85-
};
8687
}
8788

8889
return { address: contract.address, bytecode: originalBytecode };

0 commit comments

Comments
 (0)