-
Notifications
You must be signed in to change notification settings - Fork 3
/
complexcall.js
58 lines (48 loc) · 2 KB
/
complexcall.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
const Web3 = require('../ethereum/node_modules/web3/index.js')
const web3 = new Web3()
const config = require('../ethereum/config')
var Tx = require('../ethereum/node_modules/ethereumjs-tx/index.js')
const key = require('./key')
web3.setProvider(new web3.providers.HttpProvider(config.provider.dev))
const keth = require('../ethereum/node_modules/keythereum/index.js')
const fs = require('fs')
var abi = fs.readFileSync("bin/BucJSToken.abi")
var contract = web3.eth.contract(JSON.parse(abi))
var instance = require('./contract.json')
var tokenContract = contract.at(instance.contract)
//console.log(tokenContract)
//const destination = "0xB98B9dCfDF06095408530C395Dea296103469257"
//const destination = "0x3de654b603addf6255a1d88647f703210e389ee6"
const destination = "0x901Add46F7Cabb7Ba8bC51fc5777CC6f3aF47acF"
/*we simulate the call to get the payload data which we'll use in the handcrafted transaction*/
var contractData = tokenContract.transfer.getData(destination,1000)
gasPrice = web3.eth.gasPrice
gasPriceHex = web3.toHex(gasPrice)
gasLimitHex = web3.toHex(500000)
var privateKey = new Buffer(key.key, 'hex')
var sender = keth.privateKeyToAddress(key.key)
/*
since we build transaction by hand, we need the nonce of the sender account
*/
nonce = web3.eth.getTransactionCount(sender)
nonceHex = web3.toHex(nonce)
console.log("nonce is",nonce,"for",sender,"gasLimit",gasLimitHex,"gasPrice",gasPriceHex)
var rawTx = {
nonce: nonceHex,
gasPrice: gasPriceHex,
gasLimit: gasLimitHex,
to: instance.contract,
from: sender,
value: '0x00',
data: contractData
}
var tx = new Tx(rawTx);
console.log("raw",JSON.stringify(rawTx))
tx.sign(privateKey);
var serializedTx = tx.serialize()
var txhex = serializedTx.toString("hex")
if(txhex.substring(0,3) != '0x')
txhex = '0x'+txhex
console.log("sendingTX",txhex)
console.log("transaction sent with hash",web3.eth.sendRawTransaction(txhex))
console.log("balance of",sender,"is",tokenContract.balanceOf(key.address).toString(),"of",tokenContract.symbol(),"tokens")