-
Notifications
You must be signed in to change notification settings - Fork 10
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
chore: src-01 and sro-01 #1745
Closed
Closed
chore: src-01 and sro-01 #1745
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,39 @@ | ||
import { type FixedPointNumber } from '../FixedPointNumber'; | ||
import { type Txt } from '../Txt'; | ||
import { Coin } from './Coin'; | ||
|
||
/** | ||
* Represents a fungible token monetary amount. | ||
* | ||
* @extends Coin | ||
*/ | ||
class FungibleToken extends Coin { | ||
/** | ||
* Wei default decimals. | ||
*/ | ||
private static readonly WEI_DEFAULT = 18n; | ||
|
||
/** | ||
* Wei number of fractional digits to express the token with. | ||
*/ | ||
private readonly _decimals: bigint; | ||
|
||
/** | ||
* Represents this monetary amount in terms of {@link Units.wei}. | ||
* | ||
* @type {bigint} | ||
*/ | ||
public readonly wei: bigint; | ||
|
||
protected constructor( | ||
symbol: Txt, | ||
value: FixedPointNumber, | ||
decimals: bigint = FungibleToken.WEI_DEFAULT | ||
) { | ||
super(symbol, value); | ||
this._decimals = decimals; | ||
this.wei = value.dp(this._decimals).scaledValue; | ||
} | ||
} | ||
|
||
export { FungibleToken }; |
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
47 changes: 47 additions & 0 deletions
47
packages/core/tests/vcdm/currency/FungibleToken.unit.test.ts
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,47 @@ | ||
import { expect } from '@jest/globals'; | ||
import { FixedPointNumber, Txt } from '../../../src'; | ||
import { FungibleToken } from '../../../src/vcdm/currency/FungibleToken'; | ||
|
||
/** | ||
* Test FungibleToken class. | ||
* @group unit/vcdm | ||
*/ | ||
|
||
const tokenValue = '123456789.012345678'; | ||
const tokenDecimals = 20n; // 18 decimals is tested via VTHO | ||
|
||
class TestToken extends FungibleToken { | ||
constructor(value: FixedPointNumber) { | ||
super(Txt.of('TEST'), value, tokenDecimals); | ||
} | ||
} | ||
|
||
class DefaultDecimalsToken extends FungibleToken { | ||
constructor(value: FixedPointNumber) { | ||
super(Txt.of('DEFAULT'), value); | ||
} | ||
} | ||
|
||
describe('FungibleToken tests', () => { | ||
test('toString method', () => { | ||
const token = new TestToken(FixedPointNumber.of(tokenValue)); | ||
const expected = `${Txt.of(tokenValue)} ${token.code}`; | ||
const actual = token.toString(); | ||
expect(actual).toEqual(expected); | ||
}); | ||
|
||
test('Wei value', () => { | ||
const expectedWei = | ||
FixedPointNumber.of(tokenValue).dp(tokenDecimals).scaledValue; | ||
const token = new TestToken(FixedPointNumber.of(tokenValue)); | ||
const actualWei = token.wei; | ||
expect(actualWei).toEqual(expectedWei); | ||
}); | ||
|
||
test('Default decimals', () => { | ||
const expectedWei = FixedPointNumber.of(tokenValue).dp(18n).scaledValue; | ||
const token = new DefaultDecimalsToken(FixedPointNumber.of(tokenValue)); | ||
const actualWei = token.wei; | ||
expect(actualWei).toEqual(expectedWei); | ||
}); | ||
}); |
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
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
💡 Codebase verification
The review comment is valid and the suggested fix is necessary
The Units enum defines valid values from 0 to 18, but there's no runtime validation to prevent invalid values that could cause the exponent calculation to fail. The suggested range check aligns with the codebase's unit system and prevents potential runtime errors.
of
method inpackages/core/src/vcdm/currency/VTHO.ts
packages/core/tests/vcdm/currency/VTHO.unit.test.ts
to verify the validation🔗 Analysis chain
Validate
unit
range for theof
method.When choosing a
Units
value that is significantly larger thanVTHO_DECIMALS
,(VTHO.VTHO_DECIMALS - BigInt(unit))
could become negative, potentially risking incorrect or unexpected scaling. Add a check to ensureunit
remains in a valid range.Also applies to: 11-11, 13-13, 26-26, 34-34, 56-58
🏁 Scripts executed
The following scripts were executed for the analysis:
Error: No shell scripts found
Script:
Length of output: 2516
Script:
Length of output: 7040