Skip to content

Commit

Permalink
feat(connector-besu): contractAbi optional parameter
Browse files Browse the repository at this point in the history
Signed-off-by: AzaharaC <a.castano.benito@accenture.com>
  • Loading branch information
AzaharaC authored and petermetz committed Apr 23, 2021
1 parent dc09540 commit 26cf7c2
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,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": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,18 @@ export interface InvokeContractV1Request {
* @memberof InvokeContractV1Request
*/
params: Array<any>;
/**
* The application binary interface of the solidity contract, optional parameter
* @type {Array<any>}
* @memberof InvokeContractV1Request
*/
contractAbi?: Array<any>;
/**
* Address of the solidity contract, optional parameter
* @type {string}
* @memberof InvokeContractV1Request
*/
contractAddress?: string;
/**
*
* @type {string | number}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ export class PluginLedgerConnectorBesu
): Promise<InvokeContractV1Response> {
const fnTag = `${this.className}#invokeContract()`;
const contractName = req.contractName;
let contractInstance: Contract;

if (req.keychainId != undefined) {
const networkId = await this.web3.eth.net.getId();
Expand Down Expand Up @@ -287,7 +288,19 @@ export class PluginLedgerConnectorBesu
);
}

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}`);
const method: ContractSendMethod = methodRef(...req.params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
Expand Down Expand Up @@ -220,7 +221,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],
Expand All @@ -236,7 +238,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],
Expand All @@ -258,7 +261,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: [],
Expand All @@ -273,7 +277,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: [],
Expand All @@ -288,7 +293,8 @@ test(testCase, async (t: Test) => {

const response = await connector.invokeContract({
contractName,
keychainId: keychainPlugin.getKeychainId(),
contractAbi: HelloWorldContractJson.abi,
contractAddress,
invocationType: EthContractInvocationType.SEND,
methodName: "deposit",
params: [],
Expand All @@ -304,7 +310,8 @@ test(testCase, async (t: Test) => {

const { callOutput } = await connector.invokeContract({
contractName,
keychainId: keychainPlugin.getKeychainId(),
contractAbi: HelloWorldContractJson.abi,
contractAddress,
invocationType: EthContractInvocationType.CALL,
methodName: "getNameByIndex",
params: [0],
Expand Down Expand Up @@ -336,7 +343,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],
Expand All @@ -349,7 +357,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],
Expand All @@ -368,7 +377,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: [],
Expand All @@ -379,7 +389,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: [],
Expand All @@ -390,7 +401,8 @@ test(testCase, async (t: Test) => {

const response = await connector.invokeContract({
contractName,
keychainId: keychainPlugin.getKeychainId(),
contractAbi: HelloWorldContractJson.abi,
contractAddress,
invocationType: EthContractInvocationType.SEND,
methodName: "deposit",
params: [],
Expand All @@ -402,7 +414,8 @@ test(testCase, async (t: Test) => {

const { callOutput } = await connector.invokeContract({
contractName,
keychainId: keychainPlugin.getKeychainId(),
contractAbi: HelloWorldContractJson.abi,
contractAddress,
invocationType: EthContractInvocationType.CALL,
methodName: "getNameByIndex",
params: [1],
Expand Down

0 comments on commit 26cf7c2

Please sign in to comment.