Skip to content

Commit

Permalink
imp(jssdk): use BN_MAX_SUPPLY for deposit settings in gas estimation
Browse files Browse the repository at this point in the history
  • Loading branch information
Leechael committed Oct 13, 2023
1 parent 5924ed4 commit b4962c0
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 18 deletions.
13 changes: 4 additions & 9 deletions frontend/packages/sdk/src/contracts/PinkBlueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import assert from '../utils/assert'
import { randomHex } from '../utils/hex'
import signAndSend from '../utils/signAndSend'
import { PinkContractPromise } from './PinkContract'
import { BN_MAX_SUPPLY } from '../utils/constants'

Check failure on line 25 in frontend/packages/sdk/src/contracts/PinkBlueprint.ts

View workflow job for this annotation

GitHub Actions / test-jssdk

`../utils/constants` import should occur before import of `../utils/hex`

export interface PinkContractInstantiateCallOutcome extends ContractCallOutcome {
salt: string
Expand Down Expand Up @@ -367,22 +368,16 @@ export class PinkBlueprintPromise {
throw new Error(`Constructor not found: ${constructorOrId}`)
}

const { gasPrice, depositPerByte } = this.phatRegistry.clusterInfo ?? {}
if (!gasPrice || !depositPerByte) {
const { gasPrice } = this.phatRegistry.clusterInfo ?? {}
if (!gasPrice) {
throw new Error('No Gas Price or deposit Per Byte from cluster info.')
}

// We assume the address have no tokens in the cluster account, so we will deposit into it
// at the same time we submit the transaction. The gas fee is far lower then 1 PHA in general
// so we plus 1 PHA here.
const msg = this.abi.findConstructor(constructorOrId).toU8a(args)
const deposit = depositPerByte.mul(new BN(msg.length * 1.05)).add(new BN(1e12))

const [clusterBalance, onchainBalance, { gasRequired, storageDeposit }] = await Promise.all([
this.phatRegistry.getClusterBalance(address),
this.api.query.system.account<FrameSystemAccountInfo>(address),
// We estimating the gas & storage deposit cost with deposit propose.
estimate(cert.address, { cert, deposit }, ...args),
estimate(cert.address, { cert, deposit: BN_MAX_SUPPLY }, ...args),
])

// calculate the total costs
Expand Down
13 changes: 4 additions & 9 deletions frontend/packages/sdk/src/contracts/PinkContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type { AbiLike, FrameSystemAccountInfo } from '../types'
import assert from '../utils/assert'
import { randomHex } from '../utils/hex'
import signAndSend from '../utils/signAndSend'
import { BN_MAX_SUPPLY } from '../utils/constants'

Check failure on line 26 in frontend/packages/sdk/src/contracts/PinkContract.ts

View workflow job for this annotation

GitHub Actions / test-jssdk

`../utils/constants` import should occur before import of `../utils/hex`

export type PinkContractCallOutcome<ResultType> = {
output: ResultType
Expand Down Expand Up @@ -459,21 +460,15 @@ export class PinkContractPromise<
throw new Error(`Message not found: ${messageOrId}`)
}

const { gasPrice, depositPerByte } = this.phatRegistry.clusterInfo ?? {}
if (!gasPrice || !depositPerByte) {
const { gasPrice } = this.phatRegistry.clusterInfo ?? {}
if (!gasPrice) {
throw new Error('No Gas Price or deposit Per Byte from cluster info.')
}

// We assume the address have no tokens in the cluster account, so we will deposit into it
// at the same time we submit the transaction. The gas fee is far lower then 1 PHA in general
// so we plus 1 PHA here.
const msg = this.abi.findMessage(messageOrId).toU8a(args)
const deposit = depositPerByte.mul(new BN(msg.length)).add(new BN(1e12))

const [clusterBalance, onchainBalance, { gasRequired, storageDeposit }] = await Promise.all([
this.phatRegistry.getClusterBalance(address),
this.api.query.system.account<FrameSystemAccountInfo>(address),
estimate(cert.address, { cert, deposit }, ...args),
estimate(cert.address, { cert, deposit: BN_MAX_SUPPLY }, ...args),
])

// calculate the total costs
Expand Down
3 changes: 3 additions & 0 deletions frontend/packages/sdk/src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { BN } from '@polkadot/util'

export const BN_MAX_SUPPLY = new BN(1e12).mul(new BN(1e9))

0 comments on commit b4962c0

Please sign in to comment.