-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add contract read, write, mint benches
- Loading branch information
Showing
4 changed files
with
115 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
7 changes: 7 additions & 0 deletions
7
internal/benchmarks/test/fixtures/forc-projects/counter-contract/Forc.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
37 changes: 37 additions & 0 deletions
37
internal/benchmarks/test/fixtures/forc-projects/counter-contract/src/main.sw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |