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

feat(account): Add TestCoin helper class for testing purposes #3647

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

crStiv
Copy link

@crStiv crStiv commented Jan 30, 2025

Overview

This PR introduces the TestCoin helper class to simplify coin creation during testing. The class provides utility methods for generating test coins with random or specified parameters, making it easier to write and maintain tests that require coin instances.

Features

  • TestCoin.create(): Creates a single test coin with customizable parameters
  • TestCoin.many(): Generates multiple test coins with shared base parameters
  • Uses crypto-secure random generation via @fuel-ts/crypto
  • Properly handles big numbers using @fuel-ts/math

Related Issues

Closes #3445

Related PR's

Fixed #3460

Copy link

vercel bot commented Jan 30, 2025

@crStiv is attempting to deploy a commit to the Fuel Labs Team on Vercel.

A member of the Team first needs to authorize it.

Copy link

vercel bot commented Jan 30, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
ts-docs-api ❌ Failed (Inspect) Feb 4, 2025 9:33pm

Copy link
Contributor

@petertonysmith94 petertonysmith94 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @crStiv 👋🏼

Thanks for the review, could you add a test around this behaviour?

See test-message.test.ts for some inspiration.

@crStiv
Copy link
Author

crStiv commented Feb 1, 2025

@petertonysmith94 Added tests for TestCoin class as requested. The tests cover default values, custom parameters, multiple coin creation and parameter inheritance

Copy link
Member

@danielbate danielbate left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please could you also include a test case that uses TestCoin in an end to end test using a transaction? packages/account/src/providers/provider.test.ts may be the best place for this, or inside fuel-gauge.

packages/account/src/test-utils/test-coin.ts Show resolved Hide resolved
@crStiv
Copy link
Author

crStiv commented Feb 4, 2025

@danielbate I've included a test case you wanted me to do inside fuel gauge, does it seem alright?

import { describe, expect, test } from 'vitest';
import { type Coin } from '@fuel-ts/account';
import { bn, type BN } from '@fuel-ts/math';
import { getRandomB256 } from '@fuel-ts/crypto';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import { getRandomB256 } from '@fuel-ts/crypto';
import { getRandomB256 } from '@fuel-ts/address';

@@ -0,0 +1,51 @@
import { Coin } from '@fuel-ts/account';
import { bn, type BN } from '@fuel-ts/math';
import { getRandomB256 } from '@fuel-ts/crypto';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import { getRandomB256 } from '@fuel-ts/crypto';
import { getRandomB256 } from '@fuel-ts/address';

Comment on lines +12 to +17
export class TestCoin {
public readonly id: string;
public readonly owner: string;
public readonly amount: BN;
public readonly type: number;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The TestCoin should follow the same approach as the existent TestMessage:

export class TestMessage {
public readonly sender: Address;
public readonly recipient: Address;
public readonly nonce: string;
public readonly amount: number | BN;
public readonly data: string;
public readonly da_height: number;

It is intended to be used when setting up a local node, just like the TestMessage is used:

const { provider } = await launchTestNode({
nodeOptions: {
snapshotConfig: {
stateConfig: {
messages: [
new TestMessage({
nonce:
'0x381de90750098776c71544527fd253412908dec3d07ce9a7367bd1ba975908a0',
}).toChainMessage(),
],
},
},
},
});

Therefore, its properties should be the same ones as the Coin type for the stateConfig:

id: this.id,
owner: this.owner,
amount: this.amount,
type: this.type,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The property type does not exist on the Coin type

Comment on lines +323 to +334
using launched = await launchTestNode();
const { provider, wallets: [wallet] } = launched;

const baseAssetId = await provider.getBaseAssetId();
const initialBalance = await wallet.getBalance(baseAssetId);

// Create test coins with specific parameters
const testCoin = new TestCoin({
amount: bn(1000),
owner: wallet.address.toB256(),
assetId: baseAssetId,
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this test working? The transaction is being submitted with a coin not created on the local chain.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add TestCoin helper class
4 participants