From 70087b139c2fa1427b20133f5f2f8cb55c2affdb Mon Sep 17 00:00:00 2001 From: jasl Date: Wed, 9 Nov 2022 01:01:23 +0800 Subject: [PATCH 01/13] Update contract' gas to Weight type --- packages/api-contract/src/types.ts | 8 +-- .../src/interfaces/contracts/definitions.ts | 52 +++++++++---------- .../types/src/interfaces/contracts/types.ts | 52 +++++++++---------- 3 files changed, 56 insertions(+), 56 deletions(-) diff --git a/packages/api-contract/src/types.ts b/packages/api-contract/src/types.ts index 5ab721de6773..51a1043ccdf6 100644 --- a/packages/api-contract/src/types.ts +++ b/packages/api-contract/src/types.ts @@ -3,8 +3,8 @@ 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, StorageDeposit, WeightV2 } from '@polkadot/types/interfaces'; +import type { Text } from '@polkadot/types'; +import type { ContractExecResultResult, ContractSelector, StorageDeposit, Weight, WeightV2 } from '@polkadot/types/interfaces'; import type { Codec, TypeDef } from '@polkadot/types/types'; import type { BN } from '@polkadot/util'; import type { Abi } from '.'; @@ -55,8 +55,8 @@ export interface InterfaceContractCalls { export interface ContractCallOutcome { debugMessage: Text; - gasConsumed: u64; - gasRequired: u64; + gasConsumed: Weight; + gasRequired: Weight; output: Codec | null; result: ContractExecResultResult; storageDeposit: StorageDeposit; diff --git a/packages/types/src/interfaces/contracts/definitions.ts b/packages/types/src/interfaces/contracts/definitions.ts index f0faafdb9e37..38feffc239b5 100644 --- a/packages/types/src/interfaces/contracts/definitions.ts +++ b/packages/types/src/interfaces/contracts/definitions.ts @@ -45,7 +45,7 @@ export default { origin: 'AccountId', dest: 'AccountId', value: 'Balance', - gasLimit: 'u64', + gasLimit: 'Weight', storageDepositLimit: 'Option', inputData: 'Bytes' }, @@ -81,8 +81,8 @@ export default { result: 'ContractExecResultResult' }, ContractExecResult: { - gasConsumed: 'u64', - gasRequired: 'u64', + gasConsumed: 'Weight', + gasRequired: 'Weight', storageDeposit: 'StorageDeposit', debugMessage: 'Text', result: 'ContractExecResultResult' @@ -117,7 +117,7 @@ export default { flags: 'ContractReturnFlags', data: 'Bytes' }, - Gas: 'u64', + Gas: 'Weight', HostFnWeightsTo264: { caller: 'Weight', address: 'Weight', @@ -222,7 +222,7 @@ export default { InstantiateRequestV1: { origin: 'AccountId', value: 'Balance', - gasLimit: 'Gas', + gasLimit: 'u64', code: 'Bytes', data: 'Bytes', salt: 'Bytes' @@ -251,8 +251,8 @@ export default { ContractInstantiateResultTo299: 'Result', ContractInstantiateResult: { _fallback: 'ContractInstantiateResultTo299', - gasConsumed: 'u64', - gasRequired: 'u64', + gasConsumed: 'Weight', + gasRequired: 'Weight', storageDeposit: 'StorageDeposit', debugMessage: 'Text', result: 'InstantiateReturnValue' @@ -358,15 +358,15 @@ export default { }, ScheduleTo212: { version: 'u32', - putCodePerByteCost: 'Gas', - growMemCost: 'Gas', - regularOpCost: 'Gas', - returnDataPerByteCost: 'Gas', - eventDataPerByteCost: 'Gas', - eventPerTopicCost: 'Gas', - eventBaseCost: 'Gas', - sandboxDataReadCost: 'Gas', - sandboxDataWriteCost: 'Gas', + putCodePerByteCost: 'u64', + growMemCost: 'u64', + regularOpCost: 'u64', + returnDataPerByteCost: 'u64', + eventDataPerByteCost: 'u64', + eventPerTopicCost: 'u64', + eventBaseCost: 'u64', + sandboxDataReadCost: 'u64', + sandboxDataWriteCost: 'u64', maxEventTopics: 'u32', maxStackHeight: 'u32', maxMemoryPages: 'u32', @@ -375,16 +375,16 @@ export default { }, ScheduleTo258: { version: 'u32', - putCodePerByteCost: 'Gas', - growMemCost: 'Gas', - regularOpCost: 'Gas', - returnDataPerByteCost: 'Gas', - eventDataPerByteCost: 'Gas', - eventPerTopicCost: 'Gas', - eventBaseCost: 'Gas', - sandboxDataReadCost: 'Gas', - sandboxDataWriteCost: 'Gas', - transferCost: 'Gas', + putCodePerByteCost: 'u64', + growMemCost: 'u64', + regularOpCost: 'u64', + returnDataPerByteCost: 'u64', + eventDataPerByteCost: 'u64', + eventPerTopicCost: 'u64', + eventBaseCost: 'u64', + sandboxDataReadCost: 'u64', + sandboxDataWriteCost: 'u64', + transferCost: 'u64', maxEventTopics: 'u32', maxStackHeight: 'u32', maxMemoryPages: 'u32', diff --git a/packages/types/src/interfaces/contracts/types.ts b/packages/types/src/interfaces/contracts/types.ts index f49d0d6a74c1..95b88dc2897c 100644 --- a/packages/types/src/interfaces/contracts/types.ts +++ b/packages/types/src/interfaces/contracts/types.ts @@ -64,15 +64,15 @@ export interface ContractCallRequest extends Struct { readonly origin: AccountId; readonly dest: AccountId; readonly value: Balance; - readonly gasLimit: u64; + readonly gasLimit: Gas; readonly storageDepositLimit: Option; readonly inputData: Bytes; } /** @name ContractExecResult */ export interface ContractExecResult extends Struct { - readonly gasConsumed: u64; - readonly gasRequired: u64; + readonly gasConsumed: Gas; + readonly gasRequired: Gas; readonly storageDeposit: StorageDeposit; readonly debugMessage: Text; readonly result: ContractExecResultResult; @@ -139,8 +139,8 @@ export interface ContractInfo extends Enum { /** @name ContractInstantiateResult */ export interface ContractInstantiateResult extends Struct { - readonly gasConsumed: u64; - readonly gasRequired: u64; + readonly gasConsumed: Gas; + readonly gasRequired: Gas; readonly storageDeposit: StorageDeposit; readonly debugMessage: Text; readonly result: InstantiateReturnValue; @@ -181,7 +181,7 @@ export interface ExecReturnValue extends Struct { } /** @name Gas */ -export interface Gas extends u64 {} +export interface Gas extends Weight {} /** @name HostFnWeights */ export interface HostFnWeights extends Struct { @@ -303,7 +303,7 @@ export interface InstantiateRequest extends Struct { export interface InstantiateRequestV1 extends Struct { readonly origin: AccountId; readonly value: Balance; - readonly gasLimit: Gas; + readonly gasLimit: u64; readonly code: Bytes; readonly data: Bytes; readonly salt: Bytes; @@ -452,15 +452,15 @@ export interface Schedule extends Struct { /** @name ScheduleTo212 */ export interface ScheduleTo212 extends Struct { readonly version: u32; - readonly putCodePerByteCost: Gas; - readonly growMemCost: Gas; - readonly regularOpCost: Gas; - readonly returnDataPerByteCost: Gas; - readonly eventDataPerByteCost: Gas; - readonly eventPerTopicCost: Gas; - readonly eventBaseCost: Gas; - readonly sandboxDataReadCost: Gas; - readonly sandboxDataWriteCost: Gas; + readonly putCodePerByteCost: u64; + readonly growMemCost: u64; + readonly regularOpCost: u64; + readonly returnDataPerByteCost: u64; + readonly eventDataPerByteCost: u64; + readonly eventPerTopicCost: u64; + readonly eventBaseCost: u64; + readonly sandboxDataReadCost: u64; + readonly sandboxDataWriteCost: u64; readonly maxEventTopics: u32; readonly maxStackHeight: u32; readonly maxMemoryPages: u32; @@ -471,16 +471,16 @@ export interface ScheduleTo212 extends Struct { /** @name ScheduleTo258 */ export interface ScheduleTo258 extends Struct { readonly version: u32; - readonly putCodePerByteCost: Gas; - readonly growMemCost: Gas; - readonly regularOpCost: Gas; - readonly returnDataPerByteCost: Gas; - readonly eventDataPerByteCost: Gas; - readonly eventPerTopicCost: Gas; - readonly eventBaseCost: Gas; - readonly sandboxDataReadCost: Gas; - readonly sandboxDataWriteCost: Gas; - readonly transferCost: Gas; + readonly putCodePerByteCost: u64; + readonly growMemCost: u64; + readonly regularOpCost: u64; + readonly returnDataPerByteCost: u64; + readonly eventDataPerByteCost: u64; + readonly eventPerTopicCost: u64; + readonly eventBaseCost: u64; + readonly sandboxDataReadCost: u64; + readonly sandboxDataWriteCost: u64; + readonly transferCost: u64; readonly maxEventTopics: u32; readonly maxStackHeight: u32; readonly maxMemoryPages: u32; From 05093af73ecabdbe074b200a1f15b41a986da941 Mon Sep 17 00:00:00 2001 From: jasl Date: Wed, 9 Nov 2022 18:41:42 +0800 Subject: [PATCH 02/13] Make Contracts Runtime Calls support WeightV2 --- packages/api-contract/src/base/Contract.ts | 31 +++++----------------- 1 file changed, 6 insertions(+), 25 deletions(-) diff --git a/packages/api-contract/src/base/Contract.ts b/packages/api-contract/src/base/Contract.ts index 1b480ad044ad..162363152c12 100644 --- a/packages/api-contract/src/base/Contract.ts +++ b/packages/api-contract/src/base/Contract.ts @@ -4,7 +4,7 @@ import type { SubmittableExtrinsic } from '@polkadot/api/submittable/types'; import type { ApiTypes, DecorateMethod } from '@polkadot/api/types'; import type { Bytes } from '@polkadot/types'; -import type { AccountId, ContractExecResult, EventRecord, Weight, WeightV2 } from '@polkadot/types/interfaces'; +import type { AccountId, ContractExecResult, EventRecord } from '@polkadot/types/interfaces'; import type { ISubmittableResult } from '@polkadot/types/types'; import type { AbiMessage, ContractCallOutcome, ContractOptions, DecodedEvent, WeightAll } from '../types'; import type { ContractCallResult, ContractCallSend, ContractQuery, ContractTx, MapMessageQuery, MapMessageTx } from './types'; @@ -13,7 +13,7 @@ import { map } from 'rxjs'; import { SubmittableResult } from '@polkadot/api'; import { ApiBase } from '@polkadot/api/base'; -import { BN, BN_HUNDRED, BN_ONE, BN_ZERO, isUndefined, logger } from '@polkadot/util'; +import { BN_ZERO, isUndefined, logger } from '@polkadot/util'; import { Abi } from '../Abi'; import { applyOnEvent } from '../util'; @@ -24,9 +24,6 @@ export interface ContractConstructor { new(api: ApiBase, abi: string | Record | Abi, address: string | AccountId): Contract; } -// As per Rust, 5 * GAS_PER_SEC -const MAX_CALL_GAS = new BN(5_000_000_000_000).isub(BN_ONE); - const l = logger('Contract'); function createQuery (meta: AbiMessage, fn: (origin: string | AccountId | Uint8Array, options: ContractOptions, params: unknown[]) => ContractCallResult): ContractQuery { @@ -85,24 +82,6 @@ export class Contract extends Base { return this.#tx; } - #getGas = (_gasLimit: bigint | BN | string | number | WeightV2, isCall = false): WeightAll => { - const weight = convertWeight(_gasLimit); - - if (weight.v1Weight.gt(BN_ZERO)) { - return weight; - } - - return convertWeight( - isCall - ? MAX_CALL_GAS - : convertWeight( - this.api.consts.system.blockWeights - ? (this.api.consts.system.blockWeights as unknown as { maxBlock: WeightV2 }).maxBlock - : this.api.consts.system.maximumBlockWeight as Weight - ).v1Weight.muln(64).div(BN_HUNDRED) - ); - }; - #exec = (messageOrId: AbiMessage | string | number, { gasLimit = BN_ZERO, storageDepositLimit = null, value = BN_ZERO }: ContractOptions, params: unknown[]): SubmittableExtrinsic => { return this.api.tx.contracts.call( this.address, @@ -141,8 +120,10 @@ export class Contract extends Base { origin, this.address, value, - // the runtime interface still used u64 inputs - this.#getGas(gasLimit, true).v1Weight, + this._isOldWeight + // jiggle v1 weights, metadata points to latest + ? convertWeight(gasLimit).v1Weight as unknown as WeightAll['v2Weight'] + : convertWeight(gasLimit).v2Weight, storageDepositLimit, message.toU8a(params) ).pipe( From 78258e670ddec4a5d2c467d43d2490cf966b6e3a Mon Sep 17 00:00:00 2001 From: jasl Date: Wed, 9 Nov 2022 20:50:00 +0800 Subject: [PATCH 03/13] fix gasConsumed & gasConsumed type --- packages/api-contract/src/types.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/api-contract/src/types.ts b/packages/api-contract/src/types.ts index 51a1043ccdf6..53fc5b196eaa 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 } from '@polkadot/types'; -import type { ContractExecResultResult, ContractSelector, StorageDeposit, Weight, WeightV2 } from '@polkadot/types/interfaces'; +import type { ContractExecResultResult, ContractSelector, StorageDeposit, WeightV2 } from '@polkadot/types/interfaces'; import type { Codec, TypeDef } from '@polkadot/types/types'; import type { BN } from '@polkadot/util'; import type { Abi } from '.'; @@ -55,8 +55,8 @@ export interface InterfaceContractCalls { export interface ContractCallOutcome { debugMessage: Text; - gasConsumed: Weight; - gasRequired: Weight; + gasConsumed: bigint | string | number | BN | WeightV2; + gasRequired: bigint | string | number | BN | WeightV2; output: Codec | null; result: ContractExecResultResult; storageDeposit: StorageDeposit; From 4b0bcb7c4d558b0c0bfbd5bfa5335ff3956fde7a Mon Sep 17 00:00:00 2001 From: jasl Date: Wed, 9 Nov 2022 21:05:34 +0800 Subject: [PATCH 04/13] Regenerate contracts types --- packages/types/src/interfaces/contracts/runtime.ts | 4 ++-- packages/types/src/interfaces/contracts/types.ts | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/types/src/interfaces/contracts/runtime.ts b/packages/types/src/interfaces/contracts/runtime.ts index 4344518e400b..1e1093113780 100644 --- a/packages/types/src/interfaces/contracts/runtime.ts +++ b/packages/types/src/interfaces/contracts/runtime.ts @@ -24,7 +24,7 @@ export const runtime: DefinitionsCall = { }, { name: 'gasLimit', - type: 'u64' + type: 'Weight' }, { name: 'storageDepositLimit', @@ -64,7 +64,7 @@ export const runtime: DefinitionsCall = { }, { name: 'gasLimit', - type: 'u64' + type: 'Weight' }, { name: 'storageDepositLimit', diff --git a/packages/types/src/interfaces/contracts/types.ts b/packages/types/src/interfaces/contracts/types.ts index 95b88dc2897c..212589b34968 100644 --- a/packages/types/src/interfaces/contracts/types.ts +++ b/packages/types/src/interfaces/contracts/types.ts @@ -64,15 +64,15 @@ export interface ContractCallRequest extends Struct { readonly origin: AccountId; readonly dest: AccountId; readonly value: Balance; - readonly gasLimit: Gas; + readonly gasLimit: Weight; readonly storageDepositLimit: Option; readonly inputData: Bytes; } /** @name ContractExecResult */ export interface ContractExecResult extends Struct { - readonly gasConsumed: Gas; - readonly gasRequired: Gas; + readonly gasConsumed: Weight; + readonly gasRequired: Weight; readonly storageDeposit: StorageDeposit; readonly debugMessage: Text; readonly result: ContractExecResultResult; @@ -139,8 +139,8 @@ export interface ContractInfo extends Enum { /** @name ContractInstantiateResult */ export interface ContractInstantiateResult extends Struct { - readonly gasConsumed: Gas; - readonly gasRequired: Gas; + readonly gasConsumed: Weight; + readonly gasRequired: Weight; readonly storageDeposit: StorageDeposit; readonly debugMessage: Text; readonly result: InstantiateReturnValue; From 7647400e78e90525ec871dd6a78bc1fae87dd464 Mon Sep 17 00:00:00 2001 From: jasl Date: Wed, 9 Nov 2022 23:02:24 +0800 Subject: [PATCH 05/13] Correct ContractCallOutcome --- packages/api-contract/src/types.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/api-contract/src/types.ts b/packages/api-contract/src/types.ts index 53fc5b196eaa..ff6e0eafc7ca 100644 --- a/packages/api-contract/src/types.ts +++ b/packages/api-contract/src/types.ts @@ -3,7 +3,7 @@ import type { ApiBase } from '@polkadot/api/base'; import type { ApiTypes } from '@polkadot/api/types'; -import type { Text } from '@polkadot/types'; +import type { Text, u64 } from '@polkadot/types'; import type { ContractExecResultResult, ContractSelector, StorageDeposit, WeightV2 } from '@polkadot/types/interfaces'; import type { Codec, TypeDef } from '@polkadot/types/types'; import type { BN } from '@polkadot/util'; @@ -55,8 +55,8 @@ export interface InterfaceContractCalls { export interface ContractCallOutcome { debugMessage: Text; - gasConsumed: bigint | string | number | BN | WeightV2; - gasRequired: bigint | string | number | BN | WeightV2; + gasConsumed: u64 | WeightV2; + gasRequired: u64 | WeightV2; output: Codec | null; result: ContractExecResultResult; storageDeposit: StorageDeposit; From 4668785d8310aee6cd38301fe93a373a799944a0 Mon Sep 17 00:00:00 2001 From: jasl Date: Thu, 10 Nov 2022 19:04:07 +0800 Subject: [PATCH 06/13] `gasLimit` are optional --- packages/types/src/interfaces/contracts/definitions.ts | 2 +- packages/types/src/interfaces/contracts/runtime.ts | 4 ++-- packages/types/src/interfaces/contracts/types.ts | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/types/src/interfaces/contracts/definitions.ts b/packages/types/src/interfaces/contracts/definitions.ts index 38feffc239b5..1ca0df0e681a 100644 --- a/packages/types/src/interfaces/contracts/definitions.ts +++ b/packages/types/src/interfaces/contracts/definitions.ts @@ -45,7 +45,7 @@ export default { origin: 'AccountId', dest: 'AccountId', value: 'Balance', - gasLimit: 'Weight', + gasLimit: 'Option', storageDepositLimit: 'Option', inputData: 'Bytes' }, diff --git a/packages/types/src/interfaces/contracts/runtime.ts b/packages/types/src/interfaces/contracts/runtime.ts index 1e1093113780..a88a82d3a59f 100644 --- a/packages/types/src/interfaces/contracts/runtime.ts +++ b/packages/types/src/interfaces/contracts/runtime.ts @@ -24,7 +24,7 @@ export const runtime: DefinitionsCall = { }, { name: 'gasLimit', - type: 'Weight' + type: 'Option' }, { name: 'storageDepositLimit', @@ -64,7 +64,7 @@ export const runtime: DefinitionsCall = { }, { name: 'gasLimit', - type: 'Weight' + type: 'Option' }, { name: 'storageDepositLimit', diff --git a/packages/types/src/interfaces/contracts/types.ts b/packages/types/src/interfaces/contracts/types.ts index 212589b34968..5fe794445a9e 100644 --- a/packages/types/src/interfaces/contracts/types.ts +++ b/packages/types/src/interfaces/contracts/types.ts @@ -64,7 +64,7 @@ export interface ContractCallRequest extends Struct { readonly origin: AccountId; readonly dest: AccountId; readonly value: Balance; - readonly gasLimit: Weight; + readonly gasLimit: Option; readonly storageDepositLimit: Option; readonly inputData: Bytes; } From 1da7ffa591b4d6ff06b556389de800a1cf046c8c Mon Sep 17 00:00:00 2001 From: jasl Date: Fri, 11 Nov 2022 02:40:05 +0800 Subject: [PATCH 07/13] Apply suggestion --- packages/api-contract/src/types.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/api-contract/src/types.ts b/packages/api-contract/src/types.ts index ff6e0eafc7ca..51a1043ccdf6 100644 --- a/packages/api-contract/src/types.ts +++ b/packages/api-contract/src/types.ts @@ -3,8 +3,8 @@ 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, StorageDeposit, WeightV2 } from '@polkadot/types/interfaces'; +import type { Text } from '@polkadot/types'; +import type { ContractExecResultResult, ContractSelector, StorageDeposit, Weight, WeightV2 } from '@polkadot/types/interfaces'; import type { Codec, TypeDef } from '@polkadot/types/types'; import type { BN } from '@polkadot/util'; import type { Abi } from '.'; @@ -55,8 +55,8 @@ export interface InterfaceContractCalls { export interface ContractCallOutcome { debugMessage: Text; - gasConsumed: u64 | WeightV2; - gasRequired: u64 | WeightV2; + gasConsumed: Weight; + gasRequired: Weight; output: Codec | null; result: ContractExecResultResult; storageDeposit: StorageDeposit; From 13e3828dd52727d91aad776587c0139c39311cff Mon Sep 17 00:00:00 2001 From: jasl Date: Fri, 11 Nov 2022 16:38:13 +0800 Subject: [PATCH 08/13] Add ContractCallRequestV1 and specify old rpc use that --- packages/rpc-augment/src/augment/jsonrpc.ts | 6 +++--- packages/types-augment/src/registry/interfaces.ts | 3 ++- packages/types/src/interfaces/contracts/definitions.ts | 8 ++++++++ packages/types/src/interfaces/contracts/rpc.ts | 4 ++-- packages/types/src/interfaces/contracts/types.ts | 10 ++++++++++ 5 files changed, 25 insertions(+), 6 deletions(-) diff --git a/packages/rpc-augment/src/augment/jsonrpc.ts b/packages/rpc-augment/src/augment/jsonrpc.ts index d59bfc241bb6..45bbe002b591 100644 --- a/packages/rpc-augment/src/augment/jsonrpc.ts +++ b/packages/rpc-augment/src/augment/jsonrpc.ts @@ -15,7 +15,7 @@ import type { BeefySignedCommitment } from '@polkadot/types/interfaces/beefy'; import type { BlockHash } from '@polkadot/types/interfaces/chain'; import type { PrefixedStorageKey } from '@polkadot/types/interfaces/childstate'; import type { AuthorityId } from '@polkadot/types/interfaces/consensus'; -import type { CodeUploadRequest, CodeUploadResult, ContractCallRequest, ContractExecResult, ContractInstantiateResult, InstantiateRequest } from '@polkadot/types/interfaces/contracts'; +import type { CodeUploadRequest, CodeUploadResult, ContractCallRequestV1, ContractExecResult, ContractInstantiateResult, InstantiateRequestV1 } from '@polkadot/types/interfaces/contracts'; import type { BlockStats } from '@polkadot/types/interfaces/dev'; import type { CreatedBlock } from '@polkadot/types/interfaces/engine'; import type { EthAccount, EthCallRequest, EthFeeHistory, EthFilter, EthFilterChanges, EthLog, EthReceipt, EthRichBlock, EthSubKind, EthSubParams, EthSyncStatus, EthTransaction, EthTransactionRequest, EthWork } from '@polkadot/types/interfaces/eth'; @@ -145,7 +145,7 @@ declare module '@polkadot/rpc-core/types/jsonrpc' { * @deprecated Use the runtime interface `api.call.contractsApi.call` instead * Executes a call to a contract **/ - call: AugmentedRpc<(callRequest: ContractCallRequest | { origin?: any; dest?: any; value?: any; gasLimit?: any; storageDepositLimit?: any; inputData?: any } | string | Uint8Array, at?: BlockHash | string | Uint8Array) => Observable>; + call: AugmentedRpc<(callRequest: ContractCallRequestV1 | { origin?: any; dest?: any; value?: any; gasLimit?: any; storageDepositLimit?: any; inputData?: any } | string | Uint8Array, at?: BlockHash | string | Uint8Array) => Observable>; /** * @deprecated Use the runtime interface `api.call.contractsApi.getStorage` instead * Returns the value under a specified storage key in a contract @@ -155,7 +155,7 @@ declare module '@polkadot/rpc-core/types/jsonrpc' { * @deprecated Use the runtime interface `api.call.contractsApi.instantiate` instead * Instantiate a new contract **/ - instantiate: AugmentedRpc<(request: InstantiateRequest | { origin?: any; value?: any; gasLimit?: any; storageDepositLimit?: any; code?: any; data?: any; salt?: any } | string | Uint8Array, at?: BlockHash | string | Uint8Array) => Observable>; + instantiate: AugmentedRpc<(request: InstantiateRequestV1 | { origin?: any; value?: any; gasLimit?: any; code?: any; data?: any; salt?: any } | string | Uint8Array, at?: BlockHash | string | Uint8Array) => Observable>; /** * @deprecated Not available in newer versions of the contracts interfaces * Returns the projected time a given contract will be able to sustain paying its rent diff --git a/packages/types-augment/src/registry/interfaces.ts b/packages/types-augment/src/registry/interfaces.ts index 046e818518c4..5962f4738657 100644 --- a/packages/types-augment/src/registry/interfaces.ts +++ b/packages/types-augment/src/registry/interfaces.ts @@ -23,7 +23,7 @@ import type { PrefixedStorageKey } from '@polkadot/types/interfaces/childstate'; import type { StatementKind } from '@polkadot/types/interfaces/claims'; import type { CollectiveOrigin, MemberCount, ProposalIndex, Votes, VotesTo230 } from '@polkadot/types/interfaces/collective'; import type { AuthorityId, RawVRFOutput } from '@polkadot/types/interfaces/consensus'; -import type { AliveContractInfo, CodeHash, CodeSource, CodeUploadRequest, CodeUploadResult, CodeUploadResultValue, ContractCallFlags, ContractCallRequest, ContractExecResult, ContractExecResultOk, ContractExecResultResult, ContractExecResultSuccessTo255, ContractExecResultSuccessTo260, ContractExecResultTo255, ContractExecResultTo260, ContractExecResultTo267, ContractInfo, ContractInstantiateResult, ContractInstantiateResultTo267, ContractInstantiateResultTo299, ContractReturnFlags, ContractStorageKey, DeletedContract, ExecReturnValue, Gas, HostFnWeights, HostFnWeightsTo264, InstantiateRequest, InstantiateRequestV1, InstantiateRequestV2, InstantiateReturnValue, InstantiateReturnValueOk, InstantiateReturnValueTo267, InstructionWeights, Limits, LimitsTo264, PrefabWasmModule, RentProjection, Schedule, ScheduleTo212, ScheduleTo258, ScheduleTo264, SeedOf, StorageDeposit, TombstoneContractInfo, TrieId } from '@polkadot/types/interfaces/contracts'; +import type { AliveContractInfo, CodeHash, CodeSource, CodeUploadRequest, CodeUploadResult, CodeUploadResultValue, ContractCallFlags, ContractCallRequest, ContractCallRequestV1, ContractExecResult, ContractExecResultOk, ContractExecResultResult, ContractExecResultSuccessTo255, ContractExecResultSuccessTo260, ContractExecResultTo255, ContractExecResultTo260, ContractExecResultTo267, ContractInfo, ContractInstantiateResult, ContractInstantiateResultTo267, ContractInstantiateResultTo299, ContractReturnFlags, ContractStorageKey, DeletedContract, ExecReturnValue, Gas, HostFnWeights, HostFnWeightsTo264, InstantiateRequest, InstantiateRequestV1, InstantiateRequestV2, InstantiateReturnValue, InstantiateReturnValueOk, InstantiateReturnValueTo267, InstructionWeights, Limits, LimitsTo264, PrefabWasmModule, RentProjection, Schedule, ScheduleTo212, ScheduleTo258, ScheduleTo264, SeedOf, StorageDeposit, TombstoneContractInfo, TrieId } from '@polkadot/types/interfaces/contracts'; import type { ContractConstructorSpecLatest, ContractConstructorSpecV0, ContractConstructorSpecV1, ContractConstructorSpecV2, ContractConstructorSpecV3, ContractContractSpecV0, ContractContractSpecV1, ContractContractSpecV2, ContractContractSpecV3, ContractContractSpecV4, ContractCryptoHasher, ContractDiscriminant, ContractDisplayName, ContractEventParamSpecLatest, ContractEventParamSpecV0, ContractEventParamSpecV2, ContractEventSpecLatest, ContractEventSpecV0, ContractEventSpecV1, ContractEventSpecV2, ContractLayoutArray, ContractLayoutCell, ContractLayoutEnum, ContractLayoutHash, ContractLayoutHashingStrategy, ContractLayoutKey, ContractLayoutStruct, ContractLayoutStructField, ContractMessageParamSpecLatest, ContractMessageParamSpecV0, ContractMessageParamSpecV2, ContractMessageSpecLatest, ContractMessageSpecV0, ContractMessageSpecV1, ContractMessageSpecV2, ContractMetadata, ContractMetadataLatest, ContractMetadataV0, ContractMetadataV1, ContractMetadataV2, ContractMetadataV3, ContractMetadataV4, ContractProject, ContractProjectContract, ContractProjectInfo, ContractProjectSource, ContractProjectV0, ContractSelector, ContractStorageLayout, ContractTypeSpec } from '@polkadot/types/interfaces/contractsAbi'; import type { FundIndex, FundInfo, LastContribution, TrieIndex } from '@polkadot/types/interfaces/crowdloan'; import type { CollationInfo, CollationInfoV1, ConfigData, MessageId, OverweightIndex, PageCounter, PageIndexData } from '@polkadot/types/interfaces/cumulus'; @@ -244,6 +244,7 @@ declare module '@polkadot/types/types/registry' { ConsumedWeight: ConsumedWeight; ContractCallFlags: ContractCallFlags; ContractCallRequest: ContractCallRequest; + ContractCallRequestV1: ContractCallRequestV1; ContractConstructorSpecLatest: ContractConstructorSpecLatest; ContractConstructorSpecV0: ContractConstructorSpecV0; ContractConstructorSpecV1: ContractConstructorSpecV1; diff --git a/packages/types/src/interfaces/contracts/definitions.ts b/packages/types/src/interfaces/contracts/definitions.ts index 1ca0df0e681a..51c2609e944a 100644 --- a/packages/types/src/interfaces/contracts/definitions.ts +++ b/packages/types/src/interfaces/contracts/definitions.ts @@ -41,6 +41,14 @@ export default { codeHash: 'CodeHash', deposit: 'Balance' }, + ContractCallRequestV1: { + origin: 'AccountId', + dest: 'AccountId', + value: 'Balance', + gasLimit: 'u64', + storageDepositLimit: 'Option', + inputData: 'Bytes' + }, ContractCallRequest: { origin: 'AccountId', dest: 'AccountId', diff --git a/packages/types/src/interfaces/contracts/rpc.ts b/packages/types/src/interfaces/contracts/rpc.ts index 2f3b921e8401..42a5a485fb64 100644 --- a/packages/types/src/interfaces/contracts/rpc.ts +++ b/packages/types/src/interfaces/contracts/rpc.ts @@ -10,7 +10,7 @@ export const rpc: DefinitionsRpc = { params: [ { name: 'callRequest', - type: 'ContractCallRequest' + type: 'ContractCallRequestV1' }, { isHistoric: true, @@ -48,7 +48,7 @@ export const rpc: DefinitionsRpc = { params: [ { name: 'request', - type: 'InstantiateRequest' + type: 'InstantiateRequestV1' }, { isHistoric: true, diff --git a/packages/types/src/interfaces/contracts/types.ts b/packages/types/src/interfaces/contracts/types.ts index 5fe794445a9e..b8620f59cc2f 100644 --- a/packages/types/src/interfaces/contracts/types.ts +++ b/packages/types/src/interfaces/contracts/types.ts @@ -69,6 +69,16 @@ export interface ContractCallRequest extends Struct { readonly inputData: Bytes; } +/** @name ContractCallRequestV1 */ +export interface ContractCallRequestV1 extends Struct { + readonly origin: AccountId; + readonly dest: AccountId; + readonly value: Balance; + readonly gasLimit: u64; + readonly storageDepositLimit: Option; + readonly inputData: Bytes; +} + /** @name ContractExecResult */ export interface ContractExecResult extends Struct { readonly gasConsumed: Weight; From 5c0e8e16fa60e7382e1f85701c952ec4cffaf325 Mon Sep 17 00:00:00 2001 From: jasl Date: Fri, 11 Nov 2022 18:02:00 +0800 Subject: [PATCH 09/13] Add back getGas --- packages/api-contract/src/base/Contract.ts | 32 ++++++++++++++++++---- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/packages/api-contract/src/base/Contract.ts b/packages/api-contract/src/base/Contract.ts index 162363152c12..db5d85f8abd0 100644 --- a/packages/api-contract/src/base/Contract.ts +++ b/packages/api-contract/src/base/Contract.ts @@ -4,7 +4,7 @@ import type { SubmittableExtrinsic } from '@polkadot/api/submittable/types'; import type { ApiTypes, DecorateMethod } from '@polkadot/api/types'; import type { Bytes } from '@polkadot/types'; -import type { AccountId, ContractExecResult, EventRecord } from '@polkadot/types/interfaces'; +import type { AccountId, ContractExecResult, EventRecord, Weight, WeightV2 } from '@polkadot/types/interfaces'; import type { ISubmittableResult } from '@polkadot/types/types'; import type { AbiMessage, ContractCallOutcome, ContractOptions, DecodedEvent, WeightAll } from '../types'; import type { ContractCallResult, ContractCallSend, ContractQuery, ContractTx, MapMessageQuery, MapMessageTx } from './types'; @@ -13,7 +13,7 @@ import { map } from 'rxjs'; import { SubmittableResult } from '@polkadot/api'; import { ApiBase } from '@polkadot/api/base'; -import { BN_ZERO, isUndefined, logger } from '@polkadot/util'; +import { BN, BN_HUNDRED, BN_ONE, BN_ZERO, isUndefined, logger } from '@polkadot/util'; import { Abi } from '../Abi'; import { applyOnEvent } from '../util'; @@ -24,6 +24,9 @@ export interface ContractConstructor { new(api: ApiBase, abi: string | Record | Abi, address: string | AccountId): Contract; } +// As per Rust, 5 * GAS_PER_SEC +const MAX_CALL_GAS = new BN(5_000_000_000_000).isub(BN_ONE); + const l = logger('Contract'); function createQuery (meta: AbiMessage, fn: (origin: string | AccountId | Uint8Array, options: ContractOptions, params: unknown[]) => ContractCallResult): ContractQuery { @@ -82,6 +85,24 @@ export class Contract extends Base { return this.#tx; } + #getGas = (_gasLimit: bigint | BN | string | number | WeightV2, isCall = false): WeightAll => { + const weight = convertWeight(_gasLimit); + + if (weight.v1Weight.gt(BN_ZERO)) { + return weight; + } + + return convertWeight( + isCall + ? MAX_CALL_GAS + : convertWeight( + this.api.consts.system.blockWeights + ? (this.api.consts.system.blockWeights as unknown as { maxBlock: WeightV2 }).maxBlock + : this.api.consts.system.maximumBlockWeight as Weight + ).v1Weight.muln(64).div(BN_HUNDRED) + ); + }; + #exec = (messageOrId: AbiMessage | string | number, { gasLimit = BN_ZERO, storageDepositLimit = null, value = BN_ZERO }: ContractOptions, params: unknown[]): SubmittableExtrinsic => { return this.api.tx.contracts.call( this.address, @@ -120,10 +141,9 @@ export class Contract extends Base { origin, this.address, value, - this._isOldWeight - // jiggle v1 weights, metadata points to latest - ? convertWeight(gasLimit).v1Weight as unknown as WeightAll['v2Weight'] - : convertWeight(gasLimit).v2Weight, + this.api.tx.contracts.call.meta.version === 1 + ? this.#getGas(gasLimit, true).v1Weight + : this.#getGas(gasLimit, true).v2Weight, storageDepositLimit, message.toU8a(params) ).pipe( From c9f605d826bd4d33193fab2ab0f96aa47ed73c64 Mon Sep 17 00:00:00 2001 From: jasl Date: Sat, 12 Nov 2022 02:28:03 +0800 Subject: [PATCH 10/13] Port jg-ContractsApi-v2 (#5330) --- packages/api-augment/src/substrate/runtime.ts | 26 +++- .../types-augment/src/registry/interfaces.ts | 5 +- .../src/interfaces/contracts/definitions.ts | 72 ++++++----- .../types/src/interfaces/contracts/runtime.ts | 122 ++++++++++++++---- .../types/src/interfaces/contracts/types.ts | 76 ++++++----- 5 files changed, 208 insertions(+), 93 deletions(-) diff --git a/packages/api-augment/src/substrate/runtime.ts b/packages/api-augment/src/substrate/runtime.ts index 1d5369a9452d..10d9d880897d 100644 --- a/packages/api-augment/src/substrate/runtime.ts +++ b/packages/api-augment/src/substrate/runtime.ts @@ -12,12 +12,13 @@ import type { BabeEquivocationProof, BabeGenesisConfiguration, Epoch, OpaqueKeyO import type { CheckInherentsResult, InherentData } from '@polkadot/types/interfaces/blockbuilder'; import type { BlockHash } from '@polkadot/types/interfaces/chain'; import type { AuthorityId } from '@polkadot/types/interfaces/consensus'; +import type { CodeSource, CodeUploadResult, ContractExecResult, ContractInstantiateResult } from '@polkadot/types/interfaces/contracts'; import type { Extrinsic } from '@polkadot/types/interfaces/extrinsics'; import type { AuthorityList, GrandpaEquivocationProof, SetId } from '@polkadot/types/interfaces/grandpa'; import type { OpaqueMetadata } from '@polkadot/types/interfaces/metadata'; import type { MmrBatchProof, MmrEncodableOpaqueLeaf, MmrError, MmrLeafIndex, MmrProof } from '@polkadot/types/interfaces/mmr'; import type { FeeDetails, RuntimeDispatchInfo } from '@polkadot/types/interfaces/payment'; -import type { AccountId, Balance, Block, Call, Hash, Header, Index, KeyTypeId, Slot } from '@polkadot/types/interfaces/runtime'; +import type { AccountId, Balance, Block, Call, Hash, Header, Index, KeyTypeId, Slot, WeightV2 } from '@polkadot/types/interfaces/runtime'; import type { RuntimeVersion } from '@polkadot/types/interfaces/state'; import type { ApplyExtrinsicResult } from '@polkadot/types/interfaces/system'; import type { TransactionSource, TransactionValidity } from '@polkadot/types/interfaces/txqueue'; @@ -104,6 +105,29 @@ declare module '@polkadot/api-base/types/calls' { **/ [key: string]: DecoratedCallBase; }; + /** 0x68b66ba122c93fa7/2 */ + contractsApi: { + /** + * Perform a call from a specified account to a given contract. + **/ + call: AugmentedCall | null | Uint8Array | WeightV2 | { refTime?: any; proofSize?: any } | string, storageDepositLimit: Option | null | Uint8Array | Balance | AnyNumber, inputData: Bytes | string | Uint8Array) => Observable>; + /** + * Query a given storage key in a given contract. + **/ + getStorage: AugmentedCall Observable>>; + /** + * Instantiate a new contract. + **/ + instantiate: AugmentedCall | null | Uint8Array | WeightV2 | { refTime?: any; proofSize?: any } | string, storageDepositLimit: Option | null | Uint8Array | Balance | AnyNumber, code: CodeSource | { Upload: any } | { Existing: any } | string | Uint8Array, data: Bytes | string | Uint8Array, salt: Bytes | string | Uint8Array) => Observable>; + /** + * Upload new code without instantiating a contract from it. + **/ + uploadCode: AugmentedCall | null | Uint8Array | Balance | AnyNumber) => Observable>; + /** + * Generic call + **/ + [key: string]: DecoratedCallBase; + }; /** 0xdf6acb689907609b/4 */ core: { /** diff --git a/packages/types-augment/src/registry/interfaces.ts b/packages/types-augment/src/registry/interfaces.ts index 5962f4738657..d370e780b07f 100644 --- a/packages/types-augment/src/registry/interfaces.ts +++ b/packages/types-augment/src/registry/interfaces.ts @@ -23,7 +23,7 @@ import type { PrefixedStorageKey } from '@polkadot/types/interfaces/childstate'; import type { StatementKind } from '@polkadot/types/interfaces/claims'; import type { CollectiveOrigin, MemberCount, ProposalIndex, Votes, VotesTo230 } from '@polkadot/types/interfaces/collective'; import type { AuthorityId, RawVRFOutput } from '@polkadot/types/interfaces/consensus'; -import type { AliveContractInfo, CodeHash, CodeSource, CodeUploadRequest, CodeUploadResult, CodeUploadResultValue, ContractCallFlags, ContractCallRequest, ContractCallRequestV1, ContractExecResult, ContractExecResultOk, ContractExecResultResult, ContractExecResultSuccessTo255, ContractExecResultSuccessTo260, ContractExecResultTo255, ContractExecResultTo260, ContractExecResultTo267, ContractInfo, ContractInstantiateResult, ContractInstantiateResultTo267, ContractInstantiateResultTo299, ContractReturnFlags, ContractStorageKey, DeletedContract, ExecReturnValue, Gas, HostFnWeights, HostFnWeightsTo264, InstantiateRequest, InstantiateRequestV1, InstantiateRequestV2, InstantiateReturnValue, InstantiateReturnValueOk, InstantiateReturnValueTo267, InstructionWeights, Limits, LimitsTo264, PrefabWasmModule, RentProjection, Schedule, ScheduleTo212, ScheduleTo258, ScheduleTo264, SeedOf, StorageDeposit, TombstoneContractInfo, TrieId } from '@polkadot/types/interfaces/contracts'; +import type { AliveContractInfo, CodeHash, CodeSource, CodeUploadRequest, CodeUploadResult, CodeUploadResultValue, ContractCallFlags, ContractCallRequest, ContractExecResult, ContractExecResultOk, ContractExecResultResult, ContractExecResultSuccessTo255, ContractExecResultSuccessTo260, ContractExecResultTo255, ContractExecResultTo260, ContractExecResultTo267, ContractExecResultU64, ContractInfo, ContractInstantiateResult, ContractInstantiateResultTo267, ContractInstantiateResultTo299, ContractInstantiateResultU64, ContractReturnFlags, ContractStorageKey, DeletedContract, ExecReturnValue, Gas, HostFnWeights, HostFnWeightsTo264, InstantiateRequest, InstantiateRequestV1, InstantiateRequestV2, InstantiateReturnValue, InstantiateReturnValueOk, InstantiateReturnValueTo267, InstructionWeights, Limits, LimitsTo264, PrefabWasmModule, RentProjection, Schedule, ScheduleTo212, ScheduleTo258, ScheduleTo264, SeedOf, StorageDeposit, TombstoneContractInfo, TrieId } from '@polkadot/types/interfaces/contracts'; import type { ContractConstructorSpecLatest, ContractConstructorSpecV0, ContractConstructorSpecV1, ContractConstructorSpecV2, ContractConstructorSpecV3, ContractContractSpecV0, ContractContractSpecV1, ContractContractSpecV2, ContractContractSpecV3, ContractContractSpecV4, ContractCryptoHasher, ContractDiscriminant, ContractDisplayName, ContractEventParamSpecLatest, ContractEventParamSpecV0, ContractEventParamSpecV2, ContractEventSpecLatest, ContractEventSpecV0, ContractEventSpecV1, ContractEventSpecV2, ContractLayoutArray, ContractLayoutCell, ContractLayoutEnum, ContractLayoutHash, ContractLayoutHashingStrategy, ContractLayoutKey, ContractLayoutStruct, ContractLayoutStructField, ContractMessageParamSpecLatest, ContractMessageParamSpecV0, ContractMessageParamSpecV2, ContractMessageSpecLatest, ContractMessageSpecV0, ContractMessageSpecV1, ContractMessageSpecV2, ContractMetadata, ContractMetadataLatest, ContractMetadataV0, ContractMetadataV1, ContractMetadataV2, ContractMetadataV3, ContractMetadataV4, ContractProject, ContractProjectContract, ContractProjectInfo, ContractProjectSource, ContractProjectV0, ContractSelector, ContractStorageLayout, ContractTypeSpec } from '@polkadot/types/interfaces/contractsAbi'; import type { FundIndex, FundInfo, LastContribution, TrieIndex } from '@polkadot/types/interfaces/crowdloan'; import type { CollationInfo, CollationInfoV1, ConfigData, MessageId, OverweightIndex, PageCounter, PageIndexData } from '@polkadot/types/interfaces/cumulus'; @@ -244,7 +244,6 @@ declare module '@polkadot/types/types/registry' { ConsumedWeight: ConsumedWeight; ContractCallFlags: ContractCallFlags; ContractCallRequest: ContractCallRequest; - ContractCallRequestV1: ContractCallRequestV1; ContractConstructorSpecLatest: ContractConstructorSpecLatest; ContractConstructorSpecV0: ContractConstructorSpecV0; ContractConstructorSpecV1: ContractConstructorSpecV1; @@ -273,10 +272,12 @@ declare module '@polkadot/types/types/registry' { ContractExecResultTo255: ContractExecResultTo255; ContractExecResultTo260: ContractExecResultTo260; ContractExecResultTo267: ContractExecResultTo267; + ContractExecResultU64: ContractExecResultU64; ContractInfo: ContractInfo; ContractInstantiateResult: ContractInstantiateResult; ContractInstantiateResultTo267: ContractInstantiateResultTo267; ContractInstantiateResultTo299: ContractInstantiateResultTo299; + ContractInstantiateResultU64: ContractInstantiateResultU64; ContractLayoutArray: ContractLayoutArray; ContractLayoutCell: ContractLayoutCell; ContractLayoutEnum: ContractLayoutEnum; diff --git a/packages/types/src/interfaces/contracts/definitions.ts b/packages/types/src/interfaces/contracts/definitions.ts index 51c2609e944a..b0ce3756bb81 100644 --- a/packages/types/src/interfaces/contracts/definitions.ts +++ b/packages/types/src/interfaces/contracts/definitions.ts @@ -41,19 +41,11 @@ export default { codeHash: 'CodeHash', deposit: 'Balance' }, - ContractCallRequestV1: { - origin: 'AccountId', - dest: 'AccountId', - value: 'Balance', - gasLimit: 'u64', - storageDepositLimit: 'Option', - inputData: 'Bytes' - }, ContractCallRequest: { origin: 'AccountId', dest: 'AccountId', value: 'Balance', - gasLimit: 'Option', + gasLimit: 'u64', storageDepositLimit: 'Option', inputData: 'Bytes' }, @@ -95,6 +87,13 @@ export default { debugMessage: 'Text', result: 'ContractExecResultResult' }, + ContractExecResultU64: { + gasConsumed: 'u64', + gasRequired: 'u64', + storageDeposit: 'StorageDeposit', + debugMessage: 'Text', + result: 'ContractExecResultResult' + }, ContractInfo: { _enum: { Alive: 'AliveContractInfo', @@ -125,7 +124,7 @@ export default { flags: 'ContractReturnFlags', data: 'Bytes' }, - Gas: 'Weight', + Gas: 'u64', HostFnWeightsTo264: { caller: 'Weight', address: 'Weight', @@ -230,7 +229,7 @@ export default { InstantiateRequestV1: { origin: 'AccountId', value: 'Balance', - gasLimit: 'u64', + gasLimit: 'Gas', code: 'Bytes', data: 'Bytes', salt: 'Bytes' @@ -258,9 +257,18 @@ export default { ContractInstantiateResultTo267: 'Result', ContractInstantiateResultTo299: 'Result', ContractInstantiateResult: { + gasConsumed: 'WeightV2', + gasRequired: 'WeightV2', + storageDeposit: 'StorageDeposit', + debugMessage: 'Text', + result: 'InstantiateReturnValue' + }, + ContractInstantiateResultU64: { + // only this one can fail, the current version (above) _should_ be correctly + // versioned now, aka no more deprecated RPCs involved, only runtime calls _fallback: 'ContractInstantiateResultTo299', - gasConsumed: 'Weight', - gasRequired: 'Weight', + gasConsumed: 'u64', + gasRequired: 'u64', storageDeposit: 'StorageDeposit', debugMessage: 'Text', result: 'InstantiateReturnValue' @@ -366,15 +374,15 @@ export default { }, ScheduleTo212: { version: 'u32', - putCodePerByteCost: 'u64', - growMemCost: 'u64', - regularOpCost: 'u64', - returnDataPerByteCost: 'u64', - eventDataPerByteCost: 'u64', - eventPerTopicCost: 'u64', - eventBaseCost: 'u64', - sandboxDataReadCost: 'u64', - sandboxDataWriteCost: 'u64', + putCodePerByteCost: 'Gas', + growMemCost: 'Gas', + regularOpCost: 'Gas', + returnDataPerByteCost: 'Gas', + eventDataPerByteCost: 'Gas', + eventPerTopicCost: 'Gas', + eventBaseCost: 'Gas', + sandboxDataReadCost: 'Gas', + sandboxDataWriteCost: 'Gas', maxEventTopics: 'u32', maxStackHeight: 'u32', maxMemoryPages: 'u32', @@ -383,16 +391,16 @@ export default { }, ScheduleTo258: { version: 'u32', - putCodePerByteCost: 'u64', - growMemCost: 'u64', - regularOpCost: 'u64', - returnDataPerByteCost: 'u64', - eventDataPerByteCost: 'u64', - eventPerTopicCost: 'u64', - eventBaseCost: 'u64', - sandboxDataReadCost: 'u64', - sandboxDataWriteCost: 'u64', - transferCost: 'u64', + putCodePerByteCost: 'Gas', + growMemCost: 'Gas', + regularOpCost: 'Gas', + returnDataPerByteCost: 'Gas', + eventDataPerByteCost: 'Gas', + eventPerTopicCost: 'Gas', + eventBaseCost: 'Gas', + sandboxDataReadCost: 'Gas', + sandboxDataWriteCost: 'Gas', + transferCost: 'Gas', maxEventTopics: 'u32', maxStackHeight: 'u32', maxMemoryPages: 'u32', diff --git a/packages/types/src/interfaces/contracts/runtime.ts b/packages/types/src/interfaces/contracts/runtime.ts index a88a82d3a59f..3e17c2ff1d16 100644 --- a/packages/types/src/interfaces/contracts/runtime.ts +++ b/packages/types/src/interfaces/contracts/runtime.ts @@ -1,12 +1,49 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { DefinitionsCall } from '../../types'; +import type { DefinitionCall, DefinitionsCall } from '../../types'; + +import { objectSpread } from '@polkadot/util'; + +const SHARED_V1_V2: Record = { + get_storage: { + description: 'Query a given storage key in a given contract.', + params: [ + { + name: 'address', + type: 'AccountId' + }, + { + name: 'key', + type: 'Bytes' + } + ], + type: 'Option' + }, + upload_code: { + description: 'Upload new code without instantiating a contract from it.', + params: [ + { + name: 'origin', + type: 'AccountId' + }, + { + name: 'code', + type: 'Bytes' + }, + { + name: 'storageDepositLimit', + type: 'Option' + } + ], + type: 'CodeUploadResult' + } +}; export const runtime: DefinitionsCall = { ContractsApi: [ { - methods: { + methods: objectSpread({ call: { description: 'Perform a call from a specified account to a given contract.', params: [ @@ -24,7 +61,7 @@ export const runtime: DefinitionsCall = { }, { name: 'gasLimit', - type: 'Option' + type: 'Option' }, { name: 'storageDepositLimit', @@ -37,20 +74,6 @@ export const runtime: DefinitionsCall = { ], type: 'ContractExecResult' }, - get_storage: { - description: 'Query a given storage key in a given contract.', - params: [ - { - name: 'address', - type: 'AccountId' - }, - { - name: 'key', - type: 'Bytes' - } - ], - type: 'Option' - }, instantiate: { description: 'Instantiate a new contract.', params: [ @@ -64,7 +87,7 @@ export const runtime: DefinitionsCall = { }, { name: 'gasLimit', - type: 'Option' + type: 'Option' }, { name: 'storageDepositLimit', @@ -84,26 +107,77 @@ export const runtime: DefinitionsCall = { } ], type: 'ContractInstantiateResult' + } + }, SHARED_V1_V2), + version: 2 + }, + { + methods: objectSpread({ + call: { + description: 'Perform a call from a specified account to a given contract.', + params: [ + { + name: 'origin', + type: 'AccountId' + }, + { + name: 'dest', + type: 'AccountId' + }, + { + name: 'value', + type: 'Balance' + }, + { + name: 'gasLimit', + type: 'u64' + }, + { + name: 'storageDepositLimit', + type: 'Option' + }, + { + name: 'inputData', + type: 'Vec' + } + ], + type: 'ContractExecResultU64' }, - upload_code: { - description: 'Upload new code without instantiating a contract from it.', + instantiate: { + description: 'Instantiate a new contract.', params: [ { name: 'origin', type: 'AccountId' }, { - name: 'code', - type: 'Bytes' + name: 'value', + type: 'Balance' + }, + { + name: 'gasLimit', + type: 'u64' }, { name: 'storageDepositLimit', type: 'Option' + }, + { + name: 'code', + type: 'CodeSource' + }, + { + name: 'data', + type: 'Bytes' + }, + { + name: 'salt', + type: 'Bytes' } ], - type: 'CodeUploadResult' + type: 'ContractInstantiateResultU64' } - }, + }, SHARED_V1_V2), version: 1 } ] diff --git a/packages/types/src/interfaces/contracts/types.ts b/packages/types/src/interfaces/contracts/types.ts index b8620f59cc2f..2345c128cce8 100644 --- a/packages/types/src/interfaces/contracts/types.ts +++ b/packages/types/src/interfaces/contracts/types.ts @@ -2,7 +2,7 @@ /* eslint-disable */ import type { Bytes, Compact, Enum, Null, Option, Raw, Result, Set, Struct, Text, U8aFixed, bool, u32, u64, u8 } from '@polkadot/types-codec'; -import type { AccountId, Balance, BlockNumber, Hash, Weight } from '@polkadot/types/interfaces/runtime'; +import type { AccountId, Balance, BlockNumber, Hash, Weight, WeightV2 } from '@polkadot/types/interfaces/runtime'; import type { DispatchError } from '@polkadot/types/interfaces/system'; /** @name AliveContractInfo */ @@ -61,16 +61,6 @@ export interface ContractCallFlags extends Set { /** @name ContractCallRequest */ export interface ContractCallRequest extends Struct { - readonly origin: AccountId; - readonly dest: AccountId; - readonly value: Balance; - readonly gasLimit: Option; - readonly storageDepositLimit: Option; - readonly inputData: Bytes; -} - -/** @name ContractCallRequestV1 */ -export interface ContractCallRequestV1 extends Struct { readonly origin: AccountId; readonly dest: AccountId; readonly value: Balance; @@ -138,6 +128,15 @@ export interface ContractExecResultTo267 extends Struct { readonly result: ContractExecResultResult; } +/** @name ContractExecResultU64 */ +export interface ContractExecResultU64 extends Struct { + readonly gasConsumed: u64; + readonly gasRequired: u64; + readonly storageDeposit: StorageDeposit; + readonly debugMessage: Text; + readonly result: ContractExecResultResult; +} + /** @name ContractInfo */ export interface ContractInfo extends Enum { readonly isAlive: boolean; @@ -149,8 +148,8 @@ export interface ContractInfo extends Enum { /** @name ContractInstantiateResult */ export interface ContractInstantiateResult extends Struct { - readonly gasConsumed: Weight; - readonly gasRequired: Weight; + readonly gasConsumed: WeightV2; + readonly gasRequired: WeightV2; readonly storageDeposit: StorageDeposit; readonly debugMessage: Text; readonly result: InstantiateReturnValue; @@ -170,6 +169,15 @@ export interface ContractInstantiateResultTo299 extends Result Date: Sat, 12 Nov 2022 02:49:12 +0800 Subject: [PATCH 11/13] Run yarn build:metadata --- packages/rpc-augment/src/augment/jsonrpc.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/rpc-augment/src/augment/jsonrpc.ts b/packages/rpc-augment/src/augment/jsonrpc.ts index 45bbe002b591..dfb3455372c8 100644 --- a/packages/rpc-augment/src/augment/jsonrpc.ts +++ b/packages/rpc-augment/src/augment/jsonrpc.ts @@ -15,7 +15,7 @@ import type { BeefySignedCommitment } from '@polkadot/types/interfaces/beefy'; import type { BlockHash } from '@polkadot/types/interfaces/chain'; import type { PrefixedStorageKey } from '@polkadot/types/interfaces/childstate'; import type { AuthorityId } from '@polkadot/types/interfaces/consensus'; -import type { CodeUploadRequest, CodeUploadResult, ContractCallRequestV1, ContractExecResult, ContractInstantiateResult, InstantiateRequestV1 } from '@polkadot/types/interfaces/contracts'; +import type { CodeUploadRequest, CodeUploadResult, ContractExecResult, ContractInstantiateResult, InstantiateRequestV1 } from '@polkadot/types/interfaces/contracts'; import type { BlockStats } from '@polkadot/types/interfaces/dev'; import type { CreatedBlock } from '@polkadot/types/interfaces/engine'; import type { EthAccount, EthCallRequest, EthFeeHistory, EthFilter, EthFilterChanges, EthLog, EthReceipt, EthRichBlock, EthSubKind, EthSubParams, EthSyncStatus, EthTransaction, EthTransactionRequest, EthWork } from '@polkadot/types/interfaces/eth'; @@ -145,7 +145,7 @@ declare module '@polkadot/rpc-core/types/jsonrpc' { * @deprecated Use the runtime interface `api.call.contractsApi.call` instead * Executes a call to a contract **/ - call: AugmentedRpc<(callRequest: ContractCallRequestV1 | { origin?: any; dest?: any; value?: any; gasLimit?: any; storageDepositLimit?: any; inputData?: any } | string | Uint8Array, at?: BlockHash | string | Uint8Array) => Observable>; + call: AugmentedRpc<(callRequest: ContractCallRequestV1, at?: BlockHash | string | Uint8Array) => Observable>; /** * @deprecated Use the runtime interface `api.call.contractsApi.getStorage` instead * Returns the value under a specified storage key in a contract From 277b7d802a0f74624ded74ed52ef73c1cea011c8 Mon Sep 17 00:00:00 2001 From: jasl Date: Sat, 12 Nov 2022 03:07:04 +0800 Subject: [PATCH 12/13] There is no 'ContractCallRequestV1', only 'ContractCallRequest' --- packages/rpc-augment/src/augment/jsonrpc.ts | 4 ++-- packages/types/src/interfaces/contracts/rpc.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/rpc-augment/src/augment/jsonrpc.ts b/packages/rpc-augment/src/augment/jsonrpc.ts index dfb3455372c8..9e671676eaf9 100644 --- a/packages/rpc-augment/src/augment/jsonrpc.ts +++ b/packages/rpc-augment/src/augment/jsonrpc.ts @@ -15,7 +15,7 @@ import type { BeefySignedCommitment } from '@polkadot/types/interfaces/beefy'; import type { BlockHash } from '@polkadot/types/interfaces/chain'; import type { PrefixedStorageKey } from '@polkadot/types/interfaces/childstate'; import type { AuthorityId } from '@polkadot/types/interfaces/consensus'; -import type { CodeUploadRequest, CodeUploadResult, ContractExecResult, ContractInstantiateResult, InstantiateRequestV1 } from '@polkadot/types/interfaces/contracts'; +import type { CodeUploadRequest, CodeUploadResult, ContractCallRequest, ContractExecResult, ContractInstantiateResult, InstantiateRequestV1 } from '@polkadot/types/interfaces/contracts'; import type { BlockStats } from '@polkadot/types/interfaces/dev'; import type { CreatedBlock } from '@polkadot/types/interfaces/engine'; import type { EthAccount, EthCallRequest, EthFeeHistory, EthFilter, EthFilterChanges, EthLog, EthReceipt, EthRichBlock, EthSubKind, EthSubParams, EthSyncStatus, EthTransaction, EthTransactionRequest, EthWork } from '@polkadot/types/interfaces/eth'; @@ -145,7 +145,7 @@ declare module '@polkadot/rpc-core/types/jsonrpc' { * @deprecated Use the runtime interface `api.call.contractsApi.call` instead * Executes a call to a contract **/ - call: AugmentedRpc<(callRequest: ContractCallRequestV1, at?: BlockHash | string | Uint8Array) => Observable>; + call: AugmentedRpc<(callRequest: ContractCallRequest | { origin?: any; dest?: any; value?: any; gasLimit?: any; storageDepositLimit?: any; inputData?: any } | string | Uint8Array, at?: BlockHash | string | Uint8Array) => Observable>; /** * @deprecated Use the runtime interface `api.call.contractsApi.getStorage` instead * Returns the value under a specified storage key in a contract diff --git a/packages/types/src/interfaces/contracts/rpc.ts b/packages/types/src/interfaces/contracts/rpc.ts index 42a5a485fb64..2796fce3cfd9 100644 --- a/packages/types/src/interfaces/contracts/rpc.ts +++ b/packages/types/src/interfaces/contracts/rpc.ts @@ -10,7 +10,7 @@ export const rpc: DefinitionsRpc = { params: [ { name: 'callRequest', - type: 'ContractCallRequestV1' + type: 'ContractCallRequest' }, { isHistoric: true, From bcddda6548be74b038c36db8af234639b0ebcecc Mon Sep 17 00:00:00 2001 From: jasl Date: Sat, 12 Nov 2022 03:18:18 +0800 Subject: [PATCH 13/13] api.tx.contracts.call.meta no version field, _isOldWeight still work? --- packages/api-contract/src/base/Contract.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/api-contract/src/base/Contract.ts b/packages/api-contract/src/base/Contract.ts index db5d85f8abd0..89dcea5778df 100644 --- a/packages/api-contract/src/base/Contract.ts +++ b/packages/api-contract/src/base/Contract.ts @@ -141,8 +141,9 @@ export class Contract extends Base { origin, this.address, value, - this.api.tx.contracts.call.meta.version === 1 - ? this.#getGas(gasLimit, true).v1Weight + this._isOldWeight + // jiggle v1 weights, metadata points to latest + ? this.#getGas(gasLimit, true).v1Weight as unknown as WeightAll['v2Weight'] : this.#getGas(gasLimit, true).v2Weight, storageDepositLimit, message.toU8a(params)