Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit c16944f

Browse files
committed
address comments
1 parent 463eefb commit c16944f

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

token/js/src/actions/amountToUiAmount.ts

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,22 @@ export async function amountToUiAmount(
3838
* @param r - The interest rate in basis points.
3939
* @returns The calculated exponent.
4040
*/
41-
const calculateExponentForTimesAndRate = (t1: number, t2: number, r: number) => {
41+
function calculateExponentForTimesAndRate(t1: number, t2: number, r: number) {
4242
const ONE_IN_BASIS_POINTS = 10000;
4343
const SECONDS_PER_YEAR = 60 * 60 * 24 * 365.24;
4444
const timespan = t2 - t1;
4545
const numerator = r * timespan;
4646
const exponent = numerator / (SECONDS_PER_YEAR * ONE_IN_BASIS_POINTS);
4747
return Math.exp(exponent);
48-
};
48+
}
4949

5050
/**
5151
* Retrieves the current timestamp from the Solana clock sysvar.
5252
* @param connection - The Solana connection object.
5353
* @returns A promise that resolves to the current timestamp in seconds.
5454
* @throws An error if the sysvar clock cannot be fetched or parsed.
5555
*/
56-
const getSysvarClockTimestamp = async (connection: Connection): Promise<number> => {
56+
async function getSysvarClockTimestamp(connection: Connection): Promise<number> {
5757
const info = await connection.getParsedAccountInfo(new PublicKey('SysvarC1ock11111111111111111111111111111111'));
5858
if (!info) {
5959
throw new Error('Failed to fetch sysvar clock');
@@ -62,7 +62,7 @@ const getSysvarClockTimestamp = async (connection: Connection): Promise<number>
6262
return info.value.data.parsed.info.unixTimestamp;
6363
}
6464
throw new Error('Failed to parse sysvar clock');
65-
};
65+
}
6666

6767
/**
6868
* Convert amount to UiAmount for a mint with interest bearing extension without simulating a transaction
@@ -109,7 +109,7 @@ export function amountToUiAmountWithoutSimulation(
109109

110110
// Calculate post-update exponent
111111
// e^(currentRate * (currentTimestamp - lastUpdateTimestamp) / (SECONDS_PER_YEAR * ONE_IN_BASIS_POINTS))
112-
const postUpdateExp = calculateExponentForTimesAndRate(lastUpdateTimestamp, Number(currentTimestamp), currentRate);
112+
const postUpdateExp = calculateExponentForTimesAndRate(lastUpdateTimestamp, currentTimestamp, currentRate);
113113

114114
// Calculate total scale
115115
const totalScale = preUpdateExp * postUpdateExp;
@@ -134,7 +134,6 @@ export function amountToUiAmountWithoutSimulation(
134134
* @param connection Connection to use
135135
* @param mint Mint to use for calculations
136136
* @param amount Amount of tokens to be converted to Ui Amount
137-
* @param programId SPL Token program account (default: TOKEN_PROGRAM_ID)
138137
*
139138
* @return Ui Amount generated
140139
*/
@@ -175,29 +174,29 @@ export async function amountToUiAmountForMintWithoutSimulation(
175174
* Convert an amount with interest back to the original amount without interest
176175
* This implements the same logic as the CPI instruction available in /token/program-2022/src/extension/interest_bearing_mint/mod.rs
177176
*
178-
* @param uiAmount UI Amount (principle plus continuously compounding interest) to be converted back to original principle
177+
* @param uiAmount UI Amount (principal plus continuously compounding interest) to be converted back to original principal
179178
* @param decimals Number of decimals for the mint
180179
* @param currentTimestamp Current timestamp in seconds
181180
* @param lastUpdateTimestamp Last time the interest rate was updated in seconds
182181
* @param initializationTimestamp Time the interest bearing extension was initialized in seconds
183182
* @param preUpdateAverageRate Interest rate in basis points (hundredths of a percent) before the last update
184183
* @param currentRate Current interest rate in basis points
185184
*
186-
* In general to calculate the principle from the UI amount, the formula is:
185+
* In general to calculate the principal from the UI amount, the formula is:
187186
* P = A / (e^(r * t)) where
188-
* P = principle
187+
* P = principal
189188
* A = UI amount
190189
* r = annual interest rate (as a decimal, e.g., 5% = 0.05)
191190
* t = time in years
192191
*
193-
* In this case, we are calculating the principle by dividing the UI amount by the total scale factor which is the product of two exponential functions:
192+
* In this case, we are calculating the principal by dividing the UI amount by the total scale factor which is the product of two exponential functions:
194193
* totalScale = e^(r1 * t1) * e^(r2 * t2)
195194
* where r1 is the pre-update average rate, r2 is the current rate, t1 is the time in years between the initialization timestamp and the last update timestamp,
196195
* and t2 is the time in years between the last update timestamp and the current timestamp.
197-
* then to calculate the principle, we divide the UI amount by the total scale factor:
196+
* then to calculate the principal, we divide the UI amount by the total scale factor:
198197
* P = A / totalScale
199198
*
200-
* @return Original amount (principle) without interest
199+
* @return Original amount (principal) without interest
201200
*/
202201
export function uiAmountToAmountWithoutSimulation(
203202
uiAmount: string,
@@ -225,9 +224,9 @@ export function uiAmountToAmountWithoutSimulation(
225224
// Calculate total scale
226225
const totalScale = preUpdateExp * postUpdateExp;
227226

228-
// Calculate original principle by dividing the UI amount (principle + interest) by the total scale
229-
const originalPrinciple = uiAmountScaled / totalScale;
230-
return BigInt(Math.floor(originalPrinciple));
227+
// Calculate original principal by dividing the UI amount (principal + interest) by the total scale
228+
const originalprincipal = uiAmountScaled / totalScale;
229+
return BigInt(Math.trunc(originalprincipal));
231230
}
232231

233232
/**
@@ -236,7 +235,6 @@ export function uiAmountToAmountWithoutSimulation(
236235
* @param connection Connection to use
237236
* @param mint Mint to use for calculations
238237
* @param uiAmount UI Amount to be converted back to raw amount
239-
* @param programId SPL Token program account (default: TOKEN_PROGRAM_ID)
240238
*
241239
*
242240
* @return Raw amount
@@ -252,12 +250,11 @@ export async function uiAmountToAmountForMintWithoutSimulation(
252250
throw new Error('Invalid program ID');
253251
}
254252

255-
const mintInfo = await getMint(connection, mint, 'confirmed', programId);
256-
253+
const mintInfo = unpackMint(mint, accountInfo, programId);
257254
const interestBearingMintConfigState = getInterestBearingMintConfigState(mintInfo);
258255
if (!interestBearingMintConfigState) {
259256
const uiAmountScaled = parseFloat(uiAmount) * Math.pow(10, mintInfo.decimals);
260-
return BigInt(Math.floor(uiAmountScaled));
257+
return BigInt(Math.trunc(uiAmountScaled));
261258
}
262259

263260
const timestamp = await getSysvarClockTimestamp(connection);

0 commit comments

Comments
 (0)