From bafae3f3b956486b06cb3e78dc437028681b7e59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Augusto?= Date: Sat, 4 Feb 2023 00:44:48 +0000 Subject: [PATCH] feat(besu-test-ledger): send funds to already created address MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Enable sending funds to an existing account New method created: * sendEthToAccount closes #2250 Signed-off-by: André Augusto --- .../main/typescript/besu/besu-test-ledger.ts | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/packages/cactus-test-tooling/src/main/typescript/besu/besu-test-ledger.ts b/packages/cactus-test-tooling/src/main/typescript/besu/besu-test-ledger.ts index bf9cb41e00a..a064bcf9dfb 100644 --- a/packages/cactus-test-tooling/src/main/typescript/besu/besu-test-ledger.ts +++ b/packages/cactus-test-tooling/src/main/typescript/besu/besu-test-ledger.ts @@ -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, @@ -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 { - 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 { + 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, }, @@ -188,9 +205,8 @@ export class BesuTestLedger implements ITestLedger { if (receipt instanceof Error) { throw receipt; - } else { - return ethTestAccount; } + return receipt; } public async getBesuKeyPair(): Promise {