Skip to content

Instantly share code, notes, and snippets.

@tcrowe
Last active August 28, 2018 22:25
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 tcrowe/ac6d6d5ae6b9363ade9585a18ba1525b to your computer and use it in GitHub Desktop.
Save tcrowe/ac6d6d5ae6b9363ade9585a18ba1525b to your computer and use it in GitHub Desktop.
web3 transaction test: `getBalance` ➡️`unlockAccount` ➡️`sendTransaction`
/*
it will send transactions in a circle to each account
import aion account to node:
./aion.sh -a import private-key-hex
*/
let Web3 = require('../../src/index')
// let Web3 = require('aion-web3')
// let Web3 = require('web3')
let client = new Web3(new Web3.providers.HttpProvider('http://127.0.0.1:8545'))
/*
⚠️fill in these values
because it uses `unlockAccount` rpc call it needs the password
*/
let accounts = [
{
address: '',
password: '',
privateKey: ''
},
{
address: '',
password: '',
privateKey: ''
},
{
address: '',
password: '',
privateKey: ''
}
]
accounts.forEach((account, index) => {
let {address, password} = account
console.log('address', address)
client.eth.getBalance(address, (err, res) => {
if (err !== null && err !== undefined) {
console.error('error getting balance', address, err)
return
}
console.log('balance', address, res)
})
client.personal.unlockAccount(address, password, 5, (err, res) => {
if (err !== null && err !== undefined) {
console.error('error unlocking account', err)
return
}
console.log('unlocked', address)
let friend = accounts[index + 1] || accounts[0]
let from = address
let to = friend.address
let value = 1000000
let tx = {from, to, value}
client.eth.sendTransaction(tx, (err, res) => {
if (err !== null && err !== undefined) {
console.error('error sending transaction', err)
return
}
console.log('transactionHash', res)
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment