From db71c5ce1af09bd1c60b9dbc841ca13c3eb75782 Mon Sep 17 00:00:00 2001 From: Youngone Lee Date: Wed, 16 Jun 2021 09:50:14 -0500 Subject: [PATCH] feat(connector-besu): add get balance method Signed-off-by: Youngone Lee --- .../src/main/json/openapi.json | 25 ++++++ .../generated/openapi/typescript-axios/api.ts | 32 ++++++++ .../plugin-ledger-connector-besu.ts | 14 ++++ .../deploy-contract/get-balance.test.ts | 78 +++++++++++++++++++ 4 files changed, 149 insertions(+) create mode 100644 packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/get-balance.test.ts diff --git a/packages/cactus-plugin-ledger-connector-besu/src/main/json/openapi.json b/packages/cactus-plugin-ledger-connector-besu/src/main/json/openapi.json index ddfe5d1507..413e37aaa1 100644 --- a/packages/cactus-plugin-ledger-connector-besu/src/main/json/openapi.json +++ b/packages/cactus-plugin-ledger-connector-besu/src/main/json/openapi.json @@ -31,6 +31,31 @@ ], "components": { "schemas": { + "GetBalanceV1Response": { + "type": "object", + "required": [ + "balance" + ], + "properties": { + "balance": { + "type": "string" + } + } + }, + "GetBalanceV1Request": { + "type": "object", + "required":[ + "address" + ], + "properties": { + "address": { + "type":"string" + }, + "defaultBlock": { + } + } + }, + "WatchBlocksV1": { "type": "string", "enum": [ diff --git a/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/generated/openapi/typescript-axios/api.ts b/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/generated/openapi/typescript-axios/api.ts index 78f346501b..98d15c5703 100644 --- a/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/generated/openapi/typescript-axios/api.ts +++ b/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/generated/openapi/typescript-axios/api.ts @@ -187,6 +187,38 @@ export enum EthContractInvocationType { Call = 'CALL' } +/** + * + * @export + * @interface GetBalanceV1Request + */ +export interface GetBalanceV1Request { + /** + * + * @type {string} + * @memberof GetBalanceV1Request + */ + address: string; + /** + * + * @type {any} + * @memberof GetBalanceV1Request + */ + defaultBlock?: any | null; +} +/** + * + * @export + * @interface GetBalanceV1Response + */ +export interface GetBalanceV1Response { + /** + * + * @type {string} + * @memberof GetBalanceV1Response + */ + balance: string; +} /** * * @export diff --git a/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/plugin-ledger-connector-besu.ts b/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/plugin-ledger-connector-besu.ts index e0c88ca9bb..ac5930b15d 100644 --- a/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/plugin-ledger-connector-besu.ts +++ b/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/plugin-ledger-connector-besu.ts @@ -11,6 +11,10 @@ import { AbiItem } from "web3-utils"; import { Contract, ContractSendMethod } from "web3-eth-contract"; import { TransactionReceipt } from "web3-eth"; +import { + GetBalanceV1Request, + GetBalanceV1Response, +} from "./generated/openapi/typescript-axios/index"; import { ConsensusAlgorithmFamily, @@ -693,4 +697,14 @@ export class PluginLedgerConnectorBesu return Optional.empty(); } + + public async getBalance( + request: GetBalanceV1Request, + ): Promise { + const balance = await this.web3.eth.getBalance( + request.address, + request.defaultBlock, + ); + return { balance }; + } } diff --git a/packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/get-balance.test.ts b/packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/get-balance.test.ts new file mode 100644 index 0000000000..6920d3680f --- /dev/null +++ b/packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/get-balance.test.ts @@ -0,0 +1,78 @@ +import test, { Test } from "tape"; +import { v4 as uuidv4 } from "uuid"; +import { PluginRegistry } from "@hyperledger/cactus-core"; +import { + PluginLedgerConnectorBesu, + PluginFactoryLedgerConnector, + GetBalanceV1Request, +} from "../../../../../main/typescript/public-api"; +import { PluginKeychainMemory } from "@hyperledger/cactus-plugin-keychain-memory"; +import { BesuTestLedger } from "@hyperledger/cactus-test-tooling"; +import { LogLevelDesc } from "@hyperledger/cactus-common"; +import HelloWorldContractJson from "../../../../solidity/hello-world-contract/HelloWorld.json"; +import Web3 from "web3"; +import { PluginImportType } from "@hyperledger/cactus-core-api"; + +test("can get balance of an account", async (t: Test) => { + const logLevel: LogLevelDesc = "TRACE"; + const besuTestLedger = new BesuTestLedger(); + await besuTestLedger.start(); + + test.onFinish(async () => { + await besuTestLedger.stop(); + await besuTestLedger.destroy(); + }); + + const rpcApiHttpHost = await besuTestLedger.getRpcApiHttpHost(); + const rpcApiWsHost = await besuTestLedger.getRpcApiWsHost(); + + /** + * Constant defining the standard 'dev' Besu genesis.json contents. + * + * @see https://github.com/hyperledger/besu/blob/1.5.1/config/src/main/resources/dev.json + */ + const firstHighNetWorthAccount = "627306090abaB3A6e1400e9345bC60c78a8BEf57"; + /* + const besuKeyPair = { + privateKey: + "c87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3", + }; + const contractName = "HelloWorld"; + + */ + const web3 = new Web3(rpcApiHttpHost); + const testEthAccount = web3.eth.accounts.create(uuidv4()); + + const keychainEntryKey = uuidv4(); + const keychainEntryValue = testEthAccount.privateKey; + const keychainPlugin = new PluginKeychainMemory({ + instanceId: uuidv4(), + keychainId: uuidv4(), + // pre-provision keychain with mock backend holding the private key of the + // test account that we'll reference while sending requests with the + // signing credential pointing to this keychain entry. + backend: new Map([[keychainEntryKey, keychainEntryValue]]), + logLevel, + }); + keychainPlugin.set( + HelloWorldContractJson.contractName, + HelloWorldContractJson, + ); + const factory = new PluginFactoryLedgerConnector({ + pluginImportType: PluginImportType.Local, + }); + const connector: PluginLedgerConnectorBesu = await factory.create({ + rpcApiHttpHost, + rpcApiWsHost, + instanceId: uuidv4(), + pluginRegistry: new PluginRegistry({ plugins: [keychainPlugin] }), + }); + + const req: GetBalanceV1Request = { address: firstHighNetWorthAccount }; + const currentBalance = await connector.getBalance(req); + t.comment(JSON.stringify(currentBalance)); + //makes the information in to string + t.ok(currentBalance, " Balance response is OK :-)"); + t.equal(typeof currentBalance, "object", "Balance response type is OK :-)"); + t.end(); +});