Skip to content
You are reading GoQuorum development version documentation and some displayed features may not be available in the stable release. You can switch to stable version using the version box at screen bottom.

Updated on August 20, 2021

Transfer funds between accounts

This tutorial shows you how to transfer funds (ETH) between accounts in a transaction on a running network. Use the Quorum Developer Quickstart to rapidly generate a local blockchain network.

Prerequisites

Using eth_sendSignedTransaction

The simplest way to transfer funds between externally-owned accounts is using eth_sendSignedTransaction. This example uses eth_sendSignedTransaction and one of the test accounts to transfer funds to a newly created account.

Security warning

Do not use the test accounts on Ethereum MainNet or any production network.

The private key is publicly displayed, which means the account is not secure.

Create a new file eth_tx.js (or run the following commands in a JavaScript console) to send the transaction. Before making the transaction, the script checks the balances of both accounts to verify the funds transfer after the transaction.

eth_tx.js using eth_sendSignedTransaction

const web3 = new Web3(host);
// Pre-seeded account with 90000 ETH
const privateKeyA = "0x8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63";
const accountA = web3.eth.accounts.privateKeyToAccount(privateKeyA);
var accountABalance = web3.utils.fromWei(await web3.eth.getBalance(accountA.address));
console.log("Account A has balance of: " + accountABalance);

// Create a new account to transfer ETH to
var accountB = web3.eth.accounts.create();
var accountBBalance = web3.utils.fromWei(await web3.eth.getBalance(accountB.address));
console.log("Account B has balance of: " + accountBBalance);

// Send 0x100 ETH from AccountA to AccountB and sign the transaction with
// Account A's private key
const rawTxOptions = {
  nonce: web3.utils.numberToHex(await web3.eth.getTransactionCount(accountA.address)),
  from: accountA.address,
  to: accountB.address,
  value: "0x100", // Amount of ETH to transfer
  gasPrice: "0x0", // ETH per unit of gas
  gasLimit: "0x24A22" // Max number of gas units the tx is allowed to use
};
console.log("Creating transaction...");
const tx = new Tx(rawTxOptions);
console.log("Signing transaction...");
tx.sign(Buffer.from(accountA.privateKey.substring(2), "hex"));
console.log("Sending transaction...");
var serializedTx = tx.serialize();
const pTx = await web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex').toString("hex"));
console.log("tx transactionHash: " + pTx.transactionHash);

// After the transaction, check the updated balances and there should be some ETH transferred
var accountABalance = await getAccountBalance(host, accountA);
console.log("Account A has an updated balance of: " + accountABalance);
var accountBBalance = await getAccountBalance(host, accountB);
console.log("Account B has an updatedbalance of: " + accountBBalance);
}

For reference, the Quorum Developer Quickstart provides an example of a funds transfer script.

Using eth_sendTransaction

An alternative to using eth_sendSignedTransaction is using eth_sendTransaction.

Create a new file eth_tx.js (or run the following commands in a JavaScript console) to send the transaction.

eth_tx.js using eth_sendTransaction

const web3 = new Web3(host);
// Pre-seeded account with 90000 ETH
const privateKeyA = "0x8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63";
const accountA = web3.eth.accounts.privateKeyToAccount(privateKeyA);
var accountABalance = web3.utils.fromWei(await web3.eth.getBalance(accountA.address));
console.log("Account A has balance of: " + accountABalance);

// Create a new account to transfer ETH to
var accountB = web3.eth.accounts.create();
var accountBBalance = web3.utils.fromWei(await web3.eth.getBalance(accountB.address));
console.log("Account B has balance of: " + accountBBalance);

// Send some ETH from A to B
const txOptions = {
  from: accountA.address,
  to: accountB.address,
  value: "0x100",  // Amount of ETH to transfer
  gasPrice: "0x0", // ETH per unit of gas
  gasLimit: "0x24A22" // Max number of gas units the tx is allowed to use
};
console.log("Creating transaction...");
const pTx = await web3.eth.sendTransaction(txOptions);
console.log("tx transactionHash: " + pTx.transactionHash);

// After the transaction, there should be some ETH transferred
var accountABalance = await getAccountBalance(host, accountA);
console.log("Account A has an updated balance of: " + accountABalance);
var accountBBalance = await getAccountBalance(host, accountB);
console.log("Account B has an updatedbalance of: " + accountBBalance);
}
ConsenSys has acquired Quorum from J.P. Morgan. Please read the FAQ.
Questions or feedback? You can discuss issues and obtain free support on GoQuorum Slack channel.
For paid professional support by ConsenSys, contact us at quorum@consensys.net.