Skip to content

Instantly share code, notes, and snippets.

@rajeshsubhankar
Created March 28, 2019 13:59
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 rajeshsubhankar/0754b8904ac63a88272ec9fef1f0d12b to your computer and use it in GitHub Desktop.
Save rajeshsubhankar/0754b8904ac63a88272ec9fef1f0d12b to your computer and use it in GitHub Desktop.
Create and sends a raw ether transaction with ether.js and Infura
const ethers = require('ethers');
const provider = new ethers.providers.JsonRpcProvider('https://ropsten.infura.io/v2/INFURA_PROJECT_ID');
const addressFrom = 'SENDER_ADDRESS';
const privateKey = 'SENDER_ADDRESS_PRIVATE_KEY';
const wallet = new ethers.Wallet(privateKey, provider);
const addressTo = 'RECEIVER_ADDRESS';
provider.getTransactionCount(addressFrom)
.then((txCount) => {
// construct the transaction data
const txData = {
nonce: txCount,
gasLimit: 25000,
gasPrice: 10e9, // 10 Gwei
to: addressTo,
value: ethers.utils.parseEther('0.1'), // 0.1 ether
}
const serializedTx = ethers.utils.serializeTransaction(txData);
//console.log('serialized Tx: ', serializedTx);
//console.log('parsed serialized tx: ', ethers.utils.parseTransaction(serializedTx));
// parse unsigned serialized transaction and sign it
wallet.sign(ethers.utils.parseTransaction(serializedTx))
.then((signedSerializedTx) => {
//console.log('signed serialized tx: ', signedSerializedTx);
// broadcast signed serialized tx
provider.sendTransaction(signedSerializedTx)
.then((response) => {
console.log('tx response: ', response);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment