11import { getContract } from "src/contract/contract.js" ;
22import { assetInfraDeployedEvent } from "src/extensions/assets/__generated__/AssetInfraDeployer/events/AssetInfraDeployed.js" ;
33import { deployInfraProxyDeterministic } from "src/extensions/assets/__generated__/AssetInfraDeployer/write/deployInfraProxyDeterministic.js" ;
4- import { encodeInitialize } from "src/extensions/assets/__generated__/FeeManager/write/initialize.js" ;
4+ import { encodeInitialize as encodeFeeManagerInit } from "src/extensions/assets/__generated__/FeeManager/write/initialize.js" ;
5+ import { encodeInitialize as encodeRouterInit } from "src/extensions/assets/__generated__/Router/write/initialize.js" ;
56import { isContractDeployed } from "src/utils/bytecode/is-contract-deployed.js" ;
67import { keccak256 } from "src/utils/hashing/keccak256.js" ;
78import { encodePacked } from "viem" ;
@@ -26,6 +27,64 @@ import {
2627 DEFAULT_SALT ,
2728 IMPLEMENTATIONS ,
2829} from "./constants.js" ;
30+ import { deployInfraProxy } from "./deploy-infra-proxy.js" ;
31+
32+ export async function deployRouter ( options : ClientAndChainAndAccount ) {
33+ let [ feeManager , marketSaleImpl ] = await Promise . all ( [
34+ getDeployedFeeManager ( options ) ,
35+ getDeployedInfraContract ( {
36+ ...options ,
37+ contractId : "MarketSale" ,
38+ publisher : "0x6453a486d52e0EB6E79Ec4491038E2522a926936" ,
39+ } ) ,
40+ ] ) ;
41+
42+ if ( ! feeManager ) {
43+ feeManager = await deployFeeManager ( options ) ;
44+ }
45+
46+ if ( ! marketSaleImpl ) {
47+ marketSaleImpl = await getOrDeployInfraContract ( {
48+ ...options ,
49+ contractId : "MarketSale" ,
50+ publisher : "0x6453a486d52e0EB6E79Ec4491038E2522a926936" ,
51+ } ) ;
52+ }
53+
54+ const assetFactory = await getDeployedAssetFactory ( options ) ;
55+ if ( ! assetFactory ) {
56+ throw new Error ( `Asset factory not found for chain: ${ options . chain . id } ` ) ;
57+ }
58+
59+ const routerImpl = await getOrDeployInfraContract ( {
60+ ...options ,
61+ contractId : "Router" ,
62+ constructorParams : {
63+ _marketSaleImplementation : marketSaleImpl . address ,
64+ _feeManager : feeManager ,
65+ } ,
66+ publisher : "0x6453a486d52e0EB6E79Ec4491038E2522a926936" ,
67+ } ) ;
68+
69+ // encode init data
70+ const initData = encodeRouterInit ( {
71+ owner : DEFAULT_INFRA_ADMIN ,
72+ } ) ;
73+
74+ const routerProxyAddress = await deployInfraProxy ( {
75+ ...options ,
76+ initData,
77+ extraData : "0x" ,
78+ implementationAddress : routerImpl . address ,
79+ assetFactory,
80+ } ) ;
81+
82+ return getContract ( {
83+ client : options . client ,
84+ chain : options . chain ,
85+ address : routerProxyAddress ,
86+ } ) ;
87+ }
2988
3089export async function deployRewardLocker ( options : ClientAndChainAndAccount ) {
3190 let v3PositionManager = ZERO_ADDRESS ;
@@ -52,6 +111,7 @@ export async function deployRewardLocker(options: ClientAndChainAndAccount) {
52111 _v3PositionManager : v3PositionManager ,
53112 _v4PositionManager : v4PositionManager ,
54113 } ,
114+ publisher : "0x6453a486d52e0EB6E79Ec4491038E2522a926936" ,
55115 } ) ;
56116}
57117
@@ -66,10 +126,11 @@ export async function deployFeeManager(options: ClientAndChainAndAccount) {
66126 const feeManagerImpl = await getOrDeployInfraContract ( {
67127 ...options ,
68128 contractId : "FeeManager" ,
129+ publisher : "0x6453a486d52e0EB6E79Ec4491038E2522a926936" ,
69130 } ) ;
70131
71132 // encode init data
72- const initData = encodeInitialize ( {
133+ const initData = encodeFeeManagerInit ( {
73134 owner : DEFAULT_INFRA_ADMIN ,
74135 feeRecipient : DEFAULT_FEE_RECIPIENT ,
75136 defaultFee : DEFAULT_FEE_BPS ,
@@ -100,7 +161,13 @@ export async function deployFeeManager(options: ClientAndChainAndAccount) {
100161 ) ;
101162 }
102163
103- return decodedEvent [ 0 ] ?. args . proxy ;
164+ const feeManagerProxyAddress = decodedEvent [ 0 ] ?. args . proxy ;
165+
166+ return getContract ( {
167+ client : options . client ,
168+ chain : options . chain ,
169+ address : feeManagerProxyAddress ,
170+ } ) ;
104171}
105172
106173async function deployAssetFactory ( options : ClientAndChainAndAccount ) {
@@ -114,6 +181,95 @@ async function deployAssetFactory(options: ClientAndChainAndAccount) {
114181 return getOrDeployInfraContract ( {
115182 ...options ,
116183 contractId : "AssetInfraDeployer" ,
184+ publisher : "0x6453a486d52e0EB6E79Ec4491038E2522a926936" ,
185+ } ) ;
186+ }
187+
188+ export async function getDeployedRouter ( options : ClientAndChain ) {
189+ const [ feeManager , marketSaleImpl , assetFactory ] = await Promise . all ( [
190+ getDeployedFeeManager ( options ) ,
191+ getDeployedInfraContract ( {
192+ ...options ,
193+ contractId : "MarketSale" ,
194+ publisher : "0x6453a486d52e0EB6E79Ec4491038E2522a926936" ,
195+ } ) ,
196+ getDeployedAssetFactory ( options ) ,
197+ ] ) ;
198+
199+ if ( ! feeManager || ! marketSaleImpl || ! assetFactory ) {
200+ return null ;
201+ }
202+
203+ const routerImpl = await getDeployedInfraContract ( {
204+ ...options ,
205+ contractId : "Router" ,
206+ constructorParams : {
207+ _marketSaleImplementation : marketSaleImpl . address ,
208+ _feeManager : feeManager ,
209+ } ,
210+ publisher : "0x6453a486d52e0EB6E79Ec4491038E2522a926936" ,
211+ } ) ;
212+
213+ if ( ! routerImpl ) {
214+ return null ;
215+ }
216+
217+ const initCodeHash = getInitCodeHashERC1967 ( routerImpl . address ) ;
218+
219+ const saltHash = keccak256 (
220+ encodePacked (
221+ [ "bytes32" , "address" ] ,
222+ [ keccakId ( DEFAULT_SALT ) , DEFAULT_INFRA_ADMIN ] ,
223+ ) ,
224+ ) ;
225+
226+ const hashedDeployInfo = keccak256 (
227+ encodePacked (
228+ [ "bytes1" , "address" , "bytes32" , "bytes32" ] ,
229+ [ "0xff" , assetFactory . address , saltHash , initCodeHash ] ,
230+ ) ,
231+ ) ;
232+
233+ const routerProxyAddress = `0x${ hashedDeployInfo . slice ( 26 ) } ` ;
234+ const routerProxy = getContract ( {
235+ client : options . client ,
236+ chain : options . chain ,
237+ address : routerProxyAddress ,
238+ } ) ;
239+
240+ if ( ! ( await isContractDeployed ( routerProxy ) ) ) {
241+ return null ;
242+ }
243+
244+ return routerProxy ;
245+ }
246+
247+ export async function getDeployedRewardLocker ( options : ClientAndChain ) {
248+ let v3PositionManager = ZERO_ADDRESS ;
249+ let v4PositionManager = ZERO_ADDRESS ;
250+
251+ const implementations = IMPLEMENTATIONS [ options . chain . id ] ;
252+
253+ if ( implementations ) {
254+ v3PositionManager = implementations . V3PositionManager || ZERO_ADDRESS ;
255+ v4PositionManager = implementations . V4PositionManager || ZERO_ADDRESS ;
256+ }
257+
258+ const feeManager = await getDeployedFeeManager ( options ) ;
259+
260+ if ( ! feeManager ) {
261+ return null ;
262+ }
263+
264+ return await getDeployedInfraContract ( {
265+ ...options ,
266+ contractId : "RewardLocker" ,
267+ constructorParams : {
268+ _feeManager : feeManager ,
269+ _v3PositionManager : v3PositionManager ,
270+ _v4PositionManager : v4PositionManager ,
271+ } ,
272+ publisher : "0x6453a486d52e0EB6E79Ec4491038E2522a926936" ,
117273 } ) ;
118274}
119275
@@ -123,6 +279,7 @@ export async function getDeployedFeeManager(options: ClientAndChain) {
123279 getDeployedInfraContract ( {
124280 ...options ,
125281 contractId : "FeeManager" ,
282+ publisher : "0x6453a486d52e0EB6E79Ec4491038E2522a926936" ,
126283 } ) ,
127284 ] ) ;
128285
@@ -157,21 +314,22 @@ export async function getDeployedFeeManager(options: ClientAndChain) {
157314 return null ;
158315 }
159316
160- return feeManagerProxyAddress ;
317+ return feeManagerProxy ;
161318}
162319
163- async function getDeployedAssetFactory ( args : ClientAndChain ) {
320+ export async function getDeployedAssetFactory ( args : ClientAndChain ) {
164321 const assetFactory = await getDeployedInfraContract ( {
165322 ...args ,
166323 contractId : "AssetInfraDeployer" ,
324+ publisher : "0x6453a486d52e0EB6E79Ec4491038E2522a926936" ,
167325 } ) ;
168326 if ( ! assetFactory ) {
169327 return null ;
170328 }
171329 return assetFactory ;
172330}
173331
174- function getInitCodeHashERC1967 ( implementation : string ) {
332+ export function getInitCodeHashERC1967 ( implementation : string ) {
175333 // See `initCodeHashERC1967` - LibClone {https://github.com/vectorized/solady/blob/main/src/utils/LibClone.sol}
176334 return keccak256 (
177335 `0x603d3d8160223d3973${ implementation . toLowerCase ( ) . replace ( / ^ 0 x / , "" ) } 60095155f3363d3d373d3d363d7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e6038573d6000fd5b3d6000f3` ,
0 commit comments