Skip to content

Commit

Permalink
Merge pull request #80 from tinymanorg/refactor/pool-pair-ratio
Browse files Browse the repository at this point in the history
Remove decimals from pool ratio calculation
  • Loading branch information
gulcinuras authored Feb 13, 2024
2 parents 87f509a + 75236ef commit 8100b9c
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 53 deletions.
14 changes: 12 additions & 2 deletions dist/folks-lending-pools/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,21 @@ import { FolksLendingPool } from "./types";
/**
* Calculates the amount fAsset received when adding liquidity with original asset.
*/
declare function calculateDepositReturn(depositAmount: number, depositInterestIndex: bigint, depositInterestRate: bigint, lastUpdate?: number): bigint;
declare function calculateDepositReturn({ depositAmount, depositInterestIndex, depositInterestRate, lastUpdate }: {
depositAmount: number;
depositInterestIndex: bigint;
depositInterestRate: bigint;
lastUpdate?: number;
}): bigint;
/**
* Calculates the amount original asset received when removing liquidity from fAsset pool.
*/
declare function calculateWithdrawReturn(withdrawAmount: number, depositInterestIndex: bigint, depositInterestRate: bigint, lastUpdate?: number): bigint;
declare function calculateWithdrawReturn({ withdrawAmount, depositInterestIndex, depositInterestRate, lastUpdate }: {
withdrawAmount: number;
depositInterestIndex: bigint;
depositInterestRate: bigint;
lastUpdate?: number;
}): bigint;
/**
* Fetches Folks lending pool application info from the algod, parses the global state and builds FolksLendingPool object.
*/
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions dist/util/pool/common/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ export declare function getPoolShare(totalLiquidity: bigint, ownedLiquidity: big
/**
* Calculates the pair ratio for the pool reserves
*/
export declare function getPoolPairRatio(decimals: {
asset1: undefined | number;
asset2: undefined | number;
}, reserves: null | PoolReserves): null | number;
export declare function getPoolPairRatio(reserves: null | PoolReserves): null | number;
/**
* Checks if the pool is empty
*
Expand Down
15 changes: 3 additions & 12 deletions dist/util/pool/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
export declare const poolUtils: {
getPoolShare(totalLiquidity: bigint, ownedLiquidity: bigint): number;
getPoolPairRatio(decimals: {
asset1: number | undefined;
asset2: number | undefined;
}, reserves: import("./poolTypes").PoolReserves | null): number | null;
getPoolPairRatio(reserves: import("./poolTypes").PoolReserves | null): number | null;
isPoolEmpty(poolReserves: import("./poolTypes").PoolReserves | null | undefined): boolean;
isPoolNotCreated(pool: import("./poolTypes").V1PoolInfo | import("./poolTypes").V2PoolInfo | null | undefined): boolean;
isPoolReady(pool: import("./poolTypes").V1PoolInfo | import("./poolTypes").V2PoolInfo | null | undefined): boolean;
Expand All @@ -15,10 +12,7 @@ export declare const poolUtils: {
}): Promise<[import("./poolTypes").V1PoolInfo, import("./poolTypes").V2PoolInfo]>;
v1_1: {
getPoolShare(totalLiquidity: bigint, ownedLiquidity: bigint): number;
getPoolPairRatio(decimals: {
asset1: number | undefined;
asset2: number | undefined;
}, reserves: import("./poolTypes").PoolReserves | null): number | null;
getPoolPairRatio(reserves: import("./poolTypes").PoolReserves | null): number | null;
isPoolEmpty(poolReserves: import("./poolTypes").PoolReserves | null | undefined): boolean;
isPoolNotCreated(pool: import("./poolTypes").V1PoolInfo | import("./poolTypes").V2PoolInfo | null | undefined): boolean;
isPoolReady(pool: import("./poolTypes").V1PoolInfo | import("./poolTypes").V2PoolInfo | null | undefined): boolean;
Expand All @@ -43,10 +37,7 @@ export declare const poolUtils: {
};
v2: {
getPoolShare(totalLiquidity: bigint, ownedLiquidity: bigint): number;
getPoolPairRatio(decimals: {
asset1: number | undefined;
asset2: number | undefined;
}, reserves: import("./poolTypes").PoolReserves | null): number | null;
getPoolPairRatio(reserves: import("./poolTypes").PoolReserves | null): number | null;
isPoolEmpty(poolReserves: import("./poolTypes").PoolReserves | null | undefined): boolean;
isPoolNotCreated(pool: import("./poolTypes").V1PoolInfo | import("./poolTypes").V2PoolInfo | null | undefined): boolean;
isPoolReady(pool: import("./poolTypes").V1PoolInfo | import("./poolTypes").V2PoolInfo | null | undefined): boolean;
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tinymanorg/tinyman-js-sdk",
"version": "3.1.0",
"version": "3.1.1",
"description": "Tinyman JS SDK",
"author": "Tinyman Core Team",
"license": "MIT",
Expand Down
34 changes: 22 additions & 12 deletions src/folks-lending-pools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,17 @@ function getLastTimestamp(lastUpdate?: number): number {
/**
* Calculates the amount fAsset received when adding liquidity with original asset.
*/
function calculateDepositReturn(
depositAmount: number,
depositInterestIndex: bigint,
depositInterestRate: bigint,
lastUpdate?: number
) {
function calculateDepositReturn({
depositAmount,
depositInterestIndex,
depositInterestRate,
lastUpdate
}: {
depositAmount: number;
depositInterestIndex: bigint;
depositInterestRate: bigint;
lastUpdate?: number;
}) {
const latestDepositInterestIndex = getLatestDepositInterestIndex(
depositInterestIndex,
depositInterestRate,
Expand All @@ -48,12 +53,17 @@ function calculateDepositReturn(
/**
* Calculates the amount original asset received when removing liquidity from fAsset pool.
*/
function calculateWithdrawReturn(
withdrawAmount: number,
depositInterestIndex: bigint,
depositInterestRate: bigint,
lastUpdate?: number
) {
function calculateWithdrawReturn({
withdrawAmount,
depositInterestIndex,
depositInterestRate,
lastUpdate
}: {
withdrawAmount: number;
depositInterestIndex: bigint;
depositInterestRate: bigint;
lastUpdate?: number;
}) {
const latestDepositInterestIndex = getLatestDepositInterestIndex(
depositInterestIndex,
depositInterestRate,
Expand Down
22 changes: 3 additions & 19 deletions src/util/pool/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {Algodv2} from "algosdk";

import {SupportedNetwork} from "../../commonTypes";
import {convertFromBaseUnits} from "../../util";
import {V1PoolInfo, V2PoolInfo, PoolReserves, PoolStatus} from "../poolTypes";
import {getPoolInfo as getV1_1PoolInfo} from "../v1_1";
import {getPoolInfo as getV2PoolInfo} from "../v2";
Expand All @@ -24,27 +23,12 @@ export function getPoolShare(totalLiquidity: bigint, ownedLiquidity: bigint) {
/**
* Calculates the pair ratio for the pool reserves
*/
export function getPoolPairRatio(
decimals: {
asset1: undefined | number;
asset2: undefined | number;
},
reserves: null | PoolReserves
): null | number {
export function getPoolPairRatio(reserves: null | PoolReserves): null | number {
const isEmpty = isPoolEmpty(reserves);
let pairRatio: null | number = null;

if (
reserves &&
!isEmpty &&
reserves.asset1 &&
reserves.asset2 &&
typeof decimals.asset2 === "number" &&
typeof decimals.asset1 === "number"
) {
pairRatio =
convertFromBaseUnits(decimals.asset1, reserves.asset1) /
convertFromBaseUnits(decimals.asset2, reserves.asset2);
if (reserves && !isEmpty && reserves.asset1 && reserves.asset2) {
pairRatio = Number(reserves.asset1) / Number(reserves.asset2);
}

return pairRatio;
Expand Down

0 comments on commit 8100b9c

Please sign in to comment.