Created
October 28, 2020 05:56
-
-
Save yuyasugano/99296a0d884110f801526aca0ed1fa15 to your computer and use it in GitHub Desktop.
encode Rinkeby DAI sendSignedTransaction
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
const receiverAddress = "<Rinkeby receiver address>" | |
const senderAddress = "<Rinkeby sender address>"; | |
const privKey = "<sender private key>"; // do not disclose the private key | |
function sendSignedTx(transactionObject, cb) { | |
let transaction = new EthTx(transactionObject); | |
const privateKey = new Buffer.from(privKey, "hex"); | |
transaction.sign(privateKey); // sign a transaction | |
const serializedEthTx = transaction.serialize().toString("hex"); // serialize the transaction | |
web3.eth.sendSignedTransaction(`0x${serializedEthTx}`, cb); // send signed transaction | |
} | |
web3.eth.getTransactionCount(senderAddress).then(async (transactionNonce) => { | |
const transactionObject = { | |
chainId: 4, | |
nonce: web3.utils.toHex(transactionNonce), | |
gasLimit: web3.utils.toHex(500000), | |
gasPrice: web3.utils.toHex(10000000000), | |
value: 0, | |
to: daiExchangeAddress, | |
from: senderAddress, | |
data: exchangeEncodeABI | |
}; | |
sendSignedTx(transactionObject, (err, ret) => { | |
if (err) { | |
console.log("An error occurred", err) | |
return | |
} | |
console.log("The txHash is: ", ret) | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment