forked from telosnetwork/native-to-evm-escrow-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerateEVMTransaction.js
More file actions
89 lines (79 loc) · 3.04 KB
/
Copy pathgenerateEVMTransaction.js
File metadata and controls
89 lines (79 loc) · 3.04 KB
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
import { exec } from 'child_process';
import fs from 'fs';
import { TelosEvmApi } from "@telosnetwork/telosevm-js";
import fetch from "node-fetch";
import Transaction from '@ethereumjs/tx'
import {BigNumber, ethers} from 'ethers';
import contractABI from './abi/TelosEscrow.js';
import dotenv from 'dotenv/config';
const nativeAccount = process.env.NATIVE_ACCOUNT;
const provider = ethers.getDefaultProvider();
const contract = new ethers.Contract(process.env.CONTRACT_ADDRESS, contractABI, provider);
const evmApi = new TelosEvmApi({
endpoint: "https://testnet.telos.net",
chainId: '41',
ethPrivateKeys: [],
fetch: fetch,
telosContract: 'eosio.evm',
telosPrivateKeys: []
});
(async () => {
// POPULATE EVM TRANSACTION
try {
var evmAccount = await evmApi.telos.getEthAccountByTelosAccount(nativeAccount);
var evmAddress = evmAccount.address;
var nonce = parseInt(await evmApi.telos.getNonce(evmAddress), 16);
} catch(e) {
console.log(e.message);
return;
}
const feeData = await provider.getFeeData()
const gasPrice = BigNumber.from(`0x${await evmApi.telos.getGasPrice()}`)
const gasLimit = 26166;
try {
var unsignedTrx = await contract.populateTransaction.setLockDuration(process.env.DURATION);
} catch(e) {
console.log(e.message);
return;
}
unsignedTrx.nonce = nonce;
unsignedTrx.gasLimit = gasLimit;
unsignedTrx.gasPrice = gasPrice;
// SERIALIZE IT
try {
var raw = await ethers.utils.serializeTransaction(unsignedTrx);
} catch(e) {
console.log(e.message);
return;
}
raw = raw.replace(/^0x/, '');
// SAVE THE NATIVE TRANSACTION TO FILE
exec('cleos --url '+ process.env.NETWORK_ENDPOINT +' push action eosio.evm raw \'{"ram_payer": '+nativeAccount+', "tx": "'+ raw +'" , "estimate_gas": false, "sender": "'+ evmAddress.replace(/^0x/, '') +'"}\' --expiration 86400 -sjd --json-file output/transaction.json', (err, stdout, stderr) => {
if (err) {
console.error(err)
} else {
// WAIT 5s, JUST IN CASE
await new Promise((resolve) => {
setTimeout(resolve, 5000);
});
// MODIFY IT
fs.readFile('output/transaction.json', 'utf8', (err, data) => {
if (err) {
console.error(err);
} else {
var jsonData = JSON.parse(data);
jsonData.actions[0].data = jsonData.actions[0].hex_data;
delete jsonData.actions[0].hex_data;
jsonData.transaction_extensions = [];
fs.writeFile('output/transaction.json', JSON.stringify(jsonData), (err) => {
if (err) {
console.error(err);
} else {
console.log('Transaction JSON generated in output/transaction.json')
}
});
}
});
}
});
})()