@cowprotocol/sdk-trading-solana 0.3.0 → 0.4.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/dist/index.d.mts +42 -7
- package/dist/index.d.ts +42 -7
- package/dist/index.js +64 -27
- package/dist/index.mjs +59 -23
- package/package.json +4 -4
package/dist/index.d.mts
CHANGED
|
@@ -64,6 +64,12 @@ declare function encodeOrderIntent(intent: SolanaOrderIntent): Uint8Array;
|
|
|
64
64
|
*/
|
|
65
65
|
declare function hashOrderIntent(encoded: Uint8Array): Promise<Uint8Array>;
|
|
66
66
|
declare function toHex(bytes: Uint8Array): string;
|
|
67
|
+
/**
|
|
68
|
+
* Formats a uid as an order id in the same "0x"-prefixed form the order-book API returns in
|
|
69
|
+
* `EnrichedOrder.uid` (matching EVM order uids). Code that looks an order up by the API's uid
|
|
70
|
+
* (reducer batch actions, notifications, etc.) needs this to match whatever id was stored locally.
|
|
71
|
+
*/
|
|
72
|
+
declare function toOrderId(uid: Uint8Array): string;
|
|
67
73
|
|
|
68
74
|
interface SolanaQuoteParameters {
|
|
69
75
|
ownerAddress: PublicKeyInitData;
|
|
@@ -104,18 +110,47 @@ type SolanaSignAndSend = (instruction: TransactionInstruction) => Promise<{
|
|
|
104
110
|
signature: string;
|
|
105
111
|
}>;
|
|
106
112
|
|
|
107
|
-
/**
|
|
108
|
-
* Version-embedded seed shared by every settlement-program PDA (`SETTLEMENT_SEED` in
|
|
109
|
-
* cow-settlement-interface). Must be regenerated if `SOLANA_SETTLEMENT_PROGRAM_VERSION` changes.
|
|
110
|
-
*/
|
|
111
|
-
declare const SETTLEMENT_SEED: Uint8Array<ArrayBufferLike>;
|
|
112
113
|
/** Trailing seed identifying order PDAs (`ORDER_SEED` in cow-settlement-interface). */
|
|
113
114
|
declare const ORDER_SEED: Uint8Array<ArrayBufferLike>;
|
|
114
115
|
/**
|
|
115
116
|
* Derives the canonical order PDA and bump for an order's `uid`, matching `find_order_pda` in
|
|
116
117
|
* cow-settlement-interface.
|
|
118
|
+
*
|
|
119
|
+
* `env` selects the seed version and must describe the same deployment as `programId` — obtain the latter
|
|
120
|
+
* from `getSolanaSettlementProgramId(env)` rather than pairing them by hand.
|
|
121
|
+
*/
|
|
122
|
+
declare function findOrderPda(programId: PublicKey, uid: Uint8Array, env?: CowEnv): [PublicKey, number];
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Version-embedded seed shared by every settlement-program PDA of `env` (`SETTLEMENT_SEED` in
|
|
126
|
+
* cow-settlement-interface). Keyed by env because the version is a property of a deployment: each env's seed
|
|
127
|
+
* has to be derived from the version of the program id that env resolves to.
|
|
128
|
+
*
|
|
129
|
+
* Returns a copy: the cached seeds are mutable `Uint8Array`s, and a caller writing into one would silently
|
|
130
|
+
* corrupt every later derivation for that env.
|
|
131
|
+
*/
|
|
132
|
+
declare function getSettlementSeed(env?: CowEnv): Uint8Array;
|
|
133
|
+
|
|
134
|
+
/** Settlement program id deployed for `env`. */
|
|
135
|
+
declare function getSolanaSettlementProgramId(env?: CowEnv): PublicKey;
|
|
136
|
+
/**
|
|
137
|
+
* Derives the canonical settlement state PDA and bump, matching `find_state_pda` in
|
|
138
|
+
* cow-settlement-interface. The env's settlement seed is its only seed, so the address moves with every
|
|
139
|
+
* major/minor bump of the settlement program.
|
|
140
|
+
*
|
|
141
|
+
* `env` selects the seed version and must describe the same deployment as `programId`. Prefer
|
|
142
|
+
* `getSolanaDelegateAuthority`, which derives both from one env and so cannot mismatch.
|
|
143
|
+
*/
|
|
144
|
+
declare function findSettlementStatePda(programId: PublicKey, env?: CowEnv): [PublicKey, number];
|
|
145
|
+
/**
|
|
146
|
+
* The SPL delegate a sell-token account has to approve before the settlement program can move its
|
|
147
|
+
* funds — the Solana analogue of the EVM vault relayer spender.
|
|
148
|
+
*
|
|
149
|
+
* This is the settlement state PDA, not the program id: the program signs its token CPIs as that PDA,
|
|
150
|
+
* so approving anything else yields a delegate that cannot move the funds. Because the PDA seed carries
|
|
151
|
+
* the program's major/minor version, every delegation has to be renewed after a version bump.
|
|
117
152
|
*/
|
|
118
|
-
declare function
|
|
153
|
+
declare function getSolanaDelegateAuthority(env?: CowEnv): PublicKey;
|
|
119
154
|
|
|
120
155
|
interface CreateOrderInstructionParams {
|
|
121
156
|
programId: PublicKey;
|
|
@@ -169,4 +204,4 @@ declare class SolanaTradingSdk {
|
|
|
169
204
|
getQuote(params: SolanaQuoteParameters): Promise<QuoteAndPost>;
|
|
170
205
|
}
|
|
171
206
|
|
|
172
|
-
export { type CreateOrderInstructionParams, ENCODED_ORDER_INTENT_SIZE, JupiterAPI, type JupiterOrderRequest, type JupiterOrderResponse, ORDER_SEED,
|
|
207
|
+
export { type CreateOrderInstructionParams, ENCODED_ORDER_INTENT_SIZE, JupiterAPI, type JupiterOrderRequest, type JupiterOrderResponse, ORDER_SEED, type SolanaOrderIntent, type SolanaQuote, type SolanaQuoteParameters, type SolanaSignAndSend, SolanaTradingSdk, type SolanaTradingSdkOptions, buildCreateOrderInstruction, encodeOrderIntent, findOrderPda, findSettlementStatePda, getSettlementSeed, getSolanaDelegateAuthority, getSolanaQuote, getSolanaSettlementProgramId, hashOrderIntent, postSolanaSwapOrderFromQuote, toHex, toOrderId };
|
package/dist/index.d.ts
CHANGED
|
@@ -64,6 +64,12 @@ declare function encodeOrderIntent(intent: SolanaOrderIntent): Uint8Array;
|
|
|
64
64
|
*/
|
|
65
65
|
declare function hashOrderIntent(encoded: Uint8Array): Promise<Uint8Array>;
|
|
66
66
|
declare function toHex(bytes: Uint8Array): string;
|
|
67
|
+
/**
|
|
68
|
+
* Formats a uid as an order id in the same "0x"-prefixed form the order-book API returns in
|
|
69
|
+
* `EnrichedOrder.uid` (matching EVM order uids). Code that looks an order up by the API's uid
|
|
70
|
+
* (reducer batch actions, notifications, etc.) needs this to match whatever id was stored locally.
|
|
71
|
+
*/
|
|
72
|
+
declare function toOrderId(uid: Uint8Array): string;
|
|
67
73
|
|
|
68
74
|
interface SolanaQuoteParameters {
|
|
69
75
|
ownerAddress: PublicKeyInitData;
|
|
@@ -104,18 +110,47 @@ type SolanaSignAndSend = (instruction: TransactionInstruction) => Promise<{
|
|
|
104
110
|
signature: string;
|
|
105
111
|
}>;
|
|
106
112
|
|
|
107
|
-
/**
|
|
108
|
-
* Version-embedded seed shared by every settlement-program PDA (`SETTLEMENT_SEED` in
|
|
109
|
-
* cow-settlement-interface). Must be regenerated if `SOLANA_SETTLEMENT_PROGRAM_VERSION` changes.
|
|
110
|
-
*/
|
|
111
|
-
declare const SETTLEMENT_SEED: Uint8Array<ArrayBufferLike>;
|
|
112
113
|
/** Trailing seed identifying order PDAs (`ORDER_SEED` in cow-settlement-interface). */
|
|
113
114
|
declare const ORDER_SEED: Uint8Array<ArrayBufferLike>;
|
|
114
115
|
/**
|
|
115
116
|
* Derives the canonical order PDA and bump for an order's `uid`, matching `find_order_pda` in
|
|
116
117
|
* cow-settlement-interface.
|
|
118
|
+
*
|
|
119
|
+
* `env` selects the seed version and must describe the same deployment as `programId` — obtain the latter
|
|
120
|
+
* from `getSolanaSettlementProgramId(env)` rather than pairing them by hand.
|
|
121
|
+
*/
|
|
122
|
+
declare function findOrderPda(programId: PublicKey, uid: Uint8Array, env?: CowEnv): [PublicKey, number];
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Version-embedded seed shared by every settlement-program PDA of `env` (`SETTLEMENT_SEED` in
|
|
126
|
+
* cow-settlement-interface). Keyed by env because the version is a property of a deployment: each env's seed
|
|
127
|
+
* has to be derived from the version of the program id that env resolves to.
|
|
128
|
+
*
|
|
129
|
+
* Returns a copy: the cached seeds are mutable `Uint8Array`s, and a caller writing into one would silently
|
|
130
|
+
* corrupt every later derivation for that env.
|
|
131
|
+
*/
|
|
132
|
+
declare function getSettlementSeed(env?: CowEnv): Uint8Array;
|
|
133
|
+
|
|
134
|
+
/** Settlement program id deployed for `env`. */
|
|
135
|
+
declare function getSolanaSettlementProgramId(env?: CowEnv): PublicKey;
|
|
136
|
+
/**
|
|
137
|
+
* Derives the canonical settlement state PDA and bump, matching `find_state_pda` in
|
|
138
|
+
* cow-settlement-interface. The env's settlement seed is its only seed, so the address moves with every
|
|
139
|
+
* major/minor bump of the settlement program.
|
|
140
|
+
*
|
|
141
|
+
* `env` selects the seed version and must describe the same deployment as `programId`. Prefer
|
|
142
|
+
* `getSolanaDelegateAuthority`, which derives both from one env and so cannot mismatch.
|
|
143
|
+
*/
|
|
144
|
+
declare function findSettlementStatePda(programId: PublicKey, env?: CowEnv): [PublicKey, number];
|
|
145
|
+
/**
|
|
146
|
+
* The SPL delegate a sell-token account has to approve before the settlement program can move its
|
|
147
|
+
* funds — the Solana analogue of the EVM vault relayer spender.
|
|
148
|
+
*
|
|
149
|
+
* This is the settlement state PDA, not the program id: the program signs its token CPIs as that PDA,
|
|
150
|
+
* so approving anything else yields a delegate that cannot move the funds. Because the PDA seed carries
|
|
151
|
+
* the program's major/minor version, every delegation has to be renewed after a version bump.
|
|
117
152
|
*/
|
|
118
|
-
declare function
|
|
153
|
+
declare function getSolanaDelegateAuthority(env?: CowEnv): PublicKey;
|
|
119
154
|
|
|
120
155
|
interface CreateOrderInstructionParams {
|
|
121
156
|
programId: PublicKey;
|
|
@@ -169,4 +204,4 @@ declare class SolanaTradingSdk {
|
|
|
169
204
|
getQuote(params: SolanaQuoteParameters): Promise<QuoteAndPost>;
|
|
170
205
|
}
|
|
171
206
|
|
|
172
|
-
export { type CreateOrderInstructionParams, ENCODED_ORDER_INTENT_SIZE, JupiterAPI, type JupiterOrderRequest, type JupiterOrderResponse, ORDER_SEED,
|
|
207
|
+
export { type CreateOrderInstructionParams, ENCODED_ORDER_INTENT_SIZE, JupiterAPI, type JupiterOrderRequest, type JupiterOrderResponse, ORDER_SEED, type SolanaOrderIntent, type SolanaQuote, type SolanaQuoteParameters, type SolanaSignAndSend, SolanaTradingSdk, type SolanaTradingSdkOptions, buildCreateOrderInstruction, encodeOrderIntent, findOrderPda, findSettlementStatePda, getSettlementSeed, getSolanaDelegateAuthority, getSolanaQuote, getSolanaSettlementProgramId, hashOrderIntent, postSolanaSwapOrderFromQuote, toHex, toOrderId };
|
package/dist/index.js
CHANGED
|
@@ -23,15 +23,19 @@ __export(src_exports, {
|
|
|
23
23
|
ENCODED_ORDER_INTENT_SIZE: () => ENCODED_ORDER_INTENT_SIZE,
|
|
24
24
|
JupiterAPI: () => JupiterAPI,
|
|
25
25
|
ORDER_SEED: () => ORDER_SEED,
|
|
26
|
-
SETTLEMENT_SEED: () => SETTLEMENT_SEED,
|
|
27
26
|
SolanaTradingSdk: () => SolanaTradingSdk,
|
|
28
27
|
buildCreateOrderInstruction: () => buildCreateOrderInstruction,
|
|
29
28
|
encodeOrderIntent: () => encodeOrderIntent,
|
|
30
29
|
findOrderPda: () => findOrderPda,
|
|
30
|
+
findSettlementStatePda: () => findSettlementStatePda,
|
|
31
|
+
getSettlementSeed: () => getSettlementSeed,
|
|
32
|
+
getSolanaDelegateAuthority: () => getSolanaDelegateAuthority,
|
|
31
33
|
getSolanaQuote: () => getSolanaQuote,
|
|
34
|
+
getSolanaSettlementProgramId: () => getSolanaSettlementProgramId,
|
|
32
35
|
hashOrderIntent: () => hashOrderIntent,
|
|
33
36
|
postSolanaSwapOrderFromQuote: () => postSolanaSwapOrderFromQuote,
|
|
34
|
-
toHex: () => toHex
|
|
37
|
+
toHex: () => toHex,
|
|
38
|
+
toOrderId: () => toOrderId
|
|
35
39
|
});
|
|
36
40
|
module.exports = __toCommonJS(src_exports);
|
|
37
41
|
|
|
@@ -88,35 +92,67 @@ async function hashOrderIntent(encoded) {
|
|
|
88
92
|
function toHex(bytes) {
|
|
89
93
|
return Array.from(bytes).map((byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
90
94
|
}
|
|
95
|
+
function toOrderId(uid) {
|
|
96
|
+
return `0x${toHex(uid)}`;
|
|
97
|
+
}
|
|
91
98
|
|
|
92
99
|
// src/orderPda.ts
|
|
93
100
|
var import_web3 = require("@solana/web3.js");
|
|
101
|
+
|
|
102
|
+
// src/settlementSeed.ts
|
|
94
103
|
var import_sdk_config = require("@cowprotocol/sdk-config");
|
|
95
104
|
var SETTLEMENT_SEED_PREFIX = "settlement v";
|
|
96
105
|
var SETTLEMENT_SEED_VERSION_LEN = 7;
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
106
|
+
function buildSettlementSeed(version) {
|
|
107
|
+
if (version.length > SETTLEMENT_SEED_VERSION_LEN) {
|
|
108
|
+
throw new Error(
|
|
109
|
+
`Settlement program version "${version}" exceeds the ${SETTLEMENT_SEED_VERSION_LEN}-byte seed field`
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
return new TextEncoder().encode(SETTLEMENT_SEED_PREFIX + version.padEnd(SETTLEMENT_SEED_VERSION_LEN, " "));
|
|
113
|
+
}
|
|
114
|
+
var SETTLEMENT_SEEDS = {
|
|
115
|
+
prod: buildSettlementSeed(import_sdk_config.SOLANA_SETTLEMENT_PROGRAM_VERSION),
|
|
116
|
+
staging: buildSettlementSeed(import_sdk_config.SOLANA_SETTLEMENT_PROGRAM_VERSION_STAGING)
|
|
117
|
+
};
|
|
118
|
+
function getSettlementSeed(env = "prod") {
|
|
119
|
+
return SETTLEMENT_SEEDS[env].slice();
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// src/orderPda.ts
|
|
100
123
|
var ORDER_SEED = new TextEncoder().encode("order");
|
|
101
|
-
function findOrderPda(programId, uid) {
|
|
102
|
-
return import_web3.PublicKey.findProgramAddressSync([
|
|
124
|
+
function findOrderPda(programId, uid, env) {
|
|
125
|
+
return import_web3.PublicKey.findProgramAddressSync([getSettlementSeed(env), uid, ORDER_SEED], programId);
|
|
103
126
|
}
|
|
104
127
|
|
|
105
|
-
// src/
|
|
128
|
+
// src/statePda.ts
|
|
106
129
|
var import_web32 = require("@solana/web3.js");
|
|
130
|
+
var import_sdk_config2 = require("@cowprotocol/sdk-config");
|
|
131
|
+
function getSolanaSettlementProgramId(env) {
|
|
132
|
+
return new import_web32.PublicKey(env === "staging" ? import_sdk_config2.SOLANA_SETTLEMENT_PROGRAM_ID_STAGING : import_sdk_config2.SOLANA_SETTLEMENT_PROGRAM_ID);
|
|
133
|
+
}
|
|
134
|
+
function findSettlementStatePda(programId, env) {
|
|
135
|
+
return import_web32.PublicKey.findProgramAddressSync([getSettlementSeed(env)], programId);
|
|
136
|
+
}
|
|
137
|
+
function getSolanaDelegateAuthority(env) {
|
|
138
|
+
return findSettlementStatePda(getSolanaSettlementProgramId(env), env)[0];
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// src/createOrderInstruction.ts
|
|
142
|
+
var import_web33 = require("@solana/web3.js");
|
|
107
143
|
var CREATE_ORDER_DISCRIMINATOR = 2;
|
|
108
144
|
function buildCreateOrderInstruction(params) {
|
|
109
145
|
const intentBytes = encodeOrderIntent(params.intent);
|
|
110
146
|
const data = Buffer.alloc(1 + intentBytes.length);
|
|
111
147
|
data[0] = CREATE_ORDER_DISCRIMINATOR;
|
|
112
148
|
data.set(intentBytes, 1);
|
|
113
|
-
return new
|
|
149
|
+
return new import_web33.TransactionInstruction({
|
|
114
150
|
programId: params.programId,
|
|
115
151
|
keys: [
|
|
116
152
|
{ pubkey: params.owner, isSigner: true, isWritable: false },
|
|
117
153
|
{ pubkey: params.createdBy, isSigner: true, isWritable: true },
|
|
118
154
|
{ pubkey: params.orderPda, isSigner: false, isWritable: true },
|
|
119
|
-
{ pubkey:
|
|
155
|
+
{ pubkey: import_web33.SystemProgram.programId, isSigner: false, isWritable: false }
|
|
120
156
|
],
|
|
121
157
|
data
|
|
122
158
|
});
|
|
@@ -170,9 +206,8 @@ function isJupiterOrderResponse(body, request) {
|
|
|
170
206
|
}
|
|
171
207
|
|
|
172
208
|
// src/getSolanaQuote.ts
|
|
173
|
-
var
|
|
209
|
+
var import_web34 = require("@solana/web3.js");
|
|
174
210
|
var import_spl_token = require("@solana/spl-token");
|
|
175
|
-
var import_sdk_config2 = require("@cowprotocol/sdk-config");
|
|
176
211
|
var import_sdk_order_book2 = require("@cowprotocol/sdk-order-book");
|
|
177
212
|
var DEFAULT_VALID_FOR_SECONDS = 30 * 60;
|
|
178
213
|
var ZERO_APP_DATA = new Uint8Array(32);
|
|
@@ -193,12 +228,12 @@ async function getSolanaQuote(params, options = {}) {
|
|
|
193
228
|
if (!Number.isFinite(validForSeconds) || validForSeconds <= 0) {
|
|
194
229
|
throw new Error("validForSeconds must be a finite number greater than zero");
|
|
195
230
|
}
|
|
196
|
-
const owner = new
|
|
197
|
-
const receiver = new
|
|
198
|
-
const sellMint = new
|
|
199
|
-
const buyMint = new
|
|
200
|
-
const sellTokenProgram = sellTokenProgramId ? new
|
|
201
|
-
const buyTokenProgram = buyTokenProgramId ? new
|
|
231
|
+
const owner = new import_web34.PublicKey(ownerAddress);
|
|
232
|
+
const receiver = new import_web34.PublicKey(receiverAddress);
|
|
233
|
+
const sellMint = new import_web34.PublicKey(params.sellTokenAddress);
|
|
234
|
+
const buyMint = new import_web34.PublicKey(params.buyTokenAddress);
|
|
235
|
+
const sellTokenProgram = sellTokenProgramId ? new import_web34.PublicKey(sellTokenProgramId) : void 0;
|
|
236
|
+
const buyTokenProgram = buyTokenProgramId ? new import_web34.PublicKey(buyTokenProgramId) : void 0;
|
|
202
237
|
const sellTokenAddress = sellMint.toBase58();
|
|
203
238
|
const buyTokenAddress = buyMint.toBase58();
|
|
204
239
|
const jupiterOrder = await jupiterApi.getOrder({
|
|
@@ -248,10 +283,8 @@ async function getSolanaQuote(params, options = {}) {
|
|
|
248
283
|
};
|
|
249
284
|
const intentBytes = encodeOrderIntent(intent);
|
|
250
285
|
const uid = await hashOrderIntent(intentBytes);
|
|
251
|
-
const programId =
|
|
252
|
-
|
|
253
|
-
);
|
|
254
|
-
const [orderPda] = findOrderPda(programId, uid);
|
|
286
|
+
const programId = getSolanaSettlementProgramId(options.env);
|
|
287
|
+
const [orderPda] = findOrderPda(programId, uid, options.env);
|
|
255
288
|
const solanaQuote = {
|
|
256
289
|
intent,
|
|
257
290
|
intentBytes,
|
|
@@ -294,7 +327,7 @@ async function getSolanaQuote(params, options = {}) {
|
|
|
294
327
|
// src/postSwapOrderFromQuote.ts
|
|
295
328
|
var import_spl_token2 = require("@solana/spl-token");
|
|
296
329
|
var import_sdk_order_book3 = require("@cowprotocol/sdk-order-book");
|
|
297
|
-
var
|
|
330
|
+
var import_web35 = require("@solana/web3.js");
|
|
298
331
|
async function postSolanaSwapOrderFromQuote({ quoteResults, solanaQuote }, signAndSend, advancedSettings, signingStepManager) {
|
|
299
332
|
const intent = { ...solanaQuote.intent };
|
|
300
333
|
let uid = solanaQuote.uid;
|
|
@@ -304,7 +337,7 @@ async function postSolanaSwapOrderFromQuote({ quoteResults, solanaQuote }, signA
|
|
|
304
337
|
if (receiver) {
|
|
305
338
|
intent.buyTokenAccount = (0, import_spl_token2.getAssociatedTokenAddressSync)(
|
|
306
339
|
intent.buyMint,
|
|
307
|
-
new
|
|
340
|
+
new import_web35.PublicKey(receiver),
|
|
308
341
|
false,
|
|
309
342
|
solanaQuote.buyTokenProgramId
|
|
310
343
|
);
|
|
@@ -329,7 +362,7 @@ async function postSolanaSwapOrderFromQuote({ quoteResults, solanaQuote }, signA
|
|
|
329
362
|
await signingStepManager?.afterOrderSign?.();
|
|
330
363
|
const orderToSign = quoteResults.orderToSign;
|
|
331
364
|
return {
|
|
332
|
-
orderId:
|
|
365
|
+
orderId: toOrderId(uid),
|
|
333
366
|
txHash: signature,
|
|
334
367
|
signature,
|
|
335
368
|
signingScheme: import_sdk_order_book3.SigningScheme.PRESIGN,
|
|
@@ -355,13 +388,17 @@ var SolanaTradingSdk = class {
|
|
|
355
388
|
ENCODED_ORDER_INTENT_SIZE,
|
|
356
389
|
JupiterAPI,
|
|
357
390
|
ORDER_SEED,
|
|
358
|
-
SETTLEMENT_SEED,
|
|
359
391
|
SolanaTradingSdk,
|
|
360
392
|
buildCreateOrderInstruction,
|
|
361
393
|
encodeOrderIntent,
|
|
362
394
|
findOrderPda,
|
|
395
|
+
findSettlementStatePda,
|
|
396
|
+
getSettlementSeed,
|
|
397
|
+
getSolanaDelegateAuthority,
|
|
363
398
|
getSolanaQuote,
|
|
399
|
+
getSolanaSettlementProgramId,
|
|
364
400
|
hashOrderIntent,
|
|
365
401
|
postSolanaSwapOrderFromQuote,
|
|
366
|
-
toHex
|
|
402
|
+
toHex,
|
|
403
|
+
toOrderId
|
|
367
404
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -51,18 +51,53 @@ async function hashOrderIntent(encoded) {
|
|
|
51
51
|
function toHex(bytes) {
|
|
52
52
|
return Array.from(bytes).map((byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
53
53
|
}
|
|
54
|
+
function toOrderId(uid) {
|
|
55
|
+
return `0x${toHex(uid)}`;
|
|
56
|
+
}
|
|
54
57
|
|
|
55
58
|
// src/orderPda.ts
|
|
56
59
|
import { PublicKey } from "@solana/web3.js";
|
|
57
|
-
|
|
60
|
+
|
|
61
|
+
// src/settlementSeed.ts
|
|
62
|
+
import {
|
|
63
|
+
SOLANA_SETTLEMENT_PROGRAM_VERSION,
|
|
64
|
+
SOLANA_SETTLEMENT_PROGRAM_VERSION_STAGING
|
|
65
|
+
} from "@cowprotocol/sdk-config";
|
|
58
66
|
var SETTLEMENT_SEED_PREFIX = "settlement v";
|
|
59
67
|
var SETTLEMENT_SEED_VERSION_LEN = 7;
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
68
|
+
function buildSettlementSeed(version) {
|
|
69
|
+
if (version.length > SETTLEMENT_SEED_VERSION_LEN) {
|
|
70
|
+
throw new Error(
|
|
71
|
+
`Settlement program version "${version}" exceeds the ${SETTLEMENT_SEED_VERSION_LEN}-byte seed field`
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
return new TextEncoder().encode(SETTLEMENT_SEED_PREFIX + version.padEnd(SETTLEMENT_SEED_VERSION_LEN, " "));
|
|
75
|
+
}
|
|
76
|
+
var SETTLEMENT_SEEDS = {
|
|
77
|
+
prod: buildSettlementSeed(SOLANA_SETTLEMENT_PROGRAM_VERSION),
|
|
78
|
+
staging: buildSettlementSeed(SOLANA_SETTLEMENT_PROGRAM_VERSION_STAGING)
|
|
79
|
+
};
|
|
80
|
+
function getSettlementSeed(env = "prod") {
|
|
81
|
+
return SETTLEMENT_SEEDS[env].slice();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// src/orderPda.ts
|
|
63
85
|
var ORDER_SEED = new TextEncoder().encode("order");
|
|
64
|
-
function findOrderPda(programId, uid) {
|
|
65
|
-
return PublicKey.findProgramAddressSync([
|
|
86
|
+
function findOrderPda(programId, uid, env) {
|
|
87
|
+
return PublicKey.findProgramAddressSync([getSettlementSeed(env), uid, ORDER_SEED], programId);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// src/statePda.ts
|
|
91
|
+
import { PublicKey as PublicKey2 } from "@solana/web3.js";
|
|
92
|
+
import { SOLANA_SETTLEMENT_PROGRAM_ID, SOLANA_SETTLEMENT_PROGRAM_ID_STAGING } from "@cowprotocol/sdk-config";
|
|
93
|
+
function getSolanaSettlementProgramId(env) {
|
|
94
|
+
return new PublicKey2(env === "staging" ? SOLANA_SETTLEMENT_PROGRAM_ID_STAGING : SOLANA_SETTLEMENT_PROGRAM_ID);
|
|
95
|
+
}
|
|
96
|
+
function findSettlementStatePda(programId, env) {
|
|
97
|
+
return PublicKey2.findProgramAddressSync([getSettlementSeed(env)], programId);
|
|
98
|
+
}
|
|
99
|
+
function getSolanaDelegateAuthority(env) {
|
|
100
|
+
return findSettlementStatePda(getSolanaSettlementProgramId(env), env)[0];
|
|
66
101
|
}
|
|
67
102
|
|
|
68
103
|
// src/createOrderInstruction.ts
|
|
@@ -133,9 +168,8 @@ function isJupiterOrderResponse(body, request) {
|
|
|
133
168
|
}
|
|
134
169
|
|
|
135
170
|
// src/getSolanaQuote.ts
|
|
136
|
-
import { PublicKey as
|
|
171
|
+
import { PublicKey as PublicKey4 } from "@solana/web3.js";
|
|
137
172
|
import { getAssociatedTokenAddressSync } from "@solana/spl-token";
|
|
138
|
-
import { SOLANA_SETTLEMENT_PROGRAM_ID, SOLANA_SETTLEMENT_PROGRAM_ID_STAGING } from "@cowprotocol/sdk-config";
|
|
139
173
|
import { getQuoteAmountsAndCosts, OrderKind as OrderKind2 } from "@cowprotocol/sdk-order-book";
|
|
140
174
|
var DEFAULT_VALID_FOR_SECONDS = 30 * 60;
|
|
141
175
|
var ZERO_APP_DATA = new Uint8Array(32);
|
|
@@ -156,12 +190,12 @@ async function getSolanaQuote(params, options = {}) {
|
|
|
156
190
|
if (!Number.isFinite(validForSeconds) || validForSeconds <= 0) {
|
|
157
191
|
throw new Error("validForSeconds must be a finite number greater than zero");
|
|
158
192
|
}
|
|
159
|
-
const owner = new
|
|
160
|
-
const receiver = new
|
|
161
|
-
const sellMint = new
|
|
162
|
-
const buyMint = new
|
|
163
|
-
const sellTokenProgram = sellTokenProgramId ? new
|
|
164
|
-
const buyTokenProgram = buyTokenProgramId ? new
|
|
193
|
+
const owner = new PublicKey4(ownerAddress);
|
|
194
|
+
const receiver = new PublicKey4(receiverAddress);
|
|
195
|
+
const sellMint = new PublicKey4(params.sellTokenAddress);
|
|
196
|
+
const buyMint = new PublicKey4(params.buyTokenAddress);
|
|
197
|
+
const sellTokenProgram = sellTokenProgramId ? new PublicKey4(sellTokenProgramId) : void 0;
|
|
198
|
+
const buyTokenProgram = buyTokenProgramId ? new PublicKey4(buyTokenProgramId) : void 0;
|
|
165
199
|
const sellTokenAddress = sellMint.toBase58();
|
|
166
200
|
const buyTokenAddress = buyMint.toBase58();
|
|
167
201
|
const jupiterOrder = await jupiterApi.getOrder({
|
|
@@ -211,10 +245,8 @@ async function getSolanaQuote(params, options = {}) {
|
|
|
211
245
|
};
|
|
212
246
|
const intentBytes = encodeOrderIntent(intent);
|
|
213
247
|
const uid = await hashOrderIntent(intentBytes);
|
|
214
|
-
const programId =
|
|
215
|
-
|
|
216
|
-
);
|
|
217
|
-
const [orderPda] = findOrderPda(programId, uid);
|
|
248
|
+
const programId = getSolanaSettlementProgramId(options.env);
|
|
249
|
+
const [orderPda] = findOrderPda(programId, uid, options.env);
|
|
218
250
|
const solanaQuote = {
|
|
219
251
|
intent,
|
|
220
252
|
intentBytes,
|
|
@@ -257,7 +289,7 @@ async function getSolanaQuote(params, options = {}) {
|
|
|
257
289
|
// src/postSwapOrderFromQuote.ts
|
|
258
290
|
import { getAssociatedTokenAddressSync as getAssociatedTokenAddressSync2 } from "@solana/spl-token";
|
|
259
291
|
import { SigningScheme } from "@cowprotocol/sdk-order-book";
|
|
260
|
-
import { PublicKey as
|
|
292
|
+
import { PublicKey as PublicKey5 } from "@solana/web3.js";
|
|
261
293
|
async function postSolanaSwapOrderFromQuote({ quoteResults, solanaQuote }, signAndSend, advancedSettings, signingStepManager) {
|
|
262
294
|
const intent = { ...solanaQuote.intent };
|
|
263
295
|
let uid = solanaQuote.uid;
|
|
@@ -267,7 +299,7 @@ async function postSolanaSwapOrderFromQuote({ quoteResults, solanaQuote }, signA
|
|
|
267
299
|
if (receiver) {
|
|
268
300
|
intent.buyTokenAccount = getAssociatedTokenAddressSync2(
|
|
269
301
|
intent.buyMint,
|
|
270
|
-
new
|
|
302
|
+
new PublicKey5(receiver),
|
|
271
303
|
false,
|
|
272
304
|
solanaQuote.buyTokenProgramId
|
|
273
305
|
);
|
|
@@ -292,7 +324,7 @@ async function postSolanaSwapOrderFromQuote({ quoteResults, solanaQuote }, signA
|
|
|
292
324
|
await signingStepManager?.afterOrderSign?.();
|
|
293
325
|
const orderToSign = quoteResults.orderToSign;
|
|
294
326
|
return {
|
|
295
|
-
orderId:
|
|
327
|
+
orderId: toOrderId(uid),
|
|
296
328
|
txHash: signature,
|
|
297
329
|
signature,
|
|
298
330
|
signingScheme: SigningScheme.PRESIGN,
|
|
@@ -317,13 +349,17 @@ export {
|
|
|
317
349
|
ENCODED_ORDER_INTENT_SIZE,
|
|
318
350
|
JupiterAPI,
|
|
319
351
|
ORDER_SEED,
|
|
320
|
-
SETTLEMENT_SEED,
|
|
321
352
|
SolanaTradingSdk,
|
|
322
353
|
buildCreateOrderInstruction,
|
|
323
354
|
encodeOrderIntent,
|
|
324
355
|
findOrderPda,
|
|
356
|
+
findSettlementStatePda,
|
|
357
|
+
getSettlementSeed,
|
|
358
|
+
getSolanaDelegateAuthority,
|
|
325
359
|
getSolanaQuote,
|
|
360
|
+
getSolanaSettlementProgramId,
|
|
326
361
|
hashOrderIntent,
|
|
327
362
|
postSolanaSwapOrderFromQuote,
|
|
328
|
-
toHex
|
|
363
|
+
toHex,
|
|
364
|
+
toOrderId
|
|
329
365
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cowprotocol/sdk-trading-solana",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "CowProtocol Solana trading: Jupiter-sourced quotes and on-chain CreateOrder posting against the Solana settlement program",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -39,9 +39,9 @@
|
|
|
39
39
|
"dependencies": {
|
|
40
40
|
"@solana/spl-token": "0.4.14",
|
|
41
41
|
"@solana/web3.js": "1.98.4",
|
|
42
|
-
"@cowprotocol/sdk-
|
|
43
|
-
"@cowprotocol/sdk-
|
|
44
|
-
"@cowprotocol/sdk-
|
|
42
|
+
"@cowprotocol/sdk-trading": "2.4.1",
|
|
43
|
+
"@cowprotocol/sdk-config": "2.6.0",
|
|
44
|
+
"@cowprotocol/sdk-order-book": "4.0.4"
|
|
45
45
|
},
|
|
46
46
|
"scripts": {
|
|
47
47
|
"build": "tsup src/index.ts --format esm,cjs --dts",
|