-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsendERC20.js
197 lines (175 loc) · 6.84 KB
/
sendERC20.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import {config} from "dotenv-flow";
import Web3 from "web3";
import DeBridgeGateJson from "./precompiles/DeBridgeGate.json";
import IERC20Json from "@openzeppelin/contracts/build/contracts/IERC20.json"
import log4js from "log4js";
import web3Utils from "web3-utils";
const {toWei} = web3Utils;
const UINT_MAX_VALUE = '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff';
config();
const Web3RpcUrl = {
1: 'https://mainnet.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161', // //ETH Mainnet
42: 'https://kovan.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161', // //Kovan
56: 'https://bsc-dataseed.binance.org/', // //BSC
97: 'https://data-seed-prebsc-1-s1.binance.org:8545/', // //BSC Testnet
128: 'https://http-mainnet.hecochain.com', // //Heco
256: 'https://http-testnet.hecochain.com', // //Heco Testnet
137: 'https://matic-mainnet.chainstacklabs.com', // //polygon
80001: 'https://rpc-mumbai.maticvigil.com', // //polygon Testnet
42161: 'https://arb1.arbitrum.io/rpc', // //arbitrum
421611: 'https://rinkeby.arbitrum.io/rpc', // //arbitrum Testnet
};
log4js.configure(
{
appenders: {
out: {
type: 'stdout'
}
},
categories: {
default: { appenders: ['out'], level: 'debug' }
}
}
);
const logger = log4js.getLogger('sendERC20');
const tokenAddress = process.env.TOKEN_ADDRESS;
const chainIdFrom = process.env.CHAIN_ID_FROM;
const chainIdTo = process.env.CHAIN_ID_TO;
const amount = process.env.AMOUNT;
const rpc = Web3RpcUrl[chainIdFrom];
const web3 = new Web3(rpc);
const debridgeGateAddress = process.env.DEBRIDGEGATE_ADDRESS;
const debridgeGateInstance = new web3.eth.Contract(DeBridgeGateJson.abi, debridgeGateAddress);
const tokenInstance = new web3.eth.Contract(IERC20Json.abi, tokenAddress);
const privKey = process.env.PRIVATE_KEY;
const account = web3.eth.accounts.privateKeyToAccount(privKey);
const senderAddress = account.address;
logger.info(`ChainId from: ${chainIdFrom}`);
logger.info(`ChainId to: ${chainIdTo}`);
logger.info(`Amount: ${amount}`);
logger.info(`RPC : ${rpc}`);
logger.info(`senderAddress : ${senderAddress}`);
const main = async () => {
const allowance = await getAllowance();
if (allowance < toWei(amount)){
logger.info(`Insufficient allowance ${allowance} for token ${tokenAddress}, calling approve`);
await approve(UINT_MAX_VALUE);
}
await send(
toWei("0.01"), // fix fee for transfer
tokenAddress,//address _tokenAddress,
toWei(amount), // token _amount
chainIdTo,// _chainIdTo
senderAddress, //_receiver
"0x", // _permit
false, //_useAssetFee
0, //_referralCode
"0x" //_autoParams
);
}
main().catch(e => logger.error(e));
async function getAllowance() {
const allowanceString = await tokenInstance.methods.allowance(senderAddress, debridgeGateAddress).call();
return parseInt(allowanceString);
}
async function approve(newAllowance) {
logger.info(`Approving token ${tokenAddress}, amount: ${newAllowance}`);
const nonce = await web3.eth.getTransactionCount(senderAddress);
logger.info("Approve nonce current", nonce);
const gasPrice = await web3.eth.getGasPrice();
logger.info("Approve gasPrice", gasPrice.toString());
let estimateGas = await tokenInstance.methods
.approve(debridgeGateAddress, toWei(amount))
.estimateGas({from: senderAddress})
;
// sometimes not enough estimateGas
estimateGas = estimateGas*2;
logger.info("Approve estimateGas", estimateGas.toString());
const tx = {
from: senderAddress,
to: tokenAddress,
gas: estimateGas,
value: 0,
gasPrice,
nonce,
data: tokenInstance.methods.approve(debridgeGateAddress, newAllowance).encodeABI(),
};
logger.info("Approve tx", tx);
const signedTx = await web3.eth.accounts.signTransaction(tx, privKey);
logger.info("Approve signed tx", signedTx);
let result = await web3.eth.sendSignedTransaction(signedTx.rawTransaction);
logger.info("Approve result", result);
}
async function send(
fixNativeFee, // fix fee for transfer
tokenAddress, //address _tokenAddress,
amount, // uint256 _amount,
chainIdTo, //uint256 _chainIdTo,
receiver, // bytes memory _receiver,
permit, //bytes memory _permit,
useAssetFee, //bool _useAssetFee,
referralCode, //uint32 _referralCode,
autoParams// bytes calldata _autoParams
) {
logger.info("Test send");
const nonce = await web3.eth.getTransactionCount(senderAddress);
logger.info("Send nonce current", nonce);
const gasPrice = await web3.eth.getGasPrice();
logger.info("Send gasPrice", gasPrice.toString());
logger.info({
tokenAddress, //address _tokenAddress,
amount, // uint256 _amount,
chainIdTo, //uint256 _chainIdTo,
receiver, // bytes memory _receiver,
permit, //bytes memory _permit,
useAssetFee, //bool _useAssetFee,
referralCode, //uint32 _referralCode,
autoParams// bytes calldata _autoParams
});
const estimateGas = await debridgeGateInstance.methods
.send(
tokenAddress, //address _tokenAddress,
amount, // uint256 _amount,
chainIdTo, //uint256 _chainIdTo,
receiver, // bytes memory _receiver,
permit, //bytes memory _permit,
useAssetFee, //bool _useAssetFee,
referralCode, //uint32 _referralCode,
autoParams// bytes calldata _autoParams
)
.estimateGas({
from: senderAddress,
value: fixNativeFee
});
logger.info("Send estimateGas", estimateGas.toString());
const tx =
{
from: senderAddress,
to: debridgeGateAddress,
gas: estimateGas,
value: fixNativeFee,
gasPrice: gasPrice,
nonce,
data: debridgeGateInstance.methods
.send(
tokenAddress, //address _tokenAddress,
amount, // uint256 _amount,
chainIdTo, //uint256 _chainIdTo,
receiver, // bytes memory _receiver,
permit, //bytes memory _permit,
useAssetFee, //bool _useAssetFee,
referralCode, //uint32 _referralCode,
autoParams// bytes calldata _autoParams
)
.encodeABI(),
};
logger.info("Send tx", tx);
const signedTx = await web3.eth.accounts.signTransaction(tx, privKey);
logger.info("Send signed tx", signedTx);
let result = await web3.eth.sendSignedTransaction(signedTx.rawTransaction);
logger.info("Send result", result);
const logs = result.logs.find(l=>l.address===debridgeGateAddress);
const submissionId = logs.data.substring(0, 66);
logger.info(`SUBMISSION ID ${submissionId}`);
logger.info("Success");
}