Skip to content

Instantly share code, notes, and snippets.

@yuyasugano
Created October 28, 2020 05:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuyasugano/99296a0d884110f801526aca0ed1fa15 to your computer and use it in GitHub Desktop.
Save yuyasugano/99296a0d884110f801526aca0ed1fa15 to your computer and use it in GitHub Desktop.
encode Rinkeby DAI sendSignedTransaction
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