-
Notifications
You must be signed in to change notification settings - Fork 18
/
getGasLimit.ts
38 lines (34 loc) · 1.28 KB
/
getGasLimit.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { ApiPromise } from '@polkadot/api'
import type { WeightV2 } from '@polkadot/types/interfaces'
import { BN, bnToBn } from '@polkadot/util'
/**
* Helper function that returns Weights V2 `gasLimit` object.
*/
export const getGasLimit = (api: ApiPromise, _refTime: string | BN, _proofSize: string | BN) => {
const refTime = bnToBn(_refTime)
const proofSize = bnToBn(_proofSize)
return api.registry.createType('WeightV2', {
refTime,
proofSize,
}) as WeightV2
}
/**
* Helper function that returns the maximum gas limit Weights V2 object
* for an extrinsic based on the api chain constants.
* NOTE: It's reduced by a given factor (defaults to 80%) to avoid storage exhaust.
*/
export const getMaxGasLimit = (api: ApiPromise, reductionFactor = 0.8) => {
const blockWeights = api.consts.system.blockWeights.toPrimitive() as any
const maxExtrinsic = blockWeights?.perClass?.normal?.maxExtrinsic
const maxRefTime = maxExtrinsic?.refTime
? bnToBn(maxExtrinsic.refTime)
.mul(new BN(reductionFactor * 100))
.div(new BN(100))
: new BN(0)
const maxProofSize = maxExtrinsic?.proofSize
? bnToBn(maxExtrinsic.proofSize)
.mul(new BN(reductionFactor * 100))
.div(new BN(100))
: new BN(0)
return getGasLimit(api, maxRefTime, maxProofSize)
}