From c79d763e0cb093647209417cfed7a2645283f302 Mon Sep 17 00:00:00 2001 From: AzaharaC Date: Tue, 30 Mar 2021 13:56:13 +0200 Subject: [PATCH] feat(connector-quorum): contractAbi optional parameter Signed-off-by: AzaharaC --- .../deploy-contract-from-json.test.ts | 14 ------- .../src/main/json/openapi.json | 11 ++++++ .../generated/openapi/typescript-axios/api.ts | 12 ++++++ .../plugin-ledger-connector-quorum.ts | 15 ++++++- .../deploy-contract-from-json.test.ts | 39 ++++++++++++------- 5 files changed, 63 insertions(+), 28 deletions(-) diff --git a/packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/deploy-contract-from-json.test.ts b/packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/deploy-contract-from-json.test.ts index 1c96958a48..22b16e2845 100644 --- a/packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/deploy-contract-from-json.test.ts +++ b/packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/deploy-contract-from-json.test.ts @@ -31,7 +31,6 @@ import { K_CACTUS_BESU_TOTAL_TX_COUNT } from "../../../../../main/typescript/pro const testCase = "deploys contract via .json file"; const logLevel: LogLevelDesc = "TRACE"; -const contractName = "HelloWorld"; test("BEFORE " + testCase, async (t: Test) => { const pruning = pruneDockerAllIfGithubAction({ logLevel }); @@ -165,7 +164,6 @@ test(testCase, async (t: Test) => { ); const { callOutput: helloMsg } = await connector.invokeContract({ - contractName, contractAbi: HelloWorldContractJson.abi, contractAddress, invocationType: EthContractInvocationType.CALL, @@ -220,7 +218,6 @@ test(testCase, async (t: Test) => { test("invoke Web3SigningCredentialType.PRIVATEKEYHEX", async (t2: Test) => { const newName = `DrCactus${uuidv4()}`; const setNameOut = await connector.invokeContract({ - contractName, contractAbi: HelloWorldContractJson.abi, contractAddress, invocationType: EthContractInvocationType.SEND, @@ -237,7 +234,6 @@ test(testCase, async (t: Test) => { try { const setNameOutInvalid = await connector.invokeContract({ - contractName, contractAbi: HelloWorldContractJson.abi, contractAddress, invocationType: EthContractInvocationType.SEND, @@ -260,7 +256,6 @@ test(testCase, async (t: Test) => { ); } const { callOutput: getNameOut } = await connector.invokeContract({ - contractName, contractAbi: HelloWorldContractJson.abi, contractAddress, invocationType: EthContractInvocationType.CALL, @@ -276,7 +271,6 @@ test(testCase, async (t: Test) => { t2.equal(getNameOut, newName, `getName() output reflects the update OK`); const getNameOut2 = await connector.invokeContract({ - contractName, contractAbi: HelloWorldContractJson.abi, contractAddress, invocationType: EthContractInvocationType.SEND, @@ -292,7 +286,6 @@ test(testCase, async (t: Test) => { t2.ok(getNameOut2, "getName() invocation #2 output is truthy OK"); const response = await connector.invokeContract({ - contractName, contractAbi: HelloWorldContractJson.abi, contractAddress, invocationType: EthContractInvocationType.SEND, @@ -309,7 +302,6 @@ test(testCase, async (t: Test) => { t2.ok(response, "deposit() payable invocation output is truthy OK"); const { callOutput } = await connector.invokeContract({ - contractName, contractAbi: HelloWorldContractJson.abi, contractAddress, invocationType: EthContractInvocationType.CALL, @@ -342,7 +334,6 @@ test(testCase, async (t: Test) => { }; const setNameOut = await connector.invokeContract({ - contractName, contractAbi: HelloWorldContractJson.abi, contractAddress, invocationType: EthContractInvocationType.SEND, @@ -356,7 +347,6 @@ test(testCase, async (t: Test) => { try { const setNameOutInvalid = await connector.invokeContract({ - contractName, contractAbi: HelloWorldContractJson.abi, contractAddress, invocationType: EthContractInvocationType.SEND, @@ -376,7 +366,6 @@ test(testCase, async (t: Test) => { } const { callOutput: getNameOut } = await connector.invokeContract({ - contractName, contractAbi: HelloWorldContractJson.abi, contractAddress, invocationType: EthContractInvocationType.CALL, @@ -388,7 +377,6 @@ test(testCase, async (t: Test) => { t2.equal(getNameOut, newName, `getName() output reflects the update OK`); const getNameOut2 = await connector.invokeContract({ - contractName, contractAbi: HelloWorldContractJson.abi, contractAddress, invocationType: EthContractInvocationType.SEND, @@ -400,7 +388,6 @@ test(testCase, async (t: Test) => { t2.ok(getNameOut2, "getName() invocation #2 output is truthy OK"); const response = await connector.invokeContract({ - contractName, contractAbi: HelloWorldContractJson.abi, contractAddress, invocationType: EthContractInvocationType.SEND, @@ -413,7 +400,6 @@ test(testCase, async (t: Test) => { t2.ok(response, "deposit() payable invocation output is truthy OK"); const { callOutput } = await connector.invokeContract({ - contractName, contractAbi: HelloWorldContractJson.abi, contractAddress, invocationType: EthContractInvocationType.CALL, diff --git a/packages/cactus-plugin-ledger-connector-quorum/src/main/json/openapi.json b/packages/cactus-plugin-ledger-connector-quorum/src/main/json/openapi.json index 86103aec45..5817e59246 100644 --- a/packages/cactus-plugin-ledger-connector-quorum/src/main/json/openapi.json +++ b/packages/cactus-plugin-ledger-connector-quorum/src/main/json/openapi.json @@ -502,6 +502,17 @@ "default": [], "items": {} }, + "contractAbi": { + "description": "The application binary interface of the solidity contract, optional parameter", + "type": "array", + "items": {}, + "nullable": false + }, + "contractAddress": { + "description": "Address of the solidity contract, optional parameter", + "type": "string", + "nullable": false + }, "value": { "oneOf": [ { diff --git a/packages/cactus-plugin-ledger-connector-quorum/src/main/typescript/generated/openapi/typescript-axios/api.ts b/packages/cactus-plugin-ledger-connector-quorum/src/main/typescript/generated/openapi/typescript-axios/api.ts index af3967887f..4247061c00 100644 --- a/packages/cactus-plugin-ledger-connector-quorum/src/main/typescript/generated/openapi/typescript-axios/api.ts +++ b/packages/cactus-plugin-ledger-connector-quorum/src/main/typescript/generated/openapi/typescript-axios/api.ts @@ -127,6 +127,18 @@ export interface InvokeContractV1Request { * @memberof InvokeContractV1Request */ params: Array; + /** + * The application binary interface of the solidity contract, optional parameter + * @type {Array} + * @memberof InvokeContractV1Request + */ + contractAbi?: Array; + /** + * Address of the solidity contract, optional parameter + * @type {string} + * @memberof InvokeContractV1Request + */ + contractAddress?: string; /** * * @type {string | number} diff --git a/packages/cactus-plugin-ledger-connector-quorum/src/main/typescript/plugin-ledger-connector-quorum.ts b/packages/cactus-plugin-ledger-connector-quorum/src/main/typescript/plugin-ledger-connector-quorum.ts index 4abe2e3cce..157edc58f0 100644 --- a/packages/cactus-plugin-ledger-connector-quorum/src/main/typescript/plugin-ledger-connector-quorum.ts +++ b/packages/cactus-plugin-ledger-connector-quorum/src/main/typescript/plugin-ledger-connector-quorum.ts @@ -211,6 +211,7 @@ export class PluginLedgerConnectorQuorum ): Promise { const fnTag = `${this.className}#invokeContract()`; const contractName = req.contractName; + let contractInstance: Contract; if (req.keychainId != undefined) { const networkId = await this.web3.eth.net.getId(); @@ -267,7 +268,19 @@ export class PluginLedgerConnectorQuorum ); } - const contractInstance = this.contracts[contractName]; + contractInstance = this.contracts[contractName]; + if (req.contractAbi != undefined) { + let abi; + if (typeof req.contractAbi === "string") { + abi = JSON.parse(req.contractAbi); + } else { + abi = req.contractAbi; + } + + const { contractAddress } = req; + contractInstance = new this.web3.eth.Contract(abi, contractAddress); + } + const methodRef = contractInstance.methods[req.methodName]; Checks.truthy(methodRef, `${fnTag} YourContract.${req.methodName}`); diff --git a/packages/cactus-plugin-ledger-connector-quorum/src/test/typescript/integration/plugin-ledger-connector-quorum/deploy-contract/deploy-contract-from-json.test.ts b/packages/cactus-plugin-ledger-connector-quorum/src/test/typescript/integration/plugin-ledger-connector-quorum/deploy-contract/deploy-contract-from-json.test.ts index b2231d1aec..a4e3fa63bc 100644 --- a/packages/cactus-plugin-ledger-connector-quorum/src/test/typescript/integration/plugin-ledger-connector-quorum/deploy-contract/deploy-contract-from-json.test.ts +++ b/packages/cactus-plugin-ledger-connector-quorum/src/test/typescript/integration/plugin-ledger-connector-quorum/deploy-contract/deploy-contract-from-json.test.ts @@ -166,7 +166,8 @@ test(testCase, async (t: Test) => { const { callOutput: helloMsg } = await connector.invokeContract({ contractName, - keychainId: keychainPlugin.getKeychainId(), + contractAbi: HelloWorldContractJson.abi, + contractAddress, invocationType: EthContractInvocationType.CALL, methodName: "sayHello", params: [], @@ -187,7 +188,8 @@ test(testCase, async (t: Test) => { const newName = `DrCactus${uuidV4()}`; const setNameOut = await connector.invokeContract({ contractName, - keychainId: keychainPlugin.getKeychainId(), + contractAbi: HelloWorldContractJson.abi, + contractAddress, invocationType: EthContractInvocationType.SEND, methodName: "setName", params: [newName], @@ -203,7 +205,8 @@ test(testCase, async (t: Test) => { try { const setNameOutInvalid = await connector.invokeContract({ contractName, - keychainId: keychainPlugin.getKeychainId(), + contractAbi: HelloWorldContractJson.abi, + contractAddress, invocationType: EthContractInvocationType.SEND, methodName: "setName", params: [newName], @@ -226,7 +229,8 @@ test(testCase, async (t: Test) => { const getNameOut = await connector.invokeContract({ contractName, - keychainId: keychainPlugin.getKeychainId(), + contractAbi: HelloWorldContractJson.abi, + contractAddress, invocationType: EthContractInvocationType.SEND, methodName: "getName", params: [], @@ -240,7 +244,8 @@ test(testCase, async (t: Test) => { const { callOutput: getNameOut2 } = await connector.invokeContract({ contractName, - keychainId: keychainPlugin.getKeychainId(), + contractAbi: HelloWorldContractJson.abi, + contractAddress, invocationType: EthContractInvocationType.CALL, methodName: "getName", params: [], @@ -291,7 +296,8 @@ test(testCase, async (t: Test) => { const newName = `DrCactus${uuidV4()}`; const setNameOut = await connector.invokeContract({ contractName, - keychainId: keychainPlugin.getKeychainId(), + contractAbi: HelloWorldContractJson.abi, + contractAddress, invocationType: EthContractInvocationType.SEND, methodName: "setName", params: [newName], @@ -307,7 +313,8 @@ test(testCase, async (t: Test) => { try { const setNameOutInvalid = await connector.invokeContract({ contractName, - keychainId: keychainPlugin.getKeychainId(), + contractAbi: HelloWorldContractJson.abi, + contractAddress, invocationType: EthContractInvocationType.SEND, methodName: "setName", params: [newName], @@ -329,7 +336,8 @@ test(testCase, async (t: Test) => { } const { callOutput: getNameOut } = await connector.invokeContract({ contractName, - keychainId: keychainPlugin.getKeychainId(), + contractAbi: HelloWorldContractJson.abi, + contractAddress, invocationType: EthContractInvocationType.CALL, methodName: "getName", params: [], @@ -344,7 +352,8 @@ test(testCase, async (t: Test) => { const getNameOut2 = await connector.invokeContract({ contractName, - keychainId: keychainPlugin.getKeychainId(), + contractAbi: HelloWorldContractJson.abi, + contractAddress, invocationType: EthContractInvocationType.SEND, methodName: "getName", params: [], @@ -372,7 +381,8 @@ test(testCase, async (t: Test) => { const setNameOut = await connector.invokeContract({ contractName, - keychainId: keychainPlugin.getKeychainId(), + contractAbi: HelloWorldContractJson.abi, + contractAddress, invocationType: EthContractInvocationType.SEND, methodName: "setName", params: [newName], @@ -385,7 +395,8 @@ test(testCase, async (t: Test) => { try { const setNameOutInvalid = await connector.invokeContract({ contractName, - keychainId: keychainPlugin.getKeychainId(), + contractAbi: HelloWorldContractJson.abi, + contractAddress, invocationType: EthContractInvocationType.SEND, methodName: "setName", params: [newName], @@ -407,7 +418,8 @@ test(testCase, async (t: Test) => { } const { callOutput: getNameOut } = await connector.invokeContract({ contractName, - keychainId: keychainPlugin.getKeychainId(), + contractAbi: HelloWorldContractJson.abi, + contractAddress, invocationType: EthContractInvocationType.CALL, methodName: "getName", params: [], @@ -418,7 +430,8 @@ test(testCase, async (t: Test) => { const getNameOut2 = await connector.invokeContract({ contractName, - keychainId: keychainPlugin.getKeychainId(), + contractAbi: HelloWorldContractJson.abi, + contractAddress, invocationType: EthContractInvocationType.SEND, methodName: "getName", params: [],