@baseline-markets/sdk 0.0.1
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 +132 -0
- package/dist/index.cjs +5314 -0
- package/dist/index.d.cts +221 -0
- package/dist/index.d.ts +221 -0
- package/dist/index.js +5311 -0
- package/package.json +48 -0
package/README.md
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# @baseline-markets/sdk
|
|
2
|
+
|
|
3
|
+
A typed TypeScript SDK for interacting with [Baseline Protocol](https://baseline.markets) contracts via [viem](https://viem.sh).
|
|
4
|
+
|
|
5
|
+
Baseline is an onchain AMM for leveraged tokens — `@baseline-markets/sdk` exposes a single `BaselineSDK` class covering token launches, swaps, borrowing, leverage, staking, and rewards.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @baseline-markets/sdk viem
|
|
11
|
+
# or: bun add @baseline-markets/sdk viem
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
`viem` is a peer dependency — bring your own.
|
|
15
|
+
|
|
16
|
+
## Quickstart
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { BaselineSDK } from '@baseline-markets/sdk';
|
|
20
|
+
import { createPublicClient, createWalletClient, http, custom } from 'viem';
|
|
21
|
+
import { baseSepolia } from 'viem/chains';
|
|
22
|
+
|
|
23
|
+
const publicClient = createPublicClient({
|
|
24
|
+
chain: baseSepolia,
|
|
25
|
+
transport: http(),
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
// Wallet client is optional — only needed for write actions
|
|
29
|
+
const walletClient = createWalletClient({
|
|
30
|
+
chain: baseSepolia,
|
|
31
|
+
transport: custom(window.ethereum),
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const sdk = new BaselineSDK(publicClient, walletClient);
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Read-only
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
const bToken = '0x...' as const;
|
|
41
|
+
|
|
42
|
+
const price = await sdk.activePrice(bToken);
|
|
43
|
+
const reserve = await sdk.getReserve(bToken);
|
|
44
|
+
const quote = await sdk.quoteBuyExactOut(bToken, 100n);
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Swaps
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
// Buy exactly 100 bTokens, capped at `quote.reservesIn` reserve in
|
|
51
|
+
const hash = await sdk.buyTokensExactOut(bToken, 100n, quote.reservesIn, {
|
|
52
|
+
wait: true,
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// Sell 100 bTokens, accept at least `minOut` reserve out
|
|
56
|
+
const hash2 = await sdk.sellTokensExactIn(bToken, 100n, minOut);
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Borrow / leverage / stake
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
await sdk.deposit(bToken, collateral);
|
|
63
|
+
await sdk.borrow(bToken, debtAmount);
|
|
64
|
+
await sdk.leverage(bToken, collateral, targetLTV);
|
|
65
|
+
await sdk.repay(bToken, debtAmount);
|
|
66
|
+
await sdk.claim(bToken);
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
See `src/baseline-sdk.ts` for the full method list.
|
|
70
|
+
|
|
71
|
+
## Error handling
|
|
72
|
+
|
|
73
|
+
All write methods throw `SDKError`, a single error type with a `.kind` discriminator so you don't have to sniff messages:
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
import { SDKError } from '@baseline-markets/sdk';
|
|
77
|
+
|
|
78
|
+
try {
|
|
79
|
+
await sdk.buyTokensExactOut(bToken, 100n, maxIn);
|
|
80
|
+
} catch (err) {
|
|
81
|
+
if (err instanceof SDKError) {
|
|
82
|
+
switch (err.kind) {
|
|
83
|
+
case 'user_rejected': // user cancelled in their wallet
|
|
84
|
+
case 'insufficient_funds': // not enough native balance for gas
|
|
85
|
+
case 'reverted': // contract reverted (decoded reason in .message)
|
|
86
|
+
case 'network': // RPC/socket/timeout — retry-worthy
|
|
87
|
+
case 'wallet': // SDK misuse: no wallet, no account
|
|
88
|
+
case 'unknown': // anything else
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
The original viem error is preserved on `err.cause` if you need it.
|
|
95
|
+
|
|
96
|
+
## Configuration
|
|
97
|
+
|
|
98
|
+
`BaselineSDK` takes an optional third argument:
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
new BaselineSDK(publicClient, walletClient, {
|
|
102
|
+
defaultUseNative: true, // wrap/unwrap ETH automatically for native-reserve pools
|
|
103
|
+
});
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Per-call options live on `TxOpts`:
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
await sdk.buyTokensExactOut(bToken, 100n, maxIn, {
|
|
110
|
+
account: '0x...', // override which address sends the tx
|
|
111
|
+
confirmations: 2, // wait for N confirmations before returning
|
|
112
|
+
onSimulateError: (e) => {}, // hook for pre-simulation failures
|
|
113
|
+
});
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## Supported networks
|
|
117
|
+
|
|
118
|
+
Whatever your `publicClient` is connected to, as long as Baseline contracts are deployed there. The SDK reads the chain from your client — no separate chain config needed.
|
|
119
|
+
|
|
120
|
+
## Development
|
|
121
|
+
|
|
122
|
+
This package is part of the [baseline-mono](https://github.com/baseline-markets/baseline-mono) workspace.
|
|
123
|
+
|
|
124
|
+
```bash
|
|
125
|
+
bun install
|
|
126
|
+
bun run build # bundle ESM + CJS + .d.ts via tsup
|
|
127
|
+
bun test
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## License
|
|
131
|
+
|
|
132
|
+
MIT
|