Skip to content

Commit cad2b0f

Browse files
authored
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?
1 parent dd6dd83 commit cad2b0f

File tree

9 files changed

+176
-40
lines changed

9 files changed

+176
-40
lines changed

packages/api-augment/src/substrate/runtime.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ import type { BabeEquivocationProof, BabeGenesisConfiguration, Epoch, OpaqueKeyO
1212
import type { CheckInherentsResult, InherentData } from '@polkadot/types/interfaces/blockbuilder';
1313
import type { BlockHash } from '@polkadot/types/interfaces/chain';
1414
import type { AuthorityId } from '@polkadot/types/interfaces/consensus';
15+
import type { CodeSource, CodeUploadResult, ContractExecResult, ContractInstantiateResult } from '@polkadot/types/interfaces/contracts';
1516
import type { Extrinsic } from '@polkadot/types/interfaces/extrinsics';
1617
import type { AuthorityList, GrandpaEquivocationProof, SetId } from '@polkadot/types/interfaces/grandpa';
1718
import type { OpaqueMetadata } from '@polkadot/types/interfaces/metadata';
1819
import type { MmrBatchProof, MmrEncodableOpaqueLeaf, MmrError, MmrLeafIndex, MmrProof } from '@polkadot/types/interfaces/mmr';
1920
import type { FeeDetails, RuntimeDispatchInfo } from '@polkadot/types/interfaces/payment';
20-
import type { AccountId, Balance, Block, Call, Hash, Header, Index, KeyTypeId, Slot } from '@polkadot/types/interfaces/runtime';
21+
import type { AccountId, Balance, Block, Call, Hash, Header, Index, KeyTypeId, Slot, WeightV2 } from '@polkadot/types/interfaces/runtime';
2122
import type { RuntimeVersion } from '@polkadot/types/interfaces/state';
2223
import type { ApplyExtrinsicResult } from '@polkadot/types/interfaces/system';
2324
import type { TransactionSource, TransactionValidity } from '@polkadot/types/interfaces/txqueue';
@@ -104,6 +105,29 @@ declare module '@polkadot/api-base/types/calls' {
104105
**/
105106
[key: string]: DecoratedCallBase<ApiType>;
106107
};
108+
/** 0x68b66ba122c93fa7/2 */
109+
contractsApi: {
110+
/**
111+
* Perform a call from a specified account to a given contract.
112+
**/
113+
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>>;
114+
/**
115+
* Query a given storage key in a given contract.
116+
**/
117+
getStorage: AugmentedCall<ApiType, (address: AccountId | string | Uint8Array, key: Bytes | string | Uint8Array) => Observable<Option<Bytes>>>;
118+
/**
119+
* Instantiate a new contract.
120+
**/
121+
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>>;
122+
/**
123+
* Upload new code without instantiating a contract from it.
124+
**/
125+
uploadCode: AugmentedCall<ApiType, (origin: AccountId | string | Uint8Array, code: Bytes | string | Uint8Array, storageDepositLimit: Option<Balance> | null | Uint8Array | Balance | AnyNumber) => Observable<CodeUploadResult>>;
126+
/**
127+
* Generic call
128+
**/
129+
[key: string]: DecoratedCallBase<ApiType>;
130+
};
107131
/** 0xdf6acb689907609b/4 */
108132
core: {
109133
/**

packages/api-contract/src/base/Contract.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,10 @@ export class Contract<ApiType extends ApiTypes> extends Base<ApiType> {
142142
origin,
143143
this.address,
144144
value,
145-
// the runtime interface still used u64 inputs
146-
this.#getGas(gasLimit, true).v1Weight,
145+
this._isOldWeight
146+
// jiggle v1 weights, metadata points to latest
147+
? this.#getGas(gasLimit, true).v1Weight as unknown as WeightAll['v2Weight']
148+
: this.#getGas(gasLimit, true).v2Weight,
147149
storageDepositLimit,
148150
message.toU8a(params)
149151
).pipe(

packages/api-contract/src/types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
import type { ApiBase } from '@polkadot/api/base';
55
import type { ApiTypes } from '@polkadot/api/types';
6-
import type { Text, u64 } from '@polkadot/types';
7-
import type { ContractExecResultResult, ContractSelector, StorageDeposit, WeightV2 } from '@polkadot/types/interfaces';
6+
import type { Text } from '@polkadot/types';
7+
import type { ContractExecResultResult, ContractSelector, StorageDeposit, Weight, WeightV2 } from '@polkadot/types/interfaces';
88
import type { Codec, TypeDef } from '@polkadot/types/types';
99
import type { BN } from '@polkadot/util';
1010
import type { Abi } from '.';
@@ -55,8 +55,8 @@ export interface InterfaceContractCalls {
5555

5656
export interface ContractCallOutcome {
5757
debugMessage: Text;
58-
gasConsumed: u64;
59-
gasRequired: u64;
58+
gasConsumed: Weight;
59+
gasRequired: Weight;
6060
output: Codec | null;
6161
result: ContractExecResultResult;
6262
storageDeposit: StorageDeposit;

packages/rpc-augment/src/augment/jsonrpc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import type { BeefySignedCommitment } from '@polkadot/types/interfaces/beefy';
1515
import type { BlockHash } from '@polkadot/types/interfaces/chain';
1616
import type { PrefixedStorageKey } from '@polkadot/types/interfaces/childstate';
1717
import type { AuthorityId } from '@polkadot/types/interfaces/consensus';
18-
import type { CodeUploadRequest, CodeUploadResult, ContractCallRequest, ContractExecResult, ContractInstantiateResult, InstantiateRequest } from '@polkadot/types/interfaces/contracts';
18+
import type { CodeUploadRequest, CodeUploadResult, ContractCallRequest, ContractExecResult, ContractInstantiateResult, InstantiateRequestV1 } from '@polkadot/types/interfaces/contracts';
1919
import type { BlockStats } from '@polkadot/types/interfaces/dev';
2020
import type { CreatedBlock } from '@polkadot/types/interfaces/engine';
2121
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' {
155155
* @deprecated Use the runtime interface `api.call.contractsApi.instantiate` instead
156156
* Instantiate a new contract
157157
**/
158-
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>>;
158+
instantiate: AugmentedRpc<(request: InstantiateRequestV1 | { origin?: any; value?: any; gasLimit?: any; code?: any; data?: any; salt?: any } | string | Uint8Array, at?: BlockHash | string | Uint8Array) => Observable<ContractInstantiateResult>>;
159159
/**
160160
* @deprecated Not available in newer versions of the contracts interfaces
161161
* Returns the projected time a given contract will be able to sustain paying its rent

packages/types-augment/src/registry/interfaces.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import type { PrefixedStorageKey } from '@polkadot/types/interfaces/childstate';
2323
import type { StatementKind } from '@polkadot/types/interfaces/claims';
2424
import type { CollectiveOrigin, MemberCount, ProposalIndex, Votes, VotesTo230 } from '@polkadot/types/interfaces/collective';
2525
import type { AuthorityId, RawVRFOutput } from '@polkadot/types/interfaces/consensus';
26-
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';
26+
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';
2727
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';
2828
import type { FundIndex, FundInfo, LastContribution, TrieIndex } from '@polkadot/types/interfaces/crowdloan';
2929
import type { CollationInfo, CollationInfoV1, ConfigData, MessageId, OverweightIndex, PageCounter, PageIndexData } from '@polkadot/types/interfaces/cumulus';
@@ -272,10 +272,12 @@ declare module '@polkadot/types/types/registry' {
272272
ContractExecResultTo255: ContractExecResultTo255;
273273
ContractExecResultTo260: ContractExecResultTo260;
274274
ContractExecResultTo267: ContractExecResultTo267;
275+
ContractExecResultU64: ContractExecResultU64;
275276
ContractInfo: ContractInfo;
276277
ContractInstantiateResult: ContractInstantiateResult;
277278
ContractInstantiateResultTo267: ContractInstantiateResultTo267;
278279
ContractInstantiateResultTo299: ContractInstantiateResultTo299;
280+
ContractInstantiateResultU64: ContractInstantiateResultU64;
279281
ContractLayoutArray: ContractLayoutArray;
280282
ContractLayoutCell: ContractLayoutCell;
281283
ContractLayoutEnum: ContractLayoutEnum;

packages/types/src/interfaces/contracts/definitions.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ export default {
8181
result: 'ContractExecResultResult'
8282
},
8383
ContractExecResult: {
84+
gasConsumed: 'Weight',
85+
gasRequired: 'Weight',
86+
storageDeposit: 'StorageDeposit',
87+
debugMessage: 'Text',
88+
result: 'ContractExecResultResult'
89+
},
90+
ContractExecResultU64: {
8491
gasConsumed: 'u64',
8592
gasRequired: 'u64',
8693
storageDeposit: 'StorageDeposit',
@@ -250,6 +257,15 @@ export default {
250257
ContractInstantiateResultTo267: 'Result<InstantiateReturnValueTo267, Null>',
251258
ContractInstantiateResultTo299: 'Result<InstantiateReturnValueOk, Null>',
252259
ContractInstantiateResult: {
260+
gasConsumed: 'WeightV2',
261+
gasRequired: 'WeightV2',
262+
storageDeposit: 'StorageDeposit',
263+
debugMessage: 'Text',
264+
result: 'InstantiateReturnValue'
265+
},
266+
ContractInstantiateResultU64: {
267+
// only this one can fail, the current version (above) _should_ be correctly
268+
// versioned now, aka no more deprecated RPCs involved, only runtime calls
253269
_fallback: 'ContractInstantiateResultTo299',
254270
gasConsumed: 'u64',
255271
gasRequired: 'u64',

packages/types/src/interfaces/contracts/rpc.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export const rpc: DefinitionsRpc = {
4848
params: [
4949
{
5050
name: 'request',
51-
type: 'InstantiateRequest'
51+
type: 'InstantiateRequestV1'
5252
},
5353
{
5454
isHistoric: true,

packages/types/src/interfaces/contracts/runtime.ts

Lines changed: 98 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,49 @@
11
// Copyright 2017-2022 @polkadot/types authors & contributors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
import type { DefinitionsCall } from '../../types';
4+
import type { DefinitionCall, DefinitionsCall } from '../../types';
5+
6+
import { objectSpread } from '@polkadot/util';
7+
8+
const SHARED_V1_V2: Record<string, DefinitionCall> = {
9+
get_storage: {
10+
description: 'Query a given storage key in a given contract.',
11+
params: [
12+
{
13+
name: 'address',
14+
type: 'AccountId'
15+
},
16+
{
17+
name: 'key',
18+
type: 'Bytes'
19+
}
20+
],
21+
type: 'Option<Bytes>'
22+
},
23+
upload_code: {
24+
description: 'Upload new code without instantiating a contract from it.',
25+
params: [
26+
{
27+
name: 'origin',
28+
type: 'AccountId'
29+
},
30+
{
31+
name: 'code',
32+
type: 'Bytes'
33+
},
34+
{
35+
name: 'storageDepositLimit',
36+
type: 'Option<Balance>'
37+
}
38+
],
39+
type: 'CodeUploadResult'
40+
}
41+
};
542

643
export const runtime: DefinitionsCall = {
744
ContractsApi: [
845
{
9-
methods: {
46+
methods: objectSpread({
1047
call: {
1148
description: 'Perform a call from a specified account to a given contract.',
1249
params: [
@@ -24,7 +61,7 @@ export const runtime: DefinitionsCall = {
2461
},
2562
{
2663
name: 'gasLimit',
27-
type: 'u64'
64+
type: 'Option<WeightV2>'
2865
},
2966
{
3067
name: 'storageDepositLimit',
@@ -37,20 +74,6 @@ export const runtime: DefinitionsCall = {
3774
],
3875
type: 'ContractExecResult'
3976
},
40-
get_storage: {
41-
description: 'Query a given storage key in a given contract.',
42-
params: [
43-
{
44-
name: 'address',
45-
type: 'AccountId'
46-
},
47-
{
48-
name: 'key',
49-
type: 'Bytes'
50-
}
51-
],
52-
type: 'Option<Bytes>'
53-
},
5477
instantiate: {
5578
description: 'Instantiate a new contract.',
5679
params: [
@@ -64,7 +87,7 @@ export const runtime: DefinitionsCall = {
6487
},
6588
{
6689
name: 'gasLimit',
67-
type: 'u64'
90+
type: 'Option<WeightV2>'
6891
},
6992
{
7093
name: 'storageDepositLimit',
@@ -84,26 +107,77 @@ export const runtime: DefinitionsCall = {
84107
}
85108
],
86109
type: 'ContractInstantiateResult'
110+
}
111+
}, SHARED_V1_V2),
112+
version: 2
113+
},
114+
{
115+
methods: objectSpread({
116+
call: {
117+
description: 'Perform a call from a specified account to a given contract.',
118+
params: [
119+
{
120+
name: 'origin',
121+
type: 'AccountId'
122+
},
123+
{
124+
name: 'dest',
125+
type: 'AccountId'
126+
},
127+
{
128+
name: 'value',
129+
type: 'Balance'
130+
},
131+
{
132+
name: 'gasLimit',
133+
type: 'u64'
134+
},
135+
{
136+
name: 'storageDepositLimit',
137+
type: 'Option<Balance>'
138+
},
139+
{
140+
name: 'inputData',
141+
type: 'Vec<u8>'
142+
}
143+
],
144+
type: 'ContractExecResultU64'
87145
},
88-
upload_code: {
89-
description: 'Upload new code without instantiating a contract from it.',
146+
instantiate: {
147+
description: 'Instantiate a new contract.',
90148
params: [
91149
{
92150
name: 'origin',
93151
type: 'AccountId'
94152
},
95153
{
96-
name: 'code',
97-
type: 'Bytes'
154+
name: 'value',
155+
type: 'Balance'
156+
},
157+
{
158+
name: 'gasLimit',
159+
type: 'u64'
98160
},
99161
{
100162
name: 'storageDepositLimit',
101163
type: 'Option<Balance>'
164+
},
165+
{
166+
name: 'code',
167+
type: 'CodeSource'
168+
},
169+
{
170+
name: 'data',
171+
type: 'Bytes'
172+
},
173+
{
174+
name: 'salt',
175+
type: 'Bytes'
102176
}
103177
],
104-
type: 'CodeUploadResult'
178+
type: 'ContractInstantiateResultU64'
105179
}
106-
},
180+
}, SHARED_V1_V2),
107181
version: 1
108182
}
109183
]

packages/types/src/interfaces/contracts/types.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/* eslint-disable */
33

44
import type { Bytes, Compact, Enum, Null, Option, Raw, Result, Set, Struct, Text, U8aFixed, bool, u32, u64, u8 } from '@polkadot/types-codec';
5-
import type { AccountId, Balance, BlockNumber, Hash, Weight } from '@polkadot/types/interfaces/runtime';
5+
import type { AccountId, Balance, BlockNumber, Hash, Weight, WeightV2 } from '@polkadot/types/interfaces/runtime';
66
import type { DispatchError } from '@polkadot/types/interfaces/system';
77

88
/** @name AliveContractInfo */
@@ -71,8 +71,8 @@ export interface ContractCallRequest extends Struct {
7171

7272
/** @name ContractExecResult */
7373
export interface ContractExecResult extends Struct {
74-
readonly gasConsumed: u64;
75-
readonly gasRequired: u64;
74+
readonly gasConsumed: Weight;
75+
readonly gasRequired: Weight;
7676
readonly storageDeposit: StorageDeposit;
7777
readonly debugMessage: Text;
7878
readonly result: ContractExecResultResult;
@@ -128,6 +128,15 @@ export interface ContractExecResultTo267 extends Struct {
128128
readonly result: ContractExecResultResult;
129129
}
130130

131+
/** @name ContractExecResultU64 */
132+
export interface ContractExecResultU64 extends Struct {
133+
readonly gasConsumed: u64;
134+
readonly gasRequired: u64;
135+
readonly storageDeposit: StorageDeposit;
136+
readonly debugMessage: Text;
137+
readonly result: ContractExecResultResult;
138+
}
139+
131140
/** @name ContractInfo */
132141
export interface ContractInfo extends Enum {
133142
readonly isAlive: boolean;
@@ -139,8 +148,8 @@ export interface ContractInfo extends Enum {
139148

140149
/** @name ContractInstantiateResult */
141150
export interface ContractInstantiateResult extends Struct {
142-
readonly gasConsumed: u64;
143-
readonly gasRequired: u64;
151+
readonly gasConsumed: WeightV2;
152+
readonly gasRequired: WeightV2;
144153
readonly storageDeposit: StorageDeposit;
145154
readonly debugMessage: Text;
146155
readonly result: InstantiateReturnValue;
@@ -160,6 +169,15 @@ export interface ContractInstantiateResultTo299 extends Result<InstantiateReturn
160169
readonly asOk: InstantiateReturnValueOk;
161170
}
162171

172+
/** @name ContractInstantiateResultU64 */
173+
export interface ContractInstantiateResultU64 extends Struct {
174+
readonly gasConsumed: u64;
175+
readonly gasRequired: u64;
176+
readonly storageDeposit: StorageDeposit;
177+
readonly debugMessage: Text;
178+
readonly result: InstantiateReturnValue;
179+
}
180+
163181
/** @name ContractReturnFlags */
164182
export interface ContractReturnFlags extends Set {
165183
readonly isRevert: boolean;

0 commit comments

Comments
 (0)