Skip to content

Commit a980d4a

Browse files
authored
Allow for max block limit on gas (#2744)
1 parent 5c194b6 commit a980d4a

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { ContractCallResult, ContractRead, MapMessageExec, MapMessageRead } from
1111
import BN from 'bn.js';
1212
import { map } from 'rxjs/operators';
1313
import ApiBase from '@polkadot/api/base';
14-
import { assert, isFunction, isUndefined, stringCamelCase } from '@polkadot/util';
14+
import { assert, bnToBn, isFunction, isUndefined, stringCamelCase } from '@polkadot/util';
1515

1616
import Abi from '../Abi';
1717
import { encodeMessage, formatData } from '../util';
@@ -60,8 +60,16 @@ export default class Contract<ApiType extends ApiTypes> extends Base<ApiType> {
6060
return this.#tx;
6161
}
6262

63+
#getGas = (_gasLimit: BigInt | BN | string | number): BN => {
64+
const gasLimit = bnToBn(_gasLimit);
65+
66+
return gasLimit.lten(0)
67+
? this.api.consts.system.maximumBlockWeight.muln(64).divn(100)
68+
: gasLimit;
69+
}
70+
6371
#exec = (messageOrId: AbiMessage | string | number, value: BigInt | BN | string | number, gasLimit: BigInt | BN | string | number, params: CodecArg[]): SubmittableExtrinsic<ApiType> => {
64-
return this.api.tx.contracts.call(this.address, value, gasLimit, encodeMessage(this.registry, this.abi.findMessage(messageOrId), params));
72+
return this.api.tx.contracts.call(this.address, value, this.#getGas(gasLimit), encodeMessage(this.registry, this.abi.findMessage(messageOrId), params));
6573
}
6674

6775
#read = (messageOrId: AbiMessage | string | number, value: BigInt | BN | string | number, gasLimit: BigInt | BN | string | number, params: CodecArg[]): ContractRead<ApiType> => {
@@ -74,7 +82,7 @@ export default class Contract<ApiType extends ApiTypes> extends Base<ApiType> {
7482
send: this._decorateMethod((origin: string | AccountId | Uint8Array): ContractCallResult<'rpc'> =>
7583
this.api.rx.rpc.contracts.call({
7684
dest: this.address,
77-
gasLimit,
85+
gasLimit: this.#getGas(gasLimit),
7886
inputData: encodeMessage(this.registry, message, params),
7987
origin,
8088
value

0 commit comments

Comments
 (0)