Skip to content

Commit 563a7c1

Browse files
committed
Enhance Moolah SDK by adding new methods for retrieving user holdings and market runtime data.
1 parent 2c5c89e commit 563a7c1

8 files changed

Lines changed: 134 additions & 8 deletions

File tree

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,8 @@ console.log(`Remaining: ${remaining.toFormat(2)}`);
298298
| `getMarketInfo(chainId, marketId)` | API | Market metadata |
299299
| `getMarketExtraInfo(chainId, marketId)` | Chain | Market on-chain data (rates, liquidity) |
300300
| `getMarketUserData(chainId, marketId, user)` | Chain | User position data |
301+
| `getMarketUserDataWithBroker(chainId, marketId, user, broker)` | Chain | User position data with broker fixed-term merged |
302+
| `getMarketRuntimeData(chainId, marketId, wallet)` | Chain | Combined write + user runtime data |
301303
| `getWriteConfig(chainId, marketId)` | Chain | Config for write operations |
302304
| `getMarketList(params)` | API | Market discovery |
303305
| `getVaultInfo(chainId, address)` | Chain | Vault on-chain data |
@@ -308,6 +310,8 @@ console.log(`Remaining: ${remaining.toFormat(2)}`);
308310
| `getSmartMarketUserData(chainId, marketId, user)` | Chain | Smart market user position |
309311
| `getBrokerFixedTerms(chainId, brokerAddress)` | Chain | Fixed-term broker terms |
310312
| `getBrokerUserPositions(chainId, broker, user)` | Chain | Broker user positions |
313+
| `getHoldings(params)` | API | User holdings (`vault` / `market`) |
314+
| `getMarketVaultDetails(marketId, params)` | API | Vault list under a market |
311315

312316
### MoolahSDK Build Methods
313317

@@ -323,10 +327,18 @@ console.log(`Remaining: ${remaining.toFormat(2)}`);
323327
| `buildSmartSupplyCollateralParams(params)` | Build smart market collateral supply steps |
324328
| `buildSmartWithdrawDexLpParams(params)` | Build smart market LP withdraw steps |
325329
| `buildSmartWithdrawCollateralParams(params)` | Build smart market collateral withdraw |
330+
| `buildSmartWithdrawCollateralFixedParams(params)` | Build smart market collateral withdraw (fixed LP burn) |
326331
| `buildSmartRepayParams(params)` | Build smart market repay steps |
327332
| `buildBrokerBorrowParams(params)` | Build broker borrow steps |
328333
| `buildBrokerRepayParams(params)` | Build broker repay steps |
329334

335+
### MoolahSDK Simulate Methods
336+
337+
| Method | Description |
338+
| ------------------------------------- | -------------------------------------------- |
339+
| `simulateBorrowPosition(params)` | Simulate borrow position using live chain data |
340+
| `simulateRepayPosition(params)` | Simulate repay position using live chain data |
341+
330342
### Simulate Functions (Decimal-based)
331343

332344
| Function | Description |

packages/moolah-lending-sdk/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# @lista-dao/moolah-lending-sdk
22

3+
## 1.0.10
4+
5+
### Patch Changes
6+
7+
- Enhance Moolah SDK by adding new methods for retrieving user holdings and market runtime data.
8+
- Updated dependencies
9+
- @lista-dao/moolah-sdk-core@1.0.10
10+
311
## 1.0.9
412

513
### Patch Changes

packages/moolah-lending-sdk/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ const brokerRepaySteps = await sdk.buildBrokerRepayParams({
338338
| `getMarketExtraInfo(chainId, marketId)` | Chain | Market on-chain data |
339339
| `getMarketUserData(chainId, marketId, user)` | Chain | User market position |
340340
| `getMarketUserDataWithBroker(chainId, marketId, user, broker)` | Chain | User market position (fixed-term: merged broker data) |
341+
| `getMarketRuntimeData(chainId, marketId, wallet)` | Chain | Combined write + user runtime data |
341342
| `getWriteConfig(chainId, marketId)` | Chain | Config for write ops |
342343
| `getVaultInfo(chainId, vaultAddress)` | Chain | Vault on-chain data |
343344
| `getVaultUserData(chainId, vaultAddress, user)` | Chain | User vault position |
@@ -349,6 +350,7 @@ const brokerRepaySteps = await sdk.buildBrokerRepayParams({
349350
| `getMarketList(params)` | API | Market list with filters |
350351
| `getVaultMetadata(address)` | API | Vault metadata |
351352
| `getVaultList(params)` | API | Vault list with filters |
353+
| `getHoldings(params)` | API | User holdings (`vault` / `market`) |
352354
| `getMarketVaultDetails(marketId, params)` | API | Vaults for a market |
353355

354356
### Build Methods
@@ -370,6 +372,13 @@ const brokerRepaySteps = await sdk.buildBrokerRepayParams({
370372
| `buildBrokerBorrowParams(params)` | Build broker borrow steps |
371373
| `buildBrokerRepayParams(params)` | Build broker repay steps |
372374

375+
### Simulate Methods
376+
377+
| Method | Description |
378+
| -------------------------------- | -------------------------------------------- |
379+
| `simulateBorrowPosition(params)` | Simulate borrow position using live chain data |
380+
| `simulateRepayPosition(params)` | Simulate repay position using live chain data |
381+
373382
## License
374383

375384
MIT

packages/moolah-lending-sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lista-dao/moolah-lending-sdk",
3-
"version": "1.0.9",
3+
"version": "1.0.10",
44
"type": "module",
55
"main": "./dist/index.js",
66
"module": "./dist/index.js",
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { describe, it, expect, vi } from "vitest";
2+
import type { Address, PublicClient } from "viem";
3+
import {
4+
Decimal,
5+
type SmartMarketExtraInfo,
6+
} from "@lista-dao/moolah-sdk-core";
7+
8+
import { getSmartMarketUserData } from "../../../read/smart/getSmartMarketUserData.js";
9+
10+
const MOOLAH = "0x1111111111111111111111111111111111111111" as Address;
11+
const MARKET_ID =
12+
"0xe20b1f2a32a83b51ec4357fdf4eb5a18c0dc790d2f33dc2d244cd4b1ceb99c50" as Address;
13+
const USER = "0x05E3A7a66945ca9aF73f66660f22ffB36332FA54" as Address;
14+
15+
const LP_TOKEN = "0x2222222222222222222222222222222222222222" as Address;
16+
const USD1 = "0x8d0D000Ee44948FC98c9B98A4FA4921476f08B0d" as Address;
17+
const USDT = "0xdAC17F958D2ee523a2206206994597C13D831ec7" as Address;
18+
19+
describe("getSmartMarketUserData", () => {
20+
it("uses token-specific decimals when reading ERC20 balances", async () => {
21+
const mockReadContract = vi.fn(
22+
async ({
23+
functionName,
24+
address,
25+
}: {
26+
functionName: string;
27+
address: Address;
28+
}) => {
29+
if (functionName === "position") return [0n, 0n, 0n];
30+
if (functionName === "isWhiteList") return false;
31+
if (functionName === "balanceOf" && address === LP_TOKEN) {
32+
return 1000000000000000000n;
33+
}
34+
if (functionName === "balanceOf" && address === USD1) {
35+
return 20450713159115803238n;
36+
}
37+
if (functionName === "balanceOf" && address === USDT) {
38+
return 13834453n;
39+
}
40+
throw new Error(`Unexpected call: ${functionName} @ ${address}`);
41+
},
42+
);
43+
44+
const mockPublicClient = {
45+
readContract: mockReadContract,
46+
getBalance: vi.fn().mockResolvedValue(0n),
47+
} as unknown as PublicClient;
48+
49+
const extraInfo = {
50+
loanInfo: { address: USD1, decimals: 18, symbol: "USD1" },
51+
lpInfo: { address: LP_TOKEN, decimals: 18, symbol: "LP" },
52+
tokenAInfo: { address: USD1, decimals: 18, symbol: "USD1" },
53+
tokenBInfo: { address: USDT, decimals: 6, symbol: "USDT" },
54+
LLTV: new Decimal(8n, 1),
55+
loanIsNative: false,
56+
tokenAIsNative: false,
57+
tokenBIsNative: false,
58+
rateView: 0n,
59+
borrowRate: Decimal.ZERO,
60+
priceRate: Decimal.ONE,
61+
balances: [Decimal.ZERO, Decimal.ZERO] as [Decimal, Decimal],
62+
totalLp: Decimal.ONE,
63+
lastUpdate: 0n,
64+
totalBorrowShares: 0n,
65+
totalBorrowAssets: 0n,
66+
} as unknown as SmartMarketExtraInfo;
67+
68+
const result = await getSmartMarketUserData(
69+
mockPublicClient,
70+
{ moolah: MOOLAH },
71+
MARKET_ID,
72+
USER,
73+
extraInfo,
74+
);
75+
76+
expect(result.balances.tokenA.decimal).toBe(18);
77+
expect(result.balances.tokenA.toString(8)).toBe("20.45071315");
78+
79+
expect(result.balances.tokenB.decimal).toBe(6);
80+
expect(result.balances.tokenB.toString(6)).toBe("13.834453");
81+
});
82+
});

packages/moolah-lending-sdk/src/read/smart/getSmartMarketUserData.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,17 @@ export async function getSmartMarketUserData(
6363
};
6464

6565
// Get user balances
66-
const getErc20Balance = async (tokenAddress: Address): Promise<Decimal> => {
66+
const getErc20Balance = async (
67+
tokenAddress: Address,
68+
decimals: number,
69+
): Promise<Decimal> => {
6770
const balance = await publicClient.readContract({
6871
address: tokenAddress,
6972
abi: ERC20_ABI,
7073
functionName: "balanceOf",
7174
args: [userAddress],
7275
});
73-
return new Decimal(balance);
76+
return new Decimal(balance, decimals);
7477
};
7578

7679
const getNativeBalance = async (): Promise<Decimal> => {
@@ -80,10 +83,16 @@ export async function getSmartMarketUserData(
8083

8184
const [lpBalance, loanBalance, tokenABalance, tokenBBalance] =
8285
await Promise.all([
83-
getErc20Balance(lpInfo.address),
84-
loanIsNative ? getNativeBalance() : getErc20Balance(loanInfo.address),
85-
tokenAIsNative ? getNativeBalance() : getErc20Balance(tokenAInfo.address),
86-
tokenBIsNative ? getNativeBalance() : getErc20Balance(tokenBInfo.address),
86+
getErc20Balance(lpInfo.address, lpInfo.decimals),
87+
loanIsNative
88+
? getNativeBalance()
89+
: getErc20Balance(loanInfo.address, loanInfo.decimals),
90+
tokenAIsNative
91+
? getNativeBalance()
92+
: getErc20Balance(tokenAInfo.address, tokenAInfo.decimals),
93+
tokenBIsNative
94+
? getNativeBalance()
95+
: getErc20Balance(tokenBInfo.address, tokenBInfo.decimals),
8796
]);
8897

8998
const balances = {

packages/moolah-sdk-core/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# @lista-dao/moolah-sdk-core
22

3+
## 1.0.10
4+
5+
### Patch Changes
6+
7+
- Enhance Moolah SDK by adding new methods for retrieving user holdings and market runtime data.
8+
39
## 1.0.9
410

511
### Patch Changes

packages/moolah-sdk-core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lista-dao/moolah-sdk-core",
3-
"version": "1.0.9",
3+
"version": "1.0.10",
44
"type": "module",
55
"main": "./dist/index.js",
66
"module": "./dist/index.js",

0 commit comments

Comments
 (0)