Skip to content

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

gitteri
Copy link
Contributor

@gitteri gitteri commented May 8, 2025

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

  • Added amountToUiAmountForScaledUiAmountMintWithoutSimulation function to convert raw amounts to UI amounts using a configurable multiplier
  • Added uiAmountToAmountForScaledUiAmountMintWithoutSimulation function for reverse conversion
  • Updated main utility functions to detect and handle mints with the Scaled UI Amount extension
  • Improved code organization with clear section headers and extracted common helper functions
  • Enhanced documentation throughout the codebase

Tests

  • Added comprehensive test suite for the Scaled UI Amount extension functionality
  • Tests cover basic integer multipliers (2x, 3x) and decimal multipliers (0.5x, 1.5x, 0.001x)
  • Added tests for time-based multiplier transitions (current vs. future multipliers)
  • Verified compatibility with large token amounts
  • Ensured consistent behavior with decimal places and edge cases

Copy link
Contributor

@joncinque joncinque left a 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

Comment on lines 173 to 185
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');
});
Copy link
Contributor

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

Comment on lines 187 to 205
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);
});
Copy link
Contributor

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?

Comment on lines 208 to 248
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');
});
Copy link
Contributor

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

Comment on lines 250 to 268
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
});
Copy link
Contributor

Choose a reason for hiding this comment

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

And these too

Comment on lines 339 to 373
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
});
Copy link
Contributor

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

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

Successfully merging this pull request may close these issues.

2 participants