Skip to content

Commit

Permalink
feat(polkadot connector): add constructor method param
Browse files Browse the repository at this point in the history
  • Loading branch information
AnmolBansalDEV committed Nov 11, 2023
1 parent 98aad54 commit 91bdaee
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,13 @@
"format": "byte",
"nullable": false
},
"constructorMethod": {
"oneOf": [
{
"type": "string"
}
]
},
"metadata": {
"oneOf": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ export interface DeployContractInkRequest {
* @memberof DeployContractInkRequest
*/
'wasm': string;
/**
*
* @type {PolkadotTransactionConfigTransferSubmittable}
* @memberof DeployContractInkRequest
*/
'constructorMethod'?: PolkadotTransactionConfigTransferSubmittable;
/**
*
* @type {PolkadotTransactionConfigTransferSubmittable}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand All @@ -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 };
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 91bdaee

Please sign in to comment.