Skip to content

Instantly share code, notes, and snippets.

@JesseAbram
Last active January 17, 2019 17:32
Show Gist options
  • Save JesseAbram/c3728ecc02debdd57283aaf3b657b610 to your computer and use it in GitHub Desktop.
Save JesseAbram/c3728ecc02debdd57283aaf3b657b610 to your computer and use it in GitHub Desktop.
const Web3 = require('web3');
const rp = require('request-promise');
//npm install requiered dependencies
const web3 = new Web3(new Web3.providers.HttpProvider("https://ropsten.infura.io/v3/aeedec09cc774ad0a10200d34374b965")); //my infura key use in good health
const privateKey = 'input your private key';
const hash = async () => {
// const acc = await web3.eth.accounts.privateKeyToAccount(privateKey);
const acc = await web3.eth.accounts.privateKeyToAccount(privateKey);
const nonce = await web3.eth.getTransactionCount(acc.address);
const gas = 21000;
const value = 1000000;
const gasPrice = 20000000000000;
const to = "0x77488ed6c9448E1A5dBe6772d6D31dB85108A53E"; //lol send to me or someone else
const tx =
{
from: acc.address,
to,
value,
gas,
gasPrice,
nonce,
data: "",
}
console.log(tx);
const rawTx = await web3.eth.accounts.signTransaction(tx, privateKey);
console.log(rawTx);
// const txHash = await web3.eth.sendSignedTransaction(rawTx.rawTransaction);
// this web3 call can be done easily with rp. It is just sending a rawTx to a node. Do both ways, web3 will wait for transaction to be mined takes significantly longer.
const body = {
jsonrpc: "2.0",
method: "eth_sendRawTransaction",
params: [rawTx.rawTransaction],
id: 1
};
const txHash = await rp ({
method: "POST",
uri: "https://ropsten.infura.io/v3/aeedec09cc774ad0a10200d34374b965",
body,
json: true
})
console.log(txHash);
}
hash();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment