From 0f1befc8084410cd996b47983d80eaa3003de475 Mon Sep 17 00:00:00 2001 From: Anmol Bansal Date: Mon, 30 Oct 2023 08:10:03 +0000 Subject: [PATCH] feat(polkadot connector): add constructor method param --- .../src/main/json/openapi.json | 7 +++ .../generated/openapi/typescript-axios/api.ts | 6 ++ .../plugin-ledger-connector-polkadot.ts | 60 +++++++------------ 3 files changed, 33 insertions(+), 40 deletions(-) diff --git a/packages/cactus-plugin-ledger-connector-polkadot/src/main/json/openapi.json b/packages/cactus-plugin-ledger-connector-polkadot/src/main/json/openapi.json index 5a38da808e1..60892073695 100644 --- a/packages/cactus-plugin-ledger-connector-polkadot/src/main/json/openapi.json +++ b/packages/cactus-plugin-ledger-connector-polkadot/src/main/json/openapi.json @@ -351,6 +351,13 @@ "format": "byte", "nullable": false }, + "constructorMethod": { + "oneOf": [ + { + "type": "string" + } + ] + }, "metadata": { "oneOf": [ { diff --git a/packages/cactus-plugin-ledger-connector-polkadot/src/main/typescript/generated/openapi/typescript-axios/api.ts b/packages/cactus-plugin-ledger-connector-polkadot/src/main/typescript/generated/openapi/typescript-axios/api.ts index 665bc140e31..ef4f0c77bf8 100644 --- a/packages/cactus-plugin-ledger-connector-polkadot/src/main/typescript/generated/openapi/typescript-axios/api.ts +++ b/packages/cactus-plugin-ledger-connector-polkadot/src/main/typescript/generated/openapi/typescript-axios/api.ts @@ -41,6 +41,12 @@ export interface DeployContractInkRequest { * @memberof DeployContractInkRequest */ 'wasm': string; + /** + * + * @type {PolkadotTransactionConfigTransferSubmittable} + * @memberof DeployContractInkRequest + */ + 'constructorMethod'?: PolkadotTransactionConfigTransferSubmittable; /** * * @type {PolkadotTransactionConfigTransferSubmittable} diff --git a/packages/cactus-plugin-ledger-connector-polkadot/src/main/typescript/plugin-ledger-connector-polkadot.ts b/packages/cactus-plugin-ledger-connector-polkadot/src/main/typescript/plugin-ledger-connector-polkadot.ts index df1b737cbbe..cb9cdebd081 100644 --- a/packages/cactus-plugin-ledger-connector-polkadot/src/main/typescript/plugin-ledger-connector-polkadot.ts +++ b/packages/cactus-plugin-ledger-connector-polkadot/src/main/typescript/plugin-ledger-connector-polkadot.ts @@ -6,7 +6,7 @@ import { WsProvider } from "@polkadot/rpc-provider/ws"; import { RuntimeError } from "run-time-error"; import { WeightV2 } from "@polkadot/types/interfaces"; import { CodePromise, Abi, ContractPromise } from "@polkadot/api-contract"; -import { isHex } from "@polkadot/util"; +import { isHex, stringCamelCase } from "@polkadot/util"; import { PrometheusExporter } from "./prometheus-exporter/prometheus-exporter"; import { GetPrometheusMetricsEndpoint, @@ -609,26 +609,17 @@ export class PluginLedgerConnectorPolkadot }); const keyring = new Keyring({ type: "sr25519" }); const accountPair = keyring.createFromUri(mnemonic); - const tx = - req.params && req.params.length > 0 - ? contractCode.tx[contractAbi.constructors[0].method]( - { - gasLimit, - storageDepositLimit: req.storageDepositLimit, - salt: req.salt, - value: req.balance, - }, - ...req.params, - ) - : contractCode.tx[contractAbi.constructors[0].method]( - { - gasLimit, - storageDepositLimit: req.storageDepositLimit, - salt: req.salt, - value: req.balance, - }, - undefined, - ); + const params = req.params ?? []; + const constructorMethod = req.constructorMethod ?? 'new' + const tx = contractCode.tx[stringCamelCase(constructorMethod)]( + { + gasLimit, + storageDepositLimit: req.storageDepositLimit, + salt: req.salt, + value: req.balance, + }, + ...params, + ); const txResult = await new Promise<{ success: boolean; address: string | undefined; @@ -709,9 +700,10 @@ export class PluginLedgerConnectorPolkadot req.metadata, this.api.registry.getChainProperties(), ); + const methodName = stringCamelCase(req.methodName); const isSafeToCall = await this.isSafeToCallContractMethod( contractAbi, - req.methodName, + methodName, ); if (!isSafeToCall) { throw new RuntimeError( @@ -729,22 +721,16 @@ export class PluginLedgerConnectorPolkadot }); if (req.invocationType === PolkadotContractInvocationType.Query) { let success = false; - const query = - req.params && req.params.length > 0 - ? contract.query[req.methodName]( + const params = req.params ?? [] + const query = contract.query[methodName]( req.accountAddress, { gasLimit, storageDepositLimit: req.storageDepositLimit, value: req.balance, }, - ...req.params, + ...params, ) - : contract.query[req.methodName](req.accountAddress, { - gasLimit, - storageDepositLimit: req.storageDepositLimit, - value: req.balance, - }); const callOutput = await query; success = true; return { success, callOutput }; @@ -784,21 +770,15 @@ export class PluginLedgerConnectorPolkadot const keyring = new Keyring({ type: "sr25519" }); const accountPair = keyring.createFromUri(mnemonic); let success = false; - const tx = - req.params && req.params.length > 0 - ? contract.tx[req.methodName]( + const params = req.params ?? [] + const tx = contract.tx[methodName]( { gasLimit, storageDepositLimit: req.storageDepositLimit, value: req.balance, }, - ...req.params, + ...params, ) - : contract.tx[req.methodName]({ - gasLimit, - storageDepositLimit: req.storageDepositLimit, - value: req.balance, - }); const txResult = await new Promise<{ success: boolean; transactionHash: string;