Skip to content

Commit

Permalink
feat(connector-besu): contract deployment with constructor arguments
Browse files Browse the repository at this point in the history
Previously you couldn't deploy a contract that had constructor arguments
of it's own because there was no way to pass in these.
With this improvement this is now possible.

Depends on hyperledger#810

Signed-off-by: Peter Somogyvari <peter.somogyvari@accenture.com>
  • Loading branch information
petermetz committed Apr 17, 2021
1 parent d7f0097 commit 862eeb5
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ export class SupplyChainAppDummyInfrastructure {
const res = await connector.deployContract({
contractName: BookshelfRepositoryJSON.contractName,
bytecode: BookshelfRepositoryJSON.bytecode,
contractAbi: BookshelfRepositoryJSON.abi,
constructorArgs: [],
gas: 1000000,
web3SigningCredential: {
ethAccount: this.besuAccount.address,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"description": "Enumerates the possible types of receipts that can be waited for by someone or something that has requested the execution of a transaction on a ledger.",
"type": "string",
"enum": [
"NODE_TX_POOL_ACK",
"NODE_TX_POOL_ACK",
"LEDGER_BLOCK_ACK"
]
},
Expand Down Expand Up @@ -402,9 +402,11 @@
"type": "object",
"required": [
"contractName",
"contractAbi",
"bytecode",
"web3SigningCredential",
"keychainId"
"keychainId",
"constructorArgs"
],
"properties": {
"contractName": {
Expand All @@ -414,6 +416,17 @@
"maxLength": 100,
"nullable": false
},
"contractAbi": {
"description": "The application binary interface of the solidity contract",
"type": "array",
"items": {},
"nullable": false
},
"constructorArgs": {
"type": "array",
"items": {},
"default": []
},
"web3SigningCredential": {
"$ref": "#/components/schemas/Web3SigningCredential",
"nullable": false
Expand Down Expand Up @@ -634,7 +647,7 @@
"type": "string",
"description": "The keychainId for retrieve the contracts json.",
"minLength": 1,
"maxLength": 100
"maxLength": 100
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ export interface DeployContractSolidityBytecodeV1Request {
* @memberof DeployContractSolidityBytecodeV1Request
*/
contractName: string;
/**
* The application binary interface of the solidity contract
* @type {Array<any>}
* @memberof DeployContractSolidityBytecodeV1Request
*/
contractAbi: Array<any>;
/**
*
* @type {Array<any>}
* @memberof DeployContractSolidityBytecodeV1Request
*/
constructorArgs: Array<any>;
/**
*
* @type {Web3SigningCredential}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -615,12 +615,22 @@ export class PluginLedgerConnectorBesu
}
const networkId = await this.web3.eth.net.getId();

const tmpContract = new this.web3.eth.Contract(req.contractAbi);
const deployment = tmpContract.deploy({
data: req.bytecode,
arguments: req.constructorArgs,
});

this.log.debug(`Deployment object of contract: %o`, deployment);
const encodedAbi = deployment.encodeABI();
const data = `0x${encodedAbi}`;

const web3SigningCredential = req.web3SigningCredential as
| Web3SigningCredentialPrivateKeyHex
| Web3SigningCredentialCactusKeychainRef;
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 @@ -137,6 +137,8 @@ test(testCase, async (t: Test) => {
const deployOut = await connector.deployContract({
keychainId: keychainPlugin.getKeychainId(),
contractName: HelloWorldContractJson.contractName,
contractAbi: HelloWorldContractJson.abi,
constructorArgs: [],
web3SigningCredential: {
ethAccount: firstHighNetWorthAccount,
secret: besuKeyPair.privateKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ test("deploys contract via .json file", async (t: Test) => {
const deployOut = await connector.deployContract({
keychainId: keychainPlugin.getKeychainId(),
contractName: HelloWorldContractJson.contractName,
contractAbi: HelloWorldContractJson.abi,
constructorArgs: [],
web3SigningCredential: {
ethAccount: firstHighNetWorthAccount,
secret: besuKeyPair.privateKey,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 862eeb5

Please sign in to comment.