@folks-finance/xchain-sdk 0.0.20 → 0.0.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -9,9 +9,29 @@
9
9
 
10
10
  The official JavaScript SDK for the Folks Finance Cross-Chain Lending Protocol.
11
11
 
12
- ## Installation
12
+ ## Table of Contents
13
13
 
14
- ### Package manager
14
+ - [Getting Started](#getting-started)
15
+ - [Installation](#installation)
16
+ - [Package manager](#package-manager)
17
+ - [SDK Structure and Usage](#sdk-structure-and-usage)
18
+ - [FolksCore](#folkscore)
19
+ - [Modules](#modules)
20
+ - [Basic Usage](#basic-usage)
21
+ - [React Usage](#react-usage)
22
+ - [Initializing FolksCore](#initializing-folkscore)
23
+ - [Synchronizing FolksCore Signer](#synchronizing-folkscore-signer)
24
+
25
+ ## Getting Started
26
+
27
+ Before diving into the SDK, we recommend familiarizing yourself with the Folks Finance Cross-Chain Lending Protocol:
28
+
29
+ - Explore our [comprehensive documentation](https://docs.xapp.folks.finance/?utm_source=github&utm_medium=sdk-readme&utm_campaign=xchain-sdk) for in-depth information about the protocol and its features.
30
+ - Access additional resources and materials in our [Google Drive folder](https://drive.google.com/drive/folders/1P-C_V28JlIJNmoUH6pUKVgVpIfZYQYn5?usp=drive_link).
31
+
32
+ ### Installation
33
+
34
+ #### Package manager
15
35
 
16
36
  Using npm:
17
37
 
@@ -37,6 +57,207 @@ Using bun:
37
57
  bun add @folks-finance/xchain-sdk
38
58
  ```
39
59
 
60
+ ### SDK Structure and Usage
61
+
62
+ The Folks Finance Cross-Chain Lending SDK consists of two main components:
63
+
64
+ 1. `FolksCore`: This acts as the central context for the SDK, managing configuration and state that is shared across all modules.
65
+ 2. Various modules: Located in `/src/xchain/modules`, these provide specific functionalities for interacting with different aspects of the Folks Finance protocol.
66
+
67
+ #### FolksCore
68
+
69
+ `FolksCore` is responsible for initializing the SDK and maintaining the global context. It handles:
70
+
71
+ - Network selection (testnet/mainnet)
72
+ - Provider management
73
+ - Signer management
74
+
75
+ Any changes made to `FolksCore` will affect subsequent calls to the various modules. For example, changing the network or signer will impact how the modules interact with the blockchain.
76
+
77
+ #### Modules
78
+
79
+ The SDK includes several modules, each focusing on a specific area of functionality:
80
+
81
+ - `FolksAccount`: Manages account-related operations, including creating accounts, retrieving account information, and performing management operations such as inviting addresses, accepting invites, and unregistering addresses.
82
+ - `FolksLoan`: Handles all loan-related functions, including retrieving loan information, creating loans, and performing operations such as depositing, withdrawing, repaying, and more.
83
+ - `FolksOracle`: Provides access to updated price information from the oracle.
84
+ - `FolksPool`: Allows retrieval of informations about the pools.
85
+ - `FolksRewards`: Offers access to information about the rewards system.
86
+ - `FolksGmp`: Manages retry and revert functions for failed operations.
87
+
88
+ These modules use the context provided by `FolksCore` internally, so they always operate based on the current state of `FolksCore`.
89
+
90
+ ### Basic Usage
91
+
92
+ To start using the Folks Finance Cross-Chain Lending SDK:
93
+
94
+ 1. Import and initialize `FolksCore`:
95
+
96
+ ```ts
97
+ import { FolksCore, NetworkType } from "@folks-finance/xchain-sdk";
98
+
99
+ const folksConfig = {
100
+ network: NetworkType.TESTNET, // or NetworkType.MAINNET
101
+ provider: {
102
+ evm: {
103
+ // Add your EVM provider configuration here (optional)
104
+ // If not provided, default providers will be used
105
+ },
106
+ },
107
+ };
108
+
109
+ FolksCore.init(folksConfig);
110
+ ```
111
+
112
+ Note: The `provider` configuration in `folksConfig` is optional. If not provided, the SDK will use default providers defined in `src/chains/evm/common/utils/provider.ts`.
113
+
114
+ 2. Use the desired modules to interact with the protocol:
115
+
116
+ ```ts
117
+ import {
118
+ Action,
119
+ BYTES4_LENGTH,
120
+ FolksAccount,
121
+ FolksCore,
122
+ getRandomBytes,
123
+ getSupportedMessageAdapters,
124
+ MessageAdapterParamsType,
125
+ NetworkType,
126
+ } from "@folks-finance/xchain-sdk";
127
+ import { createWalletClient, http } from "viem";
128
+ import { mnemonicToAccount } from "viem/accounts";
129
+
130
+ import type { FolksChainId, Nonce } from "@folks-finance/xchain-sdk";
131
+
132
+ const generateRandomNonce = () => {
133
+ return getRandomBytes(BYTES4_LENGTH) as Nonce;
134
+ };
135
+
136
+ const MNEMONIC = "your mnemonic here";
137
+ const account = mnemonicToAccount(MNEMONIC);
138
+
139
+ // In a real environment you should already have a signer
140
+ const signer = createWalletClient({
141
+ account,
142
+ transport: http(),
143
+ });
144
+
145
+ // Example: Creating an account
146
+ const createAccount = async (sourceFolksChainId: FolksChainId) => {
147
+ const nonce = generateRandomNonce();
148
+ const {
149
+ adapterIds: [adapterId],
150
+ returnAdapterIds: [returnAdapterId],
151
+ } = getSupportedMessageAdapters({
152
+ action: Action.CreateAccount,
153
+ network: NetworkType.TESTNET,
154
+ messageAdapterParamType: MessageAdapterParamsType.Data,
155
+ sourceFolksChainId,
156
+ });
157
+ const adapters = { adapterId, returnAdapterId };
158
+
159
+ // You must set the correct signer before calling a write method that involves signing a transaction
160
+ FolksCore.setFolksSigner({
161
+ signer,
162
+ folksChainId,
163
+ });
164
+
165
+ const prepareCall = await FolksAccount.prepare.createAccount(nonce, adapters);
166
+ const hash = await FolksAccount.write.createAccount(nonce, prepareCall);
167
+
168
+ console.log("Create account hash:", hash);
169
+ };
170
+ ```
171
+
172
+ Remember that any changes made to `FolksCore` (like changing the network or signer) will affect all subsequent module calls. This design allows for flexible and context-aware interactions with the Folks Finance protocol across different chains and environments.
173
+
174
+ ### React Usage
175
+
176
+ When using the SDK with React, there are a few additional considerations to ensure proper initialization and synchronization. Here's how to set up and use the SDK in a React environment:
177
+
178
+ #### Initializing FolksCore
179
+
180
+ To initialize FolksCore only once in your React application, you can create a custom hook:
181
+
182
+ ```ts
183
+ import { useEffect } from "react";
184
+ import { FolksCore, NetworkType } from "@folks-finance/xchain-sdk";
185
+
186
+ import type { FolksCoreConfig } from "@folks-finance/xchain-sdk";
187
+
188
+ export const useInitFolksCore = () => {
189
+ useEffect(() => {
190
+ if (FolksCore.isInitialized()) return;
191
+
192
+ const folksCoreConfig: FolksCoreConfig = {
193
+ network: NetworkType.TESTNET,
194
+ provider,
195
+ };
196
+
197
+ FolksCore.init(folksCoreConfig);
198
+ }, []);
199
+ };
200
+ ```
201
+
202
+ Use this hook in a component that's common to all pages, such as the root layout in Next.js.
203
+
204
+ #### Synchronizing FolksCore Signer
205
+
206
+ To ensure that the correct signer is used for transactions, you need to synchronize the FolksCore signer whenever the chain (and consequently the associated signer) changes on the frontend:
207
+
208
+ ```ts
209
+ import { FOLKS_CHAIN, NetworkType, ChainType } from "@folks-finance/xchain-sdk";
210
+ import { useWalletClient } from "wagmi";
211
+ import { useMemo, useEffect } from "react";
212
+
213
+ import type { FolksChainId } from "@folks-finance/xchain-sdk";
214
+
215
+ function assertExhaustive(value: never, message = "Reached unexpected case in exhaustive switch"): never {
216
+ throw new Error(message);
217
+ }
218
+
219
+ const AVAILABLE_FOLKS_CHAINS = FOLKS_CHAIN[NetworkType.TESTNET];
220
+ const getFolksChainFromFolksChainId = (folksChainId: FolksChainId) => AVAILABLE_FOLKS_CHAINS[folksChainId];
221
+
222
+ const useEvmSigner = ({ chainId }: { chainId?: number }) => {
223
+ const { data: walletClient } = useWalletClient({ chainId });
224
+ const signer = useMemo(() => walletClient, [walletClient]);
225
+ return { signer };
226
+ };
227
+
228
+ export const useSyncFolksCoreSigner = () => {
229
+ // useFolksChain is a hook that returns the current selected FolksChainId on frontend
230
+ const { selectedFolksChainId } = useFolksChain();
231
+
232
+ const selectedFolksChain = selectedFolksChainId ? getFolksChainFromFolksChainId(selectedFolksChainId) : null;
233
+
234
+ const { signer: evmSigner } = useEvmSigner(
235
+ selectedFolksChain?.chainType === ChainType.EVM ? { chainId: selectedFolksChain.chainId as number } : {},
236
+ );
237
+
238
+ useEffect(() => {
239
+ if (!selectedFolksChain) return;
240
+
241
+ const folksChainId = selectedFolksChain.folksChainId;
242
+
243
+ switch (selectedFolksChain.chainType) {
244
+ case ChainType.EVM: {
245
+ if (!evmSigner) return;
246
+ FolksCore.setFolksSigner({
247
+ signer: evmSigner,
248
+ folksChainId,
249
+ });
250
+ break;
251
+ }
252
+ default:
253
+ assertExhaustive(selectedFolksChain.chainType);
254
+ }
255
+ }, [selectedFolksChain, evmSigner]);
256
+ };
257
+ ```
258
+
259
+ Like the initialization hook, `useSyncFolksCoreSigner` should be called in a component shared by all pages.
260
+
40
261
  [license-image]: https://img.shields.io/badge/License-MIT-brightgreen.svg?style=flat-square
41
262
  [license-url]: https://opensource.org/licenses/MIT
42
263
  [ci-image]: https://img.shields.io/github/actions/workflow/status/Folks-Finance/folks-finance-xchain-js-sdk/lint-and-typecheck.yml?branch=main&logo=github&style=flat-square
@@ -0,0 +1,7 @@
1
+ import type { NetworkType } from "../../../../common/types/chain.js";
2
+ import type { AccountId, LoanId } from "../../../../common/types/lending.js";
3
+ import type { LoanTypeId } from "../../../../common/types/module.js";
4
+ import type { LoanTypeInfo } from "../types/loan.js";
5
+ import type { UserRewards } from "../types/rewards.js";
6
+ import type { Client } from "viem";
7
+ export declare function getUserRewards(provider: Client, network: NetworkType, accountId: AccountId, loanIds: Array<LoanId>, loanTypesInfo: Partial<Record<LoanTypeId, LoanTypeInfo>>): Promise<UserRewards>;
@@ -0,0 +1,82 @@
1
+ import { multicall } from "viem/actions";
2
+ import { calcAccruedRewards } from "../../../../common/utils/formulae.js";
3
+ import { getHubChain } from "../utils/chain.js";
4
+ import { getLoanManagerContract } from "../utils/contract.js";
5
+ import { getUserLoans } from "./folks-hub-loan.js";
6
+ export async function getUserRewards(provider, network, accountId, loanIds, loanTypesInfo) {
7
+ const hubChain = getHubChain(network);
8
+ const loanManager = getLoanManagerContract(provider, hubChain.loanManagerAddress);
9
+ // derive the tokens/pools you are interested in
10
+ const seen = new Map();
11
+ const poolIds = [];
12
+ const folksTokenIds = [];
13
+ for (const { pools } of Object.values(loanTypesInfo)) {
14
+ for (const { poolId, folksTokenId } of Object.values(pools)) {
15
+ if (!seen.has(poolId)) {
16
+ poolIds.push(poolId);
17
+ folksTokenIds.push(folksTokenId);
18
+ seen.set(poolId, folksTokenId);
19
+ }
20
+ }
21
+ }
22
+ // fetch the account rewards which are updated
23
+ const getUsersPoolRewards = [];
24
+ for (const poolId of poolIds) {
25
+ getUsersPoolRewards.push({
26
+ address: loanManager.address,
27
+ abi: loanManager.abi,
28
+ functionName: "getUserPoolRewards",
29
+ args: [accountId, poolId],
30
+ });
31
+ }
32
+ const accountPoolRewards = (await multicall(provider, {
33
+ contracts: getUsersPoolRewards,
34
+ allowFailure: false,
35
+ }));
36
+ // initialise with all the rewards which are updated
37
+ const rewards = {};
38
+ for (const [i, accountPoolReward] of accountPoolRewards.entries()) {
39
+ const folksTokenId = folksTokenIds[i];
40
+ rewards[folksTokenId] = accountPoolReward;
41
+ }
42
+ const userRewards = { accountId, rewards };
43
+ // add all the rewards which are not updated
44
+ const userLoans = await getUserLoans(provider, network, loanIds);
45
+ for (const loanId of loanIds) {
46
+ const userLoan = userLoans.get(loanId);
47
+ if (userLoan === undefined)
48
+ throw Error("Unknown user loan");
49
+ const { accountId: userLoanAccountId, loanTypeId, colPools, borPools, userLoanCollateral, userLoanBorrow, } = userLoan;
50
+ const loanTypeInfo = loanTypesInfo[loanTypeId];
51
+ if (!loanTypeInfo)
52
+ throw new Error(`Unknown loan type id ${loanTypeId}`);
53
+ if (accountId !== userLoanAccountId)
54
+ throw new Error(`Loan ${loanId} belongs to account ${userLoanAccountId}`);
55
+ for (const [i, poolId] of colPools.entries()) {
56
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
57
+ const folksTokenId = seen.get(poolId);
58
+ const { balance, rewardIndex } = userLoanCollateral[i];
59
+ const loanPool = loanTypeInfo.pools[folksTokenId];
60
+ if (!loanPool)
61
+ throw new Error(`Unknown loan pool for token ${folksTokenId}`);
62
+ const { collateralRewardIndex } = loanPool.reward;
63
+ const accrued = calcAccruedRewards(balance, collateralRewardIndex, [rewardIndex, 18]);
64
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
65
+ userRewards.rewards[folksTokenId].collateral += accrued;
66
+ }
67
+ for (const [i, poolId] of borPools.entries()) {
68
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
69
+ const folksTokenId = seen.get(poolId);
70
+ const { amount, rewardIndex } = userLoanBorrow[i];
71
+ const loanPool = loanTypeInfo.pools[folksTokenId];
72
+ if (!loanPool)
73
+ throw new Error(`Unknown loan pool for token ${folksTokenId}`);
74
+ const { borrowRewardIndex } = loanPool.reward;
75
+ const accrued = calcAccruedRewards(amount, borrowRewardIndex, [rewardIndex, 18]);
76
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
77
+ userRewards.rewards[folksTokenId].collateral += accrued;
78
+ }
79
+ }
80
+ return userRewards;
81
+ }
82
+ //# sourceMappingURL=folks-hub-rewards.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"folks-hub-rewards.js","sourceRoot":"","sources":["../../../../../src/chains/evm/hub/modules/folks-hub-rewards.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC,OAAO,EAAE,kBAAkB,EAAE,MAAM,sCAAsC,CAAC;AAC1E,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,sBAAsB,EAAE,MAAM,sBAAsB,CAAC;AAE9D,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAUnD,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,QAAgB,EAChB,OAAoB,EACpB,SAAoB,EACpB,OAAsB,EACtB,aAAwD;IAExD,MAAM,QAAQ,GAAG,WAAW,CAAC,OAAO,CAAC,CAAC;IACtC,MAAM,WAAW,GAAG,sBAAsB,CAAC,QAAQ,EAAE,QAAQ,CAAC,kBAAkB,CAAC,CAAC;IAElF,gDAAgD;IAChD,MAAM,IAAI,GAAG,IAAI,GAAG,EAAwB,CAAC;IAC7C,MAAM,OAAO,GAAkB,EAAE,CAAC;IAClC,MAAM,aAAa,GAAwB,EAA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C,WAAW,EAAE,EAAE,CAAC,CAAC,CAAC;YACtF,oEAAoE;YACpE,WAAW,CAAC,OAAO,CAAC,YAAY,CAAE,CAAC,UAAU,IAAI,OAAO,CAAC;QAC3D,CAAC;QAED,KAAK,MAAM,CAAC,CAAC,EAAE,MAAM,CAAC,IAAI,QAAQ,CAAC,OAAO,EAAE,EAAE,CAAC;YAC7C,oEAAoE;YACpE,MAAM,YAAY,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,CAAE,CAAC;YACvC,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,GAAG,cAAc,CAAC,CAAC,CAAC,CAAC;YAElD,MAAM,QAAQ,GAAG,YAAY,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;YAClD,IAAI,CAAC,QAAQ;gBAAE,MAAM,IAAI,KAAK,CAAC,+BAA+B,YAAY,EAAE,CAAC,CAAC;YAC9E,MAAM,EAAE,iBAAiB,EAAE,GAAG,QAAQ,CAAC,MAAM,CAAC;YAE9C,MAAM,OAAO,GAAG,kBAAkB,CAAC,MAAM,EAAE,iBAAiB,EAAE,CAAC,WAAW,EAAE,EAAE,CAAC,CAAC,CAAC;YACjF,oEAAoE;YACpE,WAAW,CAAC,OAAO,CAAC,YAAY,CAAE,CAAC,UAAU,IAAI,OAAO,CAAC;QAC3D,CAAC;IACH,CAAC;IAED,OAAO,WAAW,CAAC;AACrB,CAAC"}
@@ -2,4 +2,5 @@ export * as FolksHubAccount from "./folks-hub-account.js";
2
2
  export * as FolksHubLoan from "./folks-hub-loan.js";
3
3
  export * as FolksHubOracle from "./folks-hub-oracle.js";
4
4
  export * as FolksHubPool from "./folks-hub-pool.js";
5
+ export * as FolksHubRewards from "./folks-hub-rewards.js";
5
6
  export * as FolksHubGmp from "./folks-hub-gmp.js";
@@ -2,5 +2,6 @@ export * as FolksHubAccount from "./folks-hub-account.js";
2
2
  export * as FolksHubLoan from "./folks-hub-loan.js";
3
3
  export * as FolksHubOracle from "./folks-hub-oracle.js";
4
4
  export * as FolksHubPool from "./folks-hub-pool.js";
5
+ export * as FolksHubRewards from "./folks-hub-rewards.js";
5
6
  export * as FolksHubGmp from "./folks-hub-gmp.js";
6
7
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/chains/evm/hub/modules/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,eAAe,MAAM,wBAAwB,CAAC;AAC1D,OAAO,KAAK,YAAY,MAAM,qBAAqB,CAAC;AACpD,OAAO,KAAK,cAAc,MAAM,uBAAuB,CAAC;AACxD,OAAO,KAAK,YAAY,MAAM,qBAAqB,CAAC;AACpD,OAAO,KAAK,WAAW,MAAM,oBAAoB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/chains/evm/hub/modules/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,eAAe,MAAM,wBAAwB,CAAC;AAC1D,OAAO,KAAK,YAAY,MAAM,qBAAqB,CAAC;AACpD,OAAO,KAAK,cAAc,MAAM,uBAAuB,CAAC;AACxD,OAAO,KAAK,YAAY,MAAM,qBAAqB,CAAC;AACpD,OAAO,KAAK,eAAe,MAAM,wBAAwB,CAAC;AAC1D,OAAO,KAAK,WAAW,MAAM,oBAAoB,CAAC"}
@@ -0,0 +1,12 @@
1
+ import type { AccountId } from "../../../../common/types/lending.js";
2
+ import type { FolksTokenId } from "../../../../common/types/token.js";
3
+ export type AccountPoolRewards = {
4
+ collateral: bigint;
5
+ borrow: bigint;
6
+ interestPaid: bigint;
7
+ };
8
+ export type AccountRewards = Partial<Record<FolksTokenId, AccountPoolRewards>>;
9
+ export type UserRewards = {
10
+ accountId: AccountId;
11
+ rewards: AccountRewards;
12
+ };
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=rewards.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rewards.js","sourceRoot":"","sources":["../../../../../src/chains/evm/hub/types/rewards.ts"],"names":[],"mappings":""}
@@ -4,6 +4,7 @@ export declare function calcNextPeriodReset(periodNumber: bigint, offset: bigint
4
4
  export declare function calcDepositInterestIndex(dirt1: Dnum, diit1: Dnum, latestUpdate: bigint): Dnum;
5
5
  export declare function calcBorrowInterestIndex(birt1: Dnum, biit1: Dnum, latestUpdate: bigint): Dnum;
6
6
  export declare function calcRewardIndex(used: bigint, ma: bigint, rit1: Dnum, rs: Dnum, latestUpdate: bigint): Dnum;
7
+ export declare function calcAccruedRewards(amount: bigint, rit: Dnum, ritn1: Dnum): bigint;
7
8
  export declare function toFAmount(underlyingAmount: bigint, diit: Dnum): bigint;
8
9
  export declare function toUnderlyingAmount(fAmount: bigint, diit: Dnum): bigint;
9
10
  export declare function calcCollateralAssetLoanValue(amount: bigint, tokenPrice: Dnum, tokenDecimals: number, collateralFactor: Dnum): Dnum;
@@ -24,6 +24,10 @@ export function calcRewardIndex(used, ma, rit1, rs, latestUpdate) {
24
24
  rounding: "ROUND_DOWN",
25
25
  }));
26
26
  }
27
+ export function calcAccruedRewards(amount, rit, ritn1) {
28
+ const [accruedRewards] = dn.mul([amount, 0], dn.sub(rit, ritn1), { rounding: "ROUND_DOWN" });
29
+ return accruedRewards;
30
+ }
27
31
  export function toFAmount(underlyingAmount, diit) {
28
32
  const [fAmount] = dn.div([underlyingAmount, 0], diit, { rounding: "ROUND_DOWN" });
29
33
  return fAmount;
@@ -1 +1 @@
1
- {"version":3,"file":"formulae.js","sourceRoot":"","sources":["../../../src/common/utils/formulae.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,MAAM,CAAC;AAE3B,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAIzE,MAAM,UAAU,gBAAgB,CAAC,MAAc,EAAE,MAAc;IAC7D,OAAO,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC,GAAG,MAAM,CAAC,GAAG,MAAM,CAAC;AAChD,CAAC;AAED,MAAM,UAAU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chain/core/folks-core.js";
2
- export { FolksAccount, FolksLoan, FolksOracle, FolksPool } from "./xchain/modules/index.js";
2
+ export { FolksAccount, FolksLoan, FolksOracle, FolksPool, FolksGmp, FolksRewards } from "./xchain/modules/index.js";
3
3
  export * from "./common/types/adapter.js";
4
4
  export * from "./common/types/address.js";
5
5
  export * from "./common/types/chain.js";
@@ -32,4 +32,5 @@ export * from "./chains/evm/hub/types/chain.js";
32
32
  export * from "./chains/evm/hub/types/loan.js";
33
33
  export * from "./chains/evm/hub/types/oracle.js";
34
34
  export * from "./chains/evm/hub/types/pool.js";
35
+ export * from "./chains/evm/hub/types/rewards.js";
35
36
  export * from "./chains/evm/hub/types/token.js";
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // === CORE ===
2
2
  export { FolksCore } from "./xchain/core/folks-core.js";
3
3
  // === MODULES ===
4
- export { FolksAccount, FolksLoan, FolksOracle, FolksPool } from "./xchain/modules/index.js";
4
+ export { FolksAccount, FolksLoan, FolksOracle, FolksPool, FolksGmp, FolksRewards } from "./xchain/modules/index.js";
5
5
  // === COMMON ===
6
6
  export * from "./common/types/adapter.js";
7
7
  export * from "./common/types/address.js";
@@ -39,5 +39,6 @@ export * from "./chains/evm/hub/types/chain.js";
39
39
  export * from "./chains/evm/hub/types/loan.js";
40
40
  export * from "./chains/evm/hub/types/oracle.js";
41
41
  export * from "./chains/evm/hub/types/pool.js";
42
+ export * from "./chains/evm/hub/types/rewards.js";
42
43
  export * from "./chains/evm/hub/types/token.js";
43
44
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,eAAe;AACf,OAAO,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAC;AAExD,kBAAkB;AAClB,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AAE5F,iBAAi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,CAAC;AAEzD,cAAc;AACd,OAAO,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAC7D,OAAO,EAAE,SAAS,EAAE,MAAM,qCAAqC,CAAC;AAEhE,iBAAiB;AAEjB,QAAQ;AACR,cAAc,wCAAwC,CAAC;AAEvD,cAAc,oCAAoC,CAAC;AAEnD,OAAO,EAAE,YAAY,EAAE,MAAM,oCAAoC,CAAC;AAElE,aAAa;AACb,cAAc,mCAAmC,CAAC;AAClD,cAAc,iCAAiC,CAAC;AAChD,cAAc,gCAAgC,CAAC;AAC/C,cAAc,kCAAkC,CAAC;AACjD,cAAc,gCAAgC,CAAC;AAC/C,cAAc,mCAAmC,CAAC;AAClD,cAAc,iCAAiC,CAAC"}
@@ -0,0 +1,7 @@
1
+ import type { LoanTypeInfo } from "../../chains/evm/hub/types/loan.js";
2
+ import type { UserRewards } from "../../chains/evm/hub/types/rewards.js";
3
+ import type { AccountId, LoanId } from "../../common/types/lending.js";
4
+ import type { LoanTypeId } from "../../common/types/module.js";
5
+ export declare const read: {
6
+ rewards(accountId: AccountId, loanIds: Array<LoanId>, loanTypesInfo: Partial<Record<LoanTypeId, LoanTypeInfo>>): Promise<UserRewards>;
7
+ };
@@ -0,0 +1,8 @@
1
+ import { FolksHubRewards } from "../../chains/evm/hub/modules/index.js";
2
+ import { FolksCore } from "../core/folks-core.js";
3
+ export const read = {
4
+ async rewards(accountId, loanIds, loanTypesInfo) {
5
+ return FolksHubRewards.getUserRewards(FolksCore.getHubProvider(), FolksCore.getSelectedNetwork(), accountId, loanIds, loanTypesInfo);
6
+ },
7
+ };
8
+ //# sourceMappingURL=folks-rewards.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"folks-rewards.js","sourceRoot":"","sources":["../../../src/xchain/modules/folks-rewards.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,uCAAuC,CAAC;AACxE,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAOlD,MAAM,CAAC,MAAM,IAAI,GAAG;IAClB,KAAK,CAAC,OAAO,CACX,SAAoB,EACpB,OAAsB,EACtB,aAAwD;QAExD,OAAO,eAAe,CAAC,cAAc,CACnC,SAAS,CAAC,cAAc,EAAE,EAC1B,SAAS,CAAC,kBAAkB,EAAE,EAC9B,SAAS,EACT,OAAO,EACP,aAAa,CACd,CAAC;IACJ,CAAC;CACF,CAAC"}
@@ -2,4 +2,5 @@ export * as FolksAccount from "./folks-account.js";
2
2
  export * as FolksLoan from "./folks-loan.js";
3
3
  export * as FolksOracle from "./folks-oracle.js";
4
4
  export * as FolksPool from "./folks-pool.js";
5
+ export * as FolksRewards from "./folks-rewards.js";
5
6
  export * as FolksGmp from "./folks-gmp.js";
@@ -2,5 +2,6 @@ export * as FolksAccount from "./folks-account.js";
2
2
  export * as FolksLoan from "./folks-loan.js";
3
3
  export * as FolksOracle from "./folks-oracle.js";
4
4
  export * as FolksPool from "./folks-pool.js";
5
+ export * as FolksRewards from "./folks-rewards.js";
5
6
  export * as FolksGmp from "./folks-gmp.js";
6
7
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/xchain/modules/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,YAAY,MAAM,oBAAoB,CAAC;AACnD,OAAO,KAAK,SAAS,MAAM,iBAAiB,CAAC;AAC7C,OAAO,KAAK,WAAW,MAAM,mBAAmB,CAAC;AACjD,OAAO,KAAK,SAAS,MAAM,iBAAiB,CAAC;AAC7C,OAAO,KAAK,QAAQ,MAAM,gBAAgB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/xchain/modules/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,YAAY,MAAM,oBAAoB,CAAC;AACnD,OAAO,KAAK,SAAS,MAAM,iBAAiB,CAAC;AAC7C,OAAO,KAAK,WAAW,MAAM,mBAAmB,CAAC;AACjD,OAAO,KAAK,SAAS,MAAM,iBAAiB,CAAC;AAC7C,OAAO,KAAK,YAAY,MAAM,oBAAoB,CAAC;AACnD,OAAO,KAAK,QAAQ,MAAM,gBAAgB,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@folks-finance/xchain-sdk",
3
3
  "description": "The official JavaScript SDK for the Folks Finance Cross-Chain Lending Protocol",
4
- "version": "0.0.20",
4
+ "version": "0.0.22",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
7
7
  "access": "public"
@@ -0,0 +1,114 @@
1
+ import { multicall } from "viem/actions";
2
+
3
+ import { calcAccruedRewards } from "../../../../common/utils/formulae.js";
4
+ import { getHubChain } from "../utils/chain.js";
5
+ import { getLoanManagerContract } from "../utils/contract.js";
6
+
7
+ import { getUserLoans } from "./folks-hub-loan.js";
8
+
9
+ import type { NetworkType } from "../../../../common/types/chain.js";
10
+ import type { AccountId, LoanId } from "../../../../common/types/lending.js";
11
+ import type { LoanTypeId } from "../../../../common/types/module.js";
12
+ import type { FolksTokenId } from "../../../../common/types/token.js";
13
+ import type { LoanTypeInfo } from "../types/loan.js";
14
+ import type { AccountPoolRewards, UserRewards } from "../types/rewards.js";
15
+ import type { Client, ContractFunctionParameters } from "viem";
16
+
17
+ export async function getUserRewards(
18
+ provider: Client,
19
+ network: NetworkType,
20
+ accountId: AccountId,
21
+ loanIds: Array<LoanId>,
22
+ loanTypesInfo: Partial<Record<LoanTypeId, LoanTypeInfo>>,
23
+ ): Promise<UserRewards> {
24
+ const hubChain = getHubChain(network);
25
+ const loanManager = getLoanManagerContract(provider, hubChain.loanManagerAddress);
26
+
27
+ // derive the tokens/pools you are interested in
28
+ const seen = new Map<number, FolksTokenId>();
29
+ const poolIds: Array<number> = [];
30
+ const folksTokenIds: Array<FolksTokenId> = [];
31
+ for (const { pools } of Object.values(loanTypesInfo)) {
32
+ for (const { poolId, folksTokenId } of Object.values(pools)) {
33
+ if (!seen.has(poolId)) {
34
+ poolIds.push(poolId);
35
+ folksTokenIds.push(folksTokenId);
36
+ seen.set(poolId, folksTokenId);
37
+ }
38
+ }
39
+ }
40
+
41
+ // fetch the account rewards which are updated
42
+ const getUsersPoolRewards: Array<ContractFunctionParameters> = [];
43
+ for (const poolId of poolIds) {
44
+ getUsersPoolRewards.push({
45
+ address: loanManager.address,
46
+ abi: loanManager.abi,
47
+ functionName: "getUserPoolRewards",
48
+ args: [accountId, poolId],
49
+ });
50
+ }
51
+
52
+ const accountPoolRewards: Array<AccountPoolRewards> = (await multicall(provider, {
53
+ contracts: getUsersPoolRewards,
54
+ allowFailure: false,
55
+ })) as Array<AccountPoolRewards>;
56
+
57
+ // initialise with all the rewards which are updated
58
+ const rewards: Partial<Record<FolksTokenId, AccountPoolRewards>> = {};
59
+ for (const [i, accountPoolReward] of accountPoolRewards.entries()) {
60
+ const folksTokenId = folksTokenIds[i];
61
+ rewards[folksTokenId] = accountPoolReward;
62
+ }
63
+ const userRewards: UserRewards = { accountId, rewards };
64
+
65
+ // add all the rewards which are not updated
66
+ const userLoans = await getUserLoans(provider, network, loanIds);
67
+ for (const loanId of loanIds) {
68
+ const userLoan = userLoans.get(loanId);
69
+ if (userLoan === undefined) throw Error("Unknown user loan");
70
+
71
+ const {
72
+ accountId: userLoanAccountId,
73
+ loanTypeId,
74
+ colPools,
75
+ borPools,
76
+ userLoanCollateral,
77
+ userLoanBorrow,
78
+ } = userLoan;
79
+
80
+ const loanTypeInfo = loanTypesInfo[loanTypeId];
81
+ if (!loanTypeInfo) throw new Error(`Unknown loan type id ${loanTypeId}`);
82
+ if (accountId !== userLoanAccountId) throw new Error(`Loan ${loanId} belongs to account ${userLoanAccountId}`);
83
+
84
+ for (const [i, poolId] of colPools.entries()) {
85
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
86
+ const folksTokenId = seen.get(poolId)!;
87
+ const { balance, rewardIndex } = userLoanCollateral[i];
88
+
89
+ const loanPool = loanTypeInfo.pools[folksTokenId];
90
+ if (!loanPool) throw new Error(`Unknown loan pool for token ${folksTokenId}`);
91
+ const { collateralRewardIndex } = loanPool.reward;
92
+
93
+ const accrued = calcAccruedRewards(balance, collateralRewardIndex, [rewardIndex, 18]);
94
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
95
+ userRewards.rewards[folksTokenId]!.collateral += accrued;
96
+ }
97
+
98
+ for (const [i, poolId] of borPools.entries()) {
99
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
100
+ const folksTokenId = seen.get(poolId)!;
101
+ const { amount, rewardIndex } = userLoanBorrow[i];
102
+
103
+ const loanPool = loanTypeInfo.pools[folksTokenId];
104
+ if (!loanPool) throw new Error(`Unknown loan pool for token ${folksTokenId}`);
105
+ const { borrowRewardIndex } = loanPool.reward;
106
+
107
+ const accrued = calcAccruedRewards(amount, borrowRewardIndex, [rewardIndex, 18]);
108
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
109
+ userRewards.rewards[folksTokenId]!.collateral += accrued;
110
+ }
111
+ }
112
+
113
+ return userRewards;
114
+ }
@@ -2,4 +2,5 @@ export * as FolksHubAccount from "./folks-hub-account.js";
2
2
  export * as FolksHubLoan from "./folks-hub-loan.js";
3
3
  export * as FolksHubOracle from "./folks-hub-oracle.js";
4
4
  export * as FolksHubPool from "./folks-hub-pool.js";
5
+ export * as FolksHubRewards from "./folks-hub-rewards.js";
5
6
  export * as FolksHubGmp from "./folks-hub-gmp.js";
@@ -0,0 +1,15 @@
1
+ import type { AccountId } from "../../../../common/types/lending.js";
2
+ import type { FolksTokenId } from "../../../../common/types/token.js";
3
+
4
+ export type AccountPoolRewards = {
5
+ collateral: bigint;
6
+ borrow: bigint;
7
+ interestPaid: bigint;
8
+ };
9
+
10
+ export type AccountRewards = Partial<Record<FolksTokenId, AccountPoolRewards>>;
11
+
12
+ export type UserRewards = {
13
+ accountId: AccountId;
14
+ rewards: AccountRewards;
15
+ };
@@ -46,6 +46,11 @@ export function calcRewardIndex(used: bigint, ma: bigint, rit1: Dnum, rs: Dnum,
46
46
  );
47
47
  }
48
48
 
49
+ export function calcAccruedRewards(amount: bigint, rit: Dnum, ritn1: Dnum): bigint {
50
+ const [accruedRewards] = dn.mul([amount, 0], dn.sub(rit, ritn1), { rounding: "ROUND_DOWN" });
51
+ return accruedRewards;
52
+ }
53
+
49
54
  export function toFAmount(underlyingAmount: bigint, diit: Dnum): bigint {
50
55
  const [fAmount] = dn.div([underlyingAmount, 0], diit, { rounding: "ROUND_DOWN" });
51
56
  return fAmount;
package/src/index.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  export { FolksCore } from "./xchain/core/folks-core.js";
3
3
 
4
4
  // === MODULES ===
5
- export { FolksAccount, FolksLoan, FolksOracle, FolksPool } from "./xchain/modules/index.js";
5
+ export { FolksAccount, FolksLoan, FolksOracle, FolksPool, FolksGmp, FolksRewards } from "./xchain/modules/index.js";
6
6
 
7
7
  // === COMMON ===
8
8
  export * from "./common/types/adapter.js";
@@ -49,4 +49,5 @@ export * from "./chains/evm/hub/types/chain.js";
49
49
  export * from "./chains/evm/hub/types/loan.js";
50
50
  export * from "./chains/evm/hub/types/oracle.js";
51
51
  export * from "./chains/evm/hub/types/pool.js";
52
+ export * from "./chains/evm/hub/types/rewards.js";
52
53
  export * from "./chains/evm/hub/types/token.js";
@@ -0,0 +1,23 @@
1
+ import { FolksHubRewards } from "../../chains/evm/hub/modules/index.js";
2
+ import { FolksCore } from "../core/folks-core.js";
3
+
4
+ import type { LoanTypeInfo } from "../../chains/evm/hub/types/loan.js";
5
+ import type { UserRewards } from "../../chains/evm/hub/types/rewards.js";
6
+ import type { AccountId, LoanId } from "../../common/types/lending.js";
7
+ import type { LoanTypeId } from "../../common/types/module.js";
8
+
9
+ export const read = {
10
+ async rewards(
11
+ accountId: AccountId,
12
+ loanIds: Array<LoanId>,
13
+ loanTypesInfo: Partial<Record<LoanTypeId, LoanTypeInfo>>,
14
+ ): Promise<UserRewards> {
15
+ return FolksHubRewards.getUserRewards(
16
+ FolksCore.getHubProvider(),
17
+ FolksCore.getSelectedNetwork(),
18
+ accountId,
19
+ loanIds,
20
+ loanTypesInfo,
21
+ );
22
+ },
23
+ };
@@ -2,4 +2,5 @@ export * as FolksAccount from "./folks-account.js";
2
2
  export * as FolksLoan from "./folks-loan.js";
3
3
  export * as FolksOracle from "./folks-oracle.js";
4
4
  export * as FolksPool from "./folks-pool.js";
5
+ export * as FolksRewards from "./folks-rewards.js";
5
6
  export * as FolksGmp from "./folks-gmp.js";