1+ import { encodePacked } from "viem" ;
2+ import { ZERO_ADDRESS } from "../constants/addresses.js" ;
13import { type ThirdwebContract , getContract } from "../contract/contract.js" ;
24import { getOrDeployInfraContract } from "../contract/deployment/utils/bootstrap.js" ;
5+ import { getDeployedInfraContract } from "../contract/deployment/utils/infra.js" ;
36import { 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" ;
515import {
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" ;
1320import { deployInfraProxy } from "./deploy-infra-proxy.js" ;
21+ import { getInitCodeHashERC1967 } from "./get-initcode-hash-1967.js" ;
1422
1523export 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+ }
0 commit comments