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

Resolve beacon from beacon-proxy #2139

Merged
merged 6 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/warm-clocks-sell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@thirdweb-dev/sdk": patch
---

Resolve beacon from beacon-proxy
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@
address: string,
provider: providers.Provider,
): Promise<{ address: string; bytecode: string }> {
const bytecode = await fetchBytecode(address, provider);
const [bytecode, beacon] = await Promise.all([
fetchBytecode(address, provider),
getBeaconFromStorageSlot(address, provider),
]);

// check minimal proxy first synchronously
const minimalProxyImplementationAddress =
Expand All @@ -52,6 +55,11 @@
}

// check other proxy types
if (beacon && beacon !== constants.AddressZero) {
// In case of a BeaconProxy, it is setup as BeaconProxy --> Beacon --> Implementation
// Hence we replace the proxy address with Beacon address, and continue further resolving below
address = beacon;

Check warning on line 61 in packages/sdk/src/evm/common/feature-detection/resolveContractUriFromAddress.ts

View check run for this annotation

Codecov / codecov/patch

packages/sdk/src/evm/common/feature-detection/resolveContractUriFromAddress.ts#L61

Added line #L61 was not covered by tests
}
const impl = await Promise.all([
getImplementationFromStorageSlot(address, provider),
getImplementationFromContractCall(address, provider),
Expand Down Expand Up @@ -108,6 +116,30 @@
}
}

async function getBeaconFromStorageSlot(
address: string,
provider: providers.Provider,
): Promise<string | undefined> {
/**
* The storage slot of the Beacon as defined in EIP-1967
* See https://eips.ethereum.org/EIPS/eip-1967#beacon-contract-address
*
* bytes32(uint256(keccak256('eip1967.proxy.beacon')) - 1))
*/

try {
const proxyStorage = await provider.getStorageAt(
address,
BigNumber.from(
"0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50",
),
);
return `0x${proxyStorage.slice(-40)}`;
} catch (e) {
return undefined;

Check warning on line 139 in packages/sdk/src/evm/common/feature-detection/resolveContractUriFromAddress.ts

View check run for this annotation

Codecov / codecov/patch

packages/sdk/src/evm/common/feature-detection/resolveContractUriFromAddress.ts#L139

Added line #L139 was not covered by tests
}
}

async function getImplementationFromContractCall(
address: string,
provider: providers.Provider,
Expand Down
Loading