11import type { Hex } from '@metamask/delegation-core' ;
2+ import {
3+ CHAIN_ID ,
4+ DELEGATOR_CONTRACTS ,
5+ } from '@metamask/delegation-deployments' ;
26import { numberToHex } from '@metamask/utils' ;
37
48import { t } from '../utils/i18n' ;
@@ -23,23 +27,111 @@ export type DelegationContracts = {
2327 approvalRevocationEnforcer : Hex ;
2428} ;
2529
26- const contracts : DelegationContracts = {
27- delegationManager : '0xdb9B1e94B5b69Df7e401DDbedE43491141047dB3' ,
28- eip7702StatelessDeleGatorImpl : '0x63c0c19a282a1B52b07dD5a65b58948A07DAE32B' ,
29- limitedCallsEnforcer : '0x04658B29F6b82ed55274221a06Fc97D318E25416' ,
30- erc20StreamingEnforcer : '0x56c97aE02f233B29fa03502Ecc0457266d9be00e' ,
31- erc20PeriodTransferEnforcer : '0x474e3Ae7E169e940607cC624Da8A15Eb120139aB' ,
32- nativeTokenStreamingEnforcer : '0xD10b97905a320b13a0608f7E9cC506b56747df19' ,
33- nativeTokenPeriodTransferEnforcer :
34- '0x9BC0FAf4Aca5AE429F4c06aEEaC517520CB16BD9' ,
35- valueLteEnforcer : '0x92Bf12322527cAA612fd31a0e810472BBB106A8F' ,
36- timestampEnforcer : '0x1046bb45C8d673d4ea75321280DB34899413c069' ,
37- exactCalldataEnforcer : '0x99F2e9bF15ce5eC84685604836F71aB835DBBdED' ,
38- nonceEnforcer : '0xDE4f2FAC4B3D87A1d9953Ca5FC09FCa7F366254f' ,
39- allowedCalldataEnforcer : '0xc2b0d624c1c4319760C96503BA27C347F3260f55' ,
40- redeemerEnforcer : '0xE144b0b2618071B4E56f746313528a669c7E65c5' ,
41- allowedTargetsEnforcer : '0x7F20f61b1f09b08D970938F6fa563634d65c4EeB' ,
42- approvalRevocationEnforcer : '0xe264F1f09A19505a1ca1a86D5b01E8bFdb64324A' ,
30+ const DELEGATOR_CONTRACT_VERSION = '1.3.0' ;
31+
32+ type DeployedContracts = ( typeof DELEGATOR_CONTRACTS ) [ string ] [ number ] ;
33+
34+ const getDeployedContracts = ( chainId : number ) : DeployedContracts => {
35+ const deployedContractsByChainId =
36+ DELEGATOR_CONTRACTS [ DELEGATOR_CONTRACT_VERSION ] ;
37+
38+ if ( ! deployedContractsByChainId ) {
39+ throw new Error (
40+ `Delegator contract deployments are missing for version ${ DELEGATOR_CONTRACT_VERSION } ` ,
41+ ) ;
42+ }
43+
44+ const deployedContracts =
45+ deployedContractsByChainId [ chainId ] ??
46+ deployedContractsByChainId [ CHAIN_ID . mainnet ] ;
47+
48+ if ( ! deployedContracts ) {
49+ throw new Error (
50+ `Delegator contract deployments are missing for chain ${ chainId } ` ,
51+ ) ;
52+ }
53+
54+ return deployedContracts ;
55+ } ;
56+
57+ const getDeployedContractAddress = (
58+ deployedContracts : DeployedContracts ,
59+ contractName : string ,
60+ ) : Hex => {
61+ const address = deployedContracts [ contractName ] ;
62+
63+ if ( ! address ) {
64+ throw new Error ( `Delegator contract deployment is missing ${ contractName } ` ) ;
65+ }
66+
67+ return address ;
68+ } ;
69+
70+ const getContracts = ( chainId : number ) : DelegationContracts => {
71+ const deployedContracts = getDeployedContracts ( chainId ) ;
72+
73+ return {
74+ delegationManager : getDeployedContractAddress (
75+ deployedContracts ,
76+ 'DelegationManager' ,
77+ ) ,
78+ eip7702StatelessDeleGatorImpl : getDeployedContractAddress (
79+ deployedContracts ,
80+ 'EIP7702StatelessDeleGatorImpl' ,
81+ ) ,
82+ limitedCallsEnforcer : getDeployedContractAddress (
83+ deployedContracts ,
84+ 'LimitedCallsEnforcer' ,
85+ ) ,
86+ erc20StreamingEnforcer : getDeployedContractAddress (
87+ deployedContracts ,
88+ 'ERC20StreamingEnforcer' ,
89+ ) ,
90+ erc20PeriodTransferEnforcer : getDeployedContractAddress (
91+ deployedContracts ,
92+ 'ERC20PeriodTransferEnforcer' ,
93+ ) ,
94+ nativeTokenStreamingEnforcer : getDeployedContractAddress (
95+ deployedContracts ,
96+ 'NativeTokenStreamingEnforcer' ,
97+ ) ,
98+ nativeTokenPeriodTransferEnforcer : getDeployedContractAddress (
99+ deployedContracts ,
100+ 'NativeTokenPeriodTransferEnforcer' ,
101+ ) ,
102+ valueLteEnforcer : getDeployedContractAddress (
103+ deployedContracts ,
104+ 'ValueLteEnforcer' ,
105+ ) ,
106+ timestampEnforcer : getDeployedContractAddress (
107+ deployedContracts ,
108+ 'TimestampEnforcer' ,
109+ ) ,
110+ exactCalldataEnforcer : getDeployedContractAddress (
111+ deployedContracts ,
112+ 'ExactCalldataEnforcer' ,
113+ ) ,
114+ nonceEnforcer : getDeployedContractAddress (
115+ deployedContracts ,
116+ 'NonceEnforcer' ,
117+ ) ,
118+ allowedCalldataEnforcer : getDeployedContractAddress (
119+ deployedContracts ,
120+ 'AllowedCalldataEnforcer' ,
121+ ) ,
122+ redeemerEnforcer : getDeployedContractAddress (
123+ deployedContracts ,
124+ 'RedeemerEnforcer' ,
125+ ) ,
126+ allowedTargetsEnforcer : getDeployedContractAddress (
127+ deployedContracts ,
128+ 'AllowedTargetsEnforcer' ,
129+ ) ,
130+ approvalRevocationEnforcer : getDeployedContractAddress (
131+ deployedContracts ,
132+ 'ApprovalRevocationEnforcer' ,
133+ ) ,
134+ } ;
43135} ;
44136
45137// derived from https://chainid.network/chains.json
@@ -121,7 +213,7 @@ export const getChainMetadata = ({
121213 const { explorerUrl, name } = nameAndExplorerUrlByChainId [ chainId ] ?? { } ;
122214
123215 const metadata = {
124- contracts,
216+ contracts : getContracts ( chainId ) ,
125217 name : name ?? t ( 'unknownChain' , [ numberToHex ( chainId ) ] ) ,
126218 explorerUrl,
127219 } ;
0 commit comments