Skip to content

Commit b1a5b31

Browse files
authoredAug 25, 2023
Merge pull request #136 from temptemp3/main
2 parents 09bc632 + 7b8ea7d commit b1a5b31

File tree

3 files changed

+80
-5
lines changed

3 files changed

+80
-5
lines changed
 

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@reach-sh/humble-sdk",
3-
"version": "2.1.2",
3+
"version": "2.1.3",
44
"description": "A Javascript library for interacting with the HumbleSwap DEx",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { createReachAPI, initHumbleSDK } from "../index";
2+
import {
3+
safeMintedLiquidityTokens,
4+
unsafeMintedLiquidityTokens,
5+
} from "./PoolAnnouncer";
6+
7+
initHumbleSDK();
8+
9+
const mintedLiquidityTokens = "100000000000000000000000000000";
10+
11+
const Info = jest.fn().mockImplementation(() => {
12+
const bn = createReachAPI().bigNumberify;
13+
return {
14+
liquidityToken: bn(1),
15+
lptBals: {
16+
A: bn(0),
17+
B: bn(mintedLiquidityTokens),
18+
},
19+
poolBals: {
20+
A: bn(0),
21+
B: bn(0),
22+
},
23+
protoBals: {
24+
A: bn(0),
25+
B: bn(0),
26+
},
27+
protoInfo: {
28+
locked: false,
29+
lpFee: 2,
30+
protoAddr: "",
31+
protoFee: 3,
32+
totFee: 5,
33+
},
34+
tokA: bn(1),
35+
tokB: bn(2),
36+
};
37+
});
38+
39+
describe.only("Fetch Liquidity Pool", () => {
40+
it("Converts minted liquidity to big int as string", async () => {
41+
expect(safeMintedLiquidityTokens(createReachAPI())(Info().lptBals)).toBe(
42+
mintedLiquidityTokens
43+
);
44+
});
45+
});
46+
47+
describe.only("Fetch Liquidity Pool", () => {
48+
it("Unable to convert minted liquidity tokens to number", async () => {
49+
expect(() => {
50+
try {
51+
unsafeMintedLiquidityTokens(createReachAPI())(Info().lptBals);
52+
} catch (e) {
53+
throw new Error(e);
54+
}
55+
}).toThrow("NUMERIC_FAULT")
56+
});
57+
});

‎src/participants/PoolAnnouncer.ts

+22-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {
66
formatCurrency,
77
formatAddress,
88
ReachToken,
9-
peraTokenMetadata
9+
peraTokenMetadata,
10+
ReachStdLib
1011
} from "../reach-helpers";
1112
import {
1213
FetchPoolData,
@@ -34,6 +35,22 @@ export type FetchPoolOpts = PoolFetchOpts & {
3435
/** ALIAS | Fetch data about a pool */
3536
export const fetchPool = fetchLiquidityPool;
3637

38+
/**
39+
* Get minted liquidity tokens (unsafe)
40+
* @param stdlib Reach stdlib
41+
* @param lptBals Liquidity token balances
42+
* @returns Minted liquidity tokens
43+
*/
44+
export const unsafeMintedLiquidityTokens = (stdlib: ReachStdLib) => (lptBals: {A: any; B: any}) => stdlib.bigNumberToNumber(lptBals.B)
45+
46+
/**
47+
* Get minted liquidity tokens
48+
* @param stdlib Reach stdlib
49+
* @param lptBals Liquidity token balances
50+
* @returns Minted liquidity tokens
51+
*/
52+
export const safeMintedLiquidityTokens = (stdlib: ReachStdLib) => (lptBals: {A: any; B: any}) => stdlib.bigNumberToBigInt(lptBals.B).toString()
53+
3754
/**
3855
* Fetch data about a pool
3956
* @param acc Reach Account instance
@@ -46,7 +63,8 @@ export async function fetchLiquidityPool(
4663
opts: FetchPoolOpts
4764
): Promise<TransactionResult<FetchPoolData>> {
4865
const reach = createReachAPI();
49-
const big = reach.bigNumberify;
66+
const bn = reach.bigNumberify;
67+
const getMintedLiquidityTokens = safeMintedLiquidityTokens(reach);
5068
const {
5169
poolAddress = "",
5270
n2nn = false,
@@ -105,15 +123,15 @@ export async function fetchLiquidityPool(
105123
onProgress(`Calculating fees ...`);
106124
const FEE_INFO = getFeeInfo();
107125
const totalFees = (protocolBal: number) =>
108-
big(FEE_INFO.totFee).div(big(FEE_INFO.protoFee)).mul(protocolBal);
126+
bn(FEE_INFO.totFee).div(bn(FEE_INFO.protoFee)).mul(protocolBal);
109127

110128
// subtract fees from token balances
111129
const { A: aBal, B: bBal } = poolBals;
112130
const { A: pABal, B: pBBal } = protoBals;
113131
const pool: PoolDetails = {
114132
poolAddress: ctcInfo,
115133
poolTokenId: parseAddress(liquidityToken),
116-
mintedLiquidityTokens: reach.bigNumberToNumber(lptBals.B),
134+
mintedLiquidityTokens: getMintedLiquidityTokens(lptBals),
117135
n2nn,
118136
tokenABalance: formatCurrency(aBal, tokA?.decimals),
119137
tokenAFees: formatCurrency(totalFees(pABal), tokA?.decimals),

0 commit comments

Comments
 (0)