Skip to content

Commit

Permalink
Update contract' gas to Weight type (#5324)
Browse files Browse the repository at this point in the history
* 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?
  • Loading branch information
jasl committed Nov 13, 2022
1 parent dd6dd83 commit cad2b0f
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 40 deletions.
26 changes: 25 additions & 1 deletion packages/api-augment/src/substrate/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -104,6 +105,29 @@ declare module '@polkadot/api-base/types/calls' {
**/
[key: string]: DecoratedCallBase<ApiType>;
};
/** 0x68b66ba122c93fa7/2 */
contractsApi: {
/**
* Perform a call from a specified account to a given contract.
**/
call: AugmentedCall<ApiType, (origin: AccountId | string | Uint8Array, dest: AccountId | string | Uint8Array, value: Balance | AnyNumber | Uint8Array, gasLimit: Option<WeightV2> | null | Uint8Array | WeightV2 | { refTime?: any; proofSize?: any } | string, storageDepositLimit: Option<Balance> | null | Uint8Array | Balance | AnyNumber, inputData: Bytes | string | Uint8Array) => Observable<ContractExecResult>>;
/**
* Query a given storage key in a given contract.
**/
getStorage: AugmentedCall<ApiType, (address: AccountId | string | Uint8Array, key: Bytes | string | Uint8Array) => Observable<Option<Bytes>>>;
/**
* Instantiate a new contract.
**/
instantiate: AugmentedCall<ApiType, (origin: AccountId | string | Uint8Array, value: Balance | AnyNumber | Uint8Array, gasLimit: Option<WeightV2> | null | Uint8Array | WeightV2 | { refTime?: any; proofSize?: any } | string, storageDepositLimit: Option<Balance> | null | Uint8Array | Balance | AnyNumber, code: CodeSource | { Upload: any } | { Existing: any } | string | Uint8Array, data: Bytes | string | Uint8Array, salt: Bytes | string | Uint8Array) => Observable<ContractInstantiateResult>>;
/**
* Upload new code without instantiating a contract from it.
**/
uploadCode: AugmentedCall<ApiType, (origin: AccountId | string | Uint8Array, code: Bytes | string | Uint8Array, storageDepositLimit: Option<Balance> | null | Uint8Array | Balance | AnyNumber) => Observable<CodeUploadResult>>;
/**
* Generic call
**/
[key: string]: DecoratedCallBase<ApiType>;
};
/** 0xdf6acb689907609b/4 */
core: {
/**
Expand Down
6 changes: 4 additions & 2 deletions packages/api-contract/src/base/Contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,10 @@ export class Contract<ApiType extends ApiTypes> extends Base<ApiType> {
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(
Expand Down
8 changes: 4 additions & 4 deletions packages/api-contract/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 '.';
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions packages/rpc-augment/src/augment/jsonrpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<ContractInstantiateResult>>;
instantiate: AugmentedRpc<(request: InstantiateRequestV1 | { origin?: any; value?: any; gasLimit?: any; code?: any; data?: any; salt?: any } | string | Uint8Array, at?: BlockHash | string | Uint8Array) => Observable<ContractInstantiateResult>>;
/**
* @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
Expand Down
4 changes: 3 additions & 1 deletion packages/types-augment/src/registry/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions packages/types/src/interfaces/contracts/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -250,6 +257,15 @@ export default {
ContractInstantiateResultTo267: 'Result<InstantiateReturnValueTo267, Null>',
ContractInstantiateResultTo299: 'Result<InstantiateReturnValueOk, Null>',
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',
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/interfaces/contracts/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const rpc: DefinitionsRpc = {
params: [
{
name: 'request',
type: 'InstantiateRequest'
type: 'InstantiateRequestV1'
},
{
isHistoric: true,
Expand Down
122 changes: 98 additions & 24 deletions packages/types/src/interfaces/contracts/runtime.ts
Original file line number Diff line number Diff line change
@@ -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<string, DefinitionCall> = {
get_storage: {
description: 'Query a given storage key in a given contract.',
params: [
{
name: 'address',
type: 'AccountId'
},
{
name: 'key',
type: 'Bytes'
}
],
type: 'Option<Bytes>'
},
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<Balance>'
}
],
type: 'CodeUploadResult'
}
};

export const runtime: DefinitionsCall = {
ContractsApi: [
{
methods: {
methods: objectSpread({
call: {
description: 'Perform a call from a specified account to a given contract.',
params: [
Expand All @@ -24,7 +61,7 @@ export const runtime: DefinitionsCall = {
},
{
name: 'gasLimit',
type: 'u64'
type: 'Option<WeightV2>'
},
{
name: 'storageDepositLimit',
Expand All @@ -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<Bytes>'
},
instantiate: {
description: 'Instantiate a new contract.',
params: [
Expand All @@ -64,7 +87,7 @@ export const runtime: DefinitionsCall = {
},
{
name: 'gasLimit',
type: 'u64'
type: 'Option<WeightV2>'
},
{
name: 'storageDepositLimit',
Expand All @@ -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<Balance>'
},
{
name: 'inputData',
type: 'Vec<u8>'
}
],
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<Balance>'
},
{
name: 'code',
type: 'CodeSource'
},
{
name: 'data',
type: 'Bytes'
},
{
name: 'salt',
type: 'Bytes'
}
],
type: 'CodeUploadResult'
type: 'ContractInstantiateResultU64'
}
},
}, SHARED_V1_V2),
version: 1
}
]
Expand Down
28 changes: 23 additions & 5 deletions packages/types/src/interfaces/contracts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -160,6 +169,15 @@ export interface ContractInstantiateResultTo299 extends Result<InstantiateReturn
readonly asOk: InstantiateReturnValueOk;
}

/** @name ContractInstantiateResultU64 */
export interface ContractInstantiateResultU64 extends Struct {
readonly gasConsumed: u64;
readonly gasRequired: u64;
readonly storageDeposit: StorageDeposit;
readonly debugMessage: Text;
readonly result: InstantiateReturnValue;
}

/** @name ContractReturnFlags */
export interface ContractReturnFlags extends Set {
readonly isRevert: boolean;
Expand Down

0 comments on commit cad2b0f

Please sign in to comment.