Skip to content

Commit

Permalink
feat(besu-test-ledger): send funds to already created address
Browse files Browse the repository at this point in the history
Enable sending funds to an existing account

New method created:
* sendEthToAccount

closes hyperledger-cacti#2250

Signed-off-by: André Augusto <andre.augusto@tecnico.ulisboa.pt>
Signed-off-by: Peter Somogyvari <peter.somogyvari@accenture.com>
  • Loading branch information
AndreAugusto11 authored and petermetz committed Mar 31, 2023
1 parent 187f26e commit 737bae7
Showing 1 changed file with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Joi from "joi";
import tar from "tar-stream";
import { EventEmitter } from "events";
import Web3 from "web3";
import { Account } from "web3-core";
import { Account, TransactionReceipt } from "web3-core";
import {
LogLevelDesc,
Logger,
Expand Down Expand Up @@ -164,16 +164,33 @@ export class BesuTestLedger implements ITestLedger {
* @param [seedMoney=10e8] The amount of money to seed the new test account with.
*/
public async createEthTestAccount(seedMoney = 10e8): Promise<Account> {
const fnTag = `BesuTestLedger#getEthTestAccount()`;

const rpcApiHttpHost = await this.getRpcApiHttpHost();
const web3 = new Web3(rpcApiHttpHost);
const ethTestAccount = web3.eth.accounts.create(uuidv4());

this.sendEthToAccount(ethTestAccount.address, seedMoney);

return ethTestAccount;
}

/**
* Sends seed money to a provided address to get things started.
*
* @param [seedMoney=10e8] The amount of money to seed the new test account with.
*/
public async sendEthToAccount(
address: string,
seedMoney = 10e8,
): Promise<TransactionReceipt> {
const fnTag = `BesuTestLedger#sendEthToAccount()`;

const rpcApiHttpHost = await this.getRpcApiHttpHost();
const web3 = new Web3(rpcApiHttpHost);

const tx = await web3.eth.accounts.signTransaction(
{
from: this.getGenesisAccountPubKey(),
to: ethTestAccount.address,
to: address,
value: seedMoney,
gas: 1000000,
},
Expand All @@ -188,9 +205,8 @@ export class BesuTestLedger implements ITestLedger {

if (receipt instanceof Error) {
throw receipt;
} else {
return ethTestAccount;
}
return receipt;
}

public async getBesuKeyPair(): Promise<IKeyPair> {
Expand Down

0 comments on commit 737bae7

Please sign in to comment.