@rhea-finance/cross-chain-sdk 0.1.0 → 0.1.2
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 +578 -142
- package/dist/index.cjs +45 -20
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +17 -2
- package/dist/index.d.ts +17 -2
- package/dist/index.js +43 -20
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -15,7 +15,7 @@ Cross-chain lending SDK that supports unified lending operations across multiple
|
|
|
15
15
|
|
|
16
16
|
## Supported Blockchains
|
|
17
17
|
|
|
18
|
-
- **EVM Chains**: Ethereum, Arbitrum, Optimism, Base,
|
|
18
|
+
- **EVM Chains**: Ethereum, Arbitrum, Optimism, Base, BNB Chain, etc.
|
|
19
19
|
- **Solana**
|
|
20
20
|
- **Bitcoin**
|
|
21
21
|
- **NEAR Protocol**
|
|
@@ -35,61 +35,185 @@ yarn add @rhea-finance/cross-chain-sdk
|
|
|
35
35
|
### Basic Usage
|
|
36
36
|
|
|
37
37
|
```typescript
|
|
38
|
+
// Type imports
|
|
39
|
+
import type {
|
|
40
|
+
ILendingData,
|
|
41
|
+
IAccountAllPositionsDetailed,
|
|
42
|
+
IAssetDetailed,
|
|
43
|
+
IConfig,
|
|
44
|
+
IPythInfo,
|
|
45
|
+
IMetadata,
|
|
46
|
+
IAssetFarm,
|
|
47
|
+
IPrices,
|
|
48
|
+
Portfolio,
|
|
49
|
+
} from "@rhea-finance/cross-chain-sdk";
|
|
50
|
+
|
|
51
|
+
// Function imports
|
|
38
52
|
import {
|
|
39
53
|
batchViews,
|
|
40
|
-
|
|
41
|
-
|
|
54
|
+
getAccountAllPositions,
|
|
55
|
+
getAssetsDetail,
|
|
56
|
+
getConfig,
|
|
57
|
+
getTokenPythInfos,
|
|
58
|
+
getAllMetadata,
|
|
59
|
+
getMetadata,
|
|
60
|
+
getBalance,
|
|
61
|
+
getAllFarms,
|
|
42
62
|
getPrices,
|
|
43
|
-
|
|
44
|
-
IChain,
|
|
63
|
+
getPortfolio,
|
|
45
64
|
} from "@rhea-finance/cross-chain-sdk";
|
|
46
65
|
|
|
47
|
-
//
|
|
48
|
-
const lendingData = await batchViews(accountId);
|
|
66
|
+
// Batch query - get all data at once
|
|
67
|
+
const lendingData: ILendingData = await batchViews(accountId);
|
|
49
68
|
console.log(lendingData.account_all_positions);
|
|
50
69
|
console.log(lendingData.assets_paged_detailed);
|
|
51
70
|
console.log(lendingData.config);
|
|
71
|
+
console.log(lendingData.token_pyth_infos);
|
|
72
|
+
|
|
73
|
+
// Individual queries - get data separately
|
|
74
|
+
|
|
75
|
+
// Get account all positions
|
|
76
|
+
const accountAllPositions: IAccountAllPositionsDetailed = await getAccountAllPositions(accountId);
|
|
77
|
+
// Get assets detail
|
|
78
|
+
const assetsPagedDetailed: IAssetDetailed[] = await getAssetsDetail();
|
|
79
|
+
|
|
80
|
+
// Get config
|
|
81
|
+
const config: IConfig = await getConfig();
|
|
82
|
+
|
|
83
|
+
// Get token pyth infos
|
|
84
|
+
const tokenPythInfos: Record<string, IPythInfo> = await getTokenPythInfos();
|
|
52
85
|
|
|
53
|
-
// Get
|
|
54
|
-
const
|
|
86
|
+
// Get metadata - batch query
|
|
87
|
+
const tokenIds = ["usdt.tether-token.near", "17208628f84f5d6ad33f0da3bbbeb27ffcb398eac501a31bd6ad2011e36133a1", "zec.omft.near"];
|
|
88
|
+
const allMetadata: IMetadata[] = await getAllMetadata(tokenIds);
|
|
89
|
+
|
|
90
|
+
// Get metadata - single query
|
|
91
|
+
const metadata: IMetadata | undefined = await getMetadata("token1.near");
|
|
92
|
+
|
|
93
|
+
// Get balance - get token balance in NEAR wallet
|
|
94
|
+
const balance: string = await getBalance("usdt.tether-token.near", accountId);
|
|
95
|
+
|
|
96
|
+
// Get all farms - get all lending farm data
|
|
97
|
+
const allFarms: [Record<string, string>, IAssetFarm][] = await getAllFarms();
|
|
98
|
+
|
|
99
|
+
// Get prices - get price data for all assets in lending
|
|
100
|
+
const prices: IPrices | undefined = await getPrices({
|
|
101
|
+
token_pyth_infos: tokenPythInfos,
|
|
102
|
+
config: config,
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// Get portfolio - format account data for easier use
|
|
106
|
+
const portfolio: Portfolio = getPortfolio(accountAllPositions);
|
|
55
107
|
console.log(portfolio.supplied);
|
|
56
108
|
console.log(portfolio.collateral);
|
|
57
109
|
console.log(portfolio.borrowed);
|
|
58
110
|
```
|
|
59
111
|
|
|
112
|
+
### Multi-chain Account (MCA) Management
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
// Import MCA related functions separately
|
|
116
|
+
import {
|
|
117
|
+
getMcaByWallet,
|
|
118
|
+
getListWalletsByMca,
|
|
119
|
+
getCeateMcaFee,
|
|
120
|
+
getCreateMcaFeePaged,
|
|
121
|
+
getNearValue,
|
|
122
|
+
getNearValuesPaged,
|
|
123
|
+
} from "@rhea-finance/cross-chain-sdk";
|
|
124
|
+
import type { IChain, IWallet } from "@rhea-finance/cross-chain-sdk";
|
|
125
|
+
|
|
126
|
+
// Get MCA by wallet - query multi-chain account based on logged-in wallet
|
|
127
|
+
// Parameters:
|
|
128
|
+
// chain: Supported chain type, currently supports "evm", "solana", "btc"
|
|
129
|
+
// identityKey: Unique identifier of the logged-in account on the specified chain
|
|
130
|
+
// - evm: Account ID (e.g., "0x1234...")
|
|
131
|
+
// - solana: Account ID (e.g., "ABC123...")
|
|
132
|
+
// - btc: Public key (e.g., "02abc123...")
|
|
133
|
+
const mcaId: string | null = await getMcaByWallet({
|
|
134
|
+
chain: "evm" as IChain,
|
|
135
|
+
identityKey: "0x1234...",
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
// Get list wallets by MCA - query all wallets bound to a multi-chain account
|
|
139
|
+
const wallets: IWallet[] = await getListWalletsByMca(mcaId);
|
|
140
|
+
|
|
141
|
+
// Get create MCA fee - get creation fee for a specific asset
|
|
142
|
+
const createFee: string = await getCeateMcaFee("usdt.tether-token.near");
|
|
143
|
+
|
|
144
|
+
// Get create MCA fee paged - get list of creation fee tokens
|
|
145
|
+
const createFeeList: Record<string, string> = await getCreateMcaFeePaged();
|
|
146
|
+
|
|
147
|
+
// Get near value - get exchange rate between a specific token and NEAR
|
|
148
|
+
const nearValue: string = await getNearValue("usdt.tether-token.near");
|
|
149
|
+
|
|
150
|
+
// Get near values paged - get all token exchange rates with NEAR
|
|
151
|
+
const nearValues: Record<string, string> = await getNearValuesPaged();
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## Operations
|
|
155
|
+
|
|
60
156
|
### Create Multi-chain Account (MCA)
|
|
61
157
|
|
|
158
|
+
The following steps describe how to create a multi-chain account:
|
|
159
|
+
|
|
62
160
|
```typescript
|
|
161
|
+
import type {
|
|
162
|
+
IChain,
|
|
163
|
+
IIntentsQuoteResult,
|
|
164
|
+
} from "@rhea-finance/cross-chain-sdk";
|
|
63
165
|
import {
|
|
64
|
-
getCreateMcaCustomRecipientMsg,
|
|
65
166
|
format_wallet,
|
|
167
|
+
serializationObj,
|
|
168
|
+
getCreateMcaCustomRecipientMsg,
|
|
66
169
|
intentsQuotation,
|
|
67
|
-
|
|
170
|
+
prepare_sign_message_evm,
|
|
171
|
+
process_signature_evm,
|
|
68
172
|
} from "@rhea-finance/cross-chain-sdk";
|
|
69
173
|
|
|
70
|
-
// 1
|
|
71
|
-
const
|
|
174
|
+
// Step 1: Create multi-chain wallet
|
|
175
|
+
const w = format_wallet({
|
|
176
|
+
chain: "evm" as IChain,
|
|
177
|
+
identityKey: "0x1234...",
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
// Step 2: Sign
|
|
181
|
+
// Signing content
|
|
182
|
+
const message = serializationObj([w]);
|
|
183
|
+
|
|
184
|
+
// Format signing content
|
|
185
|
+
const _message = prepare_sign_message_evm(message);
|
|
186
|
+
|
|
187
|
+
// Sign (this is a wallet SDK method, not from this SDK)
|
|
188
|
+
const signature = signMessage(_message);
|
|
72
189
|
|
|
73
|
-
//
|
|
74
|
-
const
|
|
75
|
-
wallets.map((wallet) => sign_message({ chain, message: wallets }))
|
|
76
|
-
);
|
|
190
|
+
// Process signature result
|
|
191
|
+
const signedMessage = process_signature_evm(signature);
|
|
77
192
|
|
|
78
|
-
// 3
|
|
193
|
+
// Step 3: Get customRecipientMsg
|
|
194
|
+
// useAsCollateral parameter determines whether these assets should be used as collateral.
|
|
79
195
|
const customRecipientMsg = getCreateMcaCustomRecipientMsg({
|
|
80
196
|
useAsCollateral: true,
|
|
81
|
-
wallets,
|
|
82
|
-
signedMessages,
|
|
197
|
+
wallets: [w],
|
|
198
|
+
signedMessages: [signedMessage],
|
|
83
199
|
});
|
|
84
200
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
201
|
+
// Step 4: Get intents quote to obtain depositAddress
|
|
202
|
+
const res_quote: IIntentsQuoteResult = await intentsQuotation({
|
|
203
|
+
originAsset: "nep141:xxx",
|
|
204
|
+
destinationAsset: "nep141:xxx",
|
|
205
|
+
amount: "14743",
|
|
206
|
+
refundTo: "0xxxx",
|
|
207
|
+
recipient: "multica.near",
|
|
208
|
+
isReverse: false,
|
|
209
|
+
dry: false,
|
|
210
|
+
slippageTolerance: 50,
|
|
211
|
+
customRecipientMsg: customRecipientMsg,
|
|
89
212
|
});
|
|
90
213
|
|
|
91
|
-
//
|
|
92
|
-
const depositAddress =
|
|
214
|
+
// Step 5: Get depositAddress and transfer funds to complete MCA creation
|
|
215
|
+
const depositAddress = res_quote.quoteSuccessResult?.quote?.depositAddress;
|
|
216
|
+
// Transfer funds to depositAddress using your wallet
|
|
93
217
|
```
|
|
94
218
|
|
|
95
219
|
### Cross-chain Supply
|
|
@@ -102,14 +226,14 @@ import {
|
|
|
102
226
|
config_near,
|
|
103
227
|
} from "@rhea-finance/cross-chain-sdk";
|
|
104
228
|
|
|
105
|
-
const wallet = format_wallet({ chain
|
|
229
|
+
const wallet = format_wallet({ chain, identityKey });
|
|
106
230
|
const customRecipientMsg = getSupplyCustomRecipientMsg({
|
|
107
231
|
useAsCollateral: true,
|
|
108
232
|
w: wallet,
|
|
109
233
|
});
|
|
110
234
|
|
|
111
235
|
const quoteResult = await intentsQuotation({
|
|
112
|
-
recipient:
|
|
236
|
+
recipient: "rhea00000x.multica.near", // or mca account address
|
|
113
237
|
customRecipientMsg,
|
|
114
238
|
// ... other parameters
|
|
115
239
|
});
|
|
@@ -118,29 +242,423 @@ const quoteResult = await intentsQuotation({
|
|
|
118
242
|
const depositAddress = quoteResult.quoteSuccessResult?.quote?.depositAddress;
|
|
119
243
|
```
|
|
120
244
|
|
|
121
|
-
### Cross-chain
|
|
245
|
+
### Cross-chain Repay
|
|
122
246
|
|
|
123
247
|
```typescript
|
|
124
248
|
import {
|
|
125
|
-
|
|
126
|
-
postMultichainLendingRequests,
|
|
127
|
-
pollingRelayerTransactionResult,
|
|
249
|
+
getRepayCustomRecipientMsg,
|
|
128
250
|
format_wallet,
|
|
129
|
-
|
|
130
|
-
|
|
251
|
+
intentsQuotation,
|
|
252
|
+
config_near,
|
|
131
253
|
} from "@rhea-finance/cross-chain-sdk";
|
|
132
254
|
|
|
255
|
+
const wallet = format_wallet({ chain, identityKey });
|
|
256
|
+
const customRecipientMsg = getRepayCustomRecipientMsg({
|
|
257
|
+
w: wallet,
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
const quoteResult = await intentsQuotation({
|
|
261
|
+
recipient: "rhea00000x.multica.near", // or mca account address
|
|
262
|
+
customRecipientMsg,
|
|
263
|
+
// ... other parameters
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
// Transfer to depositAddress
|
|
267
|
+
const depositAddress = quoteResult.quoteSuccessResult?.quote?.depositAddress;
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
### Cross-chain Borrow
|
|
271
|
+
|
|
272
|
+
```typescript
|
|
273
|
+
// Get simple withdraw data
|
|
274
|
+
// simpleWithdrawData: Extract a portion of assets from user's lending assets to pay the relayer
|
|
275
|
+
// User pays relayer's gas fees and NEAR required for registration
|
|
276
|
+
// Parameters:
|
|
277
|
+
// nearStorageAmount: NEAR storage amount
|
|
278
|
+
// mca: Multi-chain account ID
|
|
279
|
+
// relayerGasFees: Relayer gas fees for different chains
|
|
280
|
+
// assets: Assets data (from getAssets() or batchViews())
|
|
281
|
+
// portfolio: Portfolio data (from getPortfolio() or batchViews())
|
|
282
|
+
const simpleWithdrawData: ISimpleWithdraw | null = await getSimpleWithdrawData({
|
|
283
|
+
nearStorageAmount,
|
|
284
|
+
mca,
|
|
285
|
+
relayerGasFees,
|
|
286
|
+
assets,
|
|
287
|
+
portfolio
|
|
288
|
+
})
|
|
289
|
+
|
|
133
290
|
// Prepare borrow business data
|
|
291
|
+
// Parameters:
|
|
292
|
+
// amountBurrow: Amount of assets to withdraw from lending (precision required by lending contract)
|
|
293
|
+
// amountToken: Amount of assets to withdraw from lending (precision of the token)
|
|
294
|
+
// config: Configuration data (from getConfig() or batchViews())
|
|
295
|
+
// simpleWithdrawData: Simple withdraw data (see above for details)
|
|
134
296
|
const { businessMap, quoteResult } = await prepareBusinessDataOnBorrow({
|
|
135
297
|
mca,
|
|
136
|
-
recipient:
|
|
137
|
-
tokenId,
|
|
138
|
-
originAsset:
|
|
139
|
-
destinationAsset:
|
|
298
|
+
recipient: "0x7dxxx...",
|
|
299
|
+
tokenId: "usdt.tether-token.near",
|
|
300
|
+
originAsset: "nep141:usdt.tether-token.near",
|
|
301
|
+
destinationAsset: "xxxx",
|
|
302
|
+
amountBurrow: simpleWithdrawData?.amountBurrow || "0",
|
|
303
|
+
amountToken: simpleWithdrawData?.amountToken || "0",
|
|
304
|
+
config: config,
|
|
305
|
+
simpleWithdrawData: simpleWithdrawData,
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
const wallet = format_wallet({ chain, identityKey });
|
|
309
|
+
const signedBusiness = await sign_message({
|
|
310
|
+
chain,
|
|
311
|
+
message: serializationObj(businessMap),
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
// Submit multi-chain lending request
|
|
315
|
+
const relayer_result = await postMultichainLendingRequests({
|
|
316
|
+
mca_id: mca,
|
|
317
|
+
wallet: serializationObj(wallet),
|
|
318
|
+
request: [
|
|
319
|
+
serializationObj({
|
|
320
|
+
signer_wallet: wallet,
|
|
321
|
+
business: businessMap,
|
|
322
|
+
signature: signedBusiness,
|
|
323
|
+
attach_deposit: NDeposit(TOKEN_STORAGE_DEPOSIT_READ),
|
|
324
|
+
}),
|
|
325
|
+
],
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
// Poll transaction result
|
|
329
|
+
if (relayer_result?.code == 0) {
|
|
330
|
+
const { status, tx_hash } = await pollingRelayerTransactionResult(
|
|
331
|
+
relayer_result.data,
|
|
332
|
+
2000
|
|
333
|
+
);
|
|
334
|
+
console.log("Transaction status:", status);
|
|
335
|
+
console.log("Transaction hash:", tx_hash);
|
|
336
|
+
}
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
### Cross-chain Withdraw
|
|
340
|
+
|
|
341
|
+
```typescript
|
|
342
|
+
// Get simple withdraw data (see Cross-chain Borrow section for detailed explanation)
|
|
343
|
+
const simpleWithdrawData: ISimpleWithdraw | null = await getSimpleWithdrawData({
|
|
344
|
+
nearStorageAmount,
|
|
345
|
+
mca,
|
|
346
|
+
relayerGasFees,
|
|
347
|
+
assets,
|
|
348
|
+
portfolio
|
|
349
|
+
})
|
|
350
|
+
|
|
351
|
+
// Prepare withdraw business data
|
|
352
|
+
// Parameters:
|
|
353
|
+
// amountBurrow: Amount of assets to withdraw from lending (precision required by lending contract)
|
|
354
|
+
// amountToken: Amount of assets to withdraw from lending (precision of the token)
|
|
355
|
+
// config: Configuration data (from getConfig() or batchViews())
|
|
356
|
+
// simpleWithdrawData: Simple withdraw data (see Cross-chain Borrow section for details)
|
|
357
|
+
// isDecrease: Whether to decrease collateral
|
|
358
|
+
// decreaseCollateralAmount: Amount to decrease collateral (if isDecrease is true)
|
|
359
|
+
const { businessMap, quoteResult } = await prepareBusinessDataOnWithdraw({
|
|
360
|
+
mca,
|
|
361
|
+
recipient: "0x7dxxx...",
|
|
362
|
+
tokenId: "usdt.tether-token.near",
|
|
363
|
+
originAsset: "nep141:usdt.tether-token.near",
|
|
364
|
+
destinationAsset: "xxxx",
|
|
140
365
|
amountBurrow,
|
|
141
366
|
amountToken,
|
|
142
|
-
config,
|
|
367
|
+
config: config,
|
|
143
368
|
simpleWithdrawData,
|
|
369
|
+
isDecrease: false,
|
|
370
|
+
decreaseCollateralAmoun,
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
const wallet = format_wallet({ chain, identityKey });
|
|
374
|
+
const signedBusiness = await sign_message({
|
|
375
|
+
chain,
|
|
376
|
+
message: serializationObj(businessMap),
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
// Submit multi-chain lending request
|
|
380
|
+
const relayer_result = await postMultichainLendingRequests({
|
|
381
|
+
mca_id: mca,
|
|
382
|
+
wallet: serializationObj(wallet),
|
|
383
|
+
request: [
|
|
384
|
+
serializationObj({
|
|
385
|
+
signer_wallet: wallet,
|
|
386
|
+
business: businessMap,
|
|
387
|
+
signature: signedBusiness,
|
|
388
|
+
attach_deposit: NDeposit(TOKEN_STORAGE_DEPOSIT_READ),
|
|
389
|
+
}),
|
|
390
|
+
],
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
// Poll transaction result
|
|
394
|
+
if (relayer_result?.code == 0) {
|
|
395
|
+
const { status, tx_hash } = await pollingRelayerTransactionResult(
|
|
396
|
+
relayer_result.data,
|
|
397
|
+
2000
|
|
398
|
+
);
|
|
399
|
+
console.log("Transaction status:", status);
|
|
400
|
+
console.log("Transaction hash:", tx_hash);
|
|
401
|
+
}
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
### Add Wallet to MCA
|
|
405
|
+
|
|
406
|
+
```typescript
|
|
407
|
+
// Step 1: the new wallet to add
|
|
408
|
+
const newWallet = {
|
|
409
|
+
chain: "xxx",
|
|
410
|
+
identityKey,
|
|
411
|
+
}
|
|
412
|
+
const add_w = format_wallet({
|
|
413
|
+
chain: newWallet.chain,
|
|
414
|
+
identityKey: newWallet.identityKey,
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
// Step 2: Sign the new wallet with the new wallet itself
|
|
418
|
+
// Signing content: mca + serialized wallet
|
|
419
|
+
const signature_new_wallet = await sign_message({
|
|
420
|
+
chain: newWallet.chain,
|
|
421
|
+
message: mca + serializationObj(add_w),
|
|
422
|
+
});
|
|
423
|
+
|
|
424
|
+
// Step 3: Prepare add wallet business data
|
|
425
|
+
// Parameters:
|
|
426
|
+
// mca: Multi-chain account ID
|
|
427
|
+
// w: Wallet to add (formatted using format_wallet)
|
|
428
|
+
// signature_w: Signature of the new wallet (signed by the new wallet itself)
|
|
429
|
+
// gas_token_id: Token ID for gas payment
|
|
430
|
+
// gas_token_amount: Amount of gas token
|
|
431
|
+
const businessMap = await prepareBusinessDataOnAddWallet({
|
|
432
|
+
mca: "rhea00000x.multica.near",
|
|
433
|
+
w: add_w,
|
|
434
|
+
signature_w: signature_new_wallet,
|
|
435
|
+
gas_token_id: "usdt.tether-token.near",
|
|
436
|
+
gas_token_amount: "1000000",
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
// Step 4: Sign the business data with the signer wallet
|
|
440
|
+
const signerWallet = { chain: "xxx", identityKey };
|
|
441
|
+
const sign_w = format_wallet({ signerWallet.chain, signerWallet.identityKey });
|
|
442
|
+
const signedBusiness = await sign_message({
|
|
443
|
+
chain: signerWallet.chain,
|
|
444
|
+
message: serializationObj(businessMap),
|
|
445
|
+
});
|
|
446
|
+
|
|
447
|
+
// Submit multi-chain lending request
|
|
448
|
+
const relayer_result = await postMultichainLendingRequests({
|
|
449
|
+
mca_id: mca,
|
|
450
|
+
wallet: serializationObj(sign_w),
|
|
451
|
+
request: [
|
|
452
|
+
serializationObj({
|
|
453
|
+
signer_wallet: sign_w,
|
|
454
|
+
business: businessMap,
|
|
455
|
+
signature: signedBusiness,
|
|
456
|
+
attach_deposit: NDeposit(TOKEN_STORAGE_DEPOSIT_READ),
|
|
457
|
+
}),
|
|
458
|
+
],
|
|
459
|
+
});
|
|
460
|
+
|
|
461
|
+
// Poll transaction result
|
|
462
|
+
if (relayer_result?.code == 0) {
|
|
463
|
+
const { status, tx_hash } = await pollingRelayerTransactionResult(
|
|
464
|
+
relayer_result.data,
|
|
465
|
+
2000
|
|
466
|
+
);
|
|
467
|
+
console.log("Transaction status:", status);
|
|
468
|
+
console.log("Transaction hash:", tx_hash);
|
|
469
|
+
}
|
|
470
|
+
```
|
|
471
|
+
|
|
472
|
+
### Remove Wallet from MCA
|
|
473
|
+
|
|
474
|
+
```typescript
|
|
475
|
+
// Prepare remove wallet business data
|
|
476
|
+
// Parameters:
|
|
477
|
+
// mca: Multi-chain account ID
|
|
478
|
+
// w: Wallet to remove (formatted using format_wallet)
|
|
479
|
+
// gas_token_id: Token ID for gas payment
|
|
480
|
+
// gas_token_amount: Amount of gas token
|
|
481
|
+
const removeWallet = {
|
|
482
|
+
chain: "xxx",
|
|
483
|
+
identityKey,
|
|
484
|
+
}
|
|
485
|
+
const remove_w = format_wallet({ chain: removeWallet.chain, identityKey: removeWallet.identityKey });
|
|
486
|
+
const businessMap = await prepareBusinessDataOnRemoveWallet({
|
|
487
|
+
mca: "rhea00000x.multica.near",
|
|
488
|
+
w: _removeWallet,
|
|
489
|
+
gas_token_id: "usdt.tether-token.near",
|
|
490
|
+
gas_token_amount: "1000000",
|
|
491
|
+
});
|
|
492
|
+
|
|
493
|
+
// Sign the business data with the signer wallet
|
|
494
|
+
const signerWallet = { chain: "xxx", identityKey };
|
|
495
|
+
const _signerWallet = format_wallet({ chain: signerWallet.chain, identityKey: signerWallet.identityKey });
|
|
496
|
+
const signedBusiness = await sign_message({
|
|
497
|
+
chain: _signerWallet.chain,
|
|
498
|
+
message: serializationObj(businessMap),
|
|
499
|
+
});
|
|
500
|
+
|
|
501
|
+
// Submit multi-chain lending request
|
|
502
|
+
const relayer_result = await postMultichainLendingRequests({
|
|
503
|
+
mca_id: mca,
|
|
504
|
+
wallet: serializationObj(_signerWallet),
|
|
505
|
+
request: [
|
|
506
|
+
serializationObj({
|
|
507
|
+
signer_wallet: _signerWallet,
|
|
508
|
+
business: businessMap,
|
|
509
|
+
signature: signedBusiness,
|
|
510
|
+
attach_deposit: NDeposit(TOKEN_STORAGE_DEPOSIT_READ),
|
|
511
|
+
}),
|
|
512
|
+
],
|
|
513
|
+
});
|
|
514
|
+
|
|
515
|
+
// Poll transaction result
|
|
516
|
+
if (relayer_result?.code == 0) {
|
|
517
|
+
const { status, tx_hash } = await pollingRelayerTransactionResult(
|
|
518
|
+
relayer_result.data,
|
|
519
|
+
2000
|
|
520
|
+
);
|
|
521
|
+
console.log("Transaction status:", status);
|
|
522
|
+
console.log("Transaction hash:", tx_hash);
|
|
523
|
+
}
|
|
524
|
+
```
|
|
525
|
+
|
|
526
|
+
### Adjust Collateral
|
|
527
|
+
|
|
528
|
+
```typescript
|
|
529
|
+
// Get simple withdraw data (see Cross-chain Borrow section for detailed explanation)
|
|
530
|
+
const simpleWithdrawData: ISimpleWithdraw | null = await getSimpleWithdrawData({
|
|
531
|
+
nearStorageAmount,
|
|
532
|
+
mca,
|
|
533
|
+
relayerGasFees,
|
|
534
|
+
assets,
|
|
535
|
+
portfolio
|
|
536
|
+
})
|
|
537
|
+
|
|
538
|
+
// Prepare adjust business data
|
|
539
|
+
// Parameters:
|
|
540
|
+
// mca: Multi-chain account ID
|
|
541
|
+
// tokenId: Token ID to adjust
|
|
542
|
+
// config: Configuration data (from getConfig() or batchViews())
|
|
543
|
+
// simpleWithdrawData: Simple withdraw data (see Cross-chain Borrow section for details)
|
|
544
|
+
// isIncreaseCollateral: Whether to increase collateral
|
|
545
|
+
// increaseAmountBurrow: Amount to increase collateral (if isIncreaseCollateral is true)
|
|
546
|
+
// isDecreaseCollateral: Whether to decrease collateral
|
|
547
|
+
// decreaseAmountBurrow: Amount to decrease collateral (if isDecreaseCollateral is true)
|
|
548
|
+
const businessMap = await prepareBusinessDataOnAdjust({
|
|
549
|
+
mca: "rhea00000x.multica.near",
|
|
550
|
+
tokenId: "usdt.tether-token.near",
|
|
551
|
+
config: config,
|
|
552
|
+
simpleWithdrawData: simpleWithdrawData,
|
|
553
|
+
isIncreaseCollateral: true,
|
|
554
|
+
increaseAmountBurrow,
|
|
555
|
+
isDecreaseCollateral: false,
|
|
556
|
+
decreaseAmountBurrow,
|
|
557
|
+
});
|
|
558
|
+
|
|
559
|
+
const wallet = format_wallet({ chain, identityKey });
|
|
560
|
+
const signedBusiness = await sign_message({
|
|
561
|
+
chain,
|
|
562
|
+
message: serializationObj(businessMap),
|
|
563
|
+
});
|
|
564
|
+
|
|
565
|
+
// Submit multi-chain lending request
|
|
566
|
+
const relayer_result = await postMultichainLendingRequests({
|
|
567
|
+
mca_id: mca,
|
|
568
|
+
wallet: serializationObj(wallet),
|
|
569
|
+
request: [
|
|
570
|
+
serializationObj({
|
|
571
|
+
signer_wallet: wallet,
|
|
572
|
+
business: businessMap,
|
|
573
|
+
signature: signedBusiness,
|
|
574
|
+
attach_deposit: NDeposit(TOKEN_STORAGE_DEPOSIT_READ),
|
|
575
|
+
}),
|
|
576
|
+
],
|
|
577
|
+
});
|
|
578
|
+
|
|
579
|
+
// Poll transaction result
|
|
580
|
+
if (relayer_result?.code == 0) {
|
|
581
|
+
const { status, tx_hash } = await pollingRelayerTransactionResult(
|
|
582
|
+
relayer_result.data,
|
|
583
|
+
2000
|
|
584
|
+
);
|
|
585
|
+
console.log("Transaction status:", status);
|
|
586
|
+
console.log("Transaction hash:", tx_hash);
|
|
587
|
+
}
|
|
588
|
+
```
|
|
589
|
+
|
|
590
|
+
### Repay from Supplied
|
|
591
|
+
|
|
592
|
+
```typescript
|
|
593
|
+
// Get simple withdraw data (see Cross-chain Borrow section for detailed explanation)
|
|
594
|
+
const simpleWithdrawData: ISimpleWithdraw | null = await getSimpleWithdrawData({
|
|
595
|
+
nearStorageAmount,
|
|
596
|
+
mca,
|
|
597
|
+
relayerGasFees,
|
|
598
|
+
assets,
|
|
599
|
+
portfolio
|
|
600
|
+
})
|
|
601
|
+
|
|
602
|
+
// Prepare repay from supplied business data
|
|
603
|
+
// Parameters:
|
|
604
|
+
// mca: Multi-chain account ID
|
|
605
|
+
// tokenId: Token ID to repay
|
|
606
|
+
// config: Configuration data (from getConfig() or batchViews())
|
|
607
|
+
// simpleWithdrawData: Simple withdraw data (see Cross-chain Borrow section for details)
|
|
608
|
+
// amountBurrow: Amount to repay (precision required by lending contract)
|
|
609
|
+
// decreaseAmountBurrow: Amount to decrease collateral (precision required by lending contract)
|
|
610
|
+
const businessMap = await prepareBusinessDataOnRepayFromSupplied({
|
|
611
|
+
mca: "rhea00000x.multica.near",
|
|
612
|
+
tokenId: "usdt.tether-token.near",
|
|
613
|
+
config: config,
|
|
614
|
+
simpleWithdrawData: simpleWithdrawData,
|
|
615
|
+
amountBurrow,
|
|
616
|
+
decreaseAmountBurrow,
|
|
617
|
+
});
|
|
618
|
+
|
|
619
|
+
const wallet = format_wallet({ chain, identityKey });
|
|
620
|
+
const signedBusiness = await sign_message({
|
|
621
|
+
chain,
|
|
622
|
+
message: serializationObj(businessMap),
|
|
623
|
+
});
|
|
624
|
+
|
|
625
|
+
// Submit multi-chain lending request
|
|
626
|
+
const relayer_result = await postMultichainLendingRequests({
|
|
627
|
+
mca_id: mca,
|
|
628
|
+
wallet: serializationObj(wallet),
|
|
629
|
+
request: [
|
|
630
|
+
serializationObj({
|
|
631
|
+
signer_wallet: wallet,
|
|
632
|
+
business: businessMap,
|
|
633
|
+
signature: signedBusiness,
|
|
634
|
+
attach_deposit: NDeposit(TOKEN_STORAGE_DEPOSIT_READ),
|
|
635
|
+
}),
|
|
636
|
+
],
|
|
637
|
+
});
|
|
638
|
+
|
|
639
|
+
// Poll transaction result
|
|
640
|
+
if (relayer_result?.code == 0) {
|
|
641
|
+
const { status, tx_hash } = await pollingRelayerTransactionResult(
|
|
642
|
+
relayer_result.data,
|
|
643
|
+
2000
|
|
644
|
+
);
|
|
645
|
+
console.log("Transaction status:", status);
|
|
646
|
+
console.log("Transaction hash:", tx_hash);
|
|
647
|
+
}
|
|
648
|
+
```
|
|
649
|
+
|
|
650
|
+
### Claim Rewards
|
|
651
|
+
|
|
652
|
+
```typescript
|
|
653
|
+
// Prepare claim business data
|
|
654
|
+
// Parameters:
|
|
655
|
+
// mca: Multi-chain account ID
|
|
656
|
+
// gas_token_id: Token ID for gas payment
|
|
657
|
+
// gas_token_amount: Amount of gas token
|
|
658
|
+
const businessMap = await prepareBusinessDataOnClaim({
|
|
659
|
+
mca: "rhea00000x.multica.near",
|
|
660
|
+
gas_token_id: "usdt.tether-token.near",
|
|
661
|
+
gas_token_amount: "1000000",
|
|
144
662
|
});
|
|
145
663
|
|
|
146
664
|
const wallet = format_wallet({ chain, identityKey });
|
|
@@ -209,10 +727,7 @@ if (relayer_result?.code == 0) {
|
|
|
209
727
|
- `getLiquidations` - Get liquidation information
|
|
210
728
|
- `getMultichainLendingHistory` - Get multi-chain lending history
|
|
211
729
|
|
|
212
|
-
### Utility Functions
|
|
213
|
-
|
|
214
730
|
#### Health Factor Calculation
|
|
215
|
-
|
|
216
731
|
- `recomputeHealthFactorSupply` - Calculate health factor after supply
|
|
217
732
|
- `recomputeHealthFactorBorrow` - Calculate health factor after borrow
|
|
218
733
|
- `recomputeHealthFactorRepay` - Calculate health factor after repay
|
|
@@ -220,23 +735,36 @@ if (relayer_result?.code == 0) {
|
|
|
220
735
|
- `recomputeHealthFactorAdjust` - Calculate health factor after adjusting collateral
|
|
221
736
|
|
|
222
737
|
#### Maximum Available Amount
|
|
223
|
-
|
|
224
738
|
- `getBorrowMaxAmount` - Get maximum borrowable amount
|
|
225
739
|
- `getWithdrawMaxAmount` - Get maximum withdrawable amount
|
|
226
740
|
|
|
227
|
-
####
|
|
741
|
+
#### Core Utilities
|
|
742
|
+
|
|
743
|
+
**Lending Operations**
|
|
744
|
+
- `prepareBusinessDataOnBorrow` - Prepare borrow business data
|
|
745
|
+
- `prepareBusinessDataOnWithdraw` - Prepare withdraw business data
|
|
746
|
+
- `prepareBusinessDataOnAdjust` - Prepare adjust collateral business data
|
|
747
|
+
- `prepareBusinessDataOnRepayFromSupplied` - Prepare repay from supplied business data
|
|
748
|
+
- `prepareBusinessDataOninnerWithdraw` - Prepare inner withdraw business data
|
|
749
|
+
|
|
750
|
+
**Custom Recipient Messages**
|
|
751
|
+
- `getCreateMcaCustomRecipientMsg` - Get custom recipient message for creating MCA
|
|
752
|
+
- `getSupplyCustomRecipientMsg` - Get custom recipient message for supply
|
|
753
|
+
- `getRepayCustomRecipientMsg` - Get custom recipient message for repay
|
|
754
|
+
|
|
755
|
+
**Account Management**
|
|
756
|
+
- `prepareBusinessDataOnAddWallet` - Prepare add wallet business data
|
|
757
|
+
- `prepareBusinessDataOnRemoveWallet` - Prepare remove wallet business data
|
|
758
|
+
- `prepareBusinessDataOnClaim` - Prepare claim rewards business data
|
|
759
|
+
|
|
228
760
|
|
|
229
|
-
|
|
761
|
+
**General Utilities**
|
|
230
762
|
- `format_wallet` - Format wallet address
|
|
231
763
|
- `serializationObj` - Serialize object
|
|
232
764
|
- `computeRelayerGas` - Calculate relayer gas fee
|
|
233
765
|
- `pollingTransactionStatus` - Poll transaction status
|
|
234
766
|
- `postMultichainLendingRequests` - Submit multi-chain lending request
|
|
235
767
|
- `pollingRelayerTransactionResult` - Poll relayer transaction result
|
|
236
|
-
- `prepareBusinessDataOnBorrow` - Prepare borrow business data
|
|
237
|
-
- `getCreateMcaCustomRecipientMsg` - Get custom recipient message for creating MCA
|
|
238
|
-
- `getSupplyCustomRecipientMsg` - Get custom recipient message for supply
|
|
239
|
-
- `getRepayCustomRecipientMsg` - Get custom recipient message for repay
|
|
240
768
|
|
|
241
769
|
### Chain Interaction
|
|
242
770
|
|
|
@@ -259,34 +787,6 @@ type IChain = "evm" | "solana" | "btc";
|
|
|
259
787
|
// Wallet type
|
|
260
788
|
type IWallet = { EVM: string } | { Solana: string } | { Bitcoin: string };
|
|
261
789
|
|
|
262
|
-
// Business data
|
|
263
|
-
interface IBusiness {
|
|
264
|
-
nonce: string;
|
|
265
|
-
deadline: string;
|
|
266
|
-
tx_requests: ITxRequest[];
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
// Account positions detailed
|
|
270
|
-
interface IAccountAllPositionsDetailed {
|
|
271
|
-
supplied: IPortfolioAssetOrigin[];
|
|
272
|
-
positions: IPositionsOrigin;
|
|
273
|
-
farms: IFarm[];
|
|
274
|
-
booster_staking: IBoosterStaking;
|
|
275
|
-
booster_stakings: IBoosterStaking[];
|
|
276
|
-
has_non_farmed_assets: boolean;
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
// Portfolio
|
|
280
|
-
interface Portfolio {
|
|
281
|
-
supplied: { [tokenId: string]: PortfolioAsset };
|
|
282
|
-
collateral: { [tokenId: string]: PortfolioAsset };
|
|
283
|
-
borrowed: { [tokenId: string]: PortfolioAsset };
|
|
284
|
-
positions: IPositions;
|
|
285
|
-
farms: IFarm[];
|
|
286
|
-
staking: IBoosterStaking;
|
|
287
|
-
stakings: IBoosterStaking[];
|
|
288
|
-
hasNonFarmedAssets: boolean;
|
|
289
|
-
}
|
|
290
790
|
```
|
|
291
791
|
|
|
292
792
|
## Usage Examples
|
|
@@ -312,70 +812,6 @@ async function fetchAccountData(mcaId: string) {
|
|
|
312
812
|
};
|
|
313
813
|
}
|
|
314
814
|
```
|
|
315
|
-
|
|
316
|
-
### Example 2: Cross-chain Supply (from EVM to NEAR)
|
|
317
|
-
|
|
318
|
-
```typescript
|
|
319
|
-
import {
|
|
320
|
-
getSupplyCustomRecipientMsg,
|
|
321
|
-
format_wallet,
|
|
322
|
-
intentsQuotation,
|
|
323
|
-
config_near,
|
|
324
|
-
} from "@rhea-finance/cross-chain-sdk";
|
|
325
|
-
|
|
326
|
-
async function supplyFromEVM(
|
|
327
|
-
mca: string,
|
|
328
|
-
chain: IChain,
|
|
329
|
-
identityKey: string,
|
|
330
|
-
amount: string,
|
|
331
|
-
symbol: string
|
|
332
|
-
) {
|
|
333
|
-
const wallet = format_wallet({ chain, identityKey });
|
|
334
|
-
const customRecipientMsg = getSupplyCustomRecipientMsg({
|
|
335
|
-
useAsCollateral: true,
|
|
336
|
-
w: wallet,
|
|
337
|
-
});
|
|
338
|
-
|
|
339
|
-
const quoteResult = await intentsQuotation({
|
|
340
|
-
recipient: mca || config_near.AM_CONTRACT,
|
|
341
|
-
customRecipientMsg,
|
|
342
|
-
// ... other required parameters
|
|
343
|
-
});
|
|
344
|
-
|
|
345
|
-
const depositAddress = quoteResult.quoteSuccessResult?.quote?.depositAddress;
|
|
346
|
-
|
|
347
|
-
// Execute transfer to depositAddress
|
|
348
|
-
// ...
|
|
349
|
-
}
|
|
350
|
-
```
|
|
351
|
-
|
|
352
|
-
### Example 3: Calculate Health Factor
|
|
353
|
-
|
|
354
|
-
```typescript
|
|
355
|
-
import {
|
|
356
|
-
recomputeHealthFactorBorrow,
|
|
357
|
-
IAccountAllPositionsDetailed,
|
|
358
|
-
} from "@rhea-finance/cross-chain-sdk";
|
|
359
|
-
|
|
360
|
-
function checkBorrowSafety(
|
|
361
|
-
accountPositions: IAccountAllPositionsDetailed,
|
|
362
|
-
borrowAmount: string,
|
|
363
|
-
tokenId: string,
|
|
364
|
-
assets: Assets
|
|
365
|
-
) {
|
|
366
|
-
const newHealthFactor = recomputeHealthFactorBorrow({
|
|
367
|
-
account_all_positions: accountPositions,
|
|
368
|
-
borrow_amount: borrowAmount,
|
|
369
|
-
token_id: tokenId,
|
|
370
|
-
assets,
|
|
371
|
-
});
|
|
372
|
-
|
|
373
|
-
if (newHealthFactor < 1.0) {
|
|
374
|
-
throw new Error("Borrow would cause liquidation");
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
return newHealthFactor;
|
|
378
|
-
}
|
|
379
815
|
```
|
|
380
816
|
|
|
381
817
|
## Development
|
|
@@ -421,7 +857,7 @@ MIT
|
|
|
421
857
|
|
|
422
858
|
## Related Links
|
|
423
859
|
|
|
424
|
-
- Demo Project: [cross-chain-demo](
|
|
860
|
+
- Demo Project: [cross-chain-demo](https://github.com/xieqiancaosissi/cross-chain-demo)
|
|
425
861
|
- Rhea Finance: https://rhea.finance
|
|
426
862
|
|
|
427
863
|
## Contributing
|