Skip to content

Commit

Permalink
feat(connector-besu): add get balance method
Browse files Browse the repository at this point in the history
Signed-off-by: Youngone Lee <youngone.lee@accenture.com>
  • Loading branch information
Leeyoungone authored and petermetz committed Jun 22, 2021
1 parent d1f9560 commit db71c5c
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -693,4 +697,14 @@ export class PluginLedgerConnectorBesu

return Optional.empty();
}

public async getBalance(
request: GetBalanceV1Request,
): Promise<GetBalanceV1Response> {
const balance = await this.web3.eth.getBalance(
request.address,
request.defaultBlock,
);
return { balance };
}
}
Original file line number Diff line number Diff line change
@@ -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();
});

0 comments on commit db71c5c

Please sign in to comment.