Skip to content

Commit

Permalink
feat: add contract read, write, mint benches
Browse files Browse the repository at this point in the history
  • Loading branch information
maschad committed Sep 10, 2024
1 parent 61807c4 commit c4bbf94
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 1 deletion.
70 changes: 70 additions & 0 deletions internal/benchmarks/src/contract-interaction.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* eslint-disable import/no-extraneous-dependencies */

import type { WalletUnlocked } from 'fuels';
import { bn } from 'fuels';
import { launchTestNode, TestAssetId } from 'fuels/test-utils';
import { bench } from 'vitest';

import type { CounterContract, CallTestContract } from '../test/typegen/contracts';
import { CounterContractFactory, CallTestContractFactory } from '../test/typegen/contracts';
/**
* @group node
* @group browser
*/
describe('Contract Interaction Benchmarks', () => {
let contract: CounterContract;
let callTestContract: CallTestContract;
let wallet: WalletUnlocked;
let cleanup: () => void;
beforeEach(async () => {
const launched = await launchTestNode({
contractsConfigs: [{ factory: CounterContractFactory }, { factory: CallTestContractFactory }],
});

cleanup = launched.cleanup;
contract = launched.contracts[0];
callTestContract = launched.contracts[1];
wallet = launched.wallets[0];
});

afterEach(() => {
cleanup();
});

bench('should successfully execute a contract read function', async () => {
const tx = await contract.functions.get_count().call();

const { value } = await tx.waitForResult();

expect(JSON.stringify(value)).toEqual(JSON.stringify(bn(0)));
});

bench('should successfully execute a contract multi call', async () => {
const tx = await contract
.multiCall([contract.functions.increment_counter(100), contract.functions.get_count()])
.call();

const { value } = await tx.waitForResult();

expect(JSON.stringify(value)).toEqual(JSON.stringify([bn(100), bn(100)]));
});

bench('should successfully write to a contract', async () => {
const tx = await contract.functions.increment_counter(100).call();
await tx.waitForResult();
});

bench('should successfully execute a contract mint', async () => {
const tx = await callTestContract.functions.mint_coins(TestAssetId.A.value, bn(100)).call();

await tx.waitForResult();
});

bench('should successfully execute a contract deploy', async () => {
const factory = new CounterContractFactory(wallet);
const { waitForResult } = await factory.deploy();
const { contract: deployedContract } = await waitForResult();

expect(deployedContract).toBeDefined();
});
});
2 changes: 1 addition & 1 deletion internal/benchmarks/test/fixtures/forc-projects/Forc.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[workspace]
members = ["call-test-contract"]
members = ["call-test-contract", "counter-contract"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
name = "counter-contract"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
contract;

abi Counter {
#[storage(read)]
fn get_count() -> u64;

#[storage(write, read)]
fn increment_counter(amount: u64) -> u64;

#[storage(read, write)]
fn decrement_counter(amount: u64) -> u64;
}

storage {
counter: u64 = 0,
}

impl Counter for Contract {
#[storage(read)]
fn get_count() -> u64 {
storage.counter.read()
}

#[storage(write, read)]
fn increment_counter(amount: u64) -> u64 {
let current = storage.counter.read();
storage.counter.write(current + amount);
storage.counter.read()
}

#[storage(read, write)]
fn decrement_counter(amount: u64) -> u64 {
let current = storage.counter.read();
storage.counter.write(current - amount);
storage.counter.read()
}
}

0 comments on commit c4bbf94

Please sign in to comment.