Skip to content

Commit

Permalink
feat(connector-besu): add getBalance web service
Browse files Browse the repository at this point in the history
Fixes #1066

Signed-off-by: Youngone Lee <[email protected]>
  • Loading branch information
Leeyoungone authored and petermetz committed Jun 28, 2021
1 parent 40be742 commit 50107f6
Show file tree
Hide file tree
Showing 6 changed files with 386 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,40 @@
}
}
},
"/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-besu/get-balance": {
"post": {
"x-hyperledger-cactus": {
"http": {
"verbLowerCase": "post",
"path": "/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-besu/get-balance"
}
},
"operationId": "getbalanceV1",
"summary": "Return balance of an address of a given block",
"parameters": [],
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/GetBalanceV1Request"
}
}
}
},
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/GetBalanceV1Response"
}
}
}
}
}
}
},
"/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-besu/run-transaction": {
"post": {
"x-hyperledger-cactus": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,40 @@ export const DefaultApiAxiosParamCreator = function (configuration?: Configurati
options: localVarRequestOptions,
};
},
/**
*
* @summary Return balance of an address of a given block
* @param {GetBalanceV1Request} [getBalanceV1Request]
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
getbalanceV1: async (getBalanceV1Request?: GetBalanceV1Request, options: any = {}): Promise<RequestArgs> => {
const localVarPath = `/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-besu/get-balance`;
// use dummy base URL string because the URL constructor only accepts absolute URLs.
const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL);
let baseOptions;
if (configuration) {
baseOptions = configuration.baseOptions;
}

const localVarRequestOptions = { method: 'POST', ...baseOptions, ...options};
const localVarHeaderParameter = {} as any;
const localVarQueryParameter = {} as any;



localVarHeaderParameter['Content-Type'] = 'application/json';

setSearchParams(localVarUrlObj, localVarQueryParameter, options.query);
let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {};
localVarRequestOptions.headers = {...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers};
localVarRequestOptions.data = serializeDataIfNeeded(getBalanceV1Request, localVarRequestOptions, configuration)

return {
url: toPathString(localVarUrlObj),
options: localVarRequestOptions,
};
},
/**
* Obtain signatures of ledger from the corresponding transaction hash.
* @summary Obtain signatures of ledger from the corresponding transaction hash.
Expand Down Expand Up @@ -1076,6 +1110,17 @@ export const DefaultApiFp = function(configuration?: Configuration) {
const localVarAxiosArgs = await localVarAxiosParamCreator.getPrometheusExporterMetricsV1(options);
return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration);
},
/**
*
* @summary Return balance of an address of a given block
* @param {GetBalanceV1Request} [getBalanceV1Request]
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
async getbalanceV1(getBalanceV1Request?: GetBalanceV1Request, options?: any): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<GetBalanceV1Response>> {
const localVarAxiosArgs = await localVarAxiosParamCreator.getbalanceV1(getBalanceV1Request, options);
return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration);
},
/**
* Obtain signatures of ledger from the corresponding transaction hash.
* @summary Obtain signatures of ledger from the corresponding transaction hash.
Expand Down Expand Up @@ -1136,6 +1181,16 @@ export const DefaultApiFactory = function (configuration?: Configuration, basePa
getPrometheusExporterMetricsV1(options?: any): AxiosPromise<string> {
return localVarFp.getPrometheusExporterMetricsV1(options).then((request) => request(axios, basePath));
},
/**
*
* @summary Return balance of an address of a given block
* @param {GetBalanceV1Request} [getBalanceV1Request]
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
getbalanceV1(getBalanceV1Request?: GetBalanceV1Request, options?: any): AxiosPromise<GetBalanceV1Response> {
return localVarFp.getbalanceV1(getBalanceV1Request, options).then((request) => request(axios, basePath));
},
/**
* Obtain signatures of ledger from the corresponding transaction hash.
* @summary Obtain signatures of ledger from the corresponding transaction hash.
Expand Down Expand Up @@ -1203,6 +1258,18 @@ export class DefaultApi extends BaseAPI {
return DefaultApiFp(this.configuration).getPrometheusExporterMetricsV1(options).then((request) => request(this.axios, this.basePath));
}

/**
*
* @summary Return balance of an address of a given block
* @param {GetBalanceV1Request} [getBalanceV1Request]
* @param {*} [options] Override http request option.
* @throws {RequiredError}
* @memberof DefaultApi
*/
public getbalanceV1(getBalanceV1Request?: GetBalanceV1Request, options?: any) {
return DefaultApiFp(this.configuration).getbalanceV1(getBalanceV1Request, options).then((request) => request(this.axios, this.basePath));
}

/**
* Obtain signatures of ledger from the corresponding transaction hash.
* @summary Obtain signatures of ledger from the corresponding transaction hash.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import {
IGetPrometheusExporterMetricsEndpointV1Options,
} from "./web-services/get-prometheus-exporter-metrics-endpoint-v1";
import { WatchBlocksV1Endpoint } from "./web-services/watch-blocks-v1-endpoint";
import { GetBalanceEndpoint } from "./web-services/get-balance-endpoint";

export const E_KEYCHAIN_NOT_FOUND = "cactus.connector.besu.keychain_not_found";

Expand Down Expand Up @@ -205,6 +206,13 @@ export class PluginLedgerConnectorBesu
});
endpoints.push(endpoint);
}
{
const endpoint = new GetBalanceEndpoint({
connector: this,
logLevel: this.options.logLevel,
});
endpoints.push(endpoint);
}
{
const endpoint = new RunTransactionEndpoint({
connector: this,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { Express, Request, Response } from "express";

import {
Logger,
Checks,
LogLevelDesc,
LoggerProvider,
IAsyncProvider,
} from "@hyperledger/cactus-common";
import {
IEndpointAuthzOptions,
IExpressRequestHandler,
IWebServiceEndpoint,
} from "@hyperledger/cactus-core-api";
import { registerWebServiceEndpoint } from "@hyperledger/cactus-core";

import { PluginLedgerConnectorBesu } from "../plugin-ledger-connector-besu";

import OAS from "../../json/openapi.json";

export interface IGetBalanceEndpointOptions {
logLevel?: LogLevelDesc;
connector: PluginLedgerConnectorBesu;
}

export class GetBalanceEndpoint implements IWebServiceEndpoint {
public static readonly CLASS_NAME = "GetBalanceEndpoint";

private readonly log: Logger;

public get className(): string {
return GetBalanceEndpoint.CLASS_NAME;
}

constructor(public readonly options: IGetBalanceEndpointOptions) {
const fnTag = `${this.className}#constructor()`;
Checks.truthy(options, `${fnTag} arg options`);
Checks.truthy(options.connector, `${fnTag} arg options.connector`);

const level = this.options.logLevel || "INFO";
const label = this.className;
this.log = LoggerProvider.getOrCreate({ level, label });
}

public getOasPath() {
return OAS.paths[
"/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-besu/get-balance"
];
}

public getPath(): string {
const apiPath = this.getOasPath();
return apiPath.post["x-hyperledger-cactus"].http.path;
}

public getVerbLowerCase(): string {
const apiPath = this.getOasPath();
return apiPath.post["x-hyperledger-cactus"].http.verbLowerCase;
}

public getOperationId(): string {
return this.getOasPath().post.operationId;
}

getAuthorizationOptionsProvider(): IAsyncProvider<IEndpointAuthzOptions> {
// TODO: make this an injectable dependency in the constructor
return {
get: async () => ({
isProtected: true,
requiredRoles: [],
}),
};
}

public async registerExpress(
expressApp: Express,
): Promise<IWebServiceEndpoint> {
await registerWebServiceEndpoint(expressApp, this);
return this;
}

public getExpressRequestHandler(): IExpressRequestHandler {
return this.handleRequest.bind(this);
}

public async handleRequest(req: Request, res: Response): Promise<void> {
const reqTag = `${this.getVerbLowerCase()} - ${this.getPath()}`;
this.log.debug(reqTag);
const reqBody = req.body;
try {
const resBody = await this.options.connector.getBalance(reqBody);
res.json(resBody);
} catch (ex) {
this.log.error(`Crash while serving ${reqTag}`, ex);
res.status(500).json({
message: "Internal Server Error",
error: ex?.stack || ex?.message,
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,6 @@ test("can get balance of an account", async (t: Test) => {
* @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());

Expand Down
Loading

0 comments on commit 50107f6

Please sign in to comment.