Skip to content

Commit

Permalink
feat: allows for constructor args in quorum contract deploy
Browse files Browse the repository at this point in the history
Closes: #962
Signed-off-by: Travis Payne <travis.payne@accenture.com>
  • Loading branch information
Travis Payne authored and petermetz committed Oct 23, 2021
1 parent 55a12b6 commit cb3c8d8
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,12 @@
"type": "object",
"description": "For use when not using keychain, pass the contract in as this variable",
"nullable": false
},
"constructorArgs": {
"description": "The list of arguments to pass in to the constructor of the contract being deployed.",
"type": "array",
"default": [],
"items": {}
}
}
},
Expand Down Expand Up @@ -532,6 +538,12 @@
"$ref": "#/components/schemas/ContractJSON",
"description": "For use when not using keychain, pass the contract in as this variable",
"nullable": false
},
"constructorArgs": {
"description": "The list of arguments to pass in to the constructor of the contract being deployed.",
"type": "array",
"default": [],
"items": {}
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ export interface DeployContractSolidityBytecodeJsonObjectV1Request {
* @memberof DeployContractSolidityBytecodeJsonObjectV1Request
*/
contractJSON: ContractJSON;
/**
* The list of arguments to pass in to the constructor of the contract being deployed.
* @type {Array<any>}
* @memberof DeployContractSolidityBytecodeJsonObjectV1Request
*/
constructorArgs?: Array<any>;
}
/**
*
Expand Down Expand Up @@ -193,6 +199,12 @@ export interface DeployContractSolidityBytecodeV1Request {
* @memberof DeployContractSolidityBytecodeV1Request
*/
contractJSON?: object;
/**
* The list of arguments to pass in to the constructor of the contract being deployed.
* @type {Array<any>}
* @memberof DeployContractSolidityBytecodeV1Request
*/
constructorArgs?: Array<any>;
}
/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -503,13 +503,26 @@ export class PluginLedgerConnectorQuorum
}
}

private async generateBytecode(req: any): Promise<string> {
const tmpContract = new this.web3.eth.Contract(
(req.contractJSON as any).abi,
);
const deployment = tmpContract.deploy({
data: req.contractJSON.bytecode,
arguments: req.constructorArgs,
});
const abi = deployment.encodeABI();
return abi.startsWith("0x") ? abi : `0x${abi}`;
}

async runDeploy(req: any): Promise<DeployContractSolidityBytecodeV1Response> {
const web3SigningCredential = req.web3SigningCredential as
| Web3SigningCredentialGethKeychainPassword
| Web3SigningCredentialPrivateKeyHex;

const receipt = await this.transact({
transactionConfig: {
data: `0x${req.contractJSON.bytecode}`,
data: await this.generateBytecode(req),
from: web3SigningCredential.ethAccount,
gas: req.gas,
gasPrice: req.gasPrice,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,5 +461,35 @@ test(testCase, async (t: Test) => {
t2.end();
});

test("deploys contract via .json file with constructorArgs", async (t2: Test) => {
const deployOut = await connector.deployContract({
contractName: HelloWorldContractJson.contractName,
contractJSON: HelloWorldContractJson,
keychainId: keychainPlugin.getKeychainId(),
web3SigningCredential: {
ethAccount: firstHighNetWorthAccount,
secret: "",
type: Web3SigningCredentialType.GethKeychainPassword,
},
gas: 1000000,
constructorArgs: ["Test Arg"],
});
t2.ok(deployOut, "deployContract() output is truthy OK");
t2.ok(
deployOut.transactionReceipt,
"deployContract() output.transactionReceipt is truthy OK",
);
t2.ok(
deployOut.transactionReceipt.contractAddress,
"deployContract() output.transactionReceipt.contractAddress is truthy OK",
);

contractAddress = deployOut.transactionReceipt.contractAddress as string;
t2.ok(
typeof contractAddress === "string",
"contractAddress typeof string OK",
);
});

t.end();
});

0 comments on commit cb3c8d8

Please sign in to comment.