@depthhq/sdk 0.1.10 → 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 +1 -1
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;
|
|
@@ -47861,7 +47865,6 @@ class DepthClient {
|
|
|
47861
47865
|
const currentSide = position.position > 0n ? "long" : "short";
|
|
47862
47866
|
const direction = intent === "reduce" || intent === "close" ? currentSide === "long" ? "short" : "long" : input.direction;
|
|
47863
47867
|
encodeSide(direction);
|
|
47864
|
-
const worstPriceE8 = tradeLimit(input, direction, BigInt(market.state.oraclePriceE8.toString()));
|
|
47865
47868
|
if ((intent === "reduce" || intent === "close" || intent === "reverse") && position.position === 0n)
|
|
47866
47869
|
throw new Error("There is no position to manage");
|
|
47867
47870
|
if (intent === "increase" && position.position !== 0n && direction !== currentSide)
|
|
@@ -47880,63 +47883,89 @@ class DepthClient {
|
|
|
47880
47883
|
const inputKey = x25519.getPublicKey(secret);
|
|
47881
47884
|
const inputNonce = randomBytes3(16);
|
|
47882
47885
|
const cipher = new RescueCipher(x25519.getSharedSecret(secret, mxePublicKey));
|
|
47883
|
-
const ciphertexts = cipher.encrypt(circuits2.OrderInput.pack({
|
|
47884
|
-
side: encodeSide(direction),
|
|
47885
|
-
amount,
|
|
47886
|
-
margin: position.collateral,
|
|
47887
|
-
limit_price: worstPriceE8,
|
|
47888
|
-
max_fee: input.maxFee,
|
|
47889
|
-
intent: BigInt(["increase", "reduce", "close", "reverse"].indexOf(intent)),
|
|
47890
|
-
version: position.version
|
|
47891
|
-
}), inputNonce);
|
|
47892
47886
|
const offset2 = nonce();
|
|
47893
47887
|
const queue = await this.queue("match_order", offset2);
|
|
47894
|
-
let
|
|
47895
|
-
|
|
47896
|
-
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;
|
|
47897
47927
|
try {
|
|
47898
|
-
|
|
47899
|
-
|
|
47900
|
-
|
|
47901
|
-
|
|
47902
|
-
|
|
47903
|
-
|
|
47904
|
-
|
|
47905
|
-
|
|
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" });
|
|
47906
47953
|
} catch (error) {
|
|
47907
|
-
if (
|
|
47908
|
-
|
|
47909
|
-
|
|
47910
|
-
|
|
47911
|
-
|
|
47912
|
-
|
|
47913
|
-
|
|
47914
|
-
|
|
47915
|
-
|
|
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;
|
|
47916
47965
|
}
|
|
47966
|
+
return new DepthOperation(order, signature, this.program, this.options.connection, (id) => this.requestFillView(id), position);
|
|
47917
47967
|
}
|
|
47918
|
-
|
|
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;
|