@@ -126,19 +126,47 @@ server.listen(port, () => {
126126console . log ( "[info] Starting application initialization..." ) ;
127127
128128// ETHERS JS CONTRACT CONFIG
129- export const provider = new JsonRpcProvider ( process . env . MAINNET_RPC , undefined , { staticNetwork : true } ) ;
130-
131- export const m3ter = new Contract (
132- process . env . M3TER_CONTRACT_ADDRESS || "heads.m3ter.eth" ,
133- [ "function publicKey(uint256) view returns (bytes32)" , "function tokenID(bytes32) view returns (uint256)" ] ,
134- provider ,
135- ) ;
136-
137- export const rollup = new Contract (
138- process . env . ROLLUP_CONTRACT_ADDRESS || "rollup.m3ter.eth" ,
139- [ "function nonce(uint256) external view returns (bytes6)" ] ,
140- provider ,
141- ) ;
129+ const rpcUrl = process . env . MAINNET_RPC ;
130+ console . log ( "[info] using RPC" , rpcUrl )
131+ export const provider = new JsonRpcProvider ( rpcUrl ) ;
132+
133+ const m3terRef = process . env . M3TER_CONTRACT_ADDRESS || "heads.m3ter.eth" ;
134+ const rollupRef = process . env . ROLLUP_CONTRACT_ADDRESS || "rollup.m3ter.eth" ;
135+
136+ const m3terAbi = [ "function publicKey(uint256) view returns (bytes32)" , "function tokenID(bytes32) view returns (uint256)" ] ;
137+ const rollupAbi = [ "function nonce(uint256) external view returns (bytes6)" ] ;
138+
139+ function isHexAddress ( value : string ) : boolean {
140+ return / ^ 0 x [ a - f A - F 0 - 9 ] { 40 } $ / . test ( value ) ;
141+ }
142+
143+ async function resolveContractAddress ( contractName : string , ref : string ) : Promise < string > {
144+ if ( isHexAddress ( ref ) ) {
145+ console . log ( `[ethers] ${ contractName } : using address ${ ref } ` ) ;
146+ return ref ;
147+ }
148+
149+ const resolved = await provider . resolveName ( ref ) ;
150+ if ( ! resolved ) {
151+ throw new Error ( `[ethers] ${ contractName } : failed to resolve ENS ${ ref } ` ) ;
152+ }
153+
154+ console . log ( `[ethers] ${ contractName } : resolved ENS ${ ref } -> ${ resolved } ` ) ;
155+ return resolved ;
156+ }
157+
158+ export let m3ter ! : Contract ;
159+ export let rollup ! : Contract ;
160+
161+ export const contractsReady = ( async ( ) => {
162+ const [ m3terAddress , rollupAddress ] = await Promise . all ( [
163+ resolveContractAddress ( "m3ter" , m3terRef ) ,
164+ resolveContractAddress ( "rollup" , rollupRef ) ,
165+ ] ) ;
166+
167+ m3ter = new Contract ( m3terAddress , m3terAbi , provider ) ;
168+ rollup = new Contract ( rollupAddress , rollupAbi , provider ) ;
169+ } ) ( ) ;
142170
143171export const ccipRevenueReader = new Contract (
144172 process . env . CCIP_REVENUE_READER_ADDRESS || "0xD648cdF47e9534B2FCfb18C1E94CA9AAff07BA0E" ,
0 commit comments