-
Notifications
You must be signed in to change notification settings - Fork 71
Add Support for Scaled UI Amount Extension to JS Library #427
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
base: main
Are you sure you want to change the base?
Conversation
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.
Looks great overall! Just some nits on the tests
test('should return the correct UiAmount with scale factor of 3', async (t) => { | ||
const rpc = getMockRpc({ | ||
[CLOCK]: createMockClockAccountInfo(ONE_YEAR_IN_SECONDS * 2), | ||
[mint]: createMockMintAccountInfo(10, true, { multiplier: 3 }), | ||
}); | ||
|
||
const result = await amountToUiAmountForMintWithoutSimulation( | ||
rpc, | ||
mint, | ||
BigInt(10000000000) | ||
); | ||
t.is(result, '3'); | ||
}); |
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.
nit: it's fine to keep this, but it seems a bit redundant given the previous test is almost exactly the same thing, but more complete
test('should return the correct amount with scale factor of 2', async (t) => { | ||
const rpc = getMockRpc({ | ||
[CLOCK]: createMockClockAccountInfo(ONE_YEAR_IN_SECONDS * 2), | ||
[mint]: createMockMintAccountInfo(0, true, { multiplier: 2 }), | ||
}); | ||
|
||
const result = await uiAmountToAmountForMintWithoutSimulation(rpc, mint, '2'); | ||
t.is(result, 1n); | ||
}); | ||
|
||
test('should return the correct amount with scale factor of 3', async (t) => { | ||
const rpc = getMockRpc({ | ||
[CLOCK]: createMockClockAccountInfo(ONE_YEAR_IN_SECONDS * 2), | ||
[mint]: createMockMintAccountInfo(10, true, { multiplier: 3 }), | ||
}); | ||
|
||
const result = await uiAmountToAmountForMintWithoutSimulation(rpc, mint, '3'); | ||
t.is(result, 10000000000n); | ||
}); |
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.
nit: I liked how the other test cases were grouped, so can we have the same for these two?
test('should handle decimal multiplier of 0.5', async (t) => { | ||
const rpc = getMockRpc({ | ||
[CLOCK]: createMockClockAccountInfo(ONE_YEAR_IN_SECONDS * 2), | ||
[mint]: createMockMintAccountInfo(2, true, { multiplier: 0.5 }), | ||
}); | ||
|
||
const result = await amountToUiAmountForMintWithoutSimulation( | ||
rpc, | ||
mint, | ||
BigInt(100) | ||
); | ||
t.is(result, '0.5'); | ||
}); | ||
|
||
test('should handle decimal multiplier of 1.5', async (t) => { | ||
const rpc = getMockRpc({ | ||
[CLOCK]: createMockClockAccountInfo(ONE_YEAR_IN_SECONDS * 2), | ||
[mint]: createMockMintAccountInfo(2, true, { multiplier: 1.5 }), | ||
}); | ||
|
||
const result = await amountToUiAmountForMintWithoutSimulation( | ||
rpc, | ||
mint, | ||
BigInt(100) | ||
); | ||
t.is(result, '1.5'); | ||
}); | ||
|
||
test('should handle decimal multiplier of 0.001', async (t) => { | ||
const rpc = getMockRpc({ | ||
[CLOCK]: createMockClockAccountInfo(ONE_YEAR_IN_SECONDS * 2), | ||
[mint]: createMockMintAccountInfo(3, true, { multiplier: 0.001 }), | ||
}); | ||
|
||
const result = await amountToUiAmountForMintWithoutSimulation( | ||
rpc, | ||
mint, | ||
BigInt(1000) | ||
); | ||
t.is(result, '0.001'); | ||
}); |
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.
These can probably be grouped too
test('should convert UI amount with decimal multiplier of 0.5', async (t) => { | ||
const rpc = getMockRpc({ | ||
[CLOCK]: createMockClockAccountInfo(ONE_YEAR_IN_SECONDS * 2), | ||
[mint]: createMockMintAccountInfo(2, true, { multiplier: 0.5 }), | ||
}); | ||
|
||
const result = await uiAmountToAmountForMintWithoutSimulation(rpc, mint, '1'); | ||
t.is(result, 200n); // 1 * 100(for 2 decimals) / 0.5 | ||
}); | ||
|
||
test('should convert UI amount with decimal multiplier of 1.5', async (t) => { | ||
const rpc = getMockRpc({ | ||
[CLOCK]: createMockClockAccountInfo(ONE_YEAR_IN_SECONDS * 2), | ||
[mint]: createMockMintAccountInfo(2, true, { multiplier: 1.5 }), | ||
}); | ||
|
||
const result = await uiAmountToAmountForMintWithoutSimulation(rpc, mint, '3'); | ||
t.is(result, 200n); // 3 * 100(for 2 decimals) / 1.5 | ||
}); |
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.
And these too
test('should handle multiplier changes from decimal to decimal', async (t) => { | ||
const rpc = getMockRpc({ | ||
[CLOCK]: createMockClockAccountInfo(ONE_YEAR_IN_SECONDS * 2), | ||
[mint]: createMockMintAccountInfo(2, true, { | ||
multiplier: 0.5, | ||
newMultiplier: 1.5, | ||
newMultiplierEffectiveTimestamp: ONE_YEAR_IN_SECONDS, | ||
}), | ||
}); | ||
|
||
const result = await amountToUiAmountForMintWithoutSimulation( | ||
rpc, | ||
mint, | ||
BigInt(100) | ||
); | ||
t.is(result, '1.5'); | ||
}); | ||
|
||
test('should handle multiplier changes from decimal to decimal for UI to amount conversion', async (t) => { | ||
const rpc = getMockRpc({ | ||
[CLOCK]: createMockClockAccountInfo(ONE_YEAR_IN_SECONDS * 2), | ||
[mint]: createMockMintAccountInfo(2, true, { | ||
multiplier: 0.5, | ||
newMultiplier: 1.5, | ||
newMultiplierEffectiveTimestamp: ONE_YEAR_IN_SECONDS, | ||
}), | ||
}); | ||
|
||
const result = await uiAmountToAmountForMintWithoutSimulation( | ||
rpc, | ||
mint, | ||
'1.5' | ||
); | ||
t.is(result, 100n); // 1.5 * 100(for 2 decimals) / 1.5 | ||
}); |
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.
nit: not sure if these are needed given the other ones
Add Support for Scaled UI Amount Extension to JS Library
This PR adds support for the Scaled UI Amount extension to the js ibrary, enabling conversion between raw token amounts and UI representation based on a configurable multiplier.
Changes
Client Library
amountToUiAmountForScaledUiAmountMintWithoutSimulation
function to convert raw amounts to UI amounts using a configurable multiplieruiAmountToAmountForScaledUiAmountMintWithoutSimulation
function for reverse conversionTests