Skip to content

Instantly share code, notes, and snippets.

@helxsz
Created February 20, 2018 08:32
Show Gist options
  • Save helxsz/f3d57762395ba73defecf244ae390e32 to your computer and use it in GitHub Desktop.
Save helxsz/f3d57762395ba73defecf244ae390e32 to your computer and use it in GitHub Desktop.
send transaction
sendSigned(txData,wallet_privatekey, cb) {
var web3 = this.web3;
const tx = new Tx(txData)
var _pk = wallet_privatekey.slice(2);
var privateKey = new Buffer(_pk, 'hex');
tx.sign(privateKey)
const serializedTx = tx.serialize()
web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'), cb)
}
sendToken(fromAddress,wallet_privatekey,abiArray,contractAddress){
var web3 = this.web3;
var self = this;
var contractAddress = "0x8";
var contract = web3.eth.Contract(abiArray).at(contractAddress);
web3.eth.getTransactionCount(fromAddress).then(txCount => {
console.log("nonce:" + txCount);
const txData = {
nonce: web3.utils.toHex(txCount),
gasPrice: web3.utils.toHex(web3.utils.toWei('0.00000009', 'ether')),
gasLimit: web3.utils.toHex(30000),
to: contractAddress,
value: "0x0",
data: contract.transfer.getData("0xCb...", 10, {from: "0x26..."}),
//chainId: 1
}
console.log("send----->");
self.sendSigned(txData,wallet_privatekey, function (err, result) {
if (err) return
alert("error:" + err + "");
console.log('error', err)
console.log('sent', result)
alert("" + result + "");
})
})
}
sendData(fromAddress,toAddress,wallet_privatekey) {
var web3 = this.web3;
var self = this;
web3.eth.getTransactionCount(fromAddress).then(txCount => {
console.log("nonce:" + txCount);
const txData = {
nonce: web3.utils.toHex(txCount),
gasPrice: web3.utils.toHex(web3.utils.toWei('0.00000009', 'ether')),
gasLimit: web3.utils.toHex(30000),
to: toAddress,
value: web3.utils.numberToHex(web3.utils.toWei('0.0001', 'ether')),
data: web3.utils.asciiToHex('hello'),
//chainId: 1
}
console.log("send----->");
self.sendSigned(txData,wallet_privatekey, function (err, result) {
if (err) return
alert("error:" + err + "");
console.log('error', err)
console.log('sent', result)
alert("" + result + "");
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment