@depthhq/sdk 0.1.11 → 0.1.13
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 +87 -54
- 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;
|
|
@@ -47881,62 +47885,88 @@ 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
|
+
"Market account is processing another operation",
|
|
47908
|
+
"Waiting for a fresh oracle update",
|
|
47909
|
+
"Oracle update is stale"
|
|
47910
|
+
].includes(error.message) || Date.now() >= quoteDeadline)
|
|
47911
|
+
throw error;
|
|
47912
|
+
[market, protocol, account, app] = await this.waitForMarket(input, appId, intent);
|
|
47913
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
47914
|
+
}
|
|
47915
|
+
}
|
|
47916
|
+
signedDeadline = deadline;
|
|
47917
|
+
worstPriceE8 ??= tradeLimit(input, direction, BigInt(market.state.oraclePriceE8.toString()));
|
|
47918
|
+
const ciphertexts = cipher.encrypt(circuits2.OrderInput.pack({
|
|
47919
|
+
side: encodeSide(direction),
|
|
47920
|
+
amount,
|
|
47921
|
+
margin: position.collateral,
|
|
47922
|
+
limit_price: worstPriceE8,
|
|
47923
|
+
max_fee: input.maxFee,
|
|
47924
|
+
intent: BigInt(["increase", "reduce", "close", "reverse"].indexOf(intent)),
|
|
47925
|
+
version: position.version
|
|
47926
|
+
}), inputNonce);
|
|
47927
|
+
let signature;
|
|
47887
47928
|
try {
|
|
47888
|
-
|
|
47889
|
-
|
|
47890
|
-
|
|
47891
|
-
|
|
47892
|
-
|
|
47893
|
-
|
|
47894
|
-
|
|
47895
|
-
|
|
47929
|
+
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({
|
|
47930
|
+
...queue,
|
|
47931
|
+
trader: this.options.wallet.publicKey,
|
|
47932
|
+
protocol: this.protocolAddress,
|
|
47933
|
+
appConfig: appAddress2(appId),
|
|
47934
|
+
market: this.marketAddress,
|
|
47935
|
+
marketAccount: this.accountAddress(),
|
|
47936
|
+
order
|
|
47937
|
+
}).remainingAccounts([
|
|
47938
|
+
...keys.map((pubkey) => ({
|
|
47939
|
+
pubkey,
|
|
47940
|
+
isSigner: false,
|
|
47941
|
+
isWritable: false
|
|
47942
|
+
})),
|
|
47943
|
+
{
|
|
47944
|
+
pubkey: recoveryConfigAddress2(),
|
|
47945
|
+
isSigner: false,
|
|
47946
|
+
isWritable: false
|
|
47947
|
+
},
|
|
47948
|
+
{
|
|
47949
|
+
pubkey: pda([Buffer2.from("fee-recovery"), order.toBuffer()]),
|
|
47950
|
+
isSigner: false,
|
|
47951
|
+
isWritable: true
|
|
47952
|
+
}
|
|
47953
|
+
]).rpc({ commitment: "confirmed" });
|
|
47896
47954
|
} catch (error) {
|
|
47897
|
-
if (
|
|
47898
|
-
|
|
47899
|
-
|
|
47900
|
-
|
|
47901
|
-
|
|
47902
|
-
|
|
47903
|
-
|
|
47904
|
-
|
|
47905
|
-
|
|
47955
|
+
if (attempt === 2 || !admissionChanged(error))
|
|
47956
|
+
submissionError(order.toBase58(), error);
|
|
47957
|
+
if (await this.options.connection.getAccountInfo(order, "confirmed")) {
|
|
47958
|
+
const operation = this.trade.operation(clientNonce);
|
|
47959
|
+
const snapshot = await operation.status();
|
|
47960
|
+
if (snapshot.owner !== this.options.wallet.publicKey.toBase58())
|
|
47961
|
+
throw new Error("Order owner does not match");
|
|
47962
|
+
return operation;
|
|
47963
|
+
}
|
|
47964
|
+
[market, protocol, account, app] = await this.waitForMarket({ ...input, validForSeconds: undefined, expiresAt: signedDeadline }, appId, intent);
|
|
47965
|
+
continue;
|
|
47906
47966
|
}
|
|
47967
|
+
return new DepthOperation(order, signature, this.program, this.options.connection, (id) => this.requestFillView(id), position);
|
|
47907
47968
|
}
|
|
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);
|
|
47969
|
+
throw new Error("Quote snapshot did not stabilize before submission");
|
|
47940
47970
|
}
|
|
47941
47971
|
async waitForMarket(input, appId, intent) {
|
|
47942
47972
|
const until = Date.now() + 60000;
|
|
@@ -47955,6 +47985,7 @@ class DepthClient {
|
|
|
47955
47985
|
if (!(error instanceof Error) || ![
|
|
47956
47986
|
"Waiting for maker liquidity",
|
|
47957
47987
|
"Market is processing another order",
|
|
47988
|
+
"Market account is processing another operation",
|
|
47958
47989
|
"Waiting for a fresh oracle update",
|
|
47959
47990
|
"Oracle update is stale"
|
|
47960
47991
|
].includes(error.message) || Date.now() >= until)
|
|
@@ -47972,9 +48003,11 @@ class DepthClient {
|
|
|
47972
48003
|
if (!app.active || !app.app.equals(appId)) {
|
|
47973
48004
|
throw new Error("App registration is inactive");
|
|
47974
48005
|
}
|
|
47975
|
-
if (
|
|
48006
|
+
if (!account.market.equals(this.marketAddress) || !account.owner.equals(this.options.wallet.publicKey)) {
|
|
47976
48007
|
throw new Error("Market account is unavailable");
|
|
47977
48008
|
}
|
|
48009
|
+
if (enumName2(account.status) !== "ready")
|
|
48010
|
+
throw new Error("Market account is processing another operation");
|
|
47978
48011
|
if (market.pendingOrders !== 0)
|
|
47979
48012
|
throw new Error("Market is processing another order");
|
|
47980
48013
|
if (market.activeQuoteCount === 0)
|
|
@@ -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;
|