@folks-finance/xchain-sdk 0.0.21 → 0.0.23

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.
Files changed (33) hide show
  1. package/README.md +223 -2
  2. package/dist/chains/evm/common/constants/abi/wormhole-relayer-abi.d.ts +783 -0
  3. package/dist/chains/evm/common/constants/abi/wormhole-relayer-abi.js +501 -0
  4. package/dist/chains/evm/common/constants/abi/wormhole-relayer-abi.js.map +1 -0
  5. package/dist/chains/evm/common/types/module.d.ts +12 -0
  6. package/dist/chains/evm/common/utils/contract.d.ts +4 -1
  7. package/dist/chains/evm/common/utils/contract.js +8 -0
  8. package/dist/chains/evm/common/utils/contract.js.map +1 -1
  9. package/dist/chains/evm/hub/modules/folks-hub-rewards.d.ts +1 -1
  10. package/dist/chains/evm/hub/modules/folks-hub-rewards.js +22 -44
  11. package/dist/chains/evm/hub/modules/folks-hub-rewards.js.map +1 -1
  12. package/dist/chains/evm/hub/types/rewards.d.ts +0 -1
  13. package/dist/chains/evm/spoke/modules/folks-evm-gmp.d.ts +5 -3
  14. package/dist/chains/evm/spoke/modules/folks-evm-gmp.js +54 -2
  15. package/dist/chains/evm/spoke/modules/folks-evm-gmp.js.map +1 -1
  16. package/dist/common/types/module.d.ts +3 -1
  17. package/dist/common/types/module.js.map +1 -1
  18. package/dist/xchain/modules/folks-gmp.d.ts +5 -2
  19. package/dist/xchain/modules/folks-gmp.js +7 -0
  20. package/dist/xchain/modules/folks-gmp.js.map +1 -1
  21. package/dist/xchain/modules/folks-rewards.d.ts +1 -1
  22. package/dist/xchain/modules/folks-rewards.js +2 -2
  23. package/dist/xchain/modules/folks-rewards.js.map +1 -1
  24. package/package.json +1 -1
  25. package/src/chains/evm/common/constants/abi/wormhole-relayer-abi.ts +500 -0
  26. package/src/chains/evm/common/types/module.ts +13 -0
  27. package/src/chains/evm/common/utils/contract.ts +23 -1
  28. package/src/chains/evm/hub/modules/folks-hub-rewards.ts +23 -51
  29. package/src/chains/evm/hub/types/rewards.ts +0 -1
  30. package/src/chains/evm/spoke/modules/folks-evm-gmp.ts +95 -5
  31. package/src/common/types/module.ts +4 -0
  32. package/src/xchain/modules/folks-gmp.ts +37 -3
  33. package/src/xchain/modules/folks-rewards.ts +2 -4
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