Skip to content

Commit 80261f8

Browse files
author
Travis Payne
committed
feat: allows for constructor args in quorum contract deploy
Closes: hyperledger-cacti#962 Signed-off-by: Travis Payne <[email protected]>
1 parent bfe342e commit 80261f8

File tree

4 files changed

+89
-1
lines changed

4 files changed

+89
-1
lines changed

packages/cactus-plugin-ledger-connector-quorum/src/main/json/openapi.json

+12
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,12 @@
487487
"type": "object",
488488
"description": "For use when not using keychain, pass the contract in as this variable",
489489
"nullable": false
490+
},
491+
"constructorArgs": {
492+
"description": "The list of arguments to pass in to the constructor of the contract being deployed.",
493+
"type": "array",
494+
"default": [],
495+
"items": {}
490496
}
491497
}
492498
},
@@ -532,6 +538,12 @@
532538
"$ref": "#/components/schemas/ContractJSON",
533539
"description": "For use when not using keychain, pass the contract in as this variable",
534540
"nullable": false
541+
},
542+
"constructorArgs": {
543+
"description": "The list of arguments to pass in to the constructor of the contract being deployed.",
544+
"type": "array",
545+
"default": [],
546+
"items": {}
535547
}
536548
}
537549
},

packages/cactus-plugin-ledger-connector-quorum/src/main/typescript/generated/openapi/typescript-axios/api.ts

+12
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ export interface DeployContractSolidityBytecodeJsonObjectV1Request {
144144
* @memberof DeployContractSolidityBytecodeJsonObjectV1Request
145145
*/
146146
contractJSON: ContractJSON;
147+
/**
148+
* The list of arguments to pass in to the constructor of the contract being deployed.
149+
* @type {Array<any>}
150+
* @memberof DeployContractSolidityBytecodeJsonObjectV1Request
151+
*/
152+
constructorArgs?: Array<any>;
147153
}
148154
/**
149155
*
@@ -193,6 +199,12 @@ export interface DeployContractSolidityBytecodeV1Request {
193199
* @memberof DeployContractSolidityBytecodeV1Request
194200
*/
195201
contractJSON?: object;
202+
/**
203+
* The list of arguments to pass in to the constructor of the contract being deployed.
204+
* @type {Array<any>}
205+
* @memberof DeployContractSolidityBytecodeV1Request
206+
*/
207+
constructorArgs?: Array<any>;
196208
}
197209
/**
198210
*

packages/cactus-plugin-ledger-connector-quorum/src/main/typescript/plugin-ledger-connector-quorum.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -503,13 +503,26 @@ export class PluginLedgerConnectorQuorum
503503
}
504504
}
505505

506+
private async generateBytecode(req: any): Promise<string> {
507+
const tmpContract = new this.web3.eth.Contract(
508+
(req.contractJSON as any).abi,
509+
);
510+
const deployment = tmpContract.deploy({
511+
data: req.bytecode,
512+
arguments: req.constructorArgs,
513+
});
514+
const abi = deployment.encodeABI();
515+
return abi.startsWith("0x") ? abi : `0x${abi}`;
516+
}
517+
506518
async runDeploy(req: any): Promise<DeployContractSolidityBytecodeV1Response> {
507519
const web3SigningCredential = req.web3SigningCredential as
508520
| Web3SigningCredentialGethKeychainPassword
509521
| Web3SigningCredentialPrivateKeyHex;
522+
510523
const receipt = await this.transact({
511524
transactionConfig: {
512-
data: `0x${req.contractJSON.bytecode}`,
525+
data: await this.generateBytecode(req),
513526
from: web3SigningCredential.ethAccount,
514527
gas: req.gas,
515528
gasPrice: req.gasPrice,

packages/cactus-plugin-ledger-connector-quorum/src/test/typescript/integration/plugin-ledger-connector-quorum/deploy-contract/v2.3.0-deploy-contract-from-json.test.ts

+51
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,57 @@ test(testCase, async (t: Test) => {
189189
);
190190
});
191191

192+
test("deploys contract via .json file with constructorArgs", async (t2: Test) => {
193+
const deployOut = await connector.deployContract({
194+
contractName: HelloWorldContractJson.contractName,
195+
contractJSON: HelloWorldContractJson,
196+
keychainId: keychainPlugin.getKeychainId(),
197+
web3SigningCredential: {
198+
ethAccount: firstHighNetWorthAccount,
199+
secret: "",
200+
type: Web3SigningCredentialType.GethKeychainPassword,
201+
},
202+
bytecode: HelloWorldContractJson.bytecode,
203+
gas: 1000000,
204+
constructorArgs: ["Test Arg"],
205+
});
206+
t2.ok(deployOut, "deployContract() output is truthy OK");
207+
t2.ok(
208+
deployOut.transactionReceipt,
209+
"deployContract() output.transactionReceipt is truthy OK",
210+
);
211+
t2.ok(
212+
deployOut.transactionReceipt.contractAddress,
213+
"deployContract() output.transactionReceipt.contractAddress is truthy OK",
214+
);
215+
216+
contractAddress = deployOut.transactionReceipt.contractAddress as string;
217+
t2.ok(
218+
typeof contractAddress === "string",
219+
"contractAddress typeof string OK",
220+
);
221+
222+
const { callOutput: helloMsg } = await connector.getContractInfoKeychain({
223+
contractName,
224+
contractAbi: HelloWorldContractJson.abi,
225+
contractAddress,
226+
invocationType: EthContractInvocationType.Call,
227+
methodName: "sayHello",
228+
keychainId: keychainPlugin.getKeychainId(),
229+
params: [],
230+
signingCredential: {
231+
ethAccount: firstHighNetWorthAccount,
232+
secret: "",
233+
type: Web3SigningCredentialType.GethKeychainPassword,
234+
},
235+
});
236+
t2.ok(helloMsg, "sayHello() output is truthy");
237+
t2.true(
238+
typeof helloMsg === "string",
239+
"sayHello() output is type of string",
240+
);
241+
});
242+
192243
test("invoke Web3SigningCredentialType.GETHKEYCHAINPASSWORD", async (t2: Test) => {
193244
const newName = `DrCactus${uuidV4()}`;
194245
const setNameOut = await connector.getContractInfoKeychain({

0 commit comments

Comments
 (0)