From 5d6c2dd9f2ae604c600f56d63b956b40b91d67da Mon Sep 17 00:00:00 2001 From: Jaco Date: Sun, 26 Dec 2021 09:03:28 +0100 Subject: [PATCH] DRY cleanups for contracts (#4395) --- package.json | 2 +- packages/api-contract/src/base/Blueprint.ts | 18 +++++++++--------- packages/api-contract/src/base/Code.ts | 8 +++++--- packages/api-contract/src/base/Contract.ts | 20 +++++++++++--------- packages/api-contract/src/types.ts | 3 ++- yarn.lock | 10 +++++----- 6 files changed, 33 insertions(+), 28 deletions(-) diff --git a/package.json b/package.json index da784a0310..e3838fb985 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "@babel/core": "^7.16.5", "@babel/register": "^7.16.5", "@babel/runtime": "^7.16.5", - "@polkadot/dev": "^0.64.21", + "@polkadot/dev": "^0.64.22", "@polkadot/typegen": "workspace:packages/typegen", "@types/jest": "^27.0.3", "copyfiles": "^2.4.1" diff --git a/packages/api-contract/src/base/Blueprint.ts b/packages/api-contract/src/base/Blueprint.ts index fb86120a11..6486e87756 100644 --- a/packages/api-contract/src/base/Blueprint.ts +++ b/packages/api-contract/src/base/Blueprint.ts @@ -58,19 +58,19 @@ export class Blueprint extends Base { #deploy = (constructorOrId: AbiConstructor | string | number, { gasLimit = BN_ZERO, salt, storageDepositLimit = null, value = BN_ZERO }: BlueprintOptions, params: unknown[]): SubmittableExtrinsic> => { const hasStorageDeposit = this.api.tx.contracts.instantiate.meta.args.length === 6; - + const encParams = this.abi.findConstructor(constructorOrId).toU8a(params); + const encSalt = encodeSalt(salt); const tx = hasStorageDeposit - ? this.api.tx.contracts.instantiate(value, gasLimit, storageDepositLimit, this.codeHash, this.abi.findConstructor(constructorOrId).toU8a(params), encodeSalt(salt)) + ? this.api.tx.contracts.instantiate(value, gasLimit, storageDepositLimit, this.codeHash, encParams, encSalt) // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore old style without storage deposit - : this.api.tx.contracts.instantiate(value, gasLimit, this.codeHash, this.abi.findConstructor(constructorOrId).toU8a(params), encodeSalt(salt)); + : this.api.tx.contracts.instantiate(value, gasLimit, this.codeHash, encParams, encSalt); - return tx - .withResultTransform((result: ISubmittableResult) => - new BlueprintSubmittableResult(result, applyOnEvent(result, ['Instantiated'], ([record]: EventRecord[]) => - new Contract(this.api, this.abi, record.event.data[1] as AccountId, this._decorateMethod) - )) - ); + return tx.withResultTransform((result: ISubmittableResult) => + new BlueprintSubmittableResult(result, applyOnEvent(result, ['Instantiated'], ([record]: EventRecord[]) => + new Contract(this.api, this.abi, record.event.data[1] as AccountId, this._decorateMethod) + )) + ); }; } diff --git a/packages/api-contract/src/base/Code.ts b/packages/api-contract/src/base/Code.ts index 7210e46a64..3a8d26584a 100644 --- a/packages/api-contract/src/base/Code.ts +++ b/packages/api-contract/src/base/Code.ts @@ -63,12 +63,14 @@ export class Code extends Base { #instantiate = (constructorOrId: AbiConstructor | string | number, { gasLimit = BN_ZERO, salt, storageDepositLimit = null, value = BN_ZERO }: BlueprintOptions, params: unknown[]): SubmittableExtrinsic> => { const hasStorageDeposit = this.api.tx.contracts.instantiateWithCode.meta.args.length === 6; - + const encCode = compactAddLength(this.code); + const encParams = this.abi.findConstructor(constructorOrId).toU8a(params); + const encSalt = encodeSalt(salt); const tx = hasStorageDeposit - ? this.api.tx.contracts.instantiateWithCode(value, gasLimit, storageDepositLimit, compactAddLength(this.code), this.abi.findConstructor(constructorOrId).toU8a(params), encodeSalt(salt)) + ? this.api.tx.contracts.instantiateWithCode(value, gasLimit, storageDepositLimit, encCode, encParams, encSalt) // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore old style without storage deposit - : this.api.tx.contracts.instantiateWithCode(value, gasLimit, compactAddLength(this.code), this.abi.findConstructor(constructorOrId).toU8a(params), encodeSalt(salt)); + : this.api.tx.contracts.instantiateWithCode(value, gasLimit, encCode, encParams, encSalt); return tx.withResultTransform((result: ISubmittableResult) => new CodeSubmittableResult(result, ...(applyOnEvent(result, ['CodeStored', 'Instantiated'], (records: EventRecord[]) => diff --git a/packages/api-contract/src/base/Contract.ts b/packages/api-contract/src/base/Contract.ts index 0e02fde2ba..16b43e3f65 100644 --- a/packages/api-contract/src/base/Contract.ts +++ b/packages/api-contract/src/base/Contract.ts @@ -108,12 +108,13 @@ export class Contract extends Base { #exec = (messageOrId: AbiMessage | string | number, { gasLimit = BN_ZERO, storageDepositLimit = null, value = BN_ZERO }: ContractOptions, params: unknown[]): SubmittableExtrinsic => { const hasStorageDeposit = this.api.tx.contracts.call.meta.args.length === 5; - + const gas = this.#getGas(gasLimit); + const encParams = this.abi.findMessage(messageOrId).toU8a(params); const tx = hasStorageDeposit - ? this.api.tx.contracts.call(this.address, value, this.#getGas(gasLimit), storageDepositLimit, this.abi.findMessage(messageOrId).toU8a(params)) + ? this.api.tx.contracts.call(this.address, value, gas, storageDepositLimit, encParams) // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore old style without storage deposit - : this.api.tx.contracts.call(this.address, value, this.#getGas(gasLimit), this.abi.findMessage(messageOrId).toU8a(params)); + : this.api.tx.contracts.call(this.address, value, gas, encParams); return tx.withResultTransform((result: ISubmittableResult) => // ContractEmitted is the current generation, ContractExecution is the previous generation @@ -142,11 +143,12 @@ export class Contract extends Base { // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment send: this._decorateMethod((origin: string | AccountId | Uint8Array) => { const hasStorageDeposit = this.api.tx.contracts.call.meta.args.length === 5; + const inputData = message.toU8a(params); const rpc = hasStorageDeposit - ? this.api.rx.rpc.contracts.call({ dest: this.address, gasLimit: this.#getGas(gasLimit, true), inputData: message.toU8a(params), origin, storageDepositLimit, value }) - : this.api.rx.rpc.contracts.call({ dest: this.address, gasLimit: this.#getGas(gasLimit, true), inputData: message.toU8a(params), origin, value }); + ? this.api.rx.rpc.contracts.call({ dest: this.address, gasLimit: this.#getGas(gasLimit, true), inputData, origin, storageDepositLimit, value }) + : this.api.rx.rpc.contracts.call({ dest: this.address, gasLimit: this.#getGas(gasLimit, true), inputData, origin, value }); - const mapFn = ({ debugMessage, gasConsumed, gasRequired, result }: ContractExecResult): ContractCallOutcome => ({ + const mapFn = ({ debugMessage, gasConsumed, gasRequired, result, storageDeposit }: ContractExecResult): ContractCallOutcome => ({ debugMessage, gasConsumed, gasRequired: gasRequired && !gasRequired.isZero() @@ -155,12 +157,12 @@ export class Contract extends Base { output: result.isOk && message.returnType ? this.abi.registry.createTypeUnsafe(message.returnType.lookupName || message.returnType.type, [result.asOk.data.toU8a(true)], { isPedantic: true }) : null, - result + result, + storageDeposit }); return rpc.pipe(map(mapFn)); - } - ) + }) }; }; } diff --git a/packages/api-contract/src/types.ts b/packages/api-contract/src/types.ts index 37d208efe8..0ed528b6ac 100644 --- a/packages/api-contract/src/types.ts +++ b/packages/api-contract/src/types.ts @@ -4,7 +4,7 @@ import type { ApiBase } from '@polkadot/api/base'; import type { ApiTypes } from '@polkadot/api/types'; import type { Text, u64 } from '@polkadot/types'; -import type { ContractExecResultResult, ContractSelector } from '@polkadot/types/interfaces'; +import type { ContractExecResultResult, ContractSelector, StorageDeposit } from '@polkadot/types/interfaces'; import type { Codec, TypeDef } from '@polkadot/types/types'; import type { BN } from '@polkadot/util'; import type { Abi } from '.'; @@ -58,6 +58,7 @@ export interface ContractCallOutcome { gasRequired: u64; output: Codec | null; result: ContractExecResultResult; + storageDeposit: StorageDeposit; } export interface DecodedEvent { diff --git a/yarn.lock b/yarn.lock index cabce62f99..6ce379353f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1955,9 +1955,9 @@ __metadata: languageName: unknown linkType: soft -"@polkadot/dev@npm:^0.64.21": - version: 0.64.21 - resolution: "@polkadot/dev@npm:0.64.21" +"@polkadot/dev@npm:^0.64.22": + version: 0.64.22 + resolution: "@polkadot/dev@npm:0.64.22" dependencies: "@babel/cli": ^7.16.0 "@babel/core": ^7.16.5 @@ -2042,7 +2042,7 @@ __metadata: polkadot-exec-rollup: scripts/polkadot-exec-rollup.mjs polkadot-exec-tsc: scripts/polkadot-exec-tsc.mjs polkadot-exec-webpack: scripts/polkadot-exec-webpack.mjs - checksum: c2f1c141b00f940b4940df21dae51e39e9f71ea1caa3a33ce819701c7b5e9cf85491cf7315efb69a99c184bdb5c502cc13c906047c5cb49cacf1360bfd2ea89e + checksum: 4f2e79f7de3777b17ab877819d32a9b88ec136e18cd0fc8002885b3bf0afae0e02acf9e05b16ecc86c20c12c8a5fea89416c2f48555571c2223929ce5b5fa1a5 languageName: node linkType: hard @@ -8954,7 +8954,7 @@ resolve@^2.0.0-next.3: "@babel/core": ^7.16.5 "@babel/register": ^7.16.5 "@babel/runtime": ^7.16.5 - "@polkadot/dev": ^0.64.21 + "@polkadot/dev": ^0.64.22 "@polkadot/typegen": "workspace:packages/typegen" "@types/jest": ^27.0.3 copyfiles: ^2.4.1