Skip to content

Commit

Permalink
Simplify function
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben-Rey committed Apr 26, 2024
1 parent a9cb1d5 commit 5e8c948
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export type OptOpDetails = {

type BaseOperation = {
fee: bigint
expirePeriod: number
type: OperationType
expirePeriod?: number
}

export type RollOperation = BaseOperation & {
Expand Down
67 changes: 14 additions & 53 deletions packages/massa-web3/src/experimental/smartContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ import {
} from './basicElements'
import { BlockchainClient } from './client'
import { Account } from './account'
import {
InsufficientBalanceError,
MaxGasError,
MinimalFeeError,
} from './errors'
import { InsufficientBalanceError, MaxGasError } from './errors'

export const MAX_GAS_EXECUTE = 3980167295n
export const MAX_GAS_CALL = 4294167295n
Expand Down Expand Up @@ -252,10 +248,17 @@ export class SmartContract {
{ fee, maxGas, coins = 0n, periodToLive }: CallOptions
): Promise<Operation> {
await this.ensureBalance(account, coins)
fee = await this.ensureMinimumFee(fee)

fee = fee ?? (await this.client.getMinimalFee())

if (!maxGas) maxGas = await this.getGasEstimation()
else this.ensureGasWithinLimits(MIN_GAS_CALL, MAX_GAS_CALL, maxGas)
else {
if (maxGas > MAX_GAS_CALL) {
throw new MaxGasError({ isHigher: true, amount: MAX_GAS_CALL })
} else if (maxGas < MIN_GAS_CALL) {
throw new MaxGasError({ isHigher: false, amount: MIN_GAS_CALL })
}
}

const expirePeriod = await this.getExpirePeriod(periodToLive)

Expand Down Expand Up @@ -294,61 +297,19 @@ export class SmartContract {
}
}

/**
* Verifies that the proposed gas amount for an operation is within the allowable limits.
*
* @param maxGas - The initial max gas amount proposed for the operation.
* @param min - The minimum allowable gas limit for the operation.
* @param max - The maximum allowable gas limit for the operation.
*
* @returns The validated gas amount.
*
* @throws MaxGasError if the proposed gas amount is either too high or too low.
*/
async ensureGasWithinLimits(
min: bigint,
max: bigint,
maxGas?: bigint
): Promise<bigint> {
if (maxGas > max) {
throw new MaxGasError({ isHigher: true, amount: max })
} else if (maxGas < min) {
throw new MaxGasError({ isHigher: false, amount: min })
}
return maxGas
}

/**
* Returns the fee for an operation, ensuring it is at least the minimal fee required.
*
* @param fee - The fee proposed for the operation. If no fee is provided, the minimal fee will be used.
*
* @returns The validated or minimal fee.
*
* @throws MinimalFeeError if the fee provided is less than the minimum required fee.
*/
async ensureMinimumFee(fee?: bigint): Promise<bigint> {
const minimalFee = await this.client.getMinimalFee()
if (!fee) return minimalFee
if (fee < minimalFee) {
throw new MinimalFeeError({ minimalFee })
}
return fee
}

/**
* Returns the period when the operation should expire.
*
* @param periodToLive - The number of periods from now when the operation should expire.
*
* @returns The calculated expiration period for the operation.
*/
async getExpirePeriod(periodToLive: number): Promise<number> {
async getExpirePeriod(periodToLive?: number): Promise<number> {
const currentPeriod = await this.client.fetchPeriod()
// TODO - What should be the default value ?
let expirePeriod = currentPeriod + MIN_PERIODS_TO_LIVE
if (!periodToLive) return expirePeriod
return calculateExpirePeriod(currentPeriod, periodToLive)
return !periodToLive
? currentPeriod + MIN_PERIODS_TO_LIVE
: calculateExpirePeriod(currentPeriod, periodToLive)
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Args, fromMAS } from '@massalabs/web3-utils'
import { fromMAS } from '@massalabs/web3-utils'
import {
ByteCode,
MAX_GAS_CALL,
MIN_GAS_CALL,
SmartContract,
} from '../../../src/experimental/smartContract'
import { account, client } from './setup'
import { Args } from '../../../src/experimental/basicElements'

const CONTRACT_ADDRESS = 'AS1JsLnBg4wAKJrAgNUYSs32oTpr3WgSDJdPZib3r3o2zLLRE8sP'
const TIMEOUT = 61000
Expand Down Expand Up @@ -111,7 +112,7 @@ describe('Smart Contract', () => {
})

expect(call).rejects.toThrow(
`The gas limit for the operation call was below the minimum amount of ${MIN_GAS_CALL}`
`The gas limit for the operation was below the minimum amount of ${MIN_GAS_CALL}`
)
},
TIMEOUT
Expand All @@ -125,7 +126,7 @@ describe('Smart Contract', () => {
})

expect(call).rejects.toThrow(
`The gas limit for the operation call was higher than the maximum amount of ${MAX_GAS_CALL}`
`The gas limit for the operation was higher than the maximum amount of ${MAX_GAS_CALL}`
)
})
})
Expand Down

0 comments on commit 5e8c948

Please sign in to comment.