Skip to content

Commit

Permalink
feat(connector-besu): getTransaction method
Browse files Browse the repository at this point in the history
Signed-off-by: Jeffrey Ushry <jeffrey.ushry@accenture.com>
  • Loading branch information
Jeff-Ushry authored and petermetz committed Jun 27, 2021
1 parent cacb7bc commit d470540
Show file tree
Hide file tree
Showing 5 changed files with 266 additions and 0 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,62 @@
}
},

"EvmTransaction":{
"type":"object",
"properties":{
"hash":{
"type":"string"
},
"nonce":{
"type":"number"
},
"blockHash":{
},
"blockNumber":{
},
"transactionIndex":{
},
"from":{
"type":"string"
},
"to":{
},
"value":{
"type":"string"
},
"gasPrice":{
"type":"string"
},
"gas":{
"type":"number"
},
"input":{
"type":"string"
}
}
},
"GetTransactionV1Response": {
"type": "object",
"required": [
"transaction"
],
"properties": {
"transaction": {
"$ref":"#/components/schemas/EvmTransaction"
}
}

},
"GetTransactionV1Request": {
"type": "object",
"required": ["transactionHash"],
"properties": {
"transactionHash":{
"type": "string"
}
}

},
"WatchBlocksV1": {
"type": "string",
"enum": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,79 @@ export enum EthContractInvocationType {
Call = 'CALL'
}

/**
*
* @export
* @interface EvmTransaction
*/
export interface EvmTransaction {
/**
*
* @type {string}
* @memberof EvmTransaction
*/
hash?: string;
/**
*
* @type {number}
* @memberof EvmTransaction
*/
nonce?: number;
/**
*
* @type {any}
* @memberof EvmTransaction
*/
blockHash?: any | null;
/**
*
* @type {any}
* @memberof EvmTransaction
*/
blockNumber?: any | null;
/**
*
* @type {any}
* @memberof EvmTransaction
*/
transactionIndex?: any | null;
/**
*
* @type {string}
* @memberof EvmTransaction
*/
from?: string;
/**
*
* @type {any}
* @memberof EvmTransaction
*/
to?: any | null;
/**
*
* @type {string}
* @memberof EvmTransaction
*/
value?: string;
/**
*
* @type {string}
* @memberof EvmTransaction
*/
gasPrice?: string;
/**
*
* @type {number}
* @memberof EvmTransaction
*/
gas?: number;
/**
*
* @type {string}
* @memberof EvmTransaction
*/
input?: string;
}
/**
*
* @export
Expand Down Expand Up @@ -219,6 +292,32 @@ export interface GetBalanceV1Response {
*/
balance: string;
}
/**
*
* @export
* @interface GetTransactionV1Request
*/
export interface GetTransactionV1Request {
/**
*
* @type {string}
* @memberof GetTransactionV1Request
*/
transactionHash: string;
}
/**
*
* @export
* @interface GetTransactionV1Response
*/
export interface GetTransactionV1Response {
/**
*
* @type {EvmTransaction}
* @memberof GetTransactionV1Response
*/
transaction: EvmTransaction;
}
/**
*
* @export
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ import {
Web3SigningCredentialCactusKeychainRef,
Web3SigningCredentialPrivateKeyHex,
Web3SigningCredentialType,
GetTransactionV1Request,
GetTransactionV1Response,
} from "./generated/openapi/typescript-axios/";

import { RunTransactionEndpoint } from "./web-services/run-transaction-endpoint";
Expand Down Expand Up @@ -711,4 +713,13 @@ export class PluginLedgerConnectorBesu
);
return { balance };
}

public async getTransaction(
request: GetTransactionV1Request,
): Promise<GetTransactionV1Response> {
const transaction = await this.web3.eth.getTransaction(
request.transactionHash,
);
return { transaction };
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import test, { Test } from "tape";
import { v4 as uuidv4 } from "uuid";
import { PluginRegistry } from "@hyperledger/cactus-core";
import {
PluginLedgerConnectorBesu,
PluginFactoryLedgerConnector,
ReceiptType,
Web3SigningCredentialType,
} 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";
import { GetTransactionV1Request } from "../../../../main/typescript/generated/openapi/typescript-axios/api";

test("can get past logs 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 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 privateKey = await besuTestLedger.getGenesisAccountPrivKey();
const { transactionReceipt } = await connector.transact({
web3SigningCredential: {
ethAccount: firstHighNetWorthAccount,
secret: privateKey,
type: Web3SigningCredentialType.PrivateKeyHex,
},
consistencyStrategy: {
blockConfirmations: 0,
receiptType: ReceiptType.LedgerBlockAck,
timeoutMs: 60000,
},
transactionConfig: {
from: firstHighNetWorthAccount,
to: testEthAccount.address,
value: 10e9,
gas: 1000000,
},
});

const req: GetTransactionV1Request = {
transactionHash: transactionReceipt.transactionHash,
};
const response = await connector.getTransaction(req);
t.comment(JSON.stringify(response.transaction));
t.ok(response.transaction, "getTransaction response is OK :-)");
t.equal(
typeof response.transaction,
"object",
"getTransaction response type is OK :-)",
);
t.end();
});

0 comments on commit d470540

Please sign in to comment.