Skip to content

Commit

Permalink
Tests: add inline snapshots for gas estimation & costs for Randomness (
Browse files Browse the repository at this point in the history
…#2942)

* add inline snapshots for gas estimation & costs

* fix wrong test file overwrite

* update values & use estimation

* prettier
  • Loading branch information
pLabarta authored Sep 30, 2024
1 parent bad0570 commit 9854dad
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import "@moonbeam-network/api-augment/moonbase";
import { beforeAll, describeSuite, expect, fetchCompiledContract } from "@moonwall/cli";
import {
BALTATHAR_ADDRESS,
BALTATHAR_PRIVATE_KEY,
CONTRACT_RANDOMNESS_STATUS_DOES_NOT_EXISTS,
DEFAULT_GENESIS_BALANCE,
Expand Down Expand Up @@ -34,21 +35,36 @@ describeSuite({
gas: 500_000n,
value: 1n * GLMR,
});

await context.createBlock();

await context.createBlock([fakeBabeResultTransaction(context)]);

const estimatedGas = await context.viem().estimateContractGas({
address: "0x0000000000000000000000000000000000000809",
abi: fetchCompiledContract("Randomness").abi,
functionName: "fulfillRequest",
args: [0],
account: BALTATHAR_ADDRESS,
});
expect(estimatedGas).toMatchInlineSnapshot(`687763n`);

const rawTxn = await context.writePrecompile!({
precompileName: "Randomness",
functionName: "fulfillRequest",
args: [0],
gas: estimatedGas,
rawTxOnly: true,
gas: 700_000n, // TODO: estimate gas and snapshot the estimation
privateKey: BALTATHAR_PRIVATE_KEY,
});

// We fake the results twice, once to estimate the gas
// and once to actually fulfill the request
const { result } = await context.createBlock([fakeBabeResultTransaction(context), rawTxn]);
fulFillReceipt = await context
.viem()
.getTransactionReceipt({ hash: result![1].hash as `0x${string}` });
expect(fulFillReceipt.gasUsed).toMatchInlineSnapshot(`280576n`);
});

it({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,30 @@ describeSuite({
await context.createBlock();
await context.createBlock();
await context.createBlock();

const estimatedGas = await context.viem().estimateContractGas({
address: "0x0000000000000000000000000000000000000809",
abi: fetchCompiledContract("Randomness").abi,
functionName: "fulfillRequest",
args: [0],
});

expect(estimatedGas).toMatchInlineSnapshot(`677344n`);

const rawTxn = await context.writePrecompile!({
precompileName: "Randomness",
functionName: "fulfillRequest",
args: [0],
gas: 700_000n, // TODO: estimate gas and snapshot the estimation
gas: estimatedGas,
rawTxOnly: true,
});
const { result } = await context.createBlock(rawTxn);

fulFillReceipt = await context
.viem()
.getTransactionReceipt({ hash: result!.hash as `0x${string}` });

expect(fulFillReceipt.gasUsed).toMatchInlineSnapshot(`280576n`);
});
it({
id: "T01",
Expand Down

0 comments on commit 9854dad

Please sign in to comment.