-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
37 additions
and
11 deletions.
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 |
---|---|---|
@@ -1,25 +1,51 @@ | ||
import { Coin } from '@fuel-ts/account'; | ||
import { bn } from '@fuel-ts/math'; | ||
import { bn, type BN } from '@fuel-ts/math'; | ||
import { getRandomB256 } from '@fuel-ts/crypto'; | ||
|
||
interface TestCoinSpecs { | ||
id: string; | ||
owner: string; | ||
amount: BN; | ||
type: number; | ||
} | ||
|
||
export class TestCoin { | ||
public readonly id: string; | ||
public readonly owner: string; | ||
public readonly amount: BN; | ||
public readonly type: number; | ||
|
||
/** | ||
* A helper class to create coins for testing purposes. | ||
*/ | ||
constructor({ | ||
id = getRandomB256(), | ||
owner = getRandomB256(), | ||
amount = bn(1000000), | ||
type = 0, | ||
}: Partial<TestCoinSpecs> = {}) { | ||
this.id = id; | ||
this.owner = owner; | ||
this.amount = amount; | ||
this.type = type; | ||
} | ||
|
||
/** | ||
* Creates a single test coin with given parameters | ||
* Creates a chain-compatible coin object | ||
*/ | ||
static create(params: Partial<Coin> = {}): Coin { | ||
toCoin(): Coin { | ||
return { | ||
id: params.id || getRandomB256(), | ||
owner: params.owner || getRandomB256(), | ||
amount: params.amount || bn(1000000), | ||
type: params.type || 0, | ||
...params | ||
id: this.id, | ||
owner: this.owner, | ||
amount: this.amount, | ||
type: this.type, | ||
}; | ||
} | ||
|
||
/** | ||
* Generates an array of test coins with the same base parameters | ||
* Creates multiple test coins with the same base parameters | ||
*/ | ||
static many(params: Partial<Coin> = {}, count = 1): Coin[] { | ||
return Array.from({ length: count }, () => TestCoin.create(params)); | ||
static many(params: Partial<TestCoinSpecs> = {}, count = 1): Coin[] { | ||
return Array.from({ length: count }, () => new TestCoin(params).toCoin()); | ||
} | ||
} |