Skip to content

Instantly share code, notes, and snippets.

@totiz
Created May 15, 2019 22:11
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 totiz/7b8417818b4e806a97909395ef61f7c4 to your computer and use it in GitHub Desktop.
Save totiz/7b8417818b4e806a97909395ef61f7c4 to your computer and use it in GitHub Desktop.
const infura_token = '[[YOUR TOKEN]]' // ขอ infura token ได้ที่ infura.io (สมัครฟรี)
const privateKey = '0cb9dd3708cb7307eb6895183cafc18b13efd85e5f8f522832ce838da00245c3'
const publicKey = '0x19f0685F5FC36cc68328B730c63FD5E81514c704'
const gasPrice = '9' // gwei
const ethToSend = '0.001' // ether
const toAddress = '0x3CC0290E21A1Bf2CD6050a3139D17E6C4EF66416'
async function main() {
// โหลด web3
const Web3 = require('web3')
// เชื่อมต่อไปยัง infura
const web3 = new Web3(new Web3.providers.HttpProvider(`https://kovan.infura.io/v3/${infura_token}`))
// อ่านค่า nonce ณ ปัจจุบันเพื่อใช้สร้าง raw tx
// ค่า nonce เป็นค่า sequence number ที่จะต้องเรียงต่อกัน ตามจำนวนธุรกรรมของ address ผู้ส่ง
const nonce = await web3.eth.getTransactionCount(publicKey)
console.log('nonce', nonce)
// Raw tx
var rawTransaction = {
to: toAddress,
value: web3.utils.toWei(ethToSend, 'ether'),
gas: 100000,
gasPrice: web3.utils.toWei(gasPrice, 'gwei'),
nonce: nonce
}
// Sign ด้วย Private key
const signedTx = await web3.eth.accounts.signTransaction(rawTransaction, privateKey)
console.log('Signed Transaction', signedTx)
const receipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction)
console.log("Transaction receipt: ", receipt)
}
(async () => {
try {
var output = await main()
console.log(output)
} catch (e) {
// Deal with the fact the chain failed
console.log('error', e)
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment