@baseline-markets/sdk 0.0.1 → 0.0.3

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 CHANGED
@@ -18,22 +18,24 @@ npm install @baseline-markets/sdk viem
18
18
  ```ts
19
19
  import { BaselineSDK } from '@baseline-markets/sdk';
20
20
  import { createPublicClient, createWalletClient, http, custom } from 'viem';
21
- import { baseSepolia } from 'viem/chains';
21
+ import { base } from 'viem/chains';
22
22
 
23
23
  const publicClient = createPublicClient({
24
- chain: baseSepolia,
24
+ chain: base,
25
25
  transport: http(),
26
26
  });
27
27
 
28
28
  // Wallet client is optional — only needed for write actions
29
29
  const walletClient = createWalletClient({
30
- chain: baseSepolia,
30
+ chain: base,
31
31
  transport: custom(window.ethereum),
32
32
  });
33
33
 
34
34
  const sdk = new BaselineSDK(publicClient, walletClient);
35
35
  ```
36
36
 
37
+ Each SDK instance is **bound to a single chain** — the chain comes from `publicClient.chain`. To talk to a different chain, build a new `publicClient` (and `walletClient` if writing) for that chain and instantiate a new `BaselineSDK`. The SDK has no `chainId` parameter on its methods by design; chain selection lives in the clients you pass to the constructor. See [Switching chains](#switching-chains) below for the React + wagmi pattern.
38
+
37
39
  ### Read-only
38
40
 
39
41
  ```ts
@@ -93,6 +95,96 @@ try {
93
95
 
94
96
  The original viem error is preserved on `err.cause` if you need it.
95
97
 
98
+ ## React + wagmi
99
+
100
+ wagmi's `usePublicClient` / `useWalletClient` return viem clients, so they plug straight into `BaselineSDK`. Wrap construction in a hook and memoize — re-creating the SDK on every render is fine but wasteful.
101
+
102
+ ```tsx
103
+ import { useMemo } from 'react';
104
+ import { useChainId, usePublicClient, useWalletClient } from 'wagmi';
105
+ import { BaselineSDK } from '@baseline-markets/sdk';
106
+
107
+ export function useBaselineSDK(chainId?: number) {
108
+ const walletChainId = useChainId();
109
+ const targetChainId = chainId ?? walletChainId;
110
+
111
+ const publicClient = usePublicClient({ chainId: targetChainId });
112
+ const { data: walletClient } = useWalletClient({ chainId: targetChainId });
113
+
114
+ return useMemo(() => {
115
+ if (!publicClient) return null;
116
+ return new BaselineSDK(publicClient, walletClient ?? undefined);
117
+ }, [publicClient, walletClient]);
118
+ }
119
+ ```
120
+
121
+ `useWalletClient()` returns `undefined` until the user connects — the SDK still works for reads in that state, and `sdk.hasWallet` tells you whether write actions are available.
122
+
123
+ ### Switching chains
124
+
125
+ The SDK is **single-chain**: the chain is baked into `publicClient`, so a `BaselineSDK` instance can only talk to one network. When the user switches network, you need new clients and a new SDK.
126
+
127
+ With the hook above this is automatic — passing `chainId` to wagmi's `usePublicClient`/`useWalletClient` returns different client references per chain, the `useMemo` deps change, and a fresh `BaselineSDK` is constructed. You don't have to do anything special; just trust that:
128
+
129
+ 1. **Chain change → new clients → new SDK.** Construction is cheap (the SDK just holds client references and resolves a proxy address from the chain id), so don't try to skip the rebuild.
130
+ 2. **Don't mix chains in one SDK.** Never pass a `publicClient` for one chain and a `walletClient` for another — reads and writes will target different networks.
131
+ 3. **Read across chains?** Call the hook with an explicit `chainId` per component (e.g. `useBaselineSDK(mainnet.id)` and `useBaselineSDK(base.id)`) — you'll get two independent SDK instances, one per chain.
132
+
133
+ ### Reads with `useQuery`
134
+
135
+ ```tsx
136
+ import { useQuery } from '@tanstack/react-query';
137
+
138
+ function Price({ bToken }: { bToken: `0x${string}` }) {
139
+ const sdk = useBaselineSDK();
140
+
141
+ const { data: price } = useQuery({
142
+ queryKey: ['baseline', 'activePrice', sdk?.chainId, bToken],
143
+ queryFn: () => sdk!.activePrice(bToken),
144
+ enabled: !!sdk,
145
+ });
146
+
147
+ return <div>{price?.toString()}</div>;
148
+ }
149
+ ```
150
+
151
+ Include `sdk.chainId` in the query key so cached data is scoped per network.
152
+
153
+ ### Writes with `useMutation`
154
+
155
+ ```tsx
156
+ import { useMutation } from '@tanstack/react-query';
157
+ import { SDKError } from '@baseline-markets/sdk';
158
+
159
+ function BuyButton({ bToken, amount, maxIn }: {
160
+ bToken: `0x${string}`;
161
+ amount: bigint;
162
+ maxIn: bigint;
163
+ }) {
164
+ const sdk = useBaselineSDK();
165
+
166
+ const buy = useMutation({
167
+ mutationFn: () =>
168
+ sdk!.buyTokensExactOut(bToken, amount, maxIn, { wait: true }),
169
+ onError: (err) => {
170
+ if (err instanceof SDKError && err.kind === 'user_rejected') return;
171
+ // surface other kinds: insufficient_funds, reverted, network, ...
172
+ },
173
+ });
174
+
175
+ return (
176
+ <button
177
+ disabled={!sdk?.hasWallet || buy.isPending}
178
+ onClick={() => buy.mutate()}
179
+ >
180
+ {buy.isPending ? 'Confirming…' : 'Buy'}
181
+ </button>
182
+ );
183
+ }
184
+ ```
185
+
186
+ The `wait: true` option resolves the promise only after the receipt is mined, so `buy.isPending` covers both the wallet-prompt and the on-chain confirmation.
187
+
96
188
  ## Configuration
97
189
 
98
190
  `BaselineSDK` takes an optional third argument:
@@ -115,7 +207,18 @@ await sdk.buyTokensExactOut(bToken, 100n, maxIn, {
115
207
 
116
208
  ## Supported networks
117
209
 
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.
210
+ - Ethereum mainnet (`mainnet`, chain id `1`)
211
+ - Base (`base`, chain id `8453`)
212
+
213
+ The SDK reads the chain from your `publicClient` and throws if it isn't one of the supported chains — no separate chain config needed.
214
+
215
+ ```ts
216
+ import { supportedChainIds } from '@baseline-markets/sdk';
217
+
218
+ if (!supportedChainIds.includes(chainId)) {
219
+ // unsupported network — prompt user to switch
220
+ }
221
+ ```
119
222
 
120
223
  ## Development
121
224
 
package/dist/index.cjs CHANGED
@@ -4650,7 +4650,7 @@ var ContractFactory = class {
4650
4650
  };
4651
4651
 
4652
4652
  // ../contracts/index.ts
4653
- Object.keys(addressBook_default).map(Number);
4653
+ var supportedChainIds = Object.keys(addressBook_default).map(Number);
4654
4654
  var SDKError = class extends Error {
4655
4655
  kind = "unknown";
4656
4656
  component;
@@ -4864,7 +4864,7 @@ var BaselineSDK = class _BaselineSDK {
4864
4864
  this.publicClient = publicClient;
4865
4865
  this.walletClient = walletClient;
4866
4866
  this.config = config;
4867
- this.readFactory = new ContractFactory(publicClient);
4867
+ this.readFactory = new ContractFactory(this.publicClient);
4868
4868
  }
4869
4869
  get chainId() {
4870
4870
  return this.readFactory.chain;
@@ -5312,3 +5312,4 @@ var BaselineSDK = class _BaselineSDK {
5312
5312
 
5313
5313
  exports.BaselineSDK = BaselineSDK;
5314
5314
  exports.SDKError = SDKError;
5315
+ exports.supportedChainIds = supportedChainIds;
package/dist/index.d.cts CHANGED
@@ -91,11 +91,11 @@ declare class BaselineSDK {
91
91
  private readonly readFactory;
92
92
  private getAccount;
93
93
  private requireWallet;
94
- constructor(publicClient: PublicClient, walletClient?: WalletClient, config?: SDKConfig);
94
+ constructor(publicClient: PublicClient<any, any>, walletClient?: WalletClient<any, any, any>, config?: SDKConfig);
95
95
  get chainId(): number;
96
96
  get proxy(): Address;
97
97
  get hasWallet(): boolean;
98
- withWallet(wallet: WalletClient): BaselineSDK;
98
+ withWallet(wallet: WalletClient<any, any, any>): BaselineSDK;
99
99
  createBToken(name: string, symbol: string, totalSupply: bigint, salt: Hex, opts?: TxOpts): Promise<{
100
100
  hash: Hex;
101
101
  bToken: Address;
@@ -218,4 +218,6 @@ declare class SDKError extends Error {
218
218
  constructor(message: string, params?: Partial<SDKError>);
219
219
  }
220
220
 
221
- export { type ApprovalOpts, type ApprovalPolicy, BaselineSDK, type CreateParams, type CreditAccount, type DeleverageResult, type LeverageResult, type NativeOpts, type PoolKey, type QuoteBuyBToken, type QuoteBuyReserves, type QuoteSellBToken, type QuoteSellReserves, type SDKConfig, SDKError, type StakedAccount, type SwapOpts, type TxOpts };
221
+ declare const supportedChainIds: number[];
222
+
223
+ export { type ApprovalOpts, type ApprovalPolicy, BaselineSDK, type CreateParams, type CreditAccount, type DeleverageResult, type LeverageResult, type NativeOpts, type PoolKey, type QuoteBuyBToken, type QuoteBuyReserves, type QuoteSellBToken, type QuoteSellReserves, type SDKConfig, SDKError, type StakedAccount, type SwapOpts, type TxOpts, supportedChainIds };
package/dist/index.d.ts CHANGED
@@ -91,11 +91,11 @@ declare class BaselineSDK {
91
91
  private readonly readFactory;
92
92
  private getAccount;
93
93
  private requireWallet;
94
- constructor(publicClient: PublicClient, walletClient?: WalletClient, config?: SDKConfig);
94
+ constructor(publicClient: PublicClient<any, any>, walletClient?: WalletClient<any, any, any>, config?: SDKConfig);
95
95
  get chainId(): number;
96
96
  get proxy(): Address;
97
97
  get hasWallet(): boolean;
98
- withWallet(wallet: WalletClient): BaselineSDK;
98
+ withWallet(wallet: WalletClient<any, any, any>): BaselineSDK;
99
99
  createBToken(name: string, symbol: string, totalSupply: bigint, salt: Hex, opts?: TxOpts): Promise<{
100
100
  hash: Hex;
101
101
  bToken: Address;
@@ -218,4 +218,6 @@ declare class SDKError extends Error {
218
218
  constructor(message: string, params?: Partial<SDKError>);
219
219
  }
220
220
 
221
- export { type ApprovalOpts, type ApprovalPolicy, BaselineSDK, type CreateParams, type CreditAccount, type DeleverageResult, type LeverageResult, type NativeOpts, type PoolKey, type QuoteBuyBToken, type QuoteBuyReserves, type QuoteSellBToken, type QuoteSellReserves, type SDKConfig, SDKError, type StakedAccount, type SwapOpts, type TxOpts };
221
+ declare const supportedChainIds: number[];
222
+
223
+ export { type ApprovalOpts, type ApprovalPolicy, BaselineSDK, type CreateParams, type CreditAccount, type DeleverageResult, type LeverageResult, type NativeOpts, type PoolKey, type QuoteBuyBToken, type QuoteBuyReserves, type QuoteSellBToken, type QuoteSellReserves, type SDKConfig, SDKError, type StakedAccount, type SwapOpts, type TxOpts, supportedChainIds };
package/dist/index.js CHANGED
@@ -4648,7 +4648,7 @@ var ContractFactory = class {
4648
4648
  };
4649
4649
 
4650
4650
  // ../contracts/index.ts
4651
- Object.keys(addressBook_default).map(Number);
4651
+ var supportedChainIds = Object.keys(addressBook_default).map(Number);
4652
4652
  var SDKError = class extends Error {
4653
4653
  kind = "unknown";
4654
4654
  component;
@@ -4862,7 +4862,7 @@ var BaselineSDK = class _BaselineSDK {
4862
4862
  this.publicClient = publicClient;
4863
4863
  this.walletClient = walletClient;
4864
4864
  this.config = config;
4865
- this.readFactory = new ContractFactory(publicClient);
4865
+ this.readFactory = new ContractFactory(this.publicClient);
4866
4866
  }
4867
4867
  get chainId() {
4868
4868
  return this.readFactory.chain;
@@ -5308,4 +5308,4 @@ var BaselineSDK = class _BaselineSDK {
5308
5308
  }
5309
5309
  };
5310
5310
 
5311
- export { BaselineSDK, SDKError };
5311
+ export { BaselineSDK, SDKError, supportedChainIds };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@baseline-markets/sdk",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "description": "TypeScript SDK for Baseline — the end-to-end asset issuance protocol where tokens own their liquidity.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",