Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gas tolerance parameter #2

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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