-
Notifications
You must be signed in to change notification settings - Fork 1
/
erc20-deposit-router.ts
159 lines (124 loc) · 4.45 KB
/
erc20-deposit-router.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { BigNumber, Contract, Wallet, ethers } from "ethers";
import { Provider } from "@ethersproject/abstract-provider";
import { defaultAbiCoder } from "ethers/lib/utils";
import {
Erc20Bridger,
registerCustomArbitrumNetwork
} from "@arbitrum/sdk";
import dotenv from "dotenv";
import { reyaNetwork as childNetwork } from "../../helpers/custom-network-reya";
dotenv.config();
/**
* Set up: instantiate Parent / Child wallets connected to providers
*/
const walletPrivateKey: string = process.env.DEVNET_PRIVKEY as string;
let parentProvider = new ethers.providers.JsonRpcProvider(process.env.ParentRPC);
const childProvider = new ethers.providers.JsonRpcProvider(process.env.ChildRPC);
const parentWallet = new Wallet(walletPrivateKey, parentProvider);
const main = async () => {
console.log("Child Network Reached");
// register - needed for retryables
registerCustomArbitrumNetwork(childNetwork)
console.log("Custom Network Added");
// Set up the Erc20Bridger
const parentErc20Address = "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238";
const tokenAmount = BigNumber.from(1000000);
const erc20Bridger = new Erc20Bridger(childNetwork);
console.log("Erc20 Bridger Set Up");
// We get the address of Parent Gateway for our DappToken
// Validate that the token address is correctly set
if (!parentErc20Address) {
throw new Error("Invalid ERC20 token address.");
}
console.log("Parent ERC20 Address Validated");
// Define the ERC20 contract interface
const ERC20_ABI = [
"function balanceOf(address owner) view returns (uint256)",
"function decimals() view returns (uint8)",
];
//Get the ERC20 contract instance
const erc20Contract = new ethers.Contract(
parentErc20Address,
ERC20_ABI,
parentWallet
);
// Get the expected Parent Gateway address
const expectedParentGatewayAddress = await erc20Bridger.getParentGatewayAddress(
parentErc20Address,
parentProvider as Provider
);
console.log(
"Expected Parent Gateway Address Retrieved: ",
expectedParentGatewayAddress
);
// Check if the expectedParentGatewayAddress is valid
if (!expectedParentGatewayAddress || expectedParentGatewayAddress === "") {
throw new Error("Failed to get Parent Gateway address.");
}
// Get the initial token balance of the Bridge
const initialBridgeTokenBalance = await erc20Contract.balanceOf(
expectedParentGatewayAddress
);
// Log the initial balance
console.log(
`Initial Bridge Token Balance: ${initialBridgeTokenBalance.toString()}`
);
const walletAddress = await parentWallet.address;
// Approve the token transfer
console.log("Approving:");
const approveTx = await erc20Bridger.approveToken({
parentSigner: parentWallet,
erc20ParentAddress: parentErc20Address,
});
const approveRec = await approveTx.wait();
console.log(
`You successfully allowed the Arbitrum Bridge to spend USDC ${approveRec.transactionHash}`
);
const depositRequest = (await erc20Bridger.getDepositRequest({
amount: tokenAmount,
erc20ParentAddress: parentErc20Address,
parentProvider: parentProvider,
from: parentWallet.address,
childProvider: childProvider,
})) as any;
let retryableData = depositRequest.retryableData;
let childGaslimit = retryableData.gasLimit;
let maxFeePerGas = retryableData.maxFeePerGas;
let maxSubmissionCost = retryableData.maxSubmissionCost;
let deposit = depositRequest.retryableData.deposit;
let data1 = defaultAbiCoder.encode(
["uint256", "bytes"],
[+maxSubmissionCost.toString(), "0x"]
);
let routerABI = [
"function outboundTransferCustomRefund( address _parentToken,address _refundTo, address _to, uint256 _amount, uint256 _maxGas, uint256 _gasPriceBid, bytes calldata _data) external payable returns (bytes memory)",
];
const routerContract = new Contract(
"0xf446986e261E84aB2A55159F3Fba60F7E8AeDdAF",
routerABI,
parentWallet
);
//Deposit the token to Child
console.log("Transferring DappToken to Child:");
const { data } =
await routerContract.populateTransaction.outboundTransferCustomRefund(
parentErc20Address,
walletAddress,
walletAddress,
tokenAmount,
childGaslimit,
maxFeePerGas,
data1
);
const depositTx = await parentWallet.sendTransaction({
to: routerContract.address,
data,
value:deposit
});
let rec = await depositTx.wait();
console.log(rec);
};
main().catch((err) => {
console.error(err);
process.exit(1);
});