Skip to content

Commit

Permalink
add min gas call check
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjah committed May 28, 2024
1 parent 8ce13bf commit c59cdf1
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 8 deletions.
14 changes: 8 additions & 6 deletions packages/massa-web3/src/web3/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export class BaseClient {
params: params,
id: 0,
}
let body;
let body
try {
body = JSON.stringify(bodyData)
} catch (ex) {
Expand All @@ -206,11 +206,13 @@ export class BaseClient {

const responseData: JsonRpcResponseData<T> = await resp.json()

return {
isError: !!responseData.error,
result: responseData.error ? null : responseData.result as T,
error: responseData.error ? new Error(responseData.error.message) : null,
} as JsonRpcResponseData<T>;
return {
isError: !!responseData.error,
result: responseData.error ? null : (responseData.result as T),
error: responseData.error
? new Error(responseData.error.message)
: null,
} as JsonRpcResponseData<T>
} catch (ex) {
return {
isError: true,
Expand Down
4 changes: 4 additions & 0 deletions packages/massa-web3/src/web3/SmartContractsClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
MAX_GAS_CALL,
toMAS,
MASSA_SCALING_FACTOR,
MIN_GAS_CALL,
} from '@massalabs/web3-utils'
import { wait } from '../utils/time'
import {
Expand Down Expand Up @@ -162,6 +163,9 @@ export class SmartContractsClient
try {
const response = await this.readSmartContract(callData)
callData.maxGas = BigInt(response.info.gas_cost)
if (callData.maxGas < MIN_GAS_CALL) {
callData.maxGas = MIN_GAS_CALL
}
} catch (error) {
throw new Error(
`Operation failed: Max gas unspecified and auto-estimation failed. Error details: ${error.message}`
Expand Down
2 changes: 1 addition & 1 deletion packages/massa-web3/test/web3/mockData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ export const mockContractReadOperationData: Array<IContractReadOperationData> =
},
},
],
gas_cost: 1000000,
gas_cost: 12345678,
},
]

Expand Down
2 changes: 1 addition & 1 deletion packages/massa-web3/test/web3/smartContractsClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ describe('SmartContractsClient', () => {
expect(spy).toHaveBeenCalledWith(
expect.objectContaining({
...mockCallDataWithoutMaxGas,
maxGas: BigInt(1_000_000),
maxGas: BigInt(mockContractReadOperationData[0].gas_cost),
})
)
spy.mockRestore()
Expand Down
1 change: 1 addition & 0 deletions packages/web3-utils/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const BASE_ACCOUNT_CREATION_COST = fromMAS(0.001)
export const MAX_GAS_EXECUTE_SC = 3_980_167_295n
export const MAX_GAS_DEPLOYMENT = MAX_GAS_EXECUTE_SC
export const MAX_GAS_CALL = 4_294_167_295n
export const MIN_GAS_CALL = 2100000n

/* -------------------------------------------------------------------------- */
/* NETWORK */
Expand Down

0 comments on commit c59cdf1

Please sign in to comment.