· 5 years ago · Dec 09, 2020, 06:02 PM
1// CONSTANT VALUES
2const DERIVATE_PATH = `44'/60'/0'/0/0`;
3const GAS_PRICE = (55000000000).toString(16); // 55
4const GAS_LIMIT = (650000).toString(16); // 650000 should be more than enough
5const CONTRACT_ADDRESS = '0x63ad002AF11d80E663Cc54956769607Df5dC74a4';
6
7import Web3 from 'web3-eth';
8let infuraURL = 'https://mainnet.infura.io/v3/7195ee8b4dde40c586201cc8cae5f50e'; // Feel free to use my API key
9const web3 = new Web3(infuraURL);
10
11import Transport from "@ledgerhq/hw-transport-node-hid";
12import Eth from "@ledgerhq/hw-app-eth";
13
14import EthereumTx from 'ethereumjs-tx';
15
16// Copy the transaction data below
17// To get the transaction data:
18// 1. Go to the https://www.gameswap.org/farms and use the button to withdraw all the rewards
19// 2. A metamask window will appear
20// 3. Go to the "data" tab in the metamask window
21// 4. Copy everything from the hex data field below
22const txData = '';
23
24const txParams = {
25 nonce: '0x2cf',
26 gasPrice: '0x' + GAS_PRICE,
27 gasLimit: '0x' + GAS_LIMIT,
28 to: CONTRACT_ADDRESS,
29 value: '0x00',
30 data: txData
31};
32
33const tx = new EthereumTx.Transaction(txParams, { chain: 'mainnet' });
34
35// MAIN LOGIC
36async function main() {
37 const transport = await Transport.default.create();
38 const eth = new Eth.default(transport);
39
40 console.log('Getting FROM address');
41 let fromAddress = await eth.getAddress(DERIVATE_PATH);
42 console.log('ADDRESS: ' + fromAddress.address);
43
44 console.log('Getting FROM address balance');
45 let balance = await web3.getBalance(fromAddress.address);
46 console.log('BALANCE: ' + balance);
47
48 let totalRequiredWei = parseInt(txParams.gasPrice, 16) * parseInt(txParams.gasLimit, 16) + parseInt(txParams.value, 16);
49 console.log('REQUIRED WEI: ' + totalRequiredWei);
50
51 // Set the EIP155 Bits before signing
52 tx.raw[6] = Buffer.from([1]); // v
53 tx.raw[7] = Buffer.from([]); // r
54 tx.raw[8] = Buffer.from([]); // s
55
56 // LEDGER signing
57 eth.signTransaction(DERIVATE_PATH, tx.serialize().toString('hex')).then(result => {
58 txParams.v = Buffer.from(result['v'], 'hex');
59 txParams.r = Buffer.from(result['r'], 'hex');
60 txParams.s = Buffer.from(result['s'], 'hex');
61
62 const signedTx = new EthereumTx.Transaction(txParams, { chain: 'mainnet' });
63 let signedData = '0x' + signedTx.serialize().toString('hex');
64 web3.sendSignedTransaction(signedData);
65
66 }).catch(error => {
67 console.log(error);}
68 );
69}
70
71main();