Skip to content

Commit 8e8bcb2

Browse files
committed
get deployed entrypoint, fix router check
1 parent f9206ec commit 8e8bcb2

File tree

4 files changed

+91
-32
lines changed

4 files changed

+91
-32
lines changed

packages/thirdweb/src/assets/bootstrap.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
IMPLEMENTATIONS,
2929
} from "./constants.js";
3030
import { deployInfraProxy } from "./deploy-infra-proxy.js";
31+
import { getInitCodeHashERC1967 } from "./get-initcode-hash-1967.js";
3132

3233
export async function deployRouter(options: ClientAndChainAndAccount) {
3334
let [feeManager, marketSaleImpl] = await Promise.all([
@@ -328,10 +329,3 @@ export async function getDeployedAssetFactory(args: ClientAndChain) {
328329
}
329330
return assetFactory;
330331
}
331-
332-
function getInitCodeHashERC1967(implementation: string) {
333-
// See `initCodeHashERC1967` - LibClone {https://github.com/vectorized/solady/blob/main/src/utils/LibClone.sol}
334-
return keccak256(
335-
`0x603d3d8160223d3973${implementation.toLowerCase().replace(/^0x/, "")}60095155f3363d3d373d3d363d7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e6038573d6000fd5b3d6000f3`,
336-
);
337-
}

packages/thirdweb/src/assets/get-entrypoint-erc20.ts

Lines changed: 73 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
1+
import { encodePacked } from "viem";
2+
import { ZERO_ADDRESS } from "../constants/addresses.js";
13
import { type ThirdwebContract, getContract } from "../contract/contract.js";
24
import { getOrDeployInfraContract } from "../contract/deployment/utils/bootstrap.js";
5+
import { getDeployedInfraContract } from "../contract/deployment/utils/infra.js";
36
import { encodeInitialize } from "../extensions/assets/__generated__/AssetEntrypointERC20/write/initialize.js";
4-
import type { ClientAndChainAndAccount } from "../utils/types.js";
7+
import { keccakId } from "../utils/any-evm/keccak-id.js";
8+
import { isContractDeployed } from "../utils/bytecode/is-contract-deployed.js";
9+
import { keccak256 } from "../utils/hashing/keccak256.js";
10+
import type {
11+
ClientAndChain,
12+
ClientAndChainAndAccount,
13+
} from "../utils/types.js";
14+
import { getDeployedAssetFactory } from "./bootstrap.js";
515
import {
6-
deployRewardLocker,
7-
deployRouter,
8-
getDeployedAssetFactory,
9-
getDeployedRewardLocker,
10-
getDeployedRouter,
11-
} from "./bootstrap.js";
12-
import { DEFAULT_INFRA_ADMIN, IMPLEMENTATIONS } from "./constants.js";
16+
DEFAULT_INFRA_ADMIN,
17+
DEFAULT_SALT,
18+
IMPLEMENTATIONS,
19+
} from "./constants.js";
1320
import { deployInfraProxy } from "./deploy-infra-proxy.js";
21+
import { getInitCodeHashERC1967 } from "./get-initcode-hash-1967.js";
1422

1523
export async function getOrDeployEntrypointERC20(
1624
options: ClientAndChainAndAccount,
@@ -25,19 +33,6 @@ export async function getOrDeployEntrypointERC20(
2533
});
2634
}
2735

28-
let [router, rewardLocker] = await Promise.all([
29-
getDeployedRouter(options),
30-
getDeployedRewardLocker(options),
31-
]);
32-
33-
if (!router) {
34-
router = await deployRouter(options);
35-
}
36-
37-
if (!rewardLocker) {
38-
rewardLocker = await deployRewardLocker(options);
39-
}
40-
4136
const assetFactory = await getDeployedAssetFactory(options);
4237
if (!assetFactory) {
4338
throw new Error(`Asset factory not found for chain: ${options.chain.id}`);
@@ -53,8 +48,8 @@ export async function getOrDeployEntrypointERC20(
5348
// encode init data
5449
const initData = encodeInitialize({
5550
owner: DEFAULT_INFRA_ADMIN,
56-
router: router.address,
57-
rewardLocker: rewardLocker.address,
51+
router: ZERO_ADDRESS,
52+
rewardLocker: ZERO_ADDRESS,
5853
});
5954

6055
const entyrpointProxyAddress = await deployInfraProxy({
@@ -71,3 +66,58 @@ export async function getOrDeployEntrypointERC20(
7166
address: entyrpointProxyAddress,
7267
});
7368
}
69+
70+
export async function getDeployedEntrypointERC20(options: ClientAndChain) {
71+
const implementations = IMPLEMENTATIONS[options.chain.id];
72+
73+
if (implementations?.AssetEntrypointERC20) {
74+
return getContract({
75+
client: options.client,
76+
chain: options.chain,
77+
address: implementations.AssetEntrypointERC20,
78+
});
79+
}
80+
81+
const [assetFactory, entrypointImpl] = await Promise.all([
82+
getDeployedAssetFactory(options),
83+
getDeployedInfraContract({
84+
...options,
85+
contractId: "AssetEntrypointERC20",
86+
publisher: "0x6453a486d52e0EB6E79Ec4491038E2522a926936",
87+
version: "0.0.2",
88+
}),
89+
]);
90+
91+
if (!assetFactory || !entrypointImpl) {
92+
return null;
93+
}
94+
95+
const initCodeHash = getInitCodeHashERC1967(entrypointImpl.address);
96+
97+
const saltHash = keccak256(
98+
encodePacked(
99+
["bytes32", "address"],
100+
[keccakId(DEFAULT_SALT), DEFAULT_INFRA_ADMIN],
101+
),
102+
);
103+
104+
const hashedDeployInfo = keccak256(
105+
encodePacked(
106+
["bytes1", "address", "bytes32", "bytes32"],
107+
["0xff", assetFactory.address, saltHash, initCodeHash],
108+
),
109+
);
110+
111+
const entrypointProxyAddress = `0x${hashedDeployInfo.slice(26)}`;
112+
const entrypointProxy = getContract({
113+
client: options.client,
114+
chain: options.chain,
115+
address: entrypointProxyAddress,
116+
});
117+
118+
if (!(await isContractDeployed(entrypointProxy))) {
119+
return null;
120+
}
121+
122+
return entrypointProxy;
123+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { keccak256 } from "../utils/hashing/keccak256.js";
2+
3+
export function getInitCodeHashERC1967(implementation: string) {
4+
// See `initCodeHashERC1967` - LibClone {https://github.com/vectorized/solady/blob/main/src/utils/LibClone.sol}
5+
return keccak256(
6+
`0x603d3d8160223d3973${implementation.toLowerCase().replace(/^0x/, "")}60095155f3363d3d373d3d363d7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e6038573d6000fd5b3d6000f3`,
7+
);
8+
}

packages/thirdweb/src/assets/is-router-enabled.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
import { ZERO_ADDRESS } from "../constants/addresses.js";
2-
import type { ThirdwebContract } from "../contract/contract.js";
32
import { getRouter } from "../extensions/assets/__generated__/AssetEntrypointERC20/read/getRouter.js";
3+
import type { ClientAndChain } from "../utils/types.js";
4+
import { getDeployedEntrypointERC20 } from "./get-entrypoint-erc20.js";
45

56
export async function isRouterEnabled(
6-
entrypoint: ThirdwebContract,
7+
options: ClientAndChain,
78
): Promise<boolean> {
9+
const entrypoint = await getDeployedEntrypointERC20(options);
10+
11+
if (!entrypoint) {
12+
return false;
13+
}
14+
815
const router = await getRouter({
916
contract: entrypoint,
1017
});

0 commit comments

Comments
 (0)