@n1xyz/nord-ts 0.1.7 → 0.1.8
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/actions.js +39 -82
- package/dist/bundle.js +79181 -0
- package/dist/client/Nord.d.ts +2 -2
- package/dist/client/Nord.js +46 -78
- package/dist/client/NordAdmin.d.ts +2 -2
- package/dist/client/NordAdmin.js +57 -89
- package/dist/client/NordUser.js +118 -147
- package/dist/const.js +5 -8
- package/dist/error.js +7 -5
- package/dist/gen/nord_pb.js +88 -92
- package/dist/gen/openapi.d.ts +5 -6
- package/dist/gen/openapi.js +1 -2
- package/dist/index.js +10 -49
- package/dist/types.d.ts +1 -0
- package/dist/types.js +21 -60
- package/dist/utils.js +38 -86
- package/dist/websocket/NordWebSocketClient.js +12 -17
- package/dist/websocket/Subscriber.js +6 -7
- package/dist/websocket/events.js +1 -2
- package/dist/websocket/index.js +10 -15
- package/package.json +2 -3
- package/dist/api/client.d.ts +0 -14
- package/dist/api/client.js +0 -45
- package/dist/bridge/client.d.ts +0 -151
- package/dist/bridge/client.js +0 -434
- package/dist/bridge/const.d.ts +0 -23
- package/dist/bridge/const.js +0 -47
- package/dist/bridge/index.d.ts +0 -4
- package/dist/bridge/index.js +0 -23
- package/dist/bridge/types.d.ts +0 -120
- package/dist/bridge/types.js +0 -18
- package/dist/bridge/utils.d.ts +0 -64
- package/dist/bridge/utils.js +0 -131
- package/dist/gen/common.d.ts +0 -68
- package/dist/gen/common.js +0 -215
- package/dist/gen/nord.d.ts +0 -882
- package/dist/gen/nord.js +0 -6520
- package/dist/idl/bridge.d.ts +0 -569
- package/dist/idl/bridge.js +0 -8
- package/dist/idl/bridge.json +0 -1506
- package/dist/idl/index.d.ts +0 -607
- package/dist/idl/index.js +0 -8
- package/dist/nord/api/actions.d.ts +0 -126
- package/dist/nord/api/actions.js +0 -397
- package/dist/nord/api/core.d.ts +0 -16
- package/dist/nord/api/core.js +0 -81
- package/dist/nord/api/market.d.ts +0 -36
- package/dist/nord/api/market.js +0 -96
- package/dist/nord/api/metrics.d.ts +0 -67
- package/dist/nord/api/metrics.js +0 -229
- package/dist/nord/api/queries.d.ts +0 -46
- package/dist/nord/api/queries.js +0 -109
- package/dist/nord/api/triggers.d.ts +0 -7
- package/dist/nord/api/triggers.js +0 -38
- package/dist/nord/client/Nord.d.ts +0 -396
- package/dist/nord/client/Nord.js +0 -747
- package/dist/nord/client/NordAdmin.d.ts +0 -259
- package/dist/nord/client/NordAdmin.js +0 -395
- package/dist/nord/client/NordClient.d.ts +0 -33
- package/dist/nord/client/NordClient.js +0 -45
- package/dist/nord/client/NordUser.d.ts +0 -362
- package/dist/nord/client/NordUser.js +0 -781
- package/dist/nord/index.d.ts +0 -11
- package/dist/nord/index.js +0 -36
- package/dist/nord/models/Subscriber.d.ts +0 -37
- package/dist/nord/models/Subscriber.js +0 -25
- package/dist/nord/utils/NordError.d.ts +0 -35
- package/dist/nord/utils/NordError.js +0 -49
- package/src/actions.ts +0 -333
- package/src/client/Nord.ts +0 -934
- package/src/client/NordAdmin.ts +0 -484
- package/src/client/NordUser.ts +0 -1122
- package/src/const.ts +0 -34
- package/src/error.ts +0 -76
- package/src/gen/.gitkeep +0 -0
- package/src/gen/nord_pb.ts +0 -5053
- package/src/gen/openapi.ts +0 -2904
- package/src/index.ts +0 -11
- package/src/types.ts +0 -327
- package/src/utils.ts +0 -266
- package/src/websocket/NordWebSocketClient.ts +0 -316
- package/src/websocket/Subscriber.ts +0 -56
- package/src/websocket/events.ts +0 -31
- package/src/websocket/index.ts +0 -105
package/dist/nord/index.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export { Nord } from "./client/Nord";
|
|
2
|
-
export { NordUser } from "./client/NordUser";
|
|
3
|
-
export { NordAdmin, AclRole } from "./client/NordAdmin";
|
|
4
|
-
export { NordClient } from "./client/NordClient";
|
|
5
|
-
export type { NordClientParams } from "./client/NordClient";
|
|
6
|
-
export type { CreateTokenParams, CreateMarketParams, PythSetWormholeGuardiansParams, PythSetSymbolFeedParams, FreezeMarketParams, UnfreezeMarketParams, } from "./client/NordAdmin";
|
|
7
|
-
export { NordError } from "./utils/NordError";
|
|
8
|
-
export * from "./api/core";
|
|
9
|
-
export * from "./api/metrics";
|
|
10
|
-
export * from "./api/actions";
|
|
11
|
-
export * from "./models/Subscriber";
|
package/dist/nord/index.js
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.NordError = exports.NordClient = exports.AclRole = exports.NordAdmin = exports.NordUser = exports.Nord = void 0;
|
|
18
|
-
// Export main client classes
|
|
19
|
-
var Nord_1 = require("./client/Nord");
|
|
20
|
-
Object.defineProperty(exports, "Nord", { enumerable: true, get: function () { return Nord_1.Nord; } });
|
|
21
|
-
var NordUser_1 = require("./client/NordUser");
|
|
22
|
-
Object.defineProperty(exports, "NordUser", { enumerable: true, get: function () { return NordUser_1.NordUser; } });
|
|
23
|
-
var NordAdmin_1 = require("./client/NordAdmin");
|
|
24
|
-
Object.defineProperty(exports, "NordAdmin", { enumerable: true, get: function () { return NordAdmin_1.NordAdmin; } });
|
|
25
|
-
Object.defineProperty(exports, "AclRole", { enumerable: true, get: function () { return NordAdmin_1.AclRole; } });
|
|
26
|
-
var NordClient_1 = require("./client/NordClient");
|
|
27
|
-
Object.defineProperty(exports, "NordClient", { enumerable: true, get: function () { return NordClient_1.NordClient; } });
|
|
28
|
-
// Export utility classes
|
|
29
|
-
var NordError_1 = require("./utils/NordError");
|
|
30
|
-
Object.defineProperty(exports, "NordError", { enumerable: true, get: function () { return NordError_1.NordError; } });
|
|
31
|
-
// Export API modules
|
|
32
|
-
__exportStar(require("./api/core"), exports);
|
|
33
|
-
__exportStar(require("./api/metrics"), exports);
|
|
34
|
-
__exportStar(require("./api/actions"), exports);
|
|
35
|
-
// Export models
|
|
36
|
-
__exportStar(require("./models/Subscriber"), exports);
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { EventEmitter } from "events";
|
|
2
|
-
import { Account, DeltaEvent, OrderbookResponse, SubscriberConfig, StreamTrade, Trades } from "../../types";
|
|
3
|
-
/**
|
|
4
|
-
* Subscriber class for handling WebSocket subscriptions
|
|
5
|
-
*/
|
|
6
|
-
export declare class Subscriber {
|
|
7
|
-
streamURL: string;
|
|
8
|
-
buffer: (DeltaEvent | Trades | Account)[];
|
|
9
|
-
maxBufferLen: number;
|
|
10
|
-
/**
|
|
11
|
-
* Create a new Subscriber instance
|
|
12
|
-
* @param config Subscriber configuration
|
|
13
|
-
*/
|
|
14
|
-
constructor(config: SubscriberConfig);
|
|
15
|
-
/**
|
|
16
|
-
* Subscribe to WebSocket events
|
|
17
|
-
*/
|
|
18
|
-
subscribe(): void;
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Interface for orderbook subscription
|
|
22
|
-
*/
|
|
23
|
-
export interface OrderbookSubscription extends EventEmitter {
|
|
24
|
-
on(event: "message", listener: (data: OrderbookResponse) => void): this;
|
|
25
|
-
on(event: "error", listener: (error: Error) => void): this;
|
|
26
|
-
close(): void;
|
|
27
|
-
removeAllListeners(event?: string): this;
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Interface for trade subscription
|
|
31
|
-
*/
|
|
32
|
-
export interface TradeSubscription extends EventEmitter {
|
|
33
|
-
on(event: "message", listener: (data: StreamTrade[]) => void): this;
|
|
34
|
-
on(event: "error", listener: (error: Error) => void): this;
|
|
35
|
-
close(): void;
|
|
36
|
-
removeAllListeners(event?: string): this;
|
|
37
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Subscriber = void 0;
|
|
4
|
-
const utils_1 = require("../../utils");
|
|
5
|
-
/**
|
|
6
|
-
* Subscriber class for handling WebSocket subscriptions
|
|
7
|
-
*/
|
|
8
|
-
class Subscriber {
|
|
9
|
-
/**
|
|
10
|
-
* Create a new Subscriber instance
|
|
11
|
-
* @param config Subscriber configuration
|
|
12
|
-
*/
|
|
13
|
-
constructor(config) {
|
|
14
|
-
this.streamURL = config.streamURL;
|
|
15
|
-
this.buffer = [];
|
|
16
|
-
this.maxBufferLen = config.maxBufferLen ?? utils_1.MAX_BUFFER_LEN;
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Subscribe to WebSocket events
|
|
20
|
-
*/
|
|
21
|
-
subscribe() {
|
|
22
|
-
// TODO: Implement subscription logic
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
exports.Subscriber = Subscriber;
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Options for creating a NordError
|
|
3
|
-
*/
|
|
4
|
-
export interface NordErrorOptions {
|
|
5
|
-
/** The original error that caused this error */
|
|
6
|
-
cause?: unknown;
|
|
7
|
-
/** HTTP status code (if applicable) */
|
|
8
|
-
statusCode?: number;
|
|
9
|
-
/** Additional error details */
|
|
10
|
-
details?: Record<string, unknown>;
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* Custom error class for Nord-related errors
|
|
14
|
-
*/
|
|
15
|
-
export declare class NordError extends Error {
|
|
16
|
-
/** The original error that caused this error */
|
|
17
|
-
readonly cause?: unknown;
|
|
18
|
-
/** HTTP status code (if applicable) */
|
|
19
|
-
readonly statusCode?: number;
|
|
20
|
-
/** Additional error details */
|
|
21
|
-
readonly details?: Record<string, unknown>;
|
|
22
|
-
/**
|
|
23
|
-
* Create a new NordError
|
|
24
|
-
*
|
|
25
|
-
* @param message - Error message
|
|
26
|
-
* @param options - Error options
|
|
27
|
-
*/
|
|
28
|
-
constructor(message: string, options?: NordErrorOptions);
|
|
29
|
-
/**
|
|
30
|
-
* Convert the error to a string representation
|
|
31
|
-
*
|
|
32
|
-
* @returns String representation of the error
|
|
33
|
-
*/
|
|
34
|
-
toString(): string;
|
|
35
|
-
}
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.NordError = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* Custom error class for Nord-related errors
|
|
6
|
-
*/
|
|
7
|
-
class NordError extends Error {
|
|
8
|
-
/**
|
|
9
|
-
* Create a new NordError
|
|
10
|
-
*
|
|
11
|
-
* @param message - Error message
|
|
12
|
-
* @param options - Error options
|
|
13
|
-
*/
|
|
14
|
-
constructor(message, options = {}) {
|
|
15
|
-
super(message);
|
|
16
|
-
this.name = "NordError";
|
|
17
|
-
this.cause = options.cause;
|
|
18
|
-
this.statusCode = options.statusCode;
|
|
19
|
-
this.details = options.details;
|
|
20
|
-
// Capture stack trace
|
|
21
|
-
if (Error.captureStackTrace) {
|
|
22
|
-
Error.captureStackTrace(this, NordError);
|
|
23
|
-
}
|
|
24
|
-
// Handle nested errors
|
|
25
|
-
if (this.cause instanceof Error) {
|
|
26
|
-
this.stack =
|
|
27
|
-
this.stack + "\nCaused by: " + (this.cause.stack || this.cause.message);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Convert the error to a string representation
|
|
32
|
-
*
|
|
33
|
-
* @returns String representation of the error
|
|
34
|
-
*/
|
|
35
|
-
toString() {
|
|
36
|
-
let result = `${this.name}: ${this.message}`;
|
|
37
|
-
if (this.statusCode) {
|
|
38
|
-
result += ` \nstatus: ${this.statusCode}`;
|
|
39
|
-
}
|
|
40
|
-
if (this.details && Object.keys(this.details).length > 0) {
|
|
41
|
-
result += ` \ndetails: ${JSON.stringify(this.details, null, 2)}`;
|
|
42
|
-
}
|
|
43
|
-
if (this.cause) {
|
|
44
|
-
result += ` \ncause: ${this.cause.toString()}`;
|
|
45
|
-
}
|
|
46
|
-
return result;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
exports.NordError = NordError;
|
package/src/actions.ts
DELETED
|
@@ -1,333 +0,0 @@
|
|
|
1
|
-
import Decimal from "decimal.js";
|
|
2
|
-
import * as proto from "./gen/nord_pb";
|
|
3
|
-
import { paths } from "./gen/openapi";
|
|
4
|
-
import { Client } from "openapi-fetch";
|
|
5
|
-
import { create } from "@bufbuild/protobuf";
|
|
6
|
-
import { FillMode, fillModeToProtoFillMode, Side, QuoteSize } from "./types";
|
|
7
|
-
import {
|
|
8
|
-
assert,
|
|
9
|
-
BigIntValue,
|
|
10
|
-
decodeLengthDelimited,
|
|
11
|
-
SESSION_TTL,
|
|
12
|
-
toScaledU64,
|
|
13
|
-
signUserPayload,
|
|
14
|
-
} from "./utils";
|
|
15
|
-
import { sizeDelimitedEncode } from "@bufbuild/protobuf/wire";
|
|
16
|
-
import { NordError } from "./error";
|
|
17
|
-
import { PublicKey, Transaction } from "@solana/web3.js";
|
|
18
|
-
|
|
19
|
-
type ReceiptKind = NonNullable<proto.Receipt["kind"]>;
|
|
20
|
-
type ExtractReceiptKind<K extends ReceiptKind["case"]> = Extract<
|
|
21
|
-
ReceiptKind,
|
|
22
|
-
{ case: K }
|
|
23
|
-
>;
|
|
24
|
-
|
|
25
|
-
export function formatReceiptError(receipt: proto.Receipt): string {
|
|
26
|
-
if (receipt.kind?.case === "err") {
|
|
27
|
-
const err = receipt.kind.value;
|
|
28
|
-
return proto.Error[err] ?? err.toString();
|
|
29
|
-
}
|
|
30
|
-
return receipt.kind?.case ?? "unknown";
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export function expectReceiptKind<K extends ReceiptKind["case"]>(
|
|
34
|
-
receipt: proto.Receipt,
|
|
35
|
-
expected: K,
|
|
36
|
-
action: string,
|
|
37
|
-
): asserts receipt is proto.Receipt & { kind: ExtractReceiptKind<K> } {
|
|
38
|
-
if (receipt.kind?.case !== expected) {
|
|
39
|
-
const label = formatReceiptError(receipt);
|
|
40
|
-
throw new NordError(`Failed to ${action}: ${label}`);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
async function sessionSign(
|
|
45
|
-
signFn: (message: Uint8Array) => Promise<Uint8Array>,
|
|
46
|
-
message: Uint8Array,
|
|
47
|
-
): Promise<Uint8Array> {
|
|
48
|
-
const signature = await signFn(message);
|
|
49
|
-
return new Uint8Array([...message, ...signature]);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// Helper to create an action with common fields
|
|
53
|
-
export function createAction(
|
|
54
|
-
currentTimestamp: bigint,
|
|
55
|
-
nonce: number,
|
|
56
|
-
kind: proto.Action["kind"],
|
|
57
|
-
): proto.Action {
|
|
58
|
-
return create(proto.ActionSchema, {
|
|
59
|
-
currentTimestamp,
|
|
60
|
-
nonce,
|
|
61
|
-
kind,
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export async function sendAction(
|
|
66
|
-
client: Client<paths>,
|
|
67
|
-
makeSignedMessage: (message: Uint8Array) => Promise<Uint8Array>,
|
|
68
|
-
action: proto.Action,
|
|
69
|
-
): Promise<proto.Receipt> {
|
|
70
|
-
const body = await prepareAction(action, makeSignedMessage);
|
|
71
|
-
|
|
72
|
-
const response = await client.POST("/action", {
|
|
73
|
-
params: {
|
|
74
|
-
header: {
|
|
75
|
-
"content-type": "application/octet-stream",
|
|
76
|
-
},
|
|
77
|
-
},
|
|
78
|
-
body: body,
|
|
79
|
-
// NOTE: openapi-fetch ignores headers and types/const headers in schema, and always assume all things are JSON
|
|
80
|
-
// to handle multi type bodies, need these overrides and later adhoc parsing
|
|
81
|
-
bodySerializer: (body) => body,
|
|
82
|
-
parseAs: "stream",
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
if (response.error) {
|
|
86
|
-
throw new Error(
|
|
87
|
-
`Failed to ${action.kind.case}, HTTP status ${JSON.stringify(response.error)}`,
|
|
88
|
-
);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
const rawResp = new Uint8Array(await response.response.bytes());
|
|
92
|
-
|
|
93
|
-
const resp: proto.Receipt = decodeLengthDelimited(
|
|
94
|
-
rawResp,
|
|
95
|
-
proto.ReceiptSchema,
|
|
96
|
-
);
|
|
97
|
-
|
|
98
|
-
if (resp.kind?.case === "err") {
|
|
99
|
-
throw new Error(
|
|
100
|
-
`Could not execute ${action.kind.case}, reason: ${proto.Error[resp.kind.value]}`,
|
|
101
|
-
);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
return resp;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// Given action and signature function, prepare the signed message to send to server as `body`.
|
|
108
|
-
// `makeSignedMessage` must include the original message and signature.
|
|
109
|
-
export async function prepareAction(
|
|
110
|
-
action: proto.Action,
|
|
111
|
-
makeSignedMessage: (message: Uint8Array) => Promise<Uint8Array>,
|
|
112
|
-
) {
|
|
113
|
-
const encoded = sizeDelimitedEncode(proto.ActionSchema, action);
|
|
114
|
-
// NOTE(agent): keep in sync with MAX_ENCODED_ACTION_SIZE in Rust code
|
|
115
|
-
const MAX_ENCODED_ACTION_SIZE = 1024;
|
|
116
|
-
if (encoded.byteLength > MAX_ENCODED_ACTION_SIZE) {
|
|
117
|
-
console.warn("Encoded message:", encoded);
|
|
118
|
-
throw new Error(
|
|
119
|
-
`Encoded message size (${encoded.byteLength} bytes) is greater than max payload size (${MAX_ENCODED_ACTION_SIZE} bytes).`,
|
|
120
|
-
);
|
|
121
|
-
}
|
|
122
|
-
const body = await makeSignedMessage(encoded);
|
|
123
|
-
if (body.byteLength > MAX_ENCODED_ACTION_SIZE) {
|
|
124
|
-
console.warn("Encoded length:", encoded.byteLength);
|
|
125
|
-
throw new Error(
|
|
126
|
-
`Signed message size (${body.byteLength} bytes) is greater than max payload size (${MAX_ENCODED_ACTION_SIZE} bytes).`,
|
|
127
|
-
);
|
|
128
|
-
}
|
|
129
|
-
return body;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
export async function createSession(
|
|
133
|
-
client: Client<paths>,
|
|
134
|
-
signTransaction: (tx: Transaction) => Promise<Transaction>,
|
|
135
|
-
currentTimestamp: bigint,
|
|
136
|
-
nonce: number,
|
|
137
|
-
params: {
|
|
138
|
-
userPubkey: PublicKey;
|
|
139
|
-
sessionPubkey: PublicKey;
|
|
140
|
-
// If not specified, set to current moment plus default session TTL
|
|
141
|
-
expiryTimestamp?: bigint;
|
|
142
|
-
},
|
|
143
|
-
): Promise<{ actionId: bigint; sessionId: bigint }> {
|
|
144
|
-
let expiry = 0n;
|
|
145
|
-
|
|
146
|
-
if (params.expiryTimestamp !== undefined) {
|
|
147
|
-
expiry = params.expiryTimestamp;
|
|
148
|
-
assert(
|
|
149
|
-
expiry > currentTimestamp,
|
|
150
|
-
"Cannot set expiry timestamp in the past",
|
|
151
|
-
);
|
|
152
|
-
} else {
|
|
153
|
-
expiry = currentTimestamp + SESSION_TTL;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
const action = createAction(currentTimestamp, nonce, {
|
|
157
|
-
case: "createSession",
|
|
158
|
-
value: create(proto.Action_CreateSessionSchema, {
|
|
159
|
-
userPubkey: params.userPubkey.toBytes(),
|
|
160
|
-
blstPubkey: params.sessionPubkey.toBytes(),
|
|
161
|
-
expiryTimestamp: expiry,
|
|
162
|
-
}),
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
const resp = await sendAction(
|
|
166
|
-
client,
|
|
167
|
-
async (payload) => {
|
|
168
|
-
return new Uint8Array([
|
|
169
|
-
...payload,
|
|
170
|
-
...(await signUserPayload({
|
|
171
|
-
payload,
|
|
172
|
-
user: params.userPubkey,
|
|
173
|
-
signTransaction,
|
|
174
|
-
})),
|
|
175
|
-
]);
|
|
176
|
-
},
|
|
177
|
-
action,
|
|
178
|
-
);
|
|
179
|
-
|
|
180
|
-
if (resp.kind?.case === "createSessionResult") {
|
|
181
|
-
return {
|
|
182
|
-
actionId: resp.actionId,
|
|
183
|
-
sessionId: resp.kind.value.sessionId,
|
|
184
|
-
};
|
|
185
|
-
} else {
|
|
186
|
-
throw new Error(`Unexpected receipt kind ${resp.kind?.case}`);
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
export async function revokeSession(
|
|
191
|
-
client: Client<paths>,
|
|
192
|
-
signTransaction: (tx: Transaction) => Promise<Transaction>,
|
|
193
|
-
currentTimestamp: bigint,
|
|
194
|
-
nonce: number,
|
|
195
|
-
params: {
|
|
196
|
-
sessionId: BigIntValue;
|
|
197
|
-
userPubkey: PublicKey;
|
|
198
|
-
},
|
|
199
|
-
): Promise<{ actionId: bigint }> {
|
|
200
|
-
const action = createAction(currentTimestamp, nonce, {
|
|
201
|
-
case: "revokeSession",
|
|
202
|
-
value: create(proto.Action_RevokeSessionSchema, {
|
|
203
|
-
sessionId: BigInt(params.sessionId),
|
|
204
|
-
}),
|
|
205
|
-
});
|
|
206
|
-
|
|
207
|
-
const resp = await sendAction(
|
|
208
|
-
client,
|
|
209
|
-
async (payload) => {
|
|
210
|
-
return new Uint8Array([
|
|
211
|
-
...payload,
|
|
212
|
-
...(await signUserPayload({
|
|
213
|
-
payload,
|
|
214
|
-
user: params.userPubkey,
|
|
215
|
-
signTransaction,
|
|
216
|
-
})),
|
|
217
|
-
]);
|
|
218
|
-
},
|
|
219
|
-
action,
|
|
220
|
-
);
|
|
221
|
-
|
|
222
|
-
return { actionId: resp.actionId };
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
export type AtomicSubaction =
|
|
226
|
-
| {
|
|
227
|
-
kind: "place";
|
|
228
|
-
// Market and order parameters – identical semantics to placeOrder()
|
|
229
|
-
marketId: number;
|
|
230
|
-
side: Side;
|
|
231
|
-
fillMode: FillMode;
|
|
232
|
-
isReduceOnly: boolean;
|
|
233
|
-
// decimals for scaling
|
|
234
|
-
sizeDecimals: number;
|
|
235
|
-
priceDecimals: number;
|
|
236
|
-
// at least one of the three has to be specified; 0 treated as "not set"
|
|
237
|
-
size?: Decimal.Value;
|
|
238
|
-
price?: Decimal.Value;
|
|
239
|
-
quoteSize?: QuoteSize;
|
|
240
|
-
clientOrderId?: BigIntValue;
|
|
241
|
-
}
|
|
242
|
-
| {
|
|
243
|
-
kind: "cancel";
|
|
244
|
-
orderId: BigIntValue;
|
|
245
|
-
};
|
|
246
|
-
|
|
247
|
-
export async function atomic(
|
|
248
|
-
client: Client<paths>,
|
|
249
|
-
signFn: (message: Uint8Array) => Promise<Uint8Array>,
|
|
250
|
-
currentTimestamp: bigint,
|
|
251
|
-
nonce: number,
|
|
252
|
-
params: {
|
|
253
|
-
sessionId: BigIntValue;
|
|
254
|
-
accountId?: number;
|
|
255
|
-
actions: AtomicSubaction[];
|
|
256
|
-
},
|
|
257
|
-
): Promise<{
|
|
258
|
-
actionId: bigint;
|
|
259
|
-
results: proto.Receipt_AtomicSubactionResultKind[];
|
|
260
|
-
}> {
|
|
261
|
-
assert(
|
|
262
|
-
params.actions.length > 0 && params.actions.length <= 4,
|
|
263
|
-
"Atomic action must contain between 1 and 4 sub-actions",
|
|
264
|
-
);
|
|
265
|
-
|
|
266
|
-
const subactions: proto.AtomicSubactionKind[] = params.actions.map((a) => {
|
|
267
|
-
if (a.kind === "place") {
|
|
268
|
-
const price = toScaledU64(a.price ?? 0, a.priceDecimals);
|
|
269
|
-
const size = toScaledU64(a.size ?? 0, a.sizeDecimals);
|
|
270
|
-
const scaledQuote = a.quoteSize
|
|
271
|
-
? a.quoteSize.toWire(a.priceDecimals, a.sizeDecimals)
|
|
272
|
-
: undefined;
|
|
273
|
-
|
|
274
|
-
// Require at least one limit to be set (non-zero size, non-zero price, or quoteSize)
|
|
275
|
-
assert(
|
|
276
|
-
price > 0n || size > 0n || scaledQuote !== undefined,
|
|
277
|
-
"OrderLimit must include at least one of: size, price, or quoteSize",
|
|
278
|
-
);
|
|
279
|
-
|
|
280
|
-
const tradeOrPlace: proto.TradeOrPlace = create(
|
|
281
|
-
proto.TradeOrPlaceSchema,
|
|
282
|
-
{
|
|
283
|
-
marketId: a.marketId,
|
|
284
|
-
orderType: create(proto.OrderTypeSchema, {
|
|
285
|
-
side: a.side === Side.Bid ? proto.Side.BID : proto.Side.ASK,
|
|
286
|
-
fillMode: fillModeToProtoFillMode(a.fillMode),
|
|
287
|
-
isReduceOnly: a.isReduceOnly,
|
|
288
|
-
}),
|
|
289
|
-
limit: create(proto.OrderLimitSchema, {
|
|
290
|
-
price,
|
|
291
|
-
size,
|
|
292
|
-
quoteSize:
|
|
293
|
-
scaledQuote === undefined
|
|
294
|
-
? undefined
|
|
295
|
-
: create(proto.QuoteSizeSchema, {
|
|
296
|
-
size: scaledQuote.size,
|
|
297
|
-
price: scaledQuote.price,
|
|
298
|
-
}),
|
|
299
|
-
}),
|
|
300
|
-
clientOrderId:
|
|
301
|
-
a.clientOrderId === undefined ? undefined : BigInt(a.clientOrderId),
|
|
302
|
-
},
|
|
303
|
-
);
|
|
304
|
-
return create(proto.AtomicSubactionKindSchema, {
|
|
305
|
-
inner: { case: "tradeOrPlace", value: tradeOrPlace },
|
|
306
|
-
});
|
|
307
|
-
}
|
|
308
|
-
return create(proto.AtomicSubactionKindSchema, {
|
|
309
|
-
inner: {
|
|
310
|
-
case: "cancelOrder",
|
|
311
|
-
value: create(proto.CancelOrderSchema, { orderId: BigInt(a.orderId) }),
|
|
312
|
-
},
|
|
313
|
-
});
|
|
314
|
-
});
|
|
315
|
-
|
|
316
|
-
const action = createAction(currentTimestamp, nonce, {
|
|
317
|
-
case: "atomic",
|
|
318
|
-
value: create(proto.AtomicSchema, {
|
|
319
|
-
sessionId: BigInt(params.sessionId),
|
|
320
|
-
accountId: params.accountId, // optional
|
|
321
|
-
actions: subactions,
|
|
322
|
-
}),
|
|
323
|
-
});
|
|
324
|
-
|
|
325
|
-
const resp = await sendAction(client, (m) => sessionSign(signFn, m), action);
|
|
326
|
-
if (resp.kind?.case === "atomic") {
|
|
327
|
-
return {
|
|
328
|
-
actionId: resp.actionId,
|
|
329
|
-
results: resp.kind.value.results,
|
|
330
|
-
};
|
|
331
|
-
}
|
|
332
|
-
throw new Error(`Unexpected receipt kind ${resp.kind?.case}`);
|
|
333
|
-
}
|