Skip to content

Commit

Permalink
Make Contracts Runtime Calls support WeightV2
Browse files Browse the repository at this point in the history
  • Loading branch information
jasl committed Nov 9, 2022
1 parent 856ecf3 commit 3bbb6c0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 27 deletions.
5 changes: 3 additions & 2 deletions packages/api-augment/src/substrate/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ 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';
import type { IExtrinsic, Observable } from '@polkadot/types/types';
import type { SpWeightsWeightV2Weight } from '@polkadot/types/lookup';

export type __AugmentedCall<ApiType extends ApiTypes> = AugmentedCall<ApiType>;
export type __DecoratedCallBase<ApiType extends ApiTypes> = DecoratedCallBase<ApiType>;
Expand Down Expand Up @@ -110,15 +111,15 @@ declare module '@polkadot/api-base/types/calls' {
/**
* 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: u64 | AnyNumber | Uint8Array, storageDepositLimit: Option<Balance> | null | Uint8Array | Balance | AnyNumber, inputData: Bytes | string | Uint8Array) => Observable<ContractExecResult>>;
call: AugmentedCall<ApiType, (origin: AccountId | string | Uint8Array, dest: AccountId | string | Uint8Array, value: Balance | AnyNumber | Uint8Array, gasLimit: SpWeightsWeightV2Weight | { refTime?: any; proofSize?: any } | string | Uint8Array, 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: u64 | AnyNumber | Uint8Array, 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>>;
instantiate: AugmentedCall<ApiType, (origin: AccountId | string | Uint8Array, value: Balance | AnyNumber | Uint8Array, gasLimit: SpWeightsWeightV2Weight | { refTime?: any; proofSize?: any } | string | Uint8Array, 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.
**/
Expand Down
31 changes: 6 additions & 25 deletions packages/api-contract/src/base/Contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';
Expand All @@ -24,9 +24,6 @@ export interface ContractConstructor<ApiType extends ApiTypes> {
new(api: ApiBase<ApiType>, abi: string | Record<string, unknown> | Abi, address: string | AccountId): Contract<ApiType>;
}

// 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 <ApiType extends ApiTypes> (meta: AbiMessage, fn: (origin: string | AccountId | Uint8Array, options: ContractOptions, params: unknown[]) => ContractCallResult<ApiType, ContractCallOutcome>): ContractQuery<ApiType> {
Expand Down Expand Up @@ -85,24 +82,6 @@ export class Contract<ApiType extends ApiTypes> extends Base<ApiType> {
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<ApiType> => {
return this.api.tx.contracts.call(
this.address,
Expand Down Expand Up @@ -141,8 +120,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
? convertWeight(gasLimit).v1Weight as unknown as WeightAll['v2Weight']
: convertWeight(gasLimit).v2Weight,
storageDepositLimit,
message.toU8a(params)
).pipe(
Expand Down

0 comments on commit 3bbb6c0

Please sign in to comment.