defi-dash-sdk 0.1.3 → 0.1.5
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/LICENSE +7 -0
- package/README.md +199 -97
- package/dist/esm/index.js +40 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/protocols/base-adapter.js +78 -0
- package/dist/esm/protocols/base-adapter.js.map +1 -0
- package/dist/esm/protocols/index.js +8 -0
- package/dist/esm/protocols/index.js.map +1 -0
- package/dist/esm/protocols/navi/adapter.js +393 -0
- package/dist/esm/protocols/navi/adapter.js.map +1 -0
- package/dist/{protocols/scallop.js → esm/protocols/scallop/adapter.js} +252 -95
- package/dist/esm/protocols/scallop/adapter.js.map +1 -0
- package/dist/{lib/scallop/flash-loan-client.js → esm/protocols/scallop/flash-loan.js} +63 -8
- package/dist/esm/protocols/scallop/flash-loan.js.map +1 -0
- package/dist/esm/protocols/scallop/index.js +6 -0
- package/dist/esm/protocols/scallop/index.js.map +1 -0
- package/dist/esm/protocols/scallop/types.js +67 -0
- package/dist/esm/protocols/scallop/types.js.map +1 -0
- package/dist/esm/protocols/suilend/adapter.js +464 -0
- package/dist/esm/protocols/suilend/adapter.js.map +1 -0
- package/dist/{lib → esm/protocols}/suilend/calculators.js +41 -51
- package/dist/esm/protocols/suilend/calculators.js.map +1 -0
- package/dist/esm/protocols/suilend/constants.js +159 -0
- package/dist/{lib/suilend/const.js.map → esm/protocols/suilend/constants.js.map} +1 -1
- package/dist/{lib/suilend/suilend.js → esm/protocols/suilend/types.js} +7 -13
- package/dist/esm/protocols/suilend/types.js.map +1 -0
- package/dist/esm/sdk.js +569 -0
- package/dist/esm/sdk.js.map +1 -0
- package/dist/esm/strategies/common.js +75 -0
- package/dist/esm/strategies/common.js.map +1 -0
- package/dist/esm/strategies/deleverage.js +218 -0
- package/dist/esm/strategies/deleverage.js.map +1 -0
- package/dist/esm/strategies/index.js +10 -0
- package/dist/esm/strategies/index.js.map +1 -0
- package/dist/esm/strategies/leverage-preview.js +117 -0
- package/dist/esm/strategies/leverage-preview.js.map +1 -0
- package/dist/esm/strategies/leverage-route.js +109 -0
- package/dist/esm/strategies/leverage-route.js.map +1 -0
- package/dist/esm/strategies/leverage.js +156 -0
- package/dist/esm/strategies/leverage.js.map +1 -0
- package/dist/esm/strategies/scallop-leverage.js +140 -0
- package/dist/esm/strategies/scallop-leverage.js.map +1 -0
- package/dist/esm/types/common.js +13 -0
- package/dist/esm/types/common.js.map +1 -0
- package/dist/esm/types/config.js +5 -0
- package/dist/esm/types/config.js.map +1 -0
- package/dist/esm/types/constants.js +128 -0
- package/dist/esm/types/constants.js.map +1 -0
- package/dist/esm/types/index.js +10 -0
- package/dist/esm/types/index.js.map +1 -0
- package/dist/esm/types/position.js +5 -0
- package/dist/esm/types/position.js.map +1 -0
- package/dist/esm/types/protocol.js +5 -0
- package/dist/esm/types/protocol.js.map +1 -0
- package/dist/esm/types/strategy.js +5 -0
- package/dist/esm/types/strategy.js.map +1 -0
- package/dist/esm/utils/calculations.js +33 -0
- package/dist/esm/utils/calculations.js.map +1 -0
- package/dist/esm/utils/coin.js +54 -0
- package/dist/esm/utils/coin.js.map +1 -0
- package/dist/esm/utils/errors.js +111 -0
- package/dist/esm/utils/errors.js.map +1 -0
- package/dist/esm/utils/execution.js +106 -0
- package/dist/esm/utils/execution.js.map +1 -0
- package/dist/{lib → esm}/utils/format.js +2 -6
- package/dist/esm/utils/format.js.map +1 -0
- package/dist/esm/utils/gas.js +38 -0
- package/dist/esm/utils/gas.js.map +1 -0
- package/dist/esm/utils/index.js +13 -0
- package/dist/esm/utils/index.js.map +1 -0
- package/dist/{lib → esm}/utils/logger.js +17 -31
- package/dist/esm/utils/logger.js.map +1 -0
- package/dist/index.d.ts +19 -11
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +24 -40
- package/dist/index.js.map +1 -1
- package/dist/protocols/base-adapter.d.ts +42 -75
- package/dist/protocols/base-adapter.d.ts.map +1 -1
- package/dist/protocols/base-adapter.js +34 -77
- package/dist/protocols/base-adapter.js.map +1 -1
- package/dist/protocols/navi/adapter.d.ts +22 -2
- package/dist/protocols/navi/adapter.d.ts.map +1 -1
- package/dist/protocols/navi/adapter.js +145 -11
- package/dist/protocols/navi/adapter.js.map +1 -1
- package/dist/protocols/scallop/adapter.d.ts +26 -6
- package/dist/protocols/scallop/adapter.d.ts.map +1 -1
- package/dist/protocols/scallop/adapter.js +206 -43
- package/dist/protocols/scallop/adapter.js.map +1 -1
- package/dist/protocols/scallop/flash-loan.d.ts +18 -3
- package/dist/protocols/scallop/flash-loan.d.ts.map +1 -1
- package/dist/protocols/scallop/flash-loan.js +79 -20
- package/dist/protocols/scallop/flash-loan.js.map +1 -1
- package/dist/protocols/scallop/types.d.ts.map +1 -1
- package/dist/protocols/scallop/types.js +4 -1
- package/dist/protocols/scallop/types.js.map +1 -1
- package/dist/protocols/suilend/adapter.d.ts +19 -1
- package/dist/protocols/suilend/adapter.d.ts.map +1 -1
- package/dist/protocols/suilend/adapter.js +196 -51
- package/dist/protocols/suilend/adapter.js.map +1 -1
- package/dist/sdk.d.ts +107 -148
- package/dist/sdk.d.ts.map +1 -1
- package/dist/sdk.js +171 -523
- package/dist/sdk.js.map +1 -1
- package/dist/strategies/common.d.ts +42 -0
- package/dist/strategies/common.d.ts.map +1 -0
- package/dist/strategies/common.js +81 -0
- package/dist/strategies/common.js.map +1 -0
- package/dist/strategies/deleverage.d.ts.map +1 -1
- package/dist/strategies/deleverage.js +44 -62
- package/dist/strategies/deleverage.js.map +1 -1
- package/dist/strategies/index.d.ts +9 -1
- package/dist/strategies/index.d.ts.map +1 -1
- package/dist/strategies/index.js +11 -2
- package/dist/strategies/index.js.map +1 -1
- package/dist/strategies/leverage-preview.d.ts +27 -0
- package/dist/strategies/leverage-preview.d.ts.map +1 -0
- package/dist/strategies/leverage-preview.js +120 -0
- package/dist/strategies/leverage-preview.js.map +1 -0
- package/dist/strategies/leverage-route.d.ts +29 -0
- package/dist/strategies/leverage-route.d.ts.map +1 -0
- package/dist/strategies/leverage-route.js +112 -0
- package/dist/strategies/leverage-route.js.map +1 -0
- package/dist/strategies/leverage.d.ts +4 -44
- package/dist/strategies/leverage.d.ts.map +1 -1
- package/dist/strategies/leverage.js +43 -112
- package/dist/strategies/leverage.js.map +1 -1
- package/dist/strategies/scallop-leverage.d.ts +34 -0
- package/dist/strategies/scallop-leverage.d.ts.map +1 -0
- package/dist/strategies/scallop-leverage.js +143 -0
- package/dist/strategies/scallop-leverage.js.map +1 -0
- package/dist/types/config.d.ts +0 -7
- package/dist/types/config.d.ts.map +1 -1
- package/dist/types/constants.d.ts +8 -0
- package/dist/types/constants.d.ts.map +1 -1
- package/dist/types/constants.js +9 -1
- package/dist/types/constants.js.map +1 -1
- package/dist/types/index.d.ts +3 -3
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +2 -1
- package/dist/types/index.js.map +1 -1
- package/dist/types/position.d.ts +7 -3
- package/dist/types/position.d.ts.map +1 -1
- package/dist/types/protocol.d.ts +101 -0
- package/dist/types/protocol.d.ts.map +1 -1
- package/dist/types/strategy.d.ts +82 -38
- package/dist/types/strategy.d.ts.map +1 -1
- package/dist/utils/calculations.d.ts +10 -137
- package/dist/utils/calculations.d.ts.map +1 -1
- package/dist/utils/calculations.js +11 -162
- package/dist/utils/calculations.js.map +1 -1
- package/dist/utils/coin.d.ts +16 -7
- package/dist/utils/coin.d.ts.map +1 -1
- package/dist/utils/coin.js +32 -25
- package/dist/utils/coin.js.map +1 -1
- package/dist/utils/errors.d.ts +0 -18
- package/dist/utils/errors.d.ts.map +1 -1
- package/dist/utils/errors.js +2 -40
- package/dist/utils/errors.js.map +1 -1
- package/dist/utils/execution.d.ts +38 -0
- package/dist/utils/execution.d.ts.map +1 -0
- package/dist/utils/execution.js +110 -0
- package/dist/utils/execution.js.map +1 -0
- package/dist/utils/gas.d.ts +0 -37
- package/dist/utils/gas.d.ts.map +1 -1
- package/dist/utils/gas.js +2 -60
- package/dist/utils/gas.js.map +1 -1
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +1 -1
- package/dist/utils/index.js.map +1 -1
- package/package.json +25 -46
- package/dist/lib/navi_calculations.d.ts +0 -76
- package/dist/lib/navi_calculations.d.ts.map +0 -1
- package/dist/lib/navi_calculations.js +0 -185
- package/dist/lib/navi_calculations.js.map +0 -1
- package/dist/lib/scallop/flash-loan-client.d.ts +0 -88
- package/dist/lib/scallop/flash-loan-client.d.ts.map +0 -1
- package/dist/lib/scallop/flash-loan-client.js.map +0 -1
- package/dist/lib/scallop/flash-loan.d.ts +0 -30
- package/dist/lib/scallop/flash-loan.d.ts.map +0 -1
- package/dist/lib/scallop/flash-loan.js +0 -61
- package/dist/lib/scallop/flash-loan.js.map +0 -1
- package/dist/lib/scallop/index.d.ts +0 -3
- package/dist/lib/scallop/index.d.ts.map +0 -1
- package/dist/lib/scallop/index.js +0 -7
- package/dist/lib/scallop/index.js.map +0 -1
- package/dist/lib/scallop/scallop-addresses.d.ts +0 -24
- package/dist/lib/scallop/scallop-addresses.d.ts.map +0 -1
- package/dist/lib/scallop/scallop-addresses.js +0 -67
- package/dist/lib/scallop/scallop-addresses.js.map +0 -1
- package/dist/lib/scallop/scallop-builder.d.ts +0 -96
- package/dist/lib/scallop/scallop-builder.d.ts.map +0 -1
- package/dist/lib/scallop/scallop-builder.js +0 -163
- package/dist/lib/scallop/scallop-builder.js.map +0 -1
- package/dist/lib/suilend/calculators.d.ts +0 -44
- package/dist/lib/suilend/calculators.d.ts.map +0 -1
- package/dist/lib/suilend/calculators.js.map +0 -1
- package/dist/lib/suilend/const.d.ts +0 -9
- package/dist/lib/suilend/const.d.ts.map +0 -1
- package/dist/lib/suilend/const.js +0 -164
- package/dist/lib/suilend/suilend.d.ts +0 -15
- package/dist/lib/suilend/suilend.d.ts.map +0 -1
- package/dist/lib/suilend/suilend.js.map +0 -1
- package/dist/lib/utils/coin.d.ts +0 -21
- package/dist/lib/utils/coin.d.ts.map +0 -1
- package/dist/lib/utils/coin.js +0 -50
- package/dist/lib/utils/coin.js.map +0 -1
- package/dist/lib/utils/format.d.ts +0 -24
- package/dist/lib/utils/format.d.ts.map +0 -1
- package/dist/lib/utils/format.js.map +0 -1
- package/dist/lib/utils/index.d.ts +0 -9
- package/dist/lib/utils/index.d.ts.map +0 -1
- package/dist/lib/utils/index.js +0 -25
- package/dist/lib/utils/index.js.map +0 -1
- package/dist/lib/utils/logger.d.ts +0 -30
- package/dist/lib/utils/logger.d.ts.map +0 -1
- package/dist/lib/utils/logger.js.map +0 -1
- package/dist/protocols/interface.d.ts +0 -129
- package/dist/protocols/interface.d.ts.map +0 -1
- package/dist/protocols/interface.js +0 -8
- package/dist/protocols/interface.js.map +0 -1
- package/dist/protocols/navi.d.ts +0 -34
- package/dist/protocols/navi.d.ts.map +0 -1
- package/dist/protocols/navi.js +0 -263
- package/dist/protocols/navi.js.map +0 -1
- package/dist/protocols/scallop.d.ts +0 -297
- package/dist/protocols/scallop.d.ts.map +0 -1
- package/dist/protocols/scallop.js.map +0 -1
- package/dist/protocols/suilend.d.ts +0 -29
- package/dist/protocols/suilend.d.ts.map +0 -1
- package/dist/protocols/suilend.js +0 -313
- package/dist/protocols/suilend.js.map +0 -1
- package/dist/types.d.ts +0 -188
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -22
- package/dist/types.js.map +0 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 curg'13
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -40,7 +40,7 @@ DefiDash SDK is a **DeFi Saver-like toolkit** for Sui blockchain. It abstracts c
|
|
|
40
40
|
│ │
|
|
41
41
|
│ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ │
|
|
42
42
|
│ │ 1. Flash Loan │───▶│ 2. Swap │───▶│ 3. Lending │ │
|
|
43
|
-
│ │ (Scallop) │ │ (7k Protocol) │ │ (
|
|
43
|
+
│ │ (Scallop) │ │ (7k Protocol) │ │ (Multi-proto) │ │
|
|
44
44
|
│ └─────────────────┘ └─────────────────┘ └─────────────────┘ │
|
|
45
45
|
│ │ │ │
|
|
46
46
|
│ └────────────── Borrow to Repay ◀──────────────┘ │
|
|
@@ -51,14 +51,24 @@ DefiDash SDK is a **DeFi Saver-like toolkit** for Sui blockchain. It abstracts c
|
|
|
51
51
|
**How it works:**
|
|
52
52
|
|
|
53
53
|
1. **Flash Loan** — Borrow USDC from Scallop (no collateral needed)
|
|
54
|
-
2. **Swap** — Convert USDC to collateral asset (
|
|
55
|
-
3. **Deposit** — Deposit collateral into lending protocol
|
|
54
|
+
2. **Swap** — Convert USDC to collateral asset (XBTC, SUI, etc.) via 7k Aggregator
|
|
55
|
+
3. **Deposit** — Deposit collateral into lending protocol
|
|
56
56
|
4. **Borrow** — Borrow USDC against collateral to repay flash loan
|
|
57
57
|
|
|
58
58
|
All steps execute atomically in a single Sui Programmable Transaction Block (PTB).
|
|
59
59
|
|
|
60
60
|
---
|
|
61
61
|
|
|
62
|
+
## Supported Protocols
|
|
63
|
+
|
|
64
|
+
| Component | Protocols |
|
|
65
|
+
| ------------------- | ---------------------- |
|
|
66
|
+
| **Flash Loan** | Scallop |
|
|
67
|
+
| **Swap Aggregator** | 7k Protocol |
|
|
68
|
+
| **Lending** | Suilend, Navi, Scallop |
|
|
69
|
+
|
|
70
|
+
---
|
|
71
|
+
|
|
62
72
|
## Installation
|
|
63
73
|
|
|
64
74
|
```bash
|
|
@@ -67,75 +77,61 @@ npm install defi-dash-sdk
|
|
|
67
77
|
|
|
68
78
|
## Quick Start
|
|
69
79
|
|
|
80
|
+
### Node.js
|
|
81
|
+
|
|
70
82
|
```typescript
|
|
71
83
|
import { SuiClient, getFullnodeUrl } from "@mysten/sui/client";
|
|
72
84
|
import { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519";
|
|
85
|
+
import { Transaction } from "@mysten/sui/transactions";
|
|
73
86
|
import { DefiDashSDK, LendingProtocol } from "defi-dash-sdk";
|
|
74
87
|
|
|
75
88
|
// Initialize
|
|
76
89
|
const suiClient = new SuiClient({ url: getFullnodeUrl("mainnet") });
|
|
77
90
|
const keypair = Ed25519Keypair.fromSecretKey(YOUR_SECRET_KEY);
|
|
91
|
+
const sdk = await DefiDashSDK.create(suiClient, keypair);
|
|
78
92
|
|
|
79
|
-
|
|
80
|
-
|
|
93
|
+
// Build + Execute 2x leverage on XBTC
|
|
94
|
+
const tx = new Transaction();
|
|
95
|
+
tx.setSender(keypair.getPublicKey().toSuiAddress());
|
|
81
96
|
|
|
82
|
-
|
|
83
|
-
const result = await sdk.leverage({
|
|
97
|
+
await sdk.buildLeverageTransaction(tx, {
|
|
84
98
|
protocol: LendingProtocol.Suilend,
|
|
85
|
-
depositAsset: "
|
|
86
|
-
|
|
99
|
+
depositAsset: "XBTC",
|
|
100
|
+
depositValueUsd: 100,
|
|
87
101
|
multiplier: 2.0,
|
|
88
|
-
dryRun: false, // Set true to simulate
|
|
89
102
|
});
|
|
90
103
|
|
|
91
|
-
|
|
104
|
+
const result = await sdk.execute(tx); // or sdk.dryRun(tx) to simulate
|
|
105
|
+
console.log(result.txDigest);
|
|
92
106
|
|
|
93
107
|
// Close position
|
|
94
|
-
|
|
108
|
+
const tx2 = new Transaction();
|
|
109
|
+
tx2.setSender(keypair.getPublicKey().toSuiAddress());
|
|
110
|
+
await sdk.buildDeleverageTransaction(tx2, {
|
|
95
111
|
protocol: LendingProtocol.Suilend,
|
|
96
|
-
dryRun: false,
|
|
97
112
|
});
|
|
113
|
+
await sdk.execute(tx2);
|
|
98
114
|
```
|
|
99
115
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
## Supported Protocols
|
|
116
|
+
### Browser
|
|
103
117
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
| **Swap Aggregator** | 7k Protocol |
|
|
108
|
-
| **Lending** | Suilend, Navi |
|
|
109
|
-
|
|
110
|
-
---
|
|
111
|
-
|
|
112
|
-
## Examples
|
|
113
|
-
|
|
114
|
-
See `examples/` folder for full working examples:
|
|
115
|
-
|
|
116
|
-
```bash
|
|
117
|
-
# Setup environment
|
|
118
|
-
cp .env.example .env.test
|
|
119
|
-
# Edit .env.test with your secret key
|
|
118
|
+
```typescript
|
|
119
|
+
// No keypair needed — pass wallet address
|
|
120
|
+
const sdk = await DefiDashSDK.create(suiClient, walletAddress);
|
|
120
121
|
|
|
121
|
-
|
|
122
|
-
|
|
122
|
+
const tx = new Transaction();
|
|
123
|
+
tx.setSender(walletAddress);
|
|
124
|
+
await sdk.buildLeverageTransaction(tx, {
|
|
125
|
+
protocol: LendingProtocol.Suilend,
|
|
126
|
+
depositAsset: "XBTC",
|
|
127
|
+
depositValueUsd: 100,
|
|
128
|
+
multiplier: 2.0,
|
|
129
|
+
});
|
|
123
130
|
|
|
124
|
-
|
|
125
|
-
|
|
131
|
+
// Sign with wallet adapter (e.g., @mysten/dapp-kit)
|
|
132
|
+
await signAndExecute({ transaction: tx });
|
|
126
133
|
```
|
|
127
134
|
|
|
128
|
-
### Environment Variables
|
|
129
|
-
|
|
130
|
-
| Variable | Description | Default |
|
|
131
|
-
| ---------------------------- | ------------------------------ | --------- |
|
|
132
|
-
| `SECRET_KEY` | Sui wallet secret key (base64) | Required |
|
|
133
|
-
| `LEVERAGE_PROTOCOL` | `suilend` or `navi` | `suilend` |
|
|
134
|
-
| `LEVERAGE_DEPOSIT_COIN_TYPE` | Asset symbol or coin type | `LBTC` |
|
|
135
|
-
| `LEVERAGE_DEPOSIT_AMOUNT` | Amount in raw units | `1000` |
|
|
136
|
-
| `LEVERAGE_MULTIPLIER` | Leverage multiplier | `2` |
|
|
137
|
-
| `TX_MODE` | `dryrun` or `exec` | `dryrun` |
|
|
138
|
-
|
|
139
135
|
---
|
|
140
136
|
|
|
141
137
|
## API Reference
|
|
@@ -144,37 +140,56 @@ npm run example:deleverage
|
|
|
144
140
|
|
|
145
141
|
```typescript
|
|
146
142
|
class DefiDashSDK {
|
|
147
|
-
// Initialize
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
143
|
+
// Initialize (static factory — no constructor)
|
|
144
|
+
static create(
|
|
145
|
+
suiClient: SuiClient,
|
|
146
|
+
keypairOrAddress: Ed25519Keypair | string,
|
|
147
|
+
options?: SDKOptions,
|
|
148
|
+
): Promise<DefiDashSDK>;
|
|
149
|
+
|
|
150
|
+
// Build transactions (browser & Node.js)
|
|
151
|
+
buildLeverageTransaction(tx: Transaction, params: BrowserLeverageParams): Promise<void>;
|
|
152
|
+
buildDeleverageTransaction(tx: Transaction, params: BrowserDeleverageParams): Promise<void>;
|
|
153
|
+
|
|
154
|
+
// Preview & Route finding
|
|
155
|
+
previewLeverage(params: PreviewLeverageParams): Promise<LeveragePreview>;
|
|
156
|
+
findBestLeverageRoute(params: FindBestRouteParams): Promise<LeverageRouteResult>;
|
|
157
|
+
|
|
158
|
+
// Position queries
|
|
157
159
|
getPosition(protocol: LendingProtocol): Promise<PositionInfo | null>;
|
|
160
|
+
getOpenPositions(): Promise<Array<{ protocol: LendingProtocol; position: PositionInfo }>>;
|
|
161
|
+
getAggregatedPortfolio(): Promise<AccountPortfolio[]>;
|
|
158
162
|
|
|
159
|
-
//
|
|
160
|
-
|
|
163
|
+
// Utilities
|
|
164
|
+
getTokenPrice(asset: string): Promise<number>;
|
|
165
|
+
getSuiClient(): SuiClient;
|
|
166
|
+
getUserAddress(): string;
|
|
167
|
+
|
|
168
|
+
// Execute (Node.js only, requires keypair)
|
|
169
|
+
dryRun(tx: Transaction): Promise<StrategyResult>;
|
|
170
|
+
execute(tx: Transaction): Promise<StrategyResult>;
|
|
161
171
|
}
|
|
162
172
|
```
|
|
163
173
|
|
|
164
|
-
### Types
|
|
174
|
+
### Key Types
|
|
165
175
|
|
|
166
176
|
```typescript
|
|
167
177
|
enum LendingProtocol {
|
|
168
178
|
Suilend = "suilend",
|
|
169
179
|
Navi = "navi",
|
|
180
|
+
Scallop = "scallop",
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
interface BrowserLeverageParams {
|
|
184
|
+
protocol: LendingProtocol;
|
|
185
|
+
depositAsset: string; // "XBTC", "SUI", or full coin type
|
|
186
|
+
depositAmount?: string; // Raw amount (e.g., "100000") — either this or depositValueUsd
|
|
187
|
+
depositValueUsd?: number; // USD value (e.g., 100) — either this or depositAmount
|
|
188
|
+
multiplier: number; // 1.5, 2.0, 3.0, etc.
|
|
170
189
|
}
|
|
171
190
|
|
|
172
|
-
interface
|
|
191
|
+
interface BrowserDeleverageParams {
|
|
173
192
|
protocol: LendingProtocol;
|
|
174
|
-
depositAsset: string; // "LBTC" or full coin type
|
|
175
|
-
depositAmount: string; // Human-readable, e.g., "0.001"
|
|
176
|
-
multiplier: number; // 1.5, 2.0, 3.0, etc.
|
|
177
|
-
dryRun?: boolean;
|
|
178
193
|
}
|
|
179
194
|
|
|
180
195
|
interface StrategyResult {
|
|
@@ -185,29 +200,94 @@ interface StrategyResult {
|
|
|
185
200
|
}
|
|
186
201
|
```
|
|
187
202
|
|
|
203
|
+
### Preview & Route Finding
|
|
204
|
+
|
|
205
|
+
```typescript
|
|
206
|
+
// Compare all protocols for a given asset
|
|
207
|
+
const route = await sdk.findBestLeverageRoute({
|
|
208
|
+
depositAsset: "XBTC",
|
|
209
|
+
depositValueUsd: 100,
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
console.log(route.safeMultiplier); // e.g., 2.00
|
|
213
|
+
console.log(route.bestMaxMultiplier.protocol); // e.g., "scallop"
|
|
214
|
+
console.log(route.bestApy.protocol); // e.g., "suilend"
|
|
215
|
+
|
|
216
|
+
// Preview a specific protocol
|
|
217
|
+
const preview = await sdk.previewLeverage({
|
|
218
|
+
protocol: LendingProtocol.Suilend,
|
|
219
|
+
depositAsset: "XBTC",
|
|
220
|
+
depositValueUsd: 100,
|
|
221
|
+
multiplier: 2.0,
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
console.log(preview.totalPositionUsd); // $200
|
|
225
|
+
console.log(preview.maxMultiplier); // 2.50x
|
|
226
|
+
console.log(preview.liquidationPrice); // $51,244
|
|
227
|
+
console.log(preview.netApy); // 0.04 (4%)
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
### Position Queries
|
|
231
|
+
|
|
232
|
+
```typescript
|
|
233
|
+
// Single protocol position
|
|
234
|
+
const position = await sdk.getPosition(LendingProtocol.Suilend);
|
|
235
|
+
|
|
236
|
+
// All open positions (for strategy operations)
|
|
237
|
+
const positions = await sdk.getOpenPositions();
|
|
238
|
+
for (const { protocol, position } of positions) {
|
|
239
|
+
console.log(`[${protocol}] Net: $${position.netValueUsd}`);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Full portfolio with APY data (for dashboards)
|
|
243
|
+
const portfolios = await sdk.getAggregatedPortfolio();
|
|
244
|
+
for (const p of portfolios) {
|
|
245
|
+
console.log(`[${p.protocol}] HF: ${p.healthFactor} | Net: $${p.netValueUsd}`);
|
|
246
|
+
}
|
|
247
|
+
```
|
|
248
|
+
|
|
188
249
|
---
|
|
189
250
|
|
|
190
|
-
##
|
|
251
|
+
## Examples
|
|
252
|
+
|
|
253
|
+
See [`examples/`](examples/) for full working examples:
|
|
191
254
|
|
|
192
|
-
|
|
255
|
+
| File | Description |
|
|
256
|
+
|------|-------------|
|
|
257
|
+
| `leverage.ts` | Build and execute a leverage position |
|
|
258
|
+
| `deleverage.ts` | Close a leverage position |
|
|
259
|
+
| `preview_leverage.ts` | Preview leverage parameters before executing |
|
|
260
|
+
| `find_best_route.ts` | Compare protocols and find optimal route |
|
|
261
|
+
| `get_portfolio.ts` | Query positions and portfolio across protocols |
|
|
193
262
|
|
|
194
263
|
```bash
|
|
195
|
-
# Setup
|
|
196
|
-
cp .env.
|
|
264
|
+
# Setup environment
|
|
265
|
+
cp .env.example .env
|
|
266
|
+
# Edit .env with your secret key
|
|
267
|
+
|
|
268
|
+
# Run examples
|
|
269
|
+
npm run example:leverage
|
|
270
|
+
npm run example:deleverage
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
---
|
|
197
274
|
|
|
198
|
-
|
|
199
|
-
npm run script:suilend-leverage
|
|
200
|
-
npm run script:suilend-deleverage
|
|
275
|
+
## E2E Testing
|
|
201
276
|
|
|
202
|
-
|
|
203
|
-
npm run script:navi-leverage
|
|
204
|
-
npm run script:navi-deleverage
|
|
277
|
+
Full cycle test across all protocols: Preview → Leverage → Portfolio → Deleverage
|
|
205
278
|
|
|
206
|
-
|
|
207
|
-
|
|
279
|
+
```bash
|
|
280
|
+
# Dry run (simulation only)
|
|
281
|
+
npm run e2e
|
|
282
|
+
|
|
283
|
+
# Execute on-chain (real transactions, gas costs apply)
|
|
284
|
+
npm run e2e:execute
|
|
208
285
|
|
|
209
|
-
#
|
|
210
|
-
npm run
|
|
286
|
+
# Partial runs
|
|
287
|
+
npm run e2e:leverage # leverage only (dryrun)
|
|
288
|
+
npm run e2e:leverage:execute # leverage only (execute)
|
|
289
|
+
npm run e2e:deleverage # deleverage only (dryrun)
|
|
290
|
+
npm run e2e:deleverage:execute # deleverage only (execute)
|
|
211
291
|
```
|
|
212
292
|
|
|
213
293
|
---
|
|
@@ -217,28 +297,50 @@ npm run script:swap
|
|
|
217
297
|
```
|
|
218
298
|
defi-dash-sdk/
|
|
219
299
|
├── src/
|
|
220
|
-
│ ├── index.ts
|
|
221
|
-
│ ├── sdk.ts
|
|
222
|
-
│ ├── types
|
|
223
|
-
│ ├── protocols/
|
|
224
|
-
│ │ ├──
|
|
225
|
-
│ │
|
|
226
|
-
│ ├──
|
|
300
|
+
│ ├── index.ts # Public API exports
|
|
301
|
+
│ ├── sdk.ts # DefiDashSDK class
|
|
302
|
+
│ ├── types/ # TypeScript types & constants
|
|
303
|
+
│ ├── protocols/ # Protocol adapters
|
|
304
|
+
│ │ ├── base-adapter.ts # Abstract base (compile-time enforcement)
|
|
305
|
+
│ │ ├── suilend/
|
|
306
|
+
│ │ ├── navi/
|
|
307
|
+
│ │ └── scallop/
|
|
308
|
+
│ ├── strategies/ # Strategy builders
|
|
227
309
|
│ │ ├── leverage.ts
|
|
228
|
-
│ │
|
|
229
|
-
│
|
|
230
|
-
|
|
231
|
-
│ ├──
|
|
232
|
-
│ └──
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
├──
|
|
237
|
-
└──
|
|
310
|
+
│ │ ├── deleverage.ts
|
|
311
|
+
│ │ ├── leverage-preview.ts
|
|
312
|
+
│ │ └── leverage-route.ts
|
|
313
|
+
│ ├── utils/ # Internal utilities
|
|
314
|
+
│ └── __tests__/ # Unit & integration tests
|
|
315
|
+
├── examples/ # SDK usage examples
|
|
316
|
+
├── scripts/ # E2E test scripts
|
|
317
|
+
└── docs/ # SDK documentation
|
|
318
|
+
├── sdk-methods/ # Method-level docs
|
|
319
|
+
└── adding-new-protocol.md
|
|
238
320
|
```
|
|
239
321
|
|
|
322
|
+
### Adding New Protocols
|
|
323
|
+
|
|
324
|
+
New lending protocols can be added by extending `BaseProtocolAdapter`. The abstract class enforces compile-time method implementation — missing any required method produces a TypeScript build error.
|
|
325
|
+
|
|
326
|
+
See [`docs/adding-new-protocol.md`](docs/adding-new-protocol.md) for the full guide.
|
|
327
|
+
|
|
328
|
+
---
|
|
329
|
+
|
|
330
|
+
## Environment Variables
|
|
331
|
+
|
|
332
|
+
| Variable | Description | Default |
|
|
333
|
+
| ---------------------------- | ------------------------------ | --------- |
|
|
334
|
+
| `SECRET_KEY` | Sui wallet secret key (base64) | Required |
|
|
335
|
+
| `SUI_FULLNODE_URL` | Custom RPC endpoint | Mainnet |
|
|
336
|
+
| `LEVERAGE_PROTOCOL` | `suilend`, `navi`, or `scallop`| `suilend` |
|
|
337
|
+
| `LEVERAGE_DEPOSIT_COIN_TYPE` | Asset symbol or coin type | `XBTC` |
|
|
338
|
+
| `LEVERAGE_DEPOSIT_AMOUNT` | Amount in raw units | `1000` |
|
|
339
|
+
| `LEVERAGE_MULTIPLIER` | Leverage multiplier | `2` |
|
|
340
|
+
| `TX_MODE` | `dryrun` or `exec` | `dryrun` |
|
|
341
|
+
|
|
240
342
|
---
|
|
241
343
|
|
|
242
344
|
## License
|
|
243
345
|
|
|
244
|
-
MIT
|
|
346
|
+
[MIT](LICENSE)
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DeFi Dash SDK
|
|
3
|
+
*
|
|
4
|
+
* Multi-protocol DeFi SDK for Sui blockchain integrating leverage strategies,
|
|
5
|
+
* flash loans, and lending protocols.
|
|
6
|
+
*
|
|
7
|
+
* @module defi-dash-sdk
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { DefiDashSDK, LendingProtocol } from 'defi-dash-sdk';
|
|
12
|
+
*
|
|
13
|
+
* const sdk = await DefiDashSDK.create(suiClient, keypair);
|
|
14
|
+
*
|
|
15
|
+
* // Build leverage transaction
|
|
16
|
+
* const tx = new Transaction();
|
|
17
|
+
* tx.setSender(address);
|
|
18
|
+
* await sdk.buildLeverageTransaction(tx, {
|
|
19
|
+
* protocol: LendingProtocol.Suilend,
|
|
20
|
+
* depositAsset: 'LBTC',
|
|
21
|
+
* depositAmount: '0.001',
|
|
22
|
+
* multiplier: 2.0,
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* // Node.js: execute via SDK
|
|
26
|
+
* const result = await sdk.execute(tx);
|
|
27
|
+
*
|
|
28
|
+
* // Browser: execute via wallet adapter
|
|
29
|
+
* await signAndExecute({ transaction: tx });
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
// Main SDK
|
|
33
|
+
export { DefiDashSDK } from './sdk';
|
|
34
|
+
// Enums & Constants
|
|
35
|
+
export { LendingProtocol, USDC_COIN_TYPE, SUI_COIN_TYPE, COIN_TYPES, } from './types';
|
|
36
|
+
// Utilities (frontend-facing only)
|
|
37
|
+
export { formatUnits, parseUnits } from './utils/format';
|
|
38
|
+
export { normalizeCoinType } from './utils/coin';
|
|
39
|
+
export { DefiDashError, SDKNotInitializedError, InvalidParameterError, InvalidCoinTypeError, } from './utils/errors';
|
|
40
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAEH,WAAW;AACX,OAAO,EAAE,WAAW,EAAE,MAAM,OAAO,CAAC;AAEpC,oBAAoB;AACpB,OAAO,EACL,eAAe,EACf,cAAc,EACd,aAAa,EACb,UAAU,GACX,MAAM,SAAS,CAAC;AA2BjB,mCAAmC;AACnC,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AACzD,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,EACL,aAAa,EACb,sBAAsB,EACtB,qBAAqB,EACrB,oBAAoB,GACrB,MAAM,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base Protocol Adapter
|
|
3
|
+
*
|
|
4
|
+
* Abstract base class for all lending protocol adapters.
|
|
5
|
+
* Implements ILendingProtocol to enforce compile-time method checks.
|
|
6
|
+
*
|
|
7
|
+
* Any class extending this MUST implement all abstract methods,
|
|
8
|
+
* otherwise TypeScript will produce a build error:
|
|
9
|
+
* "Non-abstract class 'XxxAdapter' does not implement inherited abstract member 'methodName'"
|
|
10
|
+
*
|
|
11
|
+
* This is the TypeScript equivalent of Go's interface compliance pattern:
|
|
12
|
+
* var _ ILendingProtocol = (*MyAdapter)(nil)
|
|
13
|
+
*/
|
|
14
|
+
import { normalizeCoinType } from "../utils";
|
|
15
|
+
/**
|
|
16
|
+
* Abstract base class for protocol adapters.
|
|
17
|
+
*
|
|
18
|
+
* Provides common utilities (SuiClient management, coin normalization).
|
|
19
|
+
* All ILendingProtocol methods are declared abstract — subclasses MUST implement them.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* export class NewProtocolAdapter extends BaseProtocolAdapter {
|
|
24
|
+
* readonly name = "new-protocol";
|
|
25
|
+
* readonly consumesRepaymentCoin = false;
|
|
26
|
+
*
|
|
27
|
+
* async initialize(suiClient: SuiClient): Promise<void> {
|
|
28
|
+
* await super.initialize(suiClient);
|
|
29
|
+
* // protocol-specific init
|
|
30
|
+
* }
|
|
31
|
+
*
|
|
32
|
+
* // All abstract methods must be implemented:
|
|
33
|
+
* async getPosition(userAddress: string) { ... }
|
|
34
|
+
* async deposit(tx, coin, coinType, userAddress) { ... }
|
|
35
|
+
* async withdraw(tx, coinType, amount, userAddress) { ... }
|
|
36
|
+
* async borrow(tx, coinType, amount, userAddress) { ... }
|
|
37
|
+
* async repay(tx, coinType, coin, userAddress) { ... }
|
|
38
|
+
* async refreshOracles(tx, coinTypes, userAddress) { ... }
|
|
39
|
+
* async getAccountPortfolio(address) { ... }
|
|
40
|
+
* async getAssetRiskParams(coinType) { ... }
|
|
41
|
+
* async getAssetApy(coinType) { ... }
|
|
42
|
+
* }
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export class BaseProtocolAdapter {
|
|
46
|
+
constructor() {
|
|
47
|
+
this.initialized = false;
|
|
48
|
+
}
|
|
49
|
+
// ── Base implementation ──────────────────────────────────────────────────
|
|
50
|
+
async initialize(suiClient) {
|
|
51
|
+
this.suiClient = suiClient;
|
|
52
|
+
this.initialized = true;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Optional: clear pending state between transactions.
|
|
56
|
+
* Override in subclass if needed (e.g., Scallop obligation tracking).
|
|
57
|
+
*/
|
|
58
|
+
clearPendingState() {
|
|
59
|
+
// no-op by default
|
|
60
|
+
}
|
|
61
|
+
// ── Protected utilities ──────────────────────────────────────────────────
|
|
62
|
+
ensureInitialized() {
|
|
63
|
+
if (!this.initialized) {
|
|
64
|
+
throw new Error(`${this.name} adapter not initialized`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
normalizeCoin(coinType) {
|
|
68
|
+
return normalizeCoinType(coinType);
|
|
69
|
+
}
|
|
70
|
+
formatAmount(amount, decimals) {
|
|
71
|
+
return Number(amount) / Math.pow(10, decimals);
|
|
72
|
+
}
|
|
73
|
+
parseAmount(amount, decimals) {
|
|
74
|
+
const value = typeof amount === "string" ? parseFloat(amount) : amount;
|
|
75
|
+
return BigInt(Math.floor(value * Math.pow(10, decimals)));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=base-adapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base-adapter.js","sourceRoot":"","sources":["../../../src/protocols/base-adapter.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAWH,OAAO,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAE7C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,MAAM,OAAgB,mBAAmB;IAAzC;QAEY,gBAAW,GAAG,KAAK,CAAC;IAuFhC,CAAC;IAnCC,4EAA4E;IAE5E,KAAK,CAAC,UAAU,CAAC,SAAoB;QACnC,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC;IAC1B,CAAC;IAED;;;OAGG;IACH,iBAAiB;QACf,mBAAmB;IACrB,CAAC;IAED,4EAA4E;IAElE,iBAAiB;QACzB,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC;YACtB,MAAM,IAAI,KAAK,CAAC,GAAG,IAAI,CAAC,IAAI,0BAA0B,CAAC,CAAC;QAC1D,CAAC;IACH,CAAC;IAES,aAAa,CAAC,QAAgB;QACtC,OAAO,iBAAiB,CAAC,QAAQ,CAAC,CAAC;IACrC,CAAC;IAES,YAAY,CAAC,MAAc,EAAE,QAAgB;QACrD,OAAO,MAAM,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,QAAQ,CAAC,CAAC;IACjD,CAAC;IAES,WAAW,CAAC,MAAuB,EAAE,QAAgB;QAC7D,MAAM,KAAK,GAAG,OAAO,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;QACvE,OAAO,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC;IAC5D,CAAC;CACF"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DeFi Dash SDK - Protocol Adapters
|
|
3
|
+
*/
|
|
4
|
+
export { BaseProtocolAdapter } from "./base-adapter";
|
|
5
|
+
export { SuilendAdapter } from "./suilend/adapter";
|
|
6
|
+
export { NaviAdapter } from "./navi/adapter";
|
|
7
|
+
export { ScallopAdapter } from "./scallop/adapter";
|
|
8
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/protocols/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AACrD,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACnD,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC"}
|