-
Notifications
You must be signed in to change notification settings - Fork 1
/
eopt_transaction_example.js
75 lines (60 loc) · 2.45 KB
/
eopt_transaction_example.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
var fs = require('fs');
var Tx = require('ethereumjs-tx');
const Web3 = require('web3');
var BigNumber = require('bignumber.js');
//Provider examples:
//Mainnet - https://mainnet.infura.io/<api-key>
//Rinkeby - https://rinkeby.infura.io/<api-key>
//Local - http://localhost:7545
const web3 = new Web3('https://mainnet.infura.io/<api-key>'); // provider
var abiArray = JSON.parse(fs.readFileSync('EasyOptionToken.json', 'utf-8')); //token contract ABI
const privateKey = <private key>;
const contractAddress = <EOPT contract address>;
var myAddress = <sender address>;
const chainId = 0x04; //local or mainnet 0x01, ropsten 0x03, rinkeby 0x04
//send EOPT to target address
async function sender( enonce, destAddress, ammo) {
var transferAmount = new BigNumber(ammo * 1000000000000000000);
console.log(`transfer ammo: ${transferAmount}`);
let count = 0;
let balance = 0;
count = await web3.eth.getTransactionCount(myAddress); //my transactions count
console.log(`num transactions so far: ${count}`);
count = count + enonce;
//get contract
var contract = new web3.eth.Contract(abiArray, contractAddress, { from: myAddress });
balance = await contract.methods.balanceOf(myAddress).call(); //my balance
console.log(`Balance before send: ${balance}`);
var rawTransaction = {
"from": myAddress,
"nonce": "0x" + count.toString(16),
"gasPrice": "0x003b9aca00",
"gasLimit": "0x0A000",
"to": contractAddress,
"value": "0x0",
"data": contract.methods.transfer(destAddress, transferAmount).encodeABI(),
"chainId": chainId
};
var privKey = new Buffer(privateKey, 'hex');
var tx = new Tx(rawTransaction);
tx.sign(privKey); //sign with my private key
var serializedTx = tx.serialize();
console.log(`Attempting to send signed tx: ${serializedTx.toString('hex')}`);
var receipt = await web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'));
logger.debug(`Receipt info: ${JSON.stringify(receipt, null, '\t')}`);
//console.log('Complete - ${destAddress} ${transferAmount}');
}
var log4js = require('log4js');
log4js.configure({
appenders: { trans: { type: 'file', filename: 'logs/trans.log' } },
categories: { default: { appenders: ['trans'], level: 'debug' } }
});
const logger = log4js.getLogger('trans');
//example of sending EOPT:
// sender(0, 'address0', amount0 );
// sender(1, 'address1', amount1 );
// sender(2, 'address2', amount2 );
// .
// .
// .
// sender(n, 'addressn', amountn );