-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
base: master
Are you sure you want to change the base?
Conversation
@crStiv is attempting to deploy a commit to the Fuel Labs Team on Vercel. A member of the Team first needs to authorize it. |
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
There was a problem hiding this 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.
@petertonysmith94 Added tests for TestCoin class as requested. The tests cover default values, custom parameters, multiple coin creation and parameter inheritance |
There was a problem hiding this 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
.
@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'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import { getRandomB256 } from '@fuel-ts/crypto'; | |
import { getRandomB256 } from '@fuel-ts/address'; | |
export class TestCoin { | ||
public readonly id: string; | ||
public readonly owner: string; | ||
public readonly amount: BN; | ||
public readonly type: number; | ||
|
There was a problem hiding this comment.
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
:
fuels-ts/packages/account/src/test-utils/test-message.ts
Lines 18 to 24 in 01375ce
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:
fuels-ts/apps/docs/src/guide/provider/snippets/functionality/get-messages-by-nonce.ts
Lines 4 to 17 in 01375ce
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, |
There was a problem hiding this comment.
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
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, | ||
}); |
There was a problem hiding this comment.
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.
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 parametersTestCoin.many()
: Generates multiple test coins with shared base parameters@fuel-ts/crypto
@fuel-ts/math
Related Issues
Closes #3445
Related PR's
Fixed #3460