@longdotxyz/shared 0.0.11 → 0.0.12
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/package.json +1 -1
- package/readme.md +291 -1
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -1 +1,291 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @longdotxyz/shared
|
|
2
|
+
|
|
3
|
+
Shared TypeScript library for Long.xyz API containing GraphQL types, REST API contracts, and utilities.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @longdotxyz/shared
|
|
9
|
+
# or
|
|
10
|
+
pnpm add @longdotxyz/shared
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Features
|
|
14
|
+
|
|
15
|
+
- **GraphQL Types & SDK**: Auto-generated types and SDK client from GraphQL schema
|
|
16
|
+
- **REST API Contracts**: Type-safe API contracts built with [ts-rest](https://ts-rest.com)
|
|
17
|
+
- **Type Utilities**: Common types for blockchain addresses, pool keys, and more
|
|
18
|
+
- **Utilities**: Helper functions for native token handling
|
|
19
|
+
|
|
20
|
+
## GraphQL Types and SDK
|
|
21
|
+
|
|
22
|
+
The library includes auto-generated GraphQL types and a ready-to-use SDK client.
|
|
23
|
+
|
|
24
|
+
### Basic Setup
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { GraphQLClient } from 'graphql-request';
|
|
28
|
+
import { getSdk } from '@longdotxyz/shared';
|
|
29
|
+
|
|
30
|
+
// Initialize GraphQL client
|
|
31
|
+
const client = new GraphQLClient('https://graphql.long.xyz');
|
|
32
|
+
|
|
33
|
+
// Create SDK instance
|
|
34
|
+
const sdk = getSdk(client);
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Example: Fetch Auction by Address
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import { getSdk, GetAuctionByAddressQuery } from '@longdotxyz/shared';
|
|
41
|
+
|
|
42
|
+
// Query an auction
|
|
43
|
+
const { auction_pool } = await sdk.GetAuctionByAddress({
|
|
44
|
+
address: '0x123...'
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
// Type-safe access to auction data
|
|
48
|
+
if (auction_pool) {
|
|
49
|
+
console.log(auction_pool.auction_pool_address);
|
|
50
|
+
console.log(auction_pool.asset_address);
|
|
51
|
+
console.log(auction_pool.numeraire_address);
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Available GraphQL Types
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
import type {
|
|
59
|
+
Asset,
|
|
60
|
+
AuctionPool,
|
|
61
|
+
GraduationPool,
|
|
62
|
+
Asset_Bool_Exp,
|
|
63
|
+
Asset_Order_By
|
|
64
|
+
} from '@longdotxyz/shared';
|
|
65
|
+
|
|
66
|
+
// Use types for type-safe queries
|
|
67
|
+
const assetFilter: Asset_Bool_Exp = {
|
|
68
|
+
chain_id: { _eq: 1 },
|
|
69
|
+
asset_address: { _ilike: '0x%' }
|
|
70
|
+
};
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## REST API Client (ts-rest)
|
|
74
|
+
|
|
75
|
+
The library provides type-safe REST API contracts using [ts-rest](https://ts-rest.com/client/fetch).
|
|
76
|
+
|
|
77
|
+
### Initialize API Client
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
import { initClient } from '@ts-rest/core';
|
|
81
|
+
import { rootContract } from '@longdotxyz/shared';
|
|
82
|
+
|
|
83
|
+
// Create typed API client
|
|
84
|
+
const apiClient = initClient(rootContract, {
|
|
85
|
+
baseUrl: 'https://api.long.xyz',
|
|
86
|
+
baseHeaders: {
|
|
87
|
+
'Content-Type': 'application/json'
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Auction Endpoints
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
// Get dynamic auction details
|
|
96
|
+
const auctionResponse = await apiClient.auctions.getDynamicAuction({
|
|
97
|
+
params: {
|
|
98
|
+
address: '0x123...'
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
if (auctionResponse.status === 200) {
|
|
103
|
+
const { auction_pool_address, auction_base_token_symbol } = auctionResponse.body.result;
|
|
104
|
+
console.log(`Pool: ${auction_pool_address}, Symbol: ${auction_base_token_symbol}`);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Create dynamic auction
|
|
108
|
+
const createResponse = await apiClient.auctions.createDynamicAuction({
|
|
109
|
+
body: {
|
|
110
|
+
chainId: 8453, // Base
|
|
111
|
+
integrator: '0xabc...',
|
|
112
|
+
initial_supply: '1000000000000000000000000',
|
|
113
|
+
num_tokens_to_sell: '500000000000000000000000',
|
|
114
|
+
numeraire: '0xdef...'
|
|
115
|
+
// ... other params
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Quote Endpoints (Uniswap V3/V4)
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
// V4 Exact Input Quote
|
|
124
|
+
const v4Quote = await apiClient.quotes.v4ExactInputSingle({
|
|
125
|
+
body: {
|
|
126
|
+
chainId: 8453,
|
|
127
|
+
poolKey: {
|
|
128
|
+
currency0: '0x0000000000000000000000000000000000000000', // ETH
|
|
129
|
+
currency1: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913', // USDC
|
|
130
|
+
fee: 500,
|
|
131
|
+
tickSpacing: 10,
|
|
132
|
+
hooks: '0x0000000000000000000000000000000000000000'
|
|
133
|
+
},
|
|
134
|
+
zeroForOne: true,
|
|
135
|
+
exactAmount: '1000000000000000000' // 1 ETH
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
if (v4Quote.status === 200) {
|
|
140
|
+
console.log(`Output amount: ${v4Quote.body.result.amountOut}`);
|
|
141
|
+
console.log(`Gas estimate: ${v4Quote.body.result.gasEstimate}`);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// V3 Exact Output Quote
|
|
145
|
+
const v3Quote = await apiClient.quotes.v3ExactOutputSingle({
|
|
146
|
+
body: {
|
|
147
|
+
chainId: 1,
|
|
148
|
+
tokenIn: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2', // WETH
|
|
149
|
+
tokenOut: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', // USDC
|
|
150
|
+
amountOut: '1000000000', // 1000 USDC
|
|
151
|
+
fee: 500 // 0.05%
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
if (v3Quote.status === 200) {
|
|
156
|
+
console.log(`Input required: ${v3Quote.body.result.amountIn}`);
|
|
157
|
+
}
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### Community Endpoints
|
|
161
|
+
|
|
162
|
+
```typescript
|
|
163
|
+
// Fetch all communities
|
|
164
|
+
const communities = await apiClient.communities.fetchCommunities();
|
|
165
|
+
|
|
166
|
+
if (communities.status === 200) {
|
|
167
|
+
communities.body.result.forEach(community => {
|
|
168
|
+
console.log(`${community.label}: ${community.description}`);
|
|
169
|
+
console.log(`Funding: ${community.funding_amount}`);
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Get specific community
|
|
174
|
+
const community = await apiClient.communities.getCommunity({
|
|
175
|
+
params: { id: 1 }
|
|
176
|
+
});
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### IPFS Endpoints
|
|
180
|
+
|
|
181
|
+
```typescript
|
|
182
|
+
// Create IPFS content
|
|
183
|
+
const ipfsResult = await apiClient.ipfs.createFile({
|
|
184
|
+
body: {
|
|
185
|
+
name: 'Token Metadata',
|
|
186
|
+
description: 'My awesome token',
|
|
187
|
+
image: 'https://example.com/image.png',
|
|
188
|
+
animation_url: 'https://example.com/animation.mp4',
|
|
189
|
+
content: {
|
|
190
|
+
customField: 'value'
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
if (ipfsResult.status === 200) {
|
|
196
|
+
console.log(`IPFS hash: ${ipfsResult.body.ipfsHash}`);
|
|
197
|
+
console.log(`Gateway URL: ${ipfsResult.body.url}`);
|
|
198
|
+
}
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
## Type Utilities
|
|
202
|
+
|
|
203
|
+
### Pool Key Type (Uniswap V4)
|
|
204
|
+
|
|
205
|
+
```typescript
|
|
206
|
+
import { PoolKey } from '@longdotxyz/shared';
|
|
207
|
+
|
|
208
|
+
const poolKey: PoolKey = {
|
|
209
|
+
currency0: '0x0000000000000000000000000000000000000000',
|
|
210
|
+
currency1: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913',
|
|
211
|
+
fee: 500,
|
|
212
|
+
tickSpacing: 10,
|
|
213
|
+
hooks: '0x0000000000000000000000000000000000000000'
|
|
214
|
+
};
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
### Hex Address Validation
|
|
218
|
+
|
|
219
|
+
```typescript
|
|
220
|
+
import { hex } from '@longdotxyz/shared';
|
|
221
|
+
import { z } from 'zod';
|
|
222
|
+
|
|
223
|
+
// Validate and normalize hex addresses
|
|
224
|
+
const addressSchema = z.string().pipe(hex);
|
|
225
|
+
const validAddress = addressSchema.parse('0xabc...'); // Returns lowercase hex
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
### BigInt Coercion
|
|
229
|
+
|
|
230
|
+
```typescript
|
|
231
|
+
import { coerceBigInt } from '@longdotxyz/shared';
|
|
232
|
+
|
|
233
|
+
const amount = coerceBigInt.parse('1000000000000000000'); // Returns bigint
|
|
234
|
+
const amount2 = coerceBigInt.parse(1000n); // Also accepts bigint
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
## Native Token Utilities
|
|
238
|
+
|
|
239
|
+
```typescript
|
|
240
|
+
import { isNativeToken, isNativeTokenAddress } from '@longdotxyz/shared';
|
|
241
|
+
|
|
242
|
+
// Check if address is native token (ETH)
|
|
243
|
+
isNativeTokenAddress('0x0000000000000000000000000000000000000000'); // true
|
|
244
|
+
isNativeTokenAddress('0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE'); // true
|
|
245
|
+
|
|
246
|
+
// Check token object
|
|
247
|
+
isNativeToken({
|
|
248
|
+
address: '0x0000000000000000000000000000000000000000',
|
|
249
|
+
symbol: 'ETH'
|
|
250
|
+
}); // true
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
## Development
|
|
254
|
+
|
|
255
|
+
### Building the Library
|
|
256
|
+
|
|
257
|
+
```bash
|
|
258
|
+
# Generate GraphQL types
|
|
259
|
+
pnpm codegen
|
|
260
|
+
|
|
261
|
+
# Build TypeScript
|
|
262
|
+
pnpm build
|
|
263
|
+
|
|
264
|
+
# Run tests
|
|
265
|
+
pnpm test
|
|
266
|
+
|
|
267
|
+
# Format and lint
|
|
268
|
+
pnpm format
|
|
269
|
+
pnpm lint
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
### GraphQL Code Generation
|
|
273
|
+
|
|
274
|
+
The GraphQL types are generated from a schema using `@graphql-codegen`. Configuration is in `codegen.ts`.
|
|
275
|
+
|
|
276
|
+
```bash
|
|
277
|
+
pnpm codegen
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
## API Contract Structure
|
|
281
|
+
|
|
282
|
+
All API contracts are defined using ts-rest and exported from `@longdotxyz/shared/contracts`:
|
|
283
|
+
|
|
284
|
+
- **Auctions**: `/auctions/*` - Dynamic auction creation and management
|
|
285
|
+
- **Quotes**: `/quotes/*` - Uniswap V3/V4 swap quotes
|
|
286
|
+
- **Communities**: `/communities/*` - Community data management
|
|
287
|
+
- **IPFS**: `/ipfs/*` - IPFS content creation
|
|
288
|
+
|
|
289
|
+
## License
|
|
290
|
+
|
|
291
|
+
UNLICENSED
|