@rhea-finance/cross-chain-sdk 0.1.0
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 +429 -0
- package/dist/index.cjs +2466 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1039 -0
- package/dist/index.d.ts +1039 -0
- package/dist/index.js +2369 -0
- package/dist/index.js.map +1 -0
- package/package.json +62 -0
package/README.md
ADDED
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
# @rhea-finance/cross-chain-sdk
|
|
2
|
+
|
|
3
|
+
Cross-chain lending SDK that supports unified lending operations across multiple blockchains, including EVM chains, Solana, Bitcoin, and NEAR.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🔗 **Cross-chain Support**: Supports multi-chain operations on EVM, Solana, Bitcoin, and NEAR
|
|
8
|
+
- 💰 **Lending Functions**: Provides complete lending functionality including Supply, Borrow, Repay, Withdraw, etc.
|
|
9
|
+
- 📊 **Data Queries**: Supports querying asset information, portfolio, prices, balances, and other data
|
|
10
|
+
- 🏥 **Health Factor**: Automatically calculates and manages lending health factors
|
|
11
|
+
- 💼 **Multi-chain Account (MCA)**: Supports creating and managing multi-chain accounts to unify multi-chain asset management
|
|
12
|
+
- 🔐 **Wallet Management**: Supports adding and removing multi-chain wallets
|
|
13
|
+
- 📈 **Liquidity Mining**: Supports liquidity mining and reward queries
|
|
14
|
+
- ⚡ **Batch Queries**: Provides batch view queries to reduce RPC call frequency
|
|
15
|
+
|
|
16
|
+
## Supported Blockchains
|
|
17
|
+
|
|
18
|
+
- **EVM Chains**: Ethereum, Arbitrum, Optimism, Base, Avalanche, BNB Chain, etc.
|
|
19
|
+
- **Solana**
|
|
20
|
+
- **Bitcoin**
|
|
21
|
+
- **NEAR Protocol**
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install @rhea-finance/cross-chain-sdk
|
|
27
|
+
# or
|
|
28
|
+
pnpm add @rhea-finance/cross-chain-sdk
|
|
29
|
+
# or
|
|
30
|
+
yarn add @rhea-finance/cross-chain-sdk
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Quick Start
|
|
34
|
+
|
|
35
|
+
### Basic Usage
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
import {
|
|
39
|
+
batchViews,
|
|
40
|
+
getPortfolio,
|
|
41
|
+
getAssets,
|
|
42
|
+
getPrices,
|
|
43
|
+
config_near,
|
|
44
|
+
IChain,
|
|
45
|
+
} from "@rhea-finance/cross-chain-sdk";
|
|
46
|
+
|
|
47
|
+
// Query batch view data
|
|
48
|
+
const lendingData = await batchViews(accountId);
|
|
49
|
+
console.log(lendingData.account_all_positions);
|
|
50
|
+
console.log(lendingData.assets_paged_detailed);
|
|
51
|
+
console.log(lendingData.config);
|
|
52
|
+
|
|
53
|
+
// Get portfolio
|
|
54
|
+
const portfolio = getPortfolio(accountPositions);
|
|
55
|
+
console.log(portfolio.supplied);
|
|
56
|
+
console.log(portfolio.collateral);
|
|
57
|
+
console.log(portfolio.borrowed);
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Create Multi-chain Account (MCA)
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
import {
|
|
64
|
+
getCreateMcaCustomRecipientMsg,
|
|
65
|
+
format_wallet,
|
|
66
|
+
intentsQuotation,
|
|
67
|
+
IChain,
|
|
68
|
+
} from "@rhea-finance/cross-chain-sdk";
|
|
69
|
+
|
|
70
|
+
// 1. Prepare wallet list
|
|
71
|
+
const wallets = chains.map((chain) => format_wallet({ chain, identityKey }));
|
|
72
|
+
|
|
73
|
+
// 2. Sign the wallet list
|
|
74
|
+
const signedMessages = await Promise.all(
|
|
75
|
+
wallets.map((wallet) => sign_message({ chain, message: wallets }))
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
// 3. Get deposit address
|
|
79
|
+
const customRecipientMsg = getCreateMcaCustomRecipientMsg({
|
|
80
|
+
useAsCollateral: true,
|
|
81
|
+
wallets,
|
|
82
|
+
signedMessages,
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
const quoteResult = await intentsQuotation({
|
|
86
|
+
recipient: config_near.AM_CONTRACT,
|
|
87
|
+
customRecipientMsg,
|
|
88
|
+
// ... other parameters
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// 4. Transfer to depositAddress
|
|
92
|
+
const depositAddress = quoteResult.quoteSuccessResult?.quote?.depositAddress;
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Cross-chain Supply
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
import {
|
|
99
|
+
getSupplyCustomRecipientMsg,
|
|
100
|
+
format_wallet,
|
|
101
|
+
intentsQuotation,
|
|
102
|
+
config_near,
|
|
103
|
+
} from "@rhea-finance/cross-chain-sdk";
|
|
104
|
+
|
|
105
|
+
const wallet = format_wallet({ chain: "evm", identityKey });
|
|
106
|
+
const customRecipientMsg = getSupplyCustomRecipientMsg({
|
|
107
|
+
useAsCollateral: true,
|
|
108
|
+
w: wallet,
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
const quoteResult = await intentsQuotation({
|
|
112
|
+
recipient: config_near.AM_CONTRACT, // or mca account address
|
|
113
|
+
customRecipientMsg,
|
|
114
|
+
// ... other parameters
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
// Transfer to depositAddress
|
|
118
|
+
const depositAddress = quoteResult.quoteSuccessResult?.quote?.depositAddress;
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### Cross-chain Borrow
|
|
122
|
+
|
|
123
|
+
```typescript
|
|
124
|
+
import {
|
|
125
|
+
prepareBusinessDataOnBorrow,
|
|
126
|
+
postMultichainLendingRequests,
|
|
127
|
+
pollingRelayerTransactionResult,
|
|
128
|
+
format_wallet,
|
|
129
|
+
serializationObj,
|
|
130
|
+
NDeposit,
|
|
131
|
+
} from "@rhea-finance/cross-chain-sdk";
|
|
132
|
+
|
|
133
|
+
// Prepare borrow business data
|
|
134
|
+
const { businessMap, quoteResult } = await prepareBusinessDataOnBorrow({
|
|
135
|
+
mca,
|
|
136
|
+
recipient: outChainAccountId,
|
|
137
|
+
tokenId,
|
|
138
|
+
originAsset: nearChainAsset,
|
|
139
|
+
destinationAsset: outChainAsset,
|
|
140
|
+
amountBurrow,
|
|
141
|
+
amountToken,
|
|
142
|
+
config,
|
|
143
|
+
simpleWithdrawData,
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
const wallet = format_wallet({ chain, identityKey });
|
|
147
|
+
const signedBusiness = await sign_message({
|
|
148
|
+
chain,
|
|
149
|
+
message: serializationObj(businessMap),
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
// Submit multi-chain lending request
|
|
153
|
+
const relayer_result = await postMultichainLendingRequests({
|
|
154
|
+
mca_id: mca,
|
|
155
|
+
wallet: serializationObj(wallet),
|
|
156
|
+
request: [
|
|
157
|
+
serializationObj({
|
|
158
|
+
signer_wallet: wallet,
|
|
159
|
+
business: businessMap,
|
|
160
|
+
signature: signedBusiness,
|
|
161
|
+
attach_deposit: NDeposit(TOKEN_STORAGE_DEPOSIT_READ),
|
|
162
|
+
}),
|
|
163
|
+
],
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
// Poll transaction result
|
|
167
|
+
if (relayer_result?.code == 0) {
|
|
168
|
+
const { status, tx_hash } = await pollingRelayerTransactionResult(
|
|
169
|
+
relayer_result.data,
|
|
170
|
+
2000
|
|
171
|
+
);
|
|
172
|
+
console.log("Transaction status:", status);
|
|
173
|
+
console.log("Transaction hash:", tx_hash);
|
|
174
|
+
}
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
## Core API
|
|
178
|
+
|
|
179
|
+
### Actions
|
|
180
|
+
|
|
181
|
+
#### Account Management
|
|
182
|
+
|
|
183
|
+
- `createMca` - Create multi-chain account
|
|
184
|
+
- `addWallet` - Add wallet to MCA
|
|
185
|
+
- `removeWallet` - Remove wallet from MCA
|
|
186
|
+
|
|
187
|
+
#### Lending Operations
|
|
188
|
+
|
|
189
|
+
- `supply` - Supply (deposit)
|
|
190
|
+
- `borrow` - Borrow
|
|
191
|
+
- `repay` - Repay
|
|
192
|
+
- `repayFromSupplied` - Repay from supplied assets
|
|
193
|
+
- `withdraw` - Withdraw
|
|
194
|
+
- `adjust` - Adjust collateral
|
|
195
|
+
- `innnerWithdraw` - Inner withdraw
|
|
196
|
+
- `claim` - Claim rewards
|
|
197
|
+
|
|
198
|
+
### Views
|
|
199
|
+
|
|
200
|
+
- `batchViews` - Batch query views (account, assets, config, etc.)
|
|
201
|
+
- `getPortfolio` - Get portfolio
|
|
202
|
+
- `getAssets` - Get asset list
|
|
203
|
+
- `getPrices` - Get price information
|
|
204
|
+
- `getBalance` - Get balance
|
|
205
|
+
- `getFarms` - Get liquidity mining information
|
|
206
|
+
- `getConfig` - Get configuration
|
|
207
|
+
- `getBoosterTokens` - Get booster token information
|
|
208
|
+
- `getTokenDetail` - Get token details
|
|
209
|
+
- `getLiquidations` - Get liquidation information
|
|
210
|
+
- `getMultichainLendingHistory` - Get multi-chain lending history
|
|
211
|
+
|
|
212
|
+
### Utility Functions
|
|
213
|
+
|
|
214
|
+
#### Health Factor Calculation
|
|
215
|
+
|
|
216
|
+
- `recomputeHealthFactorSupply` - Calculate health factor after supply
|
|
217
|
+
- `recomputeHealthFactorBorrow` - Calculate health factor after borrow
|
|
218
|
+
- `recomputeHealthFactorRepay` - Calculate health factor after repay
|
|
219
|
+
- `recomputeHealthFactorWithdraw` - Calculate health factor after withdraw
|
|
220
|
+
- `recomputeHealthFactorAdjust` - Calculate health factor after adjusting collateral
|
|
221
|
+
|
|
222
|
+
#### Maximum Available Amount
|
|
223
|
+
|
|
224
|
+
- `getBorrowMaxAmount` - Get maximum borrowable amount
|
|
225
|
+
- `getWithdrawMaxAmount` - Get maximum withdrawable amount
|
|
226
|
+
|
|
227
|
+
#### Other Utilities
|
|
228
|
+
|
|
229
|
+
- `intentsQuotation` - Get cross-chain intent quotation
|
|
230
|
+
- `format_wallet` - Format wallet address
|
|
231
|
+
- `serializationObj` - Serialize object
|
|
232
|
+
- `computeRelayerGas` - Calculate relayer gas fee
|
|
233
|
+
- `pollingTransactionStatus` - Poll transaction status
|
|
234
|
+
- `postMultichainLendingRequests` - Submit multi-chain lending request
|
|
235
|
+
- `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
|
+
|
|
241
|
+
### Chain Interaction
|
|
242
|
+
|
|
243
|
+
- `view_on_near` - Call NEAR contract view method
|
|
244
|
+
- `getAccountBalance` - Get NEAR account balance
|
|
245
|
+
|
|
246
|
+
### Chain Configuration
|
|
247
|
+
|
|
248
|
+
- `config_near` - NEAR chain configuration
|
|
249
|
+
- `config_evm` - EVM chain configuration
|
|
250
|
+
- `config_solana` - Solana chain configuration
|
|
251
|
+
- `config_btc` - Bitcoin chain configuration
|
|
252
|
+
|
|
253
|
+
### Type Definitions
|
|
254
|
+
|
|
255
|
+
```typescript
|
|
256
|
+
// Chain type
|
|
257
|
+
type IChain = "evm" | "solana" | "btc";
|
|
258
|
+
|
|
259
|
+
// Wallet type
|
|
260
|
+
type IWallet = { EVM: string } | { Solana: string } | { Bitcoin: string };
|
|
261
|
+
|
|
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
|
+
```
|
|
291
|
+
|
|
292
|
+
## Usage Examples
|
|
293
|
+
|
|
294
|
+
Check out the [cross-chain-demo](./../cross-chain-demo) project for more complete usage examples.
|
|
295
|
+
|
|
296
|
+
### Example 1: Query Account Data
|
|
297
|
+
|
|
298
|
+
```typescript
|
|
299
|
+
import { batchViews, getPortfolio } from "@rhea-finance/cross-chain-sdk";
|
|
300
|
+
|
|
301
|
+
async function fetchAccountData(mcaId: string) {
|
|
302
|
+
// Batch query
|
|
303
|
+
const lendingData = await batchViews(mcaId);
|
|
304
|
+
|
|
305
|
+
// Convert to portfolio format
|
|
306
|
+
const portfolio = getPortfolio(lendingData.account_all_positions);
|
|
307
|
+
|
|
308
|
+
return {
|
|
309
|
+
assets: lendingData.assets_paged_detailed,
|
|
310
|
+
config: lendingData.config,
|
|
311
|
+
portfolio,
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
```
|
|
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
|
+
```
|
|
380
|
+
|
|
381
|
+
## Development
|
|
382
|
+
|
|
383
|
+
### Build
|
|
384
|
+
|
|
385
|
+
```bash
|
|
386
|
+
pnpm build
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
### Development Mode (watch file changes)
|
|
390
|
+
|
|
391
|
+
```bash
|
|
392
|
+
pnpm dev
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
### Type Check
|
|
396
|
+
|
|
397
|
+
```bash
|
|
398
|
+
pnpm type-check
|
|
399
|
+
```
|
|
400
|
+
|
|
401
|
+
### Code Formatting
|
|
402
|
+
|
|
403
|
+
```bash
|
|
404
|
+
pnpm prettier:fix
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
## Dependencies
|
|
408
|
+
|
|
409
|
+
Main dependencies include:
|
|
410
|
+
|
|
411
|
+
- `ethers` - EVM chain interaction
|
|
412
|
+
- `@solana/web3.js` - Solana chain interaction
|
|
413
|
+
- `near-api-js` - NEAR chain interaction
|
|
414
|
+
- `btc-wallet` - Bitcoin wallet support
|
|
415
|
+
- `bignumber.js` / `big.js` / `decimal.js` - Big number calculations
|
|
416
|
+
- `lodash` - Utility functions
|
|
417
|
+
|
|
418
|
+
## License
|
|
419
|
+
|
|
420
|
+
MIT
|
|
421
|
+
|
|
422
|
+
## Related Links
|
|
423
|
+
|
|
424
|
+
- Demo Project: [cross-chain-demo](./../cross-chain-demo)
|
|
425
|
+
- Rhea Finance: https://rhea.finance
|
|
426
|
+
|
|
427
|
+
## Contributing
|
|
428
|
+
|
|
429
|
+
Issues and Pull Requests are welcome!
|