Skip to content

Commit

Permalink
Merge pull request #2 from pendulum-chain/1-allow-tolerance-parameter…
Browse files Browse the repository at this point in the history
…-for-estimated-gas-fees

Add gas tolerance parameter
  • Loading branch information
TorstenStueber authored Mar 11, 2024
2 parents b49d1b9 + 71221ce commit fdf4a28
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pendulum-chain/api-solang",
"version": "0.1.1",
"version": "0.2.0",
"description": "Interface to interact with smart contracts compiled via Solang",
"main": "build/esm/index.js",
"devDependencies": {
Expand Down
13 changes: 11 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export interface MessageCallOptions {
limits: Limits;
modifyExtrinsic?: (extrinsic: Extrinsic) => Extrinsic;
lookupAbi?: (contractAddress: Address) => Abi | undefined;
gasLimitTolerancePercentage?: number;
}

export type MessageCallResult = {
Expand Down Expand Up @@ -172,10 +173,11 @@ export async function messageCall({
getSigner,
modifyExtrinsic,
lookupAbi,
gasLimitTolerancePercentage = 10,
}: MessageCallOptions): Promise<MessageCallResult> {
const contract = new ContractPromise(api, abi, contractDeploymentAddress);

const { gasRequired, output } = await rpcCall({
let { gasRequired, output } = await rpcCall({
api,
abi,
contractAddress: contractDeploymentAddress,
Expand All @@ -202,7 +204,12 @@ export async function messageCall({
return { execution: { type: "onlyQuery" }, result: output };
}

const signer = await getSigner();
if (gasLimitTolerancePercentage > 0) {
gasRequired = api.createType("WeightV2", {
refTime: (gasRequired.refTime.toBigInt() * (100n + BigInt(gasLimitTolerancePercentage))) / 100n,
proofSize: (gasRequired.proofSize.toBigInt() * (100n + BigInt(gasLimitTolerancePercentage))) / 100n,
});
}

const typesAddress = api.registry.createType("AccountId", contractDeploymentAddress);
let extrinsic = api.tx.contracts.call(
Expand All @@ -216,6 +223,8 @@ export async function messageCall({
if (modifyExtrinsic) {
extrinsic = modifyExtrinsic(extrinsic);
}

const signer = await getSigner();
const { events, status, transactionFee } = await submitExtrinsic(extrinsic, signer);

return {
Expand Down

0 comments on commit fdf4a28

Please sign in to comment.