@indigo-labs/indigo-sdk
Version:
Indigo SDK for interacting with Indigo endpoints via lucid-evolution
58 lines (52 loc) • 1.59 kB
text/typescript
import { ONE_YEAR } from '../../utils/time-helpers';
import { OCD_DECIMAL_UNIT } from '../../types/on-chain-decimal';
import { InterestOracleDatum } from './types-new';
const unitaryInterestPrecision = 1_000_000_000_000_000_000n;
export function calculateUnitaryInterest(
timePeriod: bigint,
interestRate: bigint,
): bigint {
return (
(timePeriod * interestRate * unitaryInterestPrecision) /
ONE_YEAR /
OCD_DECIMAL_UNIT
);
}
export function calculateUnitaryInterestSinceOracleLastUpdated(
now: bigint,
oracleDatum: InterestOracleDatum,
): bigint {
return calculateUnitaryInterest(
now - oracleDatum.lastUpdated,
oracleDatum.interestRate.getOnChainInt,
);
}
export function calculateAccruedInterest(
now: bigint,
unitaryInterestSnapshot: bigint,
mintedAmount: bigint,
interestLastSettled: bigint,
interestOracleDatum: InterestOracleDatum,
): bigint {
if (interestOracleDatum.unitaryInterest >= unitaryInterestSnapshot) {
const interestFromPreviousRates =
((interestOracleDatum.unitaryInterest - unitaryInterestSnapshot) *
mintedAmount) /
unitaryInterestPrecision;
const lastRateInterest =
((now - interestOracleDatum.lastUpdated) *
interestOracleDatum.interestRate.getOnChainInt *
mintedAmount) /
ONE_YEAR /
OCD_DECIMAL_UNIT;
return interestFromPreviousRates + lastRateInterest;
} else {
return (
((now - interestLastSettled) *
interestOracleDatum.interestRate.getOnChainInt *
mintedAmount) /
ONE_YEAR /
OCD_DECIMAL_UNIT
);
}
}