Skip to content

Instantly share code, notes, and snippets.

@tcrowe
Created August 28, 2018 22:30
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/b47a63e5d815b394ce49df810be7b9cf to your computer and use it in GitHub Desktop.
Save tcrowe/b47a63e5d815b394ce49df810be7b9cf to your computer and use it in GitHub Desktop.
Circle transactions with the client-side-encoded raw transaction.
/*
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(({ privateKey }, index) => {
let account = client.eth.accounts.privateKeyToAccount(privateKey);
let { address } = account;
// get the next one or the first
let friend = accounts[index + 1] || accounts[0];
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);
});
let tx = {
to: friend.address,
value: 123455678,
gas: 54321
};
account.signTransaction(tx, (err, res) => {
if (err !== null && err !== undefined) {
return console.error("error signing transaction", tx, res, err);
}
console.log(index, "messageHash", res.messageHash);
console.log(index, "rawTransaction", res.rawTransaction);
console.log(index, "signature", res.signature);
// decode the transaction if you wish to
// let rlp = require('aion-rlp')
// let decoded = rlp.decode(res.rawTransaction)
// console.log('decoded', decoded)
aion.sendSignedTransaction(res.rawTransaction, (err, res) => {
if (err !== null && err !== undefined) {
return console.error("error sending transaction", err);
}
console.log(index, "transaction hash from server", res);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment