@@ -18,6 +18,7 @@ const {
1818 ParentEthDepositTransactionReceipt,
1919} = require ( '@arbitrum/sdk' ) ;
2020const { getBaseFee } = require ( '@arbitrum/sdk/dist/lib/utils/lib' ) ;
21+ const { ERC20__factory } = require ( '@arbitrum/sdk/dist/lib/abi/factories/ERC20__factory' ) ;
2122require ( 'dotenv' ) . config ( ) ;
2223requireEnvVariables ( [ 'PRIVATE_KEY' , 'CHAIN_RPC' , 'PARENT_CHAIN_RPC' , 'TransferTo' ] ) ;
2324
@@ -50,15 +51,24 @@ const main = async () => {
5051 const ethBridger = new EthBridger ( childChainNetwork ) ;
5152 const inboxAddress = ethBridger . childNetwork . ethBridge . inbox ;
5253
54+ /**
55+ * We find out whether the child chain we are using is a custom gas token chain
56+ * We need to perform an additional approve call to transfer
57+ * the native tokens to pay for the gas of the retryable tickets.
58+ */
59+ const isCustomGasTokenChain =
60+ childChainNetwork . nativeToken && childChainNetwork . nativeToken !== ethers . constants . AddressZero ;
61+
5362 /**
5463 * We deploy EthDeposit contract to the parent chain first and send eth to
5564 * the child chain via this contract.
5665 * Funds will deposit to the contract's alias address first.
5766 */
58- const DepositContract = await (
59- await hre . ethers . getContractFactory ( 'EthDeposit' )
60- ) . connect ( parentChainWallet ) ;
61- console . log ( 'Deploying EthDeposit contract...' ) ;
67+ const depositContractName = isCustomGasTokenChain ? 'CustomGasTokenDeposit' : 'EthDeposit' ;
68+ const DepositContract = ( await hre . ethers . getContractFactory ( depositContractName ) ) . connect (
69+ parentChainWallet ,
70+ ) ;
71+ console . log ( `Deploying ${ depositContractName } contract...` ) ;
6272 const depositContract = await DepositContract . deploy ( inboxAddress ) ;
6373 await depositContract . deployed ( ) ;
6474 console . log ( `deployed to ${ depositContract . address } ` ) ;
@@ -71,18 +81,37 @@ const main = async () => {
7181
7282 console . log ( `Sending deposit transaction...` ) ;
7383
74- const ethDepositTx = await depositContract . depositToChildChain ( {
75- value : ethers . utils . parseEther ( '0.01' ) ,
76- } ) ;
77- const ethDepositRec = await ethDepositTx . wait ( ) ;
84+ let depositTx ;
85+ if ( isCustomGasTokenChain ) {
86+ // Approve the gas token to be sent to the contract
87+ console . log ( 'Giving allowance to the contract to transfer the chain native token' ) ;
88+ const nativeToken = new ethers . Contract (
89+ childChainNetwork . nativeToken ,
90+ ERC20__factory . abi ,
91+ parentChainWallet ,
92+ ) ;
93+ const approvalTransaction = await nativeToken . approve (
94+ depositContract . address ,
95+ ethers . utils . parseEther ( '1' ) ,
96+ ) ;
97+ const approvalTransactionReceipt = await approvalTransaction . wait ( ) ;
98+ console . log ( `Approval transaction receipt is: ${ approvalTransactionReceipt . transactionHash } ` ) ;
99+
100+ depositTx = await depositContract . depositToChildChain ( ethers . utils . parseEther ( '0.01' ) ) ;
101+ } else {
102+ depositTx = await depositContract . depositToChildChain ( {
103+ value : ethers . utils . parseEther ( '0.01' ) ,
104+ } ) ;
105+ }
106+ const depositReceipt = await depositTx . wait ( ) ;
78107
79- console . log ( `Deposit txn confirmed on the parent chain! 🙌 ${ ethDepositRec . transactionHash } ` ) ;
108+ console . log ( `Deposit txn confirmed on the parent chain! 🙌 ${ depositReceipt . transactionHash } ` ) ;
80109
81110 console . log (
82111 'Waiting for the execution of the deposit in the child chain. This may take up to 10-15 minutes ⏰' ,
83112 ) ;
84113
85- const parentChainDepositTxReceipt = new ParentEthDepositTransactionReceipt ( ethDepositRec ) ;
114+ const parentChainDepositTxReceipt = new ParentEthDepositTransactionReceipt ( depositReceipt ) ;
86115 const childChainDepositResult = await parentChainDepositTxReceipt . waitForChildTransactionReceipt (
87116 childChainProvider ,
88117 ) ;
@@ -103,7 +132,7 @@ const main = async () => {
103132 ) ;
104133 } else {
105134 throw new Error (
106- `Deposit to the child chain failed, EthDepositStatus is ${
135+ `Deposit to the child chain failed, DepositStatus is ${
107136 EthDepositStatus [ childChainDepositResult . message . status ]
108137 } `,
109138 ) ;
@@ -186,16 +215,31 @@ const main = async () => {
186215 /**
187216 * Call the contract's method to transfer the funds from the alias to the address you set
188217 */
189- const setTransferTx = await depositContract . moveFundsFromChildChainAliasToAnotherAddress (
190- transferTo ,
191- ethers . utils . parseEther ( '0.01' ) , // because we deposited 0.01 ether, so we also transfer 0.01 ether out here.
192- parentToChildMessageGasParams . maxSubmissionCost ,
193- parentToChildMessageGasParams . gasLimit ,
194- gasPriceBid ,
195- {
196- value : depositAmount ,
197- } ,
198- ) ;
218+ let setTransferTx ;
219+ if ( isCustomGasTokenChain ) {
220+ // We don't need to give allowance to the contract now since we already gave plenty in the
221+ // previous step
222+
223+ setTransferTx = await depositContract . moveFundsFromChildChainAliasToAnotherAddress (
224+ transferTo ,
225+ ethers . utils . parseEther ( '0.01' ) , // because we deposited 0.01 ether, so we also transfer 0.01 ether out here.
226+ parentToChildMessageGasParams . maxSubmissionCost ,
227+ parentToChildMessageGasParams . gasLimit ,
228+ gasPriceBid ,
229+ depositAmount ,
230+ ) ;
231+ } else {
232+ setTransferTx = await depositContract . moveFundsFromChildChainAliasToAnotherAddress (
233+ transferTo ,
234+ ethers . utils . parseEther ( '0.01' ) , // because we deposited 0.01 ether, so we also transfer 0.01 ether out here.
235+ parentToChildMessageGasParams . maxSubmissionCost ,
236+ parentToChildMessageGasParams . gasLimit ,
237+ gasPriceBid ,
238+ {
239+ value : depositAmount ,
240+ } ,
241+ ) ;
242+ }
199243 const setTransferRec = await setTransferTx . wait ( ) ;
200244
201245 console . log (
0 commit comments