Skip to content

Commit

Permalink
contract call dry run
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Mar 27, 2023
1 parent fc966bc commit 0f6639e
Showing 1 changed file with 34 additions and 10 deletions.
44 changes: 34 additions & 10 deletions packages/page-contracts/src/Contracts/Call.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,41 @@ function Call ({ className = '', contract, messageIndex, onCallResult, onChangeM
}, [contract, messageIndex]);

useEffect((): void => {
value && message.isMutating && setExecTx((): SubmittableExtrinsic<'promise'> | null => {
try {
return contract.tx[message.method](
{ gasLimit: weight.isWeightV2 ? weight.weightV2 : weight.weight, storageDepositLimit: null, value: message.isPayable ? value : 0 },
...params
);
} catch (error) {
return null;
async function dryRun () {
if (accountId && value && message.isMutating) {
const dryRunParams: Parameters<typeof api.call.contractsApi.call> =
[
accountId,
contract.address,
message.isPayable
? api.registry.createType('Balance', value)
: api.registry.createType('Balance', BN_ZERO),
weight.weightV2,
null,
message.toU8a(params)
];

const dryRunResult = await api.call.contractsApi.call(...dryRunParams);

setExecTx((): SubmittableExtrinsic<'promise'> | null => {
try {
return contract.tx[message.method](
{
gasLimit: dryRunResult.gasRequired,
storageDepositLimit: dryRunResult.storageDeposit.isCharge ? dryRunResult.storageDeposit.asCharge : null,
value: message.isPayable ? value : 0
},
...params
);
} catch (error) {
return null;
}
});
}
});
}, [accountId, contract, message, value, weight, params]);
}

dryRun().catch((e) => console.error(e));
}, [api, accountId, contract, message, value, weight, params]);

useEffect((): void => {
if (!accountId || !message || !dbParams || !dbValue) {
Expand Down

0 comments on commit 0f6639e

Please sign in to comment.