-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherc20-deposit.ts
151 lines (115 loc) · 4.57 KB
/
erc20-deposit.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
import { BigNumber, Wallet, ethers } from "ethers";
import { Provider } from "@ethersproject/abstract-provider";
import {
Erc20Bridger,
ParentToChildMessageStatus,
registerCustomArbitrumNetwork,
} from "@arbitrum/sdk";
//import { arbLog, requireEnvVariables } from "arb-shared-dependencies";
import dotenv from "dotenv";
import { blueberryNetwork as childNetwork} from "../../helpers/custom-network";
dotenv.config();
//requireEnvVariables(["DEVNET_PRIVKEY", "ParentRPC", "ChildRPC", "TOKEN_ADDRESS"]);
console.log("Environment Variables Loaded");
/**
* Set up: instantiate Parent / Child wallets connected to providers
*/
const walletPrivateKey: string = process.env.DEVNET_PRIVKEY as string;
const parentProvider = new ethers.providers.JsonRpcProvider(process.env.ParentRPC);
const childProvider = new ethers.providers.JsonRpcProvider(process.env.ChildRPC);
const parentWallet = new Wallet(walletPrivateKey, parentProvider);
// const childWallet = new Wallet(walletPrivateKey, childProvider);
const main = async () => {
// register - needed for retryables
registerCustomArbitrumNetwork(childNetwork);
console.log("Custom Network Added");
// Set up the Erc20Bridger
const erc20Bridger = new Erc20Bridger(childNetwork);
console.log("Erc20 Bridger Set Up");
// We get the address of Parent Gateway for USDC on ARbitrum Sepolia
const parentErc20Address = "0x75faf114eafb1BDbe2F0316DF893fd58CE46AA4d";
// 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
);
let ParentBalance = await erc20Contract.balanceOf(parentWallet.address)
console.log('Current Balance on Parent: ', ParentBalance.toString())
// 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 tokenAmount = BigNumber.from(50000)
const approveTxGas = await erc20Bridger.approveToken({
parentSigner: parentWallet,
erc20ParentAddress: parentErc20Address,
});
const approveRecGas = await approveTxGas.wait();
/// Native TOKEN
const approveTx = await erc20Bridger.approveToken({
parentSigner: parentWallet,
erc20ParentAddress: childNetwork.nativeToken!
});
const approveRec = await approveTx.wait();
console.log(
`You successfully allowed the Arbitrum Bridge to spend ERC20 ${approveRec.transactionHash}`
);
// Deposit the token to Child
console.log("Transferring DappToken to Child:");
const depositTx = await erc20Bridger.deposit({
amount: tokenAmount,
erc20ParentAddress: parentErc20Address,
parentSigner: parentWallet,
childProvider: childProvider,
});
// Wait for Parent and Child side of transactions to be confirmed
console.log(
`Deposit initiated: waiting for Child retryable (takes 10-15 minutes; current time: ${new Date().toTimeString()}) `
);
const depositRec = await depositTx.wait();
const childResult = await depositRec.waitForChildTransactionReceipt(childProvider);
// Check if the Parent to Child message was successful
childResult.complete
? console.log(
`Child message successful: status: ${ParentToChildMessageStatus[childResult.status]}`
)
: console.log(
`Child message failed: status ${ParentToChildMessageStatus[childResult.status]}`
);
// Get the final token balance of the Bridge
const finalBridgeTokenBalance = await erc20Contract.balanceOf(
expectedParentGatewayAddress
);
console.log(
`Final Bridge Token Balance: ${finalBridgeTokenBalance.toString()}`
);
};
main().catch((err) => {
console.error(err);
process.exit(1);
});