From cad2b0fefe5f88eb34cae2c8f7bf211e3399df7c Mon Sep 17 00:00:00 2001 From: Jun Jiang Date: Sun, 13 Nov 2022 14:30:33 +0800 Subject: [PATCH] Update contract' gas to Weight type (#5324) * Update contract' gas to Weight type * Make Contracts Runtime Calls support WeightV2 * fix gasConsumed & gasConsumed type * Regenerate contracts types * Correct ContractCallOutcome * `gasLimit` are optional * Apply suggestion * Add ContractCallRequestV1 and specify old rpc use that * Add back getGas * Port jg-ContractsApi-v2 (#5330) * Run yarn build:metadata * There is no 'ContractCallRequestV1', only 'ContractCallRequest' * api.tx.contracts.call.meta no version field, _isOldWeight still work? --- packages/api-augment/src/substrate/runtime.ts | 26 +++- packages/api-contract/src/base/Contract.ts | 6 +- packages/api-contract/src/types.ts | 8 +- packages/rpc-augment/src/augment/jsonrpc.ts | 4 +- .../types-augment/src/registry/interfaces.ts | 4 +- .../src/interfaces/contracts/definitions.ts | 16 +++ .../types/src/interfaces/contracts/rpc.ts | 2 +- .../types/src/interfaces/contracts/runtime.ts | 122 ++++++++++++++---- .../types/src/interfaces/contracts/types.ts | 28 +++- 9 files changed, 176 insertions(+), 40 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/api-contract/src/base/Contract.ts b/packages/api-contract/src/base/Contract.ts index aafa53806d9c..ecad4dcc10b3 100644 --- a/packages/api-contract/src/base/Contract.ts +++ b/packages/api-contract/src/base/Contract.ts @@ -142,8 +142,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 + ? this.#getGas(gasLimit, true).v1Weight as unknown as WeightAll['v2Weight'] + : this.#getGas(gasLimit, true).v2Weight, storageDepositLimit, message.toU8a(params) ).pipe( 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/rpc-augment/src/augment/jsonrpc.ts b/packages/rpc-augment/src/augment/jsonrpc.ts index d59bfc241bb6..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, ContractCallRequest, ContractExecResult, ContractInstantiateResult, InstantiateRequest } 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'; @@ -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..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, 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'; @@ -272,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 f0faafdb9e37..b0ce3756bb81 100644 --- a/packages/types/src/interfaces/contracts/definitions.ts +++ b/packages/types/src/interfaces/contracts/definitions.ts @@ -81,6 +81,13 @@ export default { result: 'ContractExecResultResult' }, ContractExecResult: { + gasConsumed: 'Weight', + gasRequired: 'Weight', + storageDeposit: 'StorageDeposit', + debugMessage: 'Text', + result: 'ContractExecResultResult' + }, + ContractExecResultU64: { gasConsumed: 'u64', gasRequired: 'u64', storageDeposit: 'StorageDeposit', @@ -250,6 +257,15 @@ 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: 'u64', gasRequired: 'u64', diff --git a/packages/types/src/interfaces/contracts/rpc.ts b/packages/types/src/interfaces/contracts/rpc.ts index 2f3b921e8401..2796fce3cfd9 100644 --- a/packages/types/src/interfaces/contracts/rpc.ts +++ b/packages/types/src/interfaces/contracts/rpc.ts @@ -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/runtime.ts b/packages/types/src/interfaces/contracts/runtime.ts index 4344518e400b..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: 'u64' + 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: 'u64' + 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 f49d0d6a74c1..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 */ @@ -71,8 +71,8 @@ export interface ContractCallRequest extends Struct { /** @name ContractExecResult */ export interface ContractExecResult extends Struct { - readonly gasConsumed: u64; - readonly gasRequired: u64; + readonly gasConsumed: Weight; + readonly gasRequired: Weight; readonly storageDeposit: StorageDeposit; readonly debugMessage: Text; readonly result: ContractExecResultResult; @@ -128,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; @@ -139,8 +148,8 @@ export interface ContractInfo extends Enum { /** @name ContractInstantiateResult */ export interface ContractInstantiateResult extends Struct { - readonly gasConsumed: u64; - readonly gasRequired: u64; + readonly gasConsumed: WeightV2; + readonly gasRequired: WeightV2; readonly storageDeposit: StorageDeposit; readonly debugMessage: Text; readonly result: InstantiateReturnValue; @@ -160,6 +169,15 @@ export interface ContractInstantiateResultTo299 extends Result