@depthhq/sdk 0.1.11 → 0.1.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/dist/catalog/data.js +1 -1
- package/dist/chain/network.d.ts +1 -1
- package/dist/index.js +82 -53
- package/dist/operation/error.d.ts +1 -0
- package/package.json +3 -3
package/dist/catalog/data.js
CHANGED
package/dist/chain/network.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ export declare const environments: {
|
|
|
6
6
|
};
|
|
7
7
|
readonly devnet: {
|
|
8
8
|
readonly rpc: "https://api.devnet.solana.com";
|
|
9
|
-
readonly service: "https://
|
|
9
|
+
readonly service: "https://api.trydepth.xyz";
|
|
10
10
|
readonly genesis: "EtWTRABZaYq6iMfeYKouRu166VU2xqa1wcaWoxPkrZBG";
|
|
11
11
|
};
|
|
12
12
|
readonly "mainnet-beta": {
|
package/dist/index.js
CHANGED
|
@@ -44575,7 +44575,7 @@ var environments = {
|
|
|
44575
44575
|
},
|
|
44576
44576
|
devnet: {
|
|
44577
44577
|
rpc: "https://api.devnet.solana.com",
|
|
44578
|
-
service: "https://
|
|
44578
|
+
service: "https://api.trydepth.xyz",
|
|
44579
44579
|
genesis: "EtWTRABZaYq6iMfeYKouRu166VU2xqa1wcaWoxPkrZBG"
|
|
44580
44580
|
},
|
|
44581
44581
|
"mainnet-beta": {
|
|
@@ -44617,6 +44617,10 @@ class DepthError extends Error {
|
|
|
44617
44617
|
this.name = "DepthError";
|
|
44618
44618
|
}
|
|
44619
44619
|
}
|
|
44620
|
+
function admissionChanged(cause) {
|
|
44621
|
+
const error = cause instanceof AnchorError ? cause : cause instanceof SendTransactionError && cause.logs ? AnchorError.parse(cause.logs) : undefined;
|
|
44622
|
+
return ["WrongQuoteSnapshot", "WrongOrderStatus"].includes(error?.error.errorCode.code ?? "");
|
|
44623
|
+
}
|
|
44620
44624
|
function submissionError(operationId, cause) {
|
|
44621
44625
|
if (cause instanceof DepthError)
|
|
44622
44626
|
throw cause;
|
|
@@ -47881,62 +47885,87 @@ class DepthClient {
|
|
|
47881
47885
|
const cipher = new RescueCipher(x25519.getSharedSecret(secret, mxePublicKey));
|
|
47882
47886
|
const offset2 = nonce();
|
|
47883
47887
|
const queue = await this.queue("match_order", offset2);
|
|
47884
|
-
let
|
|
47885
|
-
|
|
47886
|
-
for (
|
|
47888
|
+
let worstPriceE8;
|
|
47889
|
+
let signedDeadline;
|
|
47890
|
+
for (let attempt = 0;attempt < 3; attempt++) {
|
|
47891
|
+
let keys = [];
|
|
47892
|
+
const quoteDeadline = Date.now() + 60000;
|
|
47893
|
+
for (;; ) {
|
|
47894
|
+
try {
|
|
47895
|
+
const snapshot = await quoteSnapshot(this.options.connection, this.program, this.marketAddress, market.state);
|
|
47896
|
+
market = { ...market, state: snapshot.market };
|
|
47897
|
+
deadline = signedDeadline ?? tradeDeadline(input, Math.floor(Date.now() / 1000), market.state.config.maxOrderQueueSeconds);
|
|
47898
|
+
this.validateTrade(market.state, protocol, account, app, appId, intent, deadline);
|
|
47899
|
+
if (!account.version.eq(new anchor9.BN(position.version.toString())))
|
|
47900
|
+
throw new Error("Private position changed while preparing the trade");
|
|
47901
|
+
keys = snapshot.keys;
|
|
47902
|
+
break;
|
|
47903
|
+
} catch (error) {
|
|
47904
|
+
if (!(error instanceof Error) || ![
|
|
47905
|
+
"Waiting for maker liquidity",
|
|
47906
|
+
"Market is processing another order",
|
|
47907
|
+
"Waiting for a fresh oracle update",
|
|
47908
|
+
"Oracle update is stale"
|
|
47909
|
+
].includes(error.message) || Date.now() >= quoteDeadline)
|
|
47910
|
+
throw error;
|
|
47911
|
+
[market, protocol, account, app] = await this.waitForMarket(input, appId, intent);
|
|
47912
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
47913
|
+
}
|
|
47914
|
+
}
|
|
47915
|
+
signedDeadline = deadline;
|
|
47916
|
+
worstPriceE8 ??= tradeLimit(input, direction, BigInt(market.state.oraclePriceE8.toString()));
|
|
47917
|
+
const ciphertexts = cipher.encrypt(circuits2.OrderInput.pack({
|
|
47918
|
+
side: encodeSide(direction),
|
|
47919
|
+
amount,
|
|
47920
|
+
margin: position.collateral,
|
|
47921
|
+
limit_price: worstPriceE8,
|
|
47922
|
+
max_fee: input.maxFee,
|
|
47923
|
+
intent: BigInt(["increase", "reduce", "close", "reverse"].indexOf(intent)),
|
|
47924
|
+
version: position.version
|
|
47925
|
+
}), inputNonce);
|
|
47926
|
+
let signature;
|
|
47887
47927
|
try {
|
|
47888
|
-
|
|
47889
|
-
|
|
47890
|
-
|
|
47891
|
-
|
|
47892
|
-
|
|
47893
|
-
|
|
47894
|
-
|
|
47895
|
-
|
|
47928
|
+
signature = await this.program.methods.submitOrder(offset2, orderNonce, appId, app.feeBps, app.version, new anchor9.BN(deadline), market.state.revision, market.state.oracleTimestamp, ciphertexts, Array.from(inputKey), new anchor9.BN(inputNonce, "le")).accountsPartial({
|
|
47929
|
+
...queue,
|
|
47930
|
+
trader: this.options.wallet.publicKey,
|
|
47931
|
+
protocol: this.protocolAddress,
|
|
47932
|
+
appConfig: appAddress2(appId),
|
|
47933
|
+
market: this.marketAddress,
|
|
47934
|
+
marketAccount: this.accountAddress(),
|
|
47935
|
+
order
|
|
47936
|
+
}).remainingAccounts([
|
|
47937
|
+
...keys.map((pubkey) => ({
|
|
47938
|
+
pubkey,
|
|
47939
|
+
isSigner: false,
|
|
47940
|
+
isWritable: false
|
|
47941
|
+
})),
|
|
47942
|
+
{
|
|
47943
|
+
pubkey: recoveryConfigAddress2(),
|
|
47944
|
+
isSigner: false,
|
|
47945
|
+
isWritable: false
|
|
47946
|
+
},
|
|
47947
|
+
{
|
|
47948
|
+
pubkey: pda([Buffer2.from("fee-recovery"), order.toBuffer()]),
|
|
47949
|
+
isSigner: false,
|
|
47950
|
+
isWritable: true
|
|
47951
|
+
}
|
|
47952
|
+
]).rpc({ commitment: "confirmed" });
|
|
47896
47953
|
} catch (error) {
|
|
47897
|
-
if (
|
|
47898
|
-
|
|
47899
|
-
|
|
47900
|
-
|
|
47901
|
-
|
|
47902
|
-
|
|
47903
|
-
|
|
47904
|
-
|
|
47905
|
-
|
|
47954
|
+
if (attempt === 2 || !admissionChanged(error))
|
|
47955
|
+
submissionError(order.toBase58(), error);
|
|
47956
|
+
if (await this.options.connection.getAccountInfo(order, "confirmed")) {
|
|
47957
|
+
const operation = this.trade.operation(clientNonce);
|
|
47958
|
+
const snapshot = await operation.status();
|
|
47959
|
+
if (snapshot.owner !== this.options.wallet.publicKey.toBase58())
|
|
47960
|
+
throw new Error("Order owner does not match");
|
|
47961
|
+
return operation;
|
|
47962
|
+
}
|
|
47963
|
+
[market, protocol, account, app] = await this.waitForMarket({ ...input, validForSeconds: undefined, expiresAt: signedDeadline }, appId, intent);
|
|
47964
|
+
continue;
|
|
47906
47965
|
}
|
|
47966
|
+
return new DepthOperation(order, signature, this.program, this.options.connection, (id) => this.requestFillView(id), position);
|
|
47907
47967
|
}
|
|
47908
|
-
|
|
47909
|
-
const ciphertexts = cipher.encrypt(circuits2.OrderInput.pack({
|
|
47910
|
-
side: encodeSide(direction),
|
|
47911
|
-
amount,
|
|
47912
|
-
margin: position.collateral,
|
|
47913
|
-
limit_price: worstPriceE8,
|
|
47914
|
-
max_fee: input.maxFee,
|
|
47915
|
-
intent: BigInt(["increase", "reduce", "close", "reverse"].indexOf(intent)),
|
|
47916
|
-
version: position.version
|
|
47917
|
-
}), inputNonce);
|
|
47918
|
-
const signature = await this.program.methods.submitOrder(offset2, orderNonce, appId, app.feeBps, app.version, new anchor9.BN(deadline), market.state.revision, market.state.oracleTimestamp, ciphertexts, Array.from(inputKey), new anchor9.BN(inputNonce, "le")).accountsPartial({
|
|
47919
|
-
...queue,
|
|
47920
|
-
trader: this.options.wallet.publicKey,
|
|
47921
|
-
protocol: this.protocolAddress,
|
|
47922
|
-
appConfig: appAddress2(appId),
|
|
47923
|
-
market: this.marketAddress,
|
|
47924
|
-
marketAccount: this.accountAddress(),
|
|
47925
|
-
order
|
|
47926
|
-
}).remainingAccounts([
|
|
47927
|
-
...keys.map((pubkey) => ({
|
|
47928
|
-
pubkey,
|
|
47929
|
-
isSigner: false,
|
|
47930
|
-
isWritable: false
|
|
47931
|
-
})),
|
|
47932
|
-
{ pubkey: recoveryConfigAddress2(), isSigner: false, isWritable: false },
|
|
47933
|
-
{
|
|
47934
|
-
pubkey: pda([Buffer2.from("fee-recovery"), order.toBuffer()]),
|
|
47935
|
-
isSigner: false,
|
|
47936
|
-
isWritable: true
|
|
47937
|
-
}
|
|
47938
|
-
]).rpc({ commitment: "confirmed" }).catch((error) => submissionError(order.toBase58(), error));
|
|
47939
|
-
return new DepthOperation(order, signature, this.program, this.options.connection, (id) => this.requestFillView(id), position);
|
|
47968
|
+
throw new Error("Quote snapshot did not stabilize before submission");
|
|
47940
47969
|
}
|
|
47941
47970
|
async waitForMarket(input, appId, intent) {
|
|
47942
47971
|
const until = Date.now() + 60000;
|
|
@@ -7,4 +7,5 @@ export declare class DepthError extends Error {
|
|
|
7
7
|
readonly operationId?: string | undefined;
|
|
8
8
|
constructor(code: string, stage: ErrorStage, message: string, retryable: boolean, nextAction: string, operationId?: string | undefined, options?: ErrorOptions);
|
|
9
9
|
}
|
|
10
|
+
export declare function admissionChanged(cause: unknown): boolean;
|
|
10
11
|
export declare function submissionError(operationId: string, cause: unknown): never;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@depthhq/sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.12",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -44,8 +44,8 @@
|
|
|
44
44
|
"@noble/hashes": "1.8.0",
|
|
45
45
|
"@solana/web3.js": "1.99.0",
|
|
46
46
|
"buffer": "6.0.3",
|
|
47
|
-
"@depthhq/
|
|
48
|
-
"@depthhq/
|
|
47
|
+
"@depthhq/schema": "0.1.2",
|
|
48
|
+
"@depthhq/bindings": "0.1.5"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
51
|
"fake-indexeddb": "6.2.4"
|