-
Notifications
You must be signed in to change notification settings - Fork 0
/
bridgeToArb.js
161 lines (145 loc) · 3.98 KB
/
bridgeToArb.js
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
160
161
const { providers, ethers } = require("ethers");
const {
DefenderRelaySigner,
DefenderRelayProvider,
} = require("defender-relay-client/lib/ethers");
const {
L1ToL2MessageGasEstimator,
} = require("@arbitrum/sdk/dist/lib/message/L1ToL2MessageGasEstimator");
const encodeParameters = (types, values) => {
const abi = new ethers.utils.AbiCoder();
return abi.encode(types, values);
};
//mainnet
const TREASURY_ADDR = "0x6DBDe0E7e563E34A53B1130D6B779ec8eD34B4B9";
const CONNECTOR_ADDR = "0x307ED81138cA91637E432DbaBaC6E3A42699032a";
const UNION_ADDRESS = "0x5Dfe42eEA70a3e6f93EE54eD9C321aF07A85535C";
exports.handler = async function (payload) {
const { INFURA_ID } = payload.secrets;
const provider = new DefenderRelayProvider(payload);
const signer = new DefenderRelaySigner(payload, provider, {
speed: "average",
});
// Step 1: Drip Union to ArbConnector
const treasury = new ethers.Contract(TREASURY_ADDR, TREASURY_ABI, signer);
const dripTx = await treasury.drip(CONNECTOR_ADDR);
await dripTx.wait();
const union = new ethers.Contract(UNION_ADDRESS, ERC20_ABI, signer);
const balanceOfUnion = await union.balanceOf(CONNECTOR_ADDR);
console.log(
`Connector UNION balance: ${ethers.utils.formatEther(balanceOfUnion)}`
);
// Don't bridge if the balance is too small
if (balanceOfUnion.lte(ethers.utils.parseEther("100"))) return;
// Step 2: Bridge to Arbitrum
let l1Provider, l2Provider;
l1Provider = new providers.JsonRpcProvider(
"https://mainnet.infura.io/v3/" + INFURA_ID
);
l2Provider = new providers.JsonRpcProvider("https://arb1.arbitrum.io/rpc");
const dripToComptrollerBytes = encodeParameters(
["uint256"],
["800000000000000000"]
);
const dripToComptrollerBytesLength =
ethers.utils.hexDataLength(dripToComptrollerBytes) + 4;
const l1ToL2MessageGasEstimate = new L1ToL2MessageGasEstimator(l2Provider);
console.log(`dripToComptrollerBytesLength:${dripToComptrollerBytesLength}`);
const _submissionPriceWei =
await l1ToL2MessageGasEstimate.estimateSubmissionFee(
l1Provider,
await l1Provider.getGasPrice(),
dripToComptrollerBytesLength
);
console.log(`_submissionPriceWei:${_submissionPriceWei}`);
const submissionPriceWei = _submissionPriceWei.mul(5);
const maxGas = 275000;
const gasPriceBid = await l2Provider.getGasPrice();
const callValue = submissionPriceWei.add(gasPriceBid.mul(maxGas));
console.log({
gasPriceBid: gasPriceBid.toString(),
submissionPriceWei: submissionPriceWei.toString(),
callValue: callValue.toString(),
});
const connector = new ethers.Contract(CONNECTOR_ADDR, CONNECTOR_ABI, signer);
const bridgeTx = await connector.bridge(
maxGas,
gasPriceBid,
submissionPriceWei,
{
value: callValue,
}
);
console.log(
`Send Union to Arbitrum succeeded! 🙌 ${
(await bridgeTx.wait()).transactionHash
}`
);
};
const TREASURY_ABI = [
{
inputs: [
{
internalType: "address",
name: "target",
type: "address",
},
],
name: "drip",
outputs: [
{
internalType: "uint256",
name: "",
type: "uint256",
},
],
stateMutability: "nonpayable",
type: "function",
},
];
const CONNECTOR_ABI = [
{
inputs: [
{
internalType: "uint256",
name: "maxGas",
type: "uint256",
},
{
internalType: "uint256",
name: "gasPriceBid",
type: "uint256",
},
{
internalType: "uint256",
name: "maxSubmissionCost",
type: "uint256",
},
],
name: "bridge",
outputs: [],
stateMutability: "payable",
type: "function",
},
];
const ERC20_ABI = [
{
inputs: [
{
internalType: "address",
name: "account",
type: "address",
},
],
name: "balanceOf",
outputs: [
{
internalType: "uint256",
name: "",
type: "uint256",
},
],
stateMutability: "view",
type: "function",
},
];