Created
March 28, 2019 13:59
-
-
Save rajeshsubhankar/0754b8904ac63a88272ec9fef1f0d12b to your computer and use it in GitHub Desktop.
Create and sends a raw ether transaction with ether.js and Infura
This file contains hidden or 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 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