Created
April 11, 2018 18:05
-
-
Save lakamsani/47c246e91d9990bab0d89cb9f86b5d95 to your computer and use it in GitHub Desktop.
sendRawTransaction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import EthUtil from 'ethereumjs-util' | |
import EthWallet from 'ethereumjs-wallet' | |
import SolidityFunction from "web3/lib/web3/function" | |
import EthTx from 'ethereumjs-tx' | |
import contract from 'truffle-contract' | |
import lodash from 'lodash' | |
async callContract(wallet, deployedContract, methodName, params=[], options={}) { | |
let solidityFunction = new SolidityFunction('', lodash.find(deployedContract.abi, { name: methodName }), '') | |
const payloadData = solidityFunction.toPayload(params).data | |
let gasPrice = options.gasPrice || this.web3.eth.gasPrice | |
let gasLimit = options.gas || this.web3.eth.estimateGas({ to: deployedContract.address, data: payloadData }) | |
let rawTx = { | |
nonce: this.web3.toHex(this.web3.eth.getTransactionCount(wallet.publicKey)), | |
from: wallet.publicKey, | |
to: deployedContract.address, | |
data: payloadData, | |
gasLimit: this.web3.toHex(gasLimit), | |
gasPrice: this.web3.toHex(gasPrice), | |
} | |
log.info(rawTx) | |
const txData = this.signTx(wallet.privateKey) | |
const txHash = await this.web3.eth.sendRawTransaction(`0x${txData}`) | |
const txReceipt = await web3.eth.getTransactionReceipt(txHash) | |
return txReceipt | |
} | |
signTx(privateKeyHex, rawTx) { | |
const keyOnly = privateKeyHex.slice(2) | |
const pkeyBuffer = Buffer.from(keyOnly, 'hex') | |
let tx = new EthTx(rawTx) | |
tx.sign(pkeyBuffer) | |
const txData = tx.serialize().toString('hex') | |
return txData | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment