@n1xyz/nord-ts 0.0.21 → 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 +20 -16
- package/dist/api/client.d.ts +14 -0
- package/dist/api/client.js +45 -0
- package/dist/bridge/client.d.ts +151 -0
- package/dist/bridge/client.js +434 -0
- package/dist/bridge/const.d.ts +23 -0
- package/dist/bridge/const.js +47 -0
- package/dist/bridge/index.d.ts +4 -0
- package/dist/bridge/index.js +23 -0
- package/dist/bridge/types.d.ts +120 -0
- package/dist/bridge/types.js +18 -0
- package/dist/bridge/utils.d.ts +64 -0
- package/dist/bridge/utils.js +131 -0
- package/dist/gen/common.d.ts +68 -0
- package/dist/gen/common.js +215 -0
- package/dist/gen/nord_pb.d.ts +3719 -0
- package/dist/gen/nord_pb.js +945 -0
- package/dist/gen/openapi.d.ts +268 -4
- package/dist/idl/bridge.d.ts +569 -0
- package/dist/idl/bridge.js +8 -0
- package/dist/idl/bridge.json +1506 -0
- package/dist/idl/index.d.ts +607 -0
- package/dist/idl/index.js +8 -0
- package/dist/nord/api/actions.d.ts +31 -72
- package/dist/nord/api/actions.js +199 -201
- package/dist/nord/api/market.d.ts +36 -0
- package/dist/nord/api/market.js +96 -0
- package/dist/nord/api/queries.d.ts +46 -0
- package/dist/nord/api/queries.js +109 -0
- package/dist/nord/client/Nord.js +3 -3
- package/dist/nord/client/NordUser.d.ts +26 -13
- package/dist/nord/client/NordUser.js +13 -10
- package/dist/types.d.ts +12 -1
- package/dist/types.js +29 -2
- package/dist/utils.d.ts +6 -20
- package/dist/utils.js +17 -35
- package/dist/websocket/NordWebSocketClient.js +2 -6
- package/package.json +26 -23
- package/src/gen/nord_pb.ts +4257 -0
- package/src/gen/openapi.ts +268 -4
- package/src/nord/api/actions.ts +278 -369
- package/src/nord/client/Nord.ts +3 -3
- package/src/nord/client/NordUser.ts +40 -19
- package/src/types.ts +32 -1
- package/src/utils.ts +24 -43
- package/src/websocket/NordWebSocketClient.ts +2 -8
package/README.md
CHANGED
|
@@ -6,10 +6,13 @@ This package provides an interface to interact with the Nord exchange. The core
|
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
8
|
# npm
|
|
9
|
-
npm install nord-ts
|
|
9
|
+
npm install @n1xyz/nord-ts
|
|
10
10
|
|
|
11
11
|
# yarn
|
|
12
|
-
yarn add nord-ts
|
|
12
|
+
yarn add @n1xyz/nord-ts
|
|
13
|
+
|
|
14
|
+
# bun
|
|
15
|
+
bun add @n1xyz/nord-ts
|
|
13
16
|
```
|
|
14
17
|
|
|
15
18
|
## Key Components
|
|
@@ -36,7 +39,7 @@ The `NordUser` class represents a user account on the Nord exchange:
|
|
|
36
39
|
### Initializing Nord
|
|
37
40
|
|
|
38
41
|
```typescript
|
|
39
|
-
import { Nord } from "nord-ts";
|
|
42
|
+
import { Nord } from "@n1xyz/nord-ts";
|
|
40
43
|
|
|
41
44
|
// Create a Nord instance
|
|
42
45
|
const nord = new Nord({
|
|
@@ -52,7 +55,7 @@ await Nord.initNord(nord); // Initialize client (derives program ID, fetches inf
|
|
|
52
55
|
### Creating a User from Private Key
|
|
53
56
|
|
|
54
57
|
```typescript
|
|
55
|
-
import { Nord, NordUser } from "nord-ts";
|
|
58
|
+
import { Nord, NordUser } from "@n1xyz/nord-ts";
|
|
56
59
|
import { Connection } from "@solana/web3.js";
|
|
57
60
|
|
|
58
61
|
// Define Nord configuration
|
|
@@ -83,7 +86,7 @@ await user.fetchInfo();
|
|
|
83
86
|
### Trading Operations
|
|
84
87
|
|
|
85
88
|
```typescript
|
|
86
|
-
import { Nord, NordUser, Side, FillMode } from "nord-ts";
|
|
89
|
+
import { Nord, NordUser, Side, FillMode } from "@n1xyz/nord-ts";
|
|
87
90
|
|
|
88
91
|
// Assuming nord and user are already initialized
|
|
89
92
|
|
|
@@ -110,7 +113,7 @@ try {
|
|
|
110
113
|
### Deposits and Withdrawals
|
|
111
114
|
|
|
112
115
|
```typescript
|
|
113
|
-
import { Nord, NordUser } from "nord-ts";
|
|
116
|
+
import { Nord, NordUser } from "@n1xyz/nord-ts";
|
|
114
117
|
|
|
115
118
|
// Assuming nord and user are already initialized
|
|
116
119
|
|
|
@@ -119,7 +122,7 @@ try {
|
|
|
119
122
|
const tokenId = 0; // USDC
|
|
120
123
|
const amount = 100; // 100 USDC
|
|
121
124
|
|
|
122
|
-
await user.withdraw(tokenId, amount);
|
|
125
|
+
await user.withdraw({ tokenId, amount });
|
|
123
126
|
console.log(`Successfully withdrew ${amount} of token ID ${tokenId}`);
|
|
124
127
|
} catch (error) {
|
|
125
128
|
console.error(`Withdrawal error: ${error}`);
|
|
@@ -140,7 +143,7 @@ try {
|
|
|
140
143
|
### Market Data
|
|
141
144
|
|
|
142
145
|
```typescript
|
|
143
|
-
import { Nord } from "nord-ts";
|
|
146
|
+
import { Nord } from "@n1xyz/nord-ts";
|
|
144
147
|
|
|
145
148
|
// Assuming nord is already initialized
|
|
146
149
|
|
|
@@ -150,7 +153,7 @@ console.log('Bids:', orderbook.bids);
|
|
|
150
153
|
console.log('Asks:', orderbook.asks);
|
|
151
154
|
|
|
152
155
|
// Get recent trades
|
|
153
|
-
const trades = await nord.getTrades({ marketId: 0,
|
|
156
|
+
const trades = await nord.getTrades({ marketId: 0, pageSize: 10 });
|
|
154
157
|
console.log('Recent trades:', trades.trades);
|
|
155
158
|
|
|
156
159
|
// Subscribe to real-time orderbook updates
|
|
@@ -164,12 +167,12 @@ orderbookSub.on('update', (data) => {
|
|
|
164
167
|
|
|
165
168
|
|
|
166
169
|
```typescript
|
|
167
|
-
import { Nord, NordUser } from "nord-ts";
|
|
170
|
+
import { Nord, NordUser } from "@n1xyz/nord-ts";
|
|
168
171
|
|
|
169
172
|
// Assuming nord and user are already initialized
|
|
170
173
|
|
|
171
|
-
//
|
|
172
|
-
|
|
174
|
+
// Refresh user info
|
|
175
|
+
await user.fetchInfo();
|
|
173
176
|
|
|
174
177
|
// Access user balances
|
|
175
178
|
console.log('Balances:', user.balances);
|
|
@@ -185,12 +188,13 @@ console.log('Orders:', user.orders);
|
|
|
185
188
|
|
|
186
189
|
```bash
|
|
187
190
|
# Install dependencies
|
|
188
|
-
|
|
191
|
+
bun
|
|
189
192
|
|
|
190
193
|
# Build the package
|
|
191
|
-
|
|
194
|
+
bun run build
|
|
192
195
|
```
|
|
193
196
|
|
|
194
|
-
##
|
|
197
|
+
## Internals
|
|
195
198
|
|
|
196
|
-
|
|
199
|
+
- `openapi` provides the OpenAPI-generated interfaces for HTTP (and partially WS) API of Nord
|
|
200
|
+
- `nord_pb` provides the Protobuf-generated binary interface to build actions to be posted to `/action` API.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { paths } from "../gen/openapi";
|
|
2
|
+
export interface ClientConfig {
|
|
3
|
+
baseUrl: string;
|
|
4
|
+
}
|
|
5
|
+
export declare function createApiClient(config: ClientConfig): import("openapi-fetch").Client<paths, `${string}/${string}`>;
|
|
6
|
+
export declare function handleApiResponse<T>(response: Promise<{
|
|
7
|
+
data: T;
|
|
8
|
+
error?: unknown;
|
|
9
|
+
response: Response;
|
|
10
|
+
} | {
|
|
11
|
+
data?: never;
|
|
12
|
+
error?: unknown;
|
|
13
|
+
response: Response;
|
|
14
|
+
}>): Promise<T>;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createApiClient = createApiClient;
|
|
7
|
+
exports.handleApiResponse = handleApiResponse;
|
|
8
|
+
const openapi_fetch_1 = __importDefault(require("openapi-fetch"));
|
|
9
|
+
const NordError_1 = require("../nord/utils/NordError");
|
|
10
|
+
function createApiClient(config) {
|
|
11
|
+
const client = (0, openapi_fetch_1.default)({
|
|
12
|
+
baseUrl: config.baseUrl,
|
|
13
|
+
});
|
|
14
|
+
client.use({
|
|
15
|
+
onResponse({ response }) {
|
|
16
|
+
if (!response.ok) {
|
|
17
|
+
throw new NordError_1.NordError(`HTTP ${response.status}: ${response.statusText}`, {
|
|
18
|
+
statusCode: response.status,
|
|
19
|
+
details: { url: response.url }
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
return response;
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
return client;
|
|
26
|
+
}
|
|
27
|
+
async function handleApiResponse(response) {
|
|
28
|
+
const result = await response;
|
|
29
|
+
if (result.error || !result.response.ok) {
|
|
30
|
+
const errorMessage = result.error
|
|
31
|
+
? `HTTP ${result.response.status}: ${result.response.statusText}`
|
|
32
|
+
: `Request failed: ${result.response.statusText}`;
|
|
33
|
+
throw new NordError_1.NordError(errorMessage, {
|
|
34
|
+
statusCode: result.response.status,
|
|
35
|
+
details: { url: result.response.url }
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
if (!('data' in result)) {
|
|
39
|
+
throw new NordError_1.NordError("No data in response", {
|
|
40
|
+
statusCode: 500,
|
|
41
|
+
details: { url: result.response.url }
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
return result.data;
|
|
45
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import * as anchor from "@coral-xyz/anchor";
|
|
2
|
+
import { AnchorProvider } from "@coral-xyz/anchor";
|
|
3
|
+
import { Connection, Keypair, PublicKey } from "@solana/web3.js";
|
|
4
|
+
import { BlockFacts, DepositSplParams, PdaSeedType, SolanaBridgeConfig, WithdrawalParams } from "./types";
|
|
5
|
+
/**
|
|
6
|
+
* Solana Bridge Client for interacting with the bridge program
|
|
7
|
+
*/
|
|
8
|
+
export declare class SolanaBridgeClient {
|
|
9
|
+
/** Anchor program instance */
|
|
10
|
+
program: any;
|
|
11
|
+
/** Solana connection */
|
|
12
|
+
connection: Connection;
|
|
13
|
+
/** Program ID */
|
|
14
|
+
programId: PublicKey;
|
|
15
|
+
/** Configuration */
|
|
16
|
+
config: SolanaBridgeConfig;
|
|
17
|
+
/** Anchor provider */
|
|
18
|
+
provider: AnchorProvider;
|
|
19
|
+
bridge: PublicKey;
|
|
20
|
+
/**
|
|
21
|
+
* Create a new Solana Bridge Client
|
|
22
|
+
*
|
|
23
|
+
* @param config Bridge configuration
|
|
24
|
+
* @param wallet Anchor wallet for signing transactions
|
|
25
|
+
*/
|
|
26
|
+
constructor(config: SolanaBridgeConfig, wallet: anchor.Wallet);
|
|
27
|
+
/**
|
|
28
|
+
* Derive a PDA (Program Derived Address) for the given seeds
|
|
29
|
+
*
|
|
30
|
+
* Seeds can be of type:
|
|
31
|
+
* - Buffer: raw bytes
|
|
32
|
+
* - PublicKey: Solana public key
|
|
33
|
+
* - string: string encoded as UTF-8 bytes
|
|
34
|
+
* - number: u64 encoded as little-endian 8 bytes
|
|
35
|
+
*
|
|
36
|
+
* @param type PDA seed type
|
|
37
|
+
* @param seeds Additional seeds
|
|
38
|
+
* @returns [PDA, bump]
|
|
39
|
+
*/
|
|
40
|
+
findPda(type: PdaSeedType, ...seeds: any[]): Promise<[PublicKey, number]>;
|
|
41
|
+
/**
|
|
42
|
+
* Find the asset config PDA for a token mint
|
|
43
|
+
*
|
|
44
|
+
* @param mint Token mint address
|
|
45
|
+
* @returns [PDA, bump]
|
|
46
|
+
*/
|
|
47
|
+
findAssetConfigPda(mint: PublicKey): Promise<[PublicKey, number]>;
|
|
48
|
+
/**
|
|
49
|
+
* Find the deposit storage PDA for a deposit index
|
|
50
|
+
*
|
|
51
|
+
* @param depositIndex Deposit index
|
|
52
|
+
* @returns [PDA, bump]
|
|
53
|
+
*/
|
|
54
|
+
findDepositStoragePda(depositIndex: number): Promise<[PublicKey, number]>;
|
|
55
|
+
/**
|
|
56
|
+
* Find the block storage PDA for a block ID
|
|
57
|
+
*
|
|
58
|
+
* @param blockId Block ID
|
|
59
|
+
* @returns [PDA, bump]
|
|
60
|
+
*/
|
|
61
|
+
findBlockStoragePda(blockId: number): Promise<[PublicKey, number]>;
|
|
62
|
+
/**
|
|
63
|
+
* Find the withdrawal nullifier PDA
|
|
64
|
+
*
|
|
65
|
+
* @param blockId Block ID
|
|
66
|
+
* @param leafIndex Leaf index
|
|
67
|
+
* @returns [PDA, bump]
|
|
68
|
+
*/
|
|
69
|
+
findWithdrawalNullifierPda(blockId: number, leafIndex: number): Promise<[PublicKey, number]>;
|
|
70
|
+
/**
|
|
71
|
+
* Find the authority PDA
|
|
72
|
+
*
|
|
73
|
+
* @returns [PDA, bump]
|
|
74
|
+
*/
|
|
75
|
+
findAuthorityPda(): Promise<[PublicKey, number]>;
|
|
76
|
+
/**
|
|
77
|
+
* Helper method to sign and send a transaction
|
|
78
|
+
*
|
|
79
|
+
* @param transaction Transaction to sign and send
|
|
80
|
+
* @param signers Additional signers (beyond the provider's wallet)
|
|
81
|
+
* @returns Transaction signature
|
|
82
|
+
*/
|
|
83
|
+
private signAndSendTransaction;
|
|
84
|
+
/**
|
|
85
|
+
* Deposit SPL tokens to the bridge
|
|
86
|
+
*
|
|
87
|
+
* @param params Deposit parameters
|
|
88
|
+
* @param signer Signer keypair
|
|
89
|
+
* @returns Transaction signature
|
|
90
|
+
*/
|
|
91
|
+
depositSpl(params: DepositSplParams): Promise<string>;
|
|
92
|
+
/**
|
|
93
|
+
* Withdraw tokens from the bridge
|
|
94
|
+
*
|
|
95
|
+
* @param params Withdrawal parameters
|
|
96
|
+
* @param signer Signer keypair
|
|
97
|
+
* @returns Transaction signature
|
|
98
|
+
*/
|
|
99
|
+
withdraw(params: WithdrawalParams, signer: Keypair): Promise<string>;
|
|
100
|
+
/**
|
|
101
|
+
* Whitelist an asset (token) for use with the bridge
|
|
102
|
+
*
|
|
103
|
+
* @param mint Token mint address
|
|
104
|
+
* @param signer Operator keypair
|
|
105
|
+
* @returns Transaction signature
|
|
106
|
+
*/
|
|
107
|
+
whitelistAsset(mint: PublicKey, signer: Keypair): Promise<string>;
|
|
108
|
+
/**
|
|
109
|
+
* Propose a new block
|
|
110
|
+
*
|
|
111
|
+
* @param facts Block facts
|
|
112
|
+
* @param signer Operator keypair
|
|
113
|
+
* @returns Transaction signature
|
|
114
|
+
*/
|
|
115
|
+
proposeBlock(facts: BlockFacts, signer: Keypair): Promise<string>;
|
|
116
|
+
/**
|
|
117
|
+
* Finalize a block
|
|
118
|
+
*
|
|
119
|
+
* @param blockId Block ID
|
|
120
|
+
* @param stateUpdateId State update ID
|
|
121
|
+
* @param signer Payer keypair
|
|
122
|
+
* @returns Transaction signature
|
|
123
|
+
*/
|
|
124
|
+
finalizeBlock(blockId: number, stateUpdateId: number, signer: Keypair): Promise<string>;
|
|
125
|
+
/**
|
|
126
|
+
* Finalize a DA fact
|
|
127
|
+
*
|
|
128
|
+
* @param fact DA fact (32-byte array)
|
|
129
|
+
* @param signer Payer keypair
|
|
130
|
+
* @returns Transaction signature
|
|
131
|
+
*/
|
|
132
|
+
finalizeDaFact(fact: Buffer, signer: Keypair): Promise<string>;
|
|
133
|
+
/**
|
|
134
|
+
* Initialize the bridge contract
|
|
135
|
+
*
|
|
136
|
+
* @param operator Operator public key
|
|
137
|
+
* @param initialAppStateCommitment Initial app state commitment (32-byte array)
|
|
138
|
+
* @param signer Payer keypair
|
|
139
|
+
* @returns Transaction signature
|
|
140
|
+
*/
|
|
141
|
+
initialize(operator: PublicKey, initialAppStateCommitment: Buffer, signer: Keypair): Promise<string>;
|
|
142
|
+
/**
|
|
143
|
+
* Create an associated token account if it doesn't exist
|
|
144
|
+
*
|
|
145
|
+
* @param mint Token mint
|
|
146
|
+
* @param owner Account owner
|
|
147
|
+
* @param payer Transaction payer
|
|
148
|
+
* @returns Associated token account address
|
|
149
|
+
*/
|
|
150
|
+
createTokenAccountIfNeeded(mint: PublicKey, owner: PublicKey, payer: Keypair): Promise<PublicKey>;
|
|
151
|
+
}
|