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: hyperledger-cacti#962
Signed-off-by: Travis Payne <[email protected]>
  • Loading branch information
Travis Payne committed Oct 7, 2021
1 parent 4d06ec1 commit 41d914a
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,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 @@ -498,6 +504,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
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ export interface DeployContractSolidityBytecodeJsonObjectV1Request {
* @memberof DeployContractSolidityBytecodeJsonObjectV1Request
*/
contractJSON?: object;
/**
* 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 @@ -124,6 +130,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 @@ -587,13 +587,33 @@ export class PluginLedgerConnectorQuorum
);
}
const networkId = await this.web3.eth.net.getId();
let data = "";

if (req.constructorArgs && req.constructorArgs.length > 0) {
if (!keychainPlugin.has(req.contractName)) {
throw new Error(
`${fnTag} Cannot create an instance of the contract because the contractName and the contractName of the JSON doesn't match`,
);
}
const tmpContract = new this.web3.eth.Contract(
(req.contractJSON as any).abi,
);
const deployment = tmpContract.deploy({
data: req.bytecode,
arguments: req.constructorArgs,
});

const abi = deployment.encodeABI();
data = abi.startsWith("0x") ? abi : `0x${abi}`;
this.log.debug(`Deploying "${req.contractName}" with data %o`, data);
}

const web3SigningCredential = req.web3SigningCredential as
| Web3SigningCredentialGethKeychainPassword
| Web3SigningCredentialPrivateKeyHex;
const receipt = await this.transact({
transactionConfig: {
data: `0x${req.bytecode}`,
data: data === "" ? `0x${req.bytecode}` : data,
from: web3SigningCredential.ethAccount,
gas: req.gas,
gasPrice: req.gasPrice,
Expand Down Expand Up @@ -636,12 +656,22 @@ export class PluginLedgerConnectorQuorum
if (req.contractJSON != undefined) {
const networkId = await this.web3.eth.net.getId();

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

const web3SigningCredential = req.web3SigningCredential as
| Web3SigningCredentialGethKeychainPassword
| Web3SigningCredentialPrivateKeyHex;
const receipt = await this.transact({
transactionConfig: {
data: `0x${req.bytecode}`,
data,
from: web3SigningCredential.ethAccount,
gas: req.gas,
gasPrice: req.gasPrice,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,57 @@ test(testCase, async (t: Test) => {
);
});

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,
},
bytecode: HelloWorldContractJson.bytecode,
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",
);

const { callOutput: helloMsg } = await connector.getContractInfoKeychain({
contractName,
contractAbi: HelloWorldContractJson.abi,
contractAddress,
invocationType: EthContractInvocationType.Call,
methodName: "sayHello",
keychainId: keychainPlugin.getKeychainId(),
params: [],
signingCredential: {
ethAccount: firstHighNetWorthAccount,
secret: "",
type: Web3SigningCredentialType.GethKeychainPassword,
},
});
t2.ok(helloMsg, "sayHello() output is truthy");
t2.true(
typeof helloMsg === "string",
"sayHello() output is type of string",
);
});

test("invoke Web3SigningCredentialType.GETHKEYCHAINPASSWORD", async (t2: Test) => {
const newName = `DrCactus${uuidV4()}`;
const setNameOut = await connector.getContractInfoKeychain({
Expand Down

0 comments on commit 41d914a

Please sign in to comment.