Skip to content

Commit

Permalink
move gasEstimation in publicJsonProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjah committed Feb 17, 2025
1 parent 6bf0457 commit ba651d3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 28 deletions.
27 changes: 0 additions & 27 deletions src/provider/jsonRpcProvider/jsonRpcProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
CallSCParams,
DeploySCParams,
ExecuteScParams,
GAS_ESTIMATION_TOLERANCE,
Provider,
ReadSCData,
ReadSCParams,
Expand All @@ -13,7 +12,6 @@ import {
Address,
MAX_GAS_CALL,
MIN_GAS_CALL,
minBigInt,
populateDatastore,
PublicAPI,
PublicApiUrl,
Expand All @@ -35,7 +33,6 @@ import {
OperationManager,
} from '../../operation/operationManager'
import { execute } from '../../basicElements/bytecode'
import { U64_t } from '../../basicElements/serializers/number/u64'
import { ErrorMaxGas, ErrorInsufficientBalance } from '../../errors'
import { JsonRpcPublicProvider } from './jsonRpcPublicProvider'

Expand Down Expand Up @@ -287,30 +284,6 @@ export class JsonRpcProvider extends JsonRpcPublicProvider implements Provider {
return manager.send(details)
}

/**
* Returns the gas estimation for a given function.
*
* @remarks To avoid running out of gas, the gas estimation is increased by 20%.
*
* @param params - callSCParams.
* @throws If the read operation returns an error.
* @returns The gas estimation for the function.
*/
protected async getGasEstimation(params: CallSCParams): Promise<U64_t> {
const result = await this.readSC(params)

if (result.info.error) {
throw new Error(result.info.error)
}

const gasCost = BigInt(result.info.gasCost)
return minBigInt(
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
gasCost + (gasCost * GAS_ESTIMATION_TOLERANCE) / 100n,
MAX_GAS_CALL
)
}

protected async checkAccountBalance(coins: Mas): Promise<void> {
if (coins > 0n) {
const balance = await this.client.getBalance(
Expand Down
39 changes: 38 additions & 1 deletion src/provider/jsonRpcProvider/jsonRpcPublicProvider.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { NodeStatusInfo, PublicProvider, ReadSCData, ReadSCParams } from '..'
import {
GAS_ESTIMATION_TOLERANCE,
NodeStatusInfo,
PublicProvider,
ReadSCData,
ReadSCParams,
} from '..'
import {
Account,
CHAIN_ID,
DatastoreEntry,
EventFilter,
Mas,
MAX_GAS_CALL,
minBigInt,
Network,
NetworkName,
PublicAPI,
Expand All @@ -14,6 +22,7 @@ import {
import { rpcTypes as t } from '../../generated'
import { OperationStatus } from '../../operation'
import { formatNodeStatusObject } from '../../client/formatObjects'
import { U64_t } from '../../basicElements/serializers/number/u64'

export class JsonRpcPublicProvider implements PublicProvider {
constructor(public client: PublicAPI) {}
Expand Down Expand Up @@ -113,4 +122,32 @@ export class JsonRpcPublicProvider implements PublicProvider {
const entries: DatastoreEntry[] = keys.map((key) => ({ address, key }))
return this.client.getDatastoreEntries(entries, final)
}

/**
* Returns the gas estimation for a given function.
*
* @remarks To avoid running out of gas, the gas estimation is increased by 20%.
*
* @param params - ReadSCParams. caller must be provided
* @throws If the read operation returns an error.
* @returns The gas estimation for the operation execution.
*/
public async getGasEstimation(params: ReadSCParams): Promise<U64_t> {
if (!params.caller) {
throw new Error('Caller must be provided for gas estimation')
}

const result = await this.readSC(params)

if (result.info.error) {
throw new Error(result.info.error)
}

const gasCost = BigInt(result.info.gasCost)
return minBigInt(
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
gasCost + (gasCost * GAS_ESTIMATION_TOLERANCE) / 100n,
MAX_GAS_CALL
)
}
}

0 comments on commit ba651d3

Please sign in to comment.