@vordium/sdk 0.1.3 → 0.2.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/README.md +40 -1
- package/dist/cjs/builder.js +256 -0
- package/dist/cjs/client.js +137 -8
- package/dist/cjs/http.js +12 -4
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/rounding.js +4 -0
- package/dist/cjs/types.js +4 -0
- package/dist/esm/builder.d.ts +158 -0
- package/dist/esm/builder.js +235 -0
- package/dist/esm/client.d.ts +34 -0
- package/dist/esm/client.js +138 -9
- package/dist/esm/http.d.ts +5 -0
- package/dist/esm/http.js +11 -3
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/rounding.js +4 -0
- package/dist/esm/types.d.ts +3 -0
- package/dist/esm/types.js +4 -0
- package/package.json +1 -1
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { VordiumError } from './errors.js';
|
|
2
|
+
import type { PlaceOrderValue } from './orders.js';
|
|
3
|
+
/**
|
|
4
|
+
* Builder codes: a fee an app or bot (the builder) adds to the orders it sends, up to what the trader approved.
|
|
5
|
+
*
|
|
6
|
+
* Three signed operations in the VordCore domain, each signed as 65 bytes `r‖s‖v`:
|
|
7
|
+
* - `ApproveBuilderFee` (POST /builder/approve), the trader's own key: let `builder` charge up to `maxFeeCbps`.
|
|
8
|
+
* A revoke is an approval of 0.
|
|
9
|
+
* - `PlaceOrderV3` (POST /order/place): an order that names a builder and its fee.
|
|
10
|
+
* - `ClaimBuilderFees` (POST /builder/claim), the builder's own key: move its accrued fees to its balance.
|
|
11
|
+
*
|
|
12
|
+
* Fees are in cbps, hundredths of a basis point: 1,000 cbps = 0.1 %, 500 cbps = 5 bps = 0.05 %. The network publishes
|
|
13
|
+
* its limits in its reads: the cap per market kind (`caps_cbps` {perps, spot} of the stats read), the most builders an
|
|
14
|
+
* account may approve (`max` of the approvals read), and the account value a builder needs for orders that name it to
|
|
15
|
+
* be accepted (`min_account_value_6dec`, with `account_value_ok`). The SDK checks against these reads, never against
|
|
16
|
+
* fixed figures; a read without them is refused before signing. The reads carry `active`: whether builder fees are
|
|
17
|
+
* active at the next block; nothing is signed while it is not true.
|
|
18
|
+
*/
|
|
19
|
+
export declare const BUILDER_ROUTES: {
|
|
20
|
+
readonly approve: "/builder/approve";
|
|
21
|
+
readonly claim: "/builder/claim";
|
|
22
|
+
readonly order: "/order/place";
|
|
23
|
+
};
|
|
24
|
+
/** keccak256 of each type string (checked against the strings in the tests). */
|
|
25
|
+
export declare const BUILDER_TYPEHASHES: {
|
|
26
|
+
readonly ApproveBuilderFee: "0xe63fa83d341ba3dfe6b57bc221d00f03edce9f043613f5243078e294ff0c849a";
|
|
27
|
+
readonly ClaimBuilderFees: "0xd5774785d91fe35740b030286fca8338c06609f3ba19595ee63a21d6e020500f";
|
|
28
|
+
readonly PlaceOrderV3: "0xe328eb68341f2a35a289620b7147dc72672f356f6dff3b10098ae02f19a6c5d5";
|
|
29
|
+
};
|
|
30
|
+
/** The market kinds a builder fee cap is published for (`caps_cbps` of the stats read). */
|
|
31
|
+
export declare const MARKET_KINDS: readonly ["perps", "spot"];
|
|
32
|
+
export type MarketKind = (typeof MARKET_KINDS)[number];
|
|
33
|
+
export type Caps = Record<MarketKind, number>;
|
|
34
|
+
/** An approval deadline is at most this many seconds ahead of the network's block time. */
|
|
35
|
+
export declare const MAX_AHEAD_S = 300;
|
|
36
|
+
export declare const DEFAULT_AHEAD_S = 120;
|
|
37
|
+
/** The `source` an order placed through this SDK signs. */
|
|
38
|
+
export declare const ORDER_SOURCE = "sdk";
|
|
39
|
+
/** A builder operation the network would refuse, caught before anything was signed. `code` is the network's code. */
|
|
40
|
+
export declare class BuilderError extends VordiumError {
|
|
41
|
+
constructor(code: string, message: string);
|
|
42
|
+
}
|
|
43
|
+
/** Builder fees are not active on this network. Nothing was signed. */
|
|
44
|
+
export declare class BuilderNotActive extends BuilderError {
|
|
45
|
+
constructor(message?: string);
|
|
46
|
+
}
|
|
47
|
+
export interface BuilderApprovals {
|
|
48
|
+
user: string;
|
|
49
|
+
approvals: Array<{
|
|
50
|
+
builder: string;
|
|
51
|
+
max_fee_cbps: number;
|
|
52
|
+
}>;
|
|
53
|
+
count: number;
|
|
54
|
+
max: number;
|
|
55
|
+
nonce: number;
|
|
56
|
+
active: boolean;
|
|
57
|
+
source?: string;
|
|
58
|
+
head_height?: number;
|
|
59
|
+
}
|
|
60
|
+
export interface BuilderStats {
|
|
61
|
+
builder: string;
|
|
62
|
+
volume_6dec: string;
|
|
63
|
+
fees_6dec: string;
|
|
64
|
+
unclaimed_6dec: string;
|
|
65
|
+
claimed_6dec: string;
|
|
66
|
+
users_30d: number;
|
|
67
|
+
account_value_ok: boolean;
|
|
68
|
+
min_account_value_6dec: string;
|
|
69
|
+
claim_nonce: number;
|
|
70
|
+
caps_cbps: Caps;
|
|
71
|
+
active: boolean;
|
|
72
|
+
source?: string;
|
|
73
|
+
head_height?: number;
|
|
74
|
+
}
|
|
75
|
+
/** An order's builder: its address and the fee in cbps. */
|
|
76
|
+
export interface OrderBuilder {
|
|
77
|
+
address: string;
|
|
78
|
+
feeCbps: number;
|
|
79
|
+
}
|
|
80
|
+
/** Raise {@link BuilderNotActive} unless a builder read says `active: true`. */
|
|
81
|
+
export declare function requireActive(read: unknown): void;
|
|
82
|
+
/** The caps in cbps from a stats read (`caps_cbps` {perps, spot}); a read without both is refused (`unreadable`). */
|
|
83
|
+
export declare function capsFrom(read: unknown): Caps;
|
|
84
|
+
/** A fee in plain words: 500 → "5 bps (0.05%)". */
|
|
85
|
+
export declare function describeCbps(cbps: number): string;
|
|
86
|
+
/** A builder fee on an order: above 0 and at most the cap of `kind`. */
|
|
87
|
+
export declare function checkFee(feeCbps: unknown, kind: MarketKind, caps: Caps): number;
|
|
88
|
+
/** The most a builder may charge under an approval: above 0 and at most the highest cap. 0 is a revoke. */
|
|
89
|
+
export declare function checkMaxFee(maxFeeCbps: unknown, caps: Caps): number;
|
|
90
|
+
/** The `validUntil` to sign (unix seconds): at most 300 s ahead of `nowS`, and after it. */
|
|
91
|
+
export declare function deadline(validUntil: number | null, nowS: number, aheadS?: number): number;
|
|
92
|
+
/** The fee (cbps) an approvals read shows for `builder`, or null. */
|
|
93
|
+
export declare function approvalOf(read: unknown, builder: string): number | null;
|
|
94
|
+
/** What the network checks for an order that names a builder, from the two reads. */
|
|
95
|
+
export declare function checkOrderBuilder(owner: string, builder: string, feeCbps: number, approvals: unknown, stats: unknown): void;
|
|
96
|
+
/** `{address, feeCbps}` checked: a checksummed address and a whole fee. */
|
|
97
|
+
export declare function parseBuilder(b: unknown): OrderBuilder;
|
|
98
|
+
export interface ApproveBuilderFeeValue {
|
|
99
|
+
user: string;
|
|
100
|
+
builder: string;
|
|
101
|
+
maxFeeCbps: number;
|
|
102
|
+
nonce: bigint;
|
|
103
|
+
validUntil: bigint;
|
|
104
|
+
}
|
|
105
|
+
export interface ClaimBuilderFeesValue {
|
|
106
|
+
builder: string;
|
|
107
|
+
nonce: bigint;
|
|
108
|
+
validUntil: bigint;
|
|
109
|
+
}
|
|
110
|
+
export interface PlaceOrderV3Value {
|
|
111
|
+
owner: string;
|
|
112
|
+
pairId: number;
|
|
113
|
+
side: number;
|
|
114
|
+
size: bigint;
|
|
115
|
+
price: bigint;
|
|
116
|
+
leverageBps: number;
|
|
117
|
+
orderType: number;
|
|
118
|
+
reduceOnly: boolean;
|
|
119
|
+
postOnly: boolean;
|
|
120
|
+
triggerPrice8dec: bigint;
|
|
121
|
+
triggerDir: number;
|
|
122
|
+
timeInForce: number;
|
|
123
|
+
expiresAt: bigint;
|
|
124
|
+
clientNonce: bigint;
|
|
125
|
+
validUntil: bigint;
|
|
126
|
+
worstPrice8dec: bigint;
|
|
127
|
+
source: string;
|
|
128
|
+
builder: string;
|
|
129
|
+
builderFeeCbps: number;
|
|
130
|
+
}
|
|
131
|
+
export declare function approveBuilderFeeValue(user: string, builder: string, maxFeeCbps: number, nonce: number | bigint, validUntil: number | bigint): ApproveBuilderFeeValue;
|
|
132
|
+
export declare function approveBuilderFeeBody(v: ApproveBuilderFeeValue, signature: string): Record<string, unknown>;
|
|
133
|
+
export declare function claimBuilderFeesValue(builder: string, nonce: number | bigint, validUntil: number | bigint): ClaimBuilderFeesValue;
|
|
134
|
+
export declare function claimBuilderFeesBody(v: ClaimBuilderFeesValue, signature: string): Record<string, unknown>;
|
|
135
|
+
/**
|
|
136
|
+
* The signed PlaceOrderV3 value from a checked PlaceOrder value, its deadline and its builder. Market and limit only.
|
|
137
|
+
* A market order's `worstPrice8` (already on the market's price step) is signed as both `price` (the slippage cap: the
|
|
138
|
+
* network refuses `slippage` when the market is past it) and `worstPrice8dec` (refused `worst_price`); 0 = no limit.
|
|
139
|
+
* A limit order signs its price and a worst price of 0.
|
|
140
|
+
*/
|
|
141
|
+
export declare function placeOrderV3Value(v: PlaceOrderValue, validUntil: number | bigint, b: OrderBuilder, source?: string, worstPrice8?: bigint): PlaceOrderV3Value;
|
|
142
|
+
/** The `/order/place` body of a signed PlaceOrderV3 (`signature` = the 65-byte form). A market order carries no price. */
|
|
143
|
+
export declare function placeOrderV3Body(v: PlaceOrderV3Value, signature: string): Record<string, unknown>;
|
|
144
|
+
/** An account's referral level as the network reads it (amounts are 6-decimal USDC strings). */
|
|
145
|
+
export interface ReferralLevel {
|
|
146
|
+
level: 'L1' | 'L2' | 'PARTNER' | string;
|
|
147
|
+
shareBps: number | null;
|
|
148
|
+
discountBps: number | null;
|
|
149
|
+
l2Progress: Record<string, unknown>;
|
|
150
|
+
nextCheckMs: number | null;
|
|
151
|
+
ownVolume6dec: string | null;
|
|
152
|
+
minOwnVolume6dec: string | null;
|
|
153
|
+
meetsMin: boolean | null;
|
|
154
|
+
limits: Record<string, unknown>;
|
|
155
|
+
refereeVolume6dec: string | null;
|
|
156
|
+
}
|
|
157
|
+
/** The level fields of a `/referral/stats` read, or null when it carries no `level` (levels not in force). */
|
|
158
|
+
export declare function referralLevel(read: unknown): ReferralLevel | null;
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import { getAddress } from 'ethers';
|
|
2
|
+
import { VordiumError } from './errors.js';
|
|
3
|
+
import { U32, U64, U128, uint } from './units.js';
|
|
4
|
+
/**
|
|
5
|
+
* Builder codes: a fee an app or bot (the builder) adds to the orders it sends, up to what the trader approved.
|
|
6
|
+
*
|
|
7
|
+
* Three signed operations in the VordCore domain, each signed as 65 bytes `r‖s‖v`:
|
|
8
|
+
* - `ApproveBuilderFee` (POST /builder/approve), the trader's own key: let `builder` charge up to `maxFeeCbps`.
|
|
9
|
+
* A revoke is an approval of 0.
|
|
10
|
+
* - `PlaceOrderV3` (POST /order/place): an order that names a builder and its fee.
|
|
11
|
+
* - `ClaimBuilderFees` (POST /builder/claim), the builder's own key: move its accrued fees to its balance.
|
|
12
|
+
*
|
|
13
|
+
* Fees are in cbps, hundredths of a basis point: 1,000 cbps = 0.1 %, 500 cbps = 5 bps = 0.05 %. The network publishes
|
|
14
|
+
* its limits in its reads: the cap per market kind (`caps_cbps` {perps, spot} of the stats read), the most builders an
|
|
15
|
+
* account may approve (`max` of the approvals read), and the account value a builder needs for orders that name it to
|
|
16
|
+
* be accepted (`min_account_value_6dec`, with `account_value_ok`). The SDK checks against these reads, never against
|
|
17
|
+
* fixed figures; a read without them is refused before signing. The reads carry `active`: whether builder fees are
|
|
18
|
+
* active at the next block; nothing is signed while it is not true.
|
|
19
|
+
*/
|
|
20
|
+
export const BUILDER_ROUTES = { approve: '/builder/approve', claim: '/builder/claim', order: '/order/place' };
|
|
21
|
+
/** keccak256 of each type string (checked against the strings in the tests). */
|
|
22
|
+
export const BUILDER_TYPEHASHES = {
|
|
23
|
+
ApproveBuilderFee: '0xe63fa83d341ba3dfe6b57bc221d00f03edce9f043613f5243078e294ff0c849a',
|
|
24
|
+
ClaimBuilderFees: '0xd5774785d91fe35740b030286fca8338c06609f3ba19595ee63a21d6e020500f',
|
|
25
|
+
PlaceOrderV3: '0xe328eb68341f2a35a289620b7147dc72672f356f6dff3b10098ae02f19a6c5d5',
|
|
26
|
+
};
|
|
27
|
+
/** The market kinds a builder fee cap is published for (`caps_cbps` of the stats read). */
|
|
28
|
+
export const MARKET_KINDS = ['perps', 'spot'];
|
|
29
|
+
/** An approval deadline is at most this many seconds ahead of the network's block time. */
|
|
30
|
+
export const MAX_AHEAD_S = 300;
|
|
31
|
+
export const DEFAULT_AHEAD_S = 120;
|
|
32
|
+
/** The `source` an order placed through this SDK signs. */
|
|
33
|
+
export const ORDER_SOURCE = 'sdk';
|
|
34
|
+
/** A builder operation the network would refuse, caught before anything was signed. `code` is the network's code. */
|
|
35
|
+
export class BuilderError extends VordiumError {
|
|
36
|
+
constructor(code, message) {
|
|
37
|
+
super(code, message);
|
|
38
|
+
this.name = 'BuilderError';
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/** Builder fees are not active on this network. Nothing was signed. */
|
|
42
|
+
export class BuilderNotActive extends BuilderError {
|
|
43
|
+
constructor(message = 'Builder fees are not active on this network, so nothing was signed.') {
|
|
44
|
+
super('builder_not_active', message);
|
|
45
|
+
this.name = 'BuilderNotActive';
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
function addr(v, what) {
|
|
49
|
+
try {
|
|
50
|
+
return getAddress(String(v));
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
throw new BuilderError('invalid_body', `${what} must be an address (0x and 40 hex characters).`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function whole(v, max, what) {
|
|
57
|
+
try {
|
|
58
|
+
return uint(v, max, what);
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
throw new BuilderError('invalid_body', `${what} must be a whole number from 0 to ${max}.`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/** Raise {@link BuilderNotActive} unless a builder read says `active: true`. */
|
|
65
|
+
export function requireActive(read) {
|
|
66
|
+
if (!read || typeof read !== 'object' || read.active !== true)
|
|
67
|
+
throw new BuilderNotActive();
|
|
68
|
+
}
|
|
69
|
+
/** The caps in cbps from a stats read (`caps_cbps` {perps, spot}); a read without both is refused (`unreadable`). */
|
|
70
|
+
export function capsFrom(read) {
|
|
71
|
+
const got = read && typeof read === 'object' ? read.caps_cbps : null;
|
|
72
|
+
const caps = {};
|
|
73
|
+
for (const k of MARKET_KINDS) {
|
|
74
|
+
const v = got && typeof got === 'object' ? got[k] : undefined;
|
|
75
|
+
if (typeof v !== 'number' || !Number.isSafeInteger(v) || v < 0)
|
|
76
|
+
throw new BuilderError('unreadable', "The network's builder read gives no fee caps (caps_cbps), so the fee cannot be checked and nothing was signed.");
|
|
77
|
+
caps[k] = v;
|
|
78
|
+
}
|
|
79
|
+
return caps;
|
|
80
|
+
}
|
|
81
|
+
function isKind(kind) { return kind === 'perps' || kind === 'spot'; }
|
|
82
|
+
/** A fee in plain words: 500 → "5 bps (0.05%)". */
|
|
83
|
+
export function describeCbps(cbps) {
|
|
84
|
+
const pct = (cbps / 10000).toFixed(4).replace(/0+$/, '').replace(/\.$/, '');
|
|
85
|
+
return `${cbps / 100} bps (${pct}%)`;
|
|
86
|
+
}
|
|
87
|
+
/** A builder fee on an order: above 0 and at most the cap of `kind`. */
|
|
88
|
+
export function checkFee(feeCbps, kind, caps) {
|
|
89
|
+
if (!isKind(kind))
|
|
90
|
+
throw new BuilderError('invalid_body', 'kind is perps or spot.');
|
|
91
|
+
if (!caps || typeof caps[kind] !== 'number')
|
|
92
|
+
throw new BuilderError('unreadable', 'No fee cap for this market kind; nothing was signed.');
|
|
93
|
+
const fee = Number(whole(feeCbps, U32, 'The builder fee'));
|
|
94
|
+
if (fee === 0)
|
|
95
|
+
throw new BuilderError('builder_fee_zero', 'A builder was named with a fee of 0: name no builder, or a fee above 0.');
|
|
96
|
+
if (fee > caps[kind])
|
|
97
|
+
throw new BuilderError('builder_fee_above_cap', `A builder fee of ${fee} cbps is above the ${kind} cap of ${caps[kind]} cbps (${describeCbps(caps[kind])}). Use a fee at or below the cap.`);
|
|
98
|
+
return fee;
|
|
99
|
+
}
|
|
100
|
+
/** The most a builder may charge under an approval: above 0 and at most the highest cap. 0 is a revoke. */
|
|
101
|
+
export function checkMaxFee(maxFeeCbps, caps) {
|
|
102
|
+
const fee = Number(whole(maxFeeCbps, U32, 'The approved fee'));
|
|
103
|
+
if (fee === 0)
|
|
104
|
+
throw new BuilderError('builder_fee_zero', 'An approval of 0 is a revoke: use revokeBuilderFee.');
|
|
105
|
+
const top = Math.max(caps.perps, caps.spot);
|
|
106
|
+
if (fee > top)
|
|
107
|
+
throw new BuilderError('builder_fee_above_cap', `An approved fee of ${fee} cbps is above the highest cap of ${top} cbps (${describeCbps(top)}). Approve a fee at or below it.`);
|
|
108
|
+
return fee;
|
|
109
|
+
}
|
|
110
|
+
/** The `validUntil` to sign (unix seconds): at most 300 s ahead of `nowS`, and after it. */
|
|
111
|
+
export function deadline(validUntil, nowS, aheadS = DEFAULT_AHEAD_S) {
|
|
112
|
+
const vu = validUntil ?? nowS + aheadS;
|
|
113
|
+
if (!Number.isSafeInteger(vu))
|
|
114
|
+
throw new BuilderError('invalid_body', 'validUntil must be whole unix seconds.');
|
|
115
|
+
if (vu - nowS > MAX_AHEAD_S)
|
|
116
|
+
throw new BuilderError('approval_too_far', `Not signed: the deadline is ${vu - nowS} s ahead; the network takes at most ${MAX_AHEAD_S} s.`);
|
|
117
|
+
if (vu <= nowS)
|
|
118
|
+
throw new BuilderError('approval_expired', 'Not signed: the deadline has already passed.');
|
|
119
|
+
return vu;
|
|
120
|
+
}
|
|
121
|
+
/** The fee (cbps) an approvals read shows for `builder`, or null. */
|
|
122
|
+
export function approvalOf(read, builder) {
|
|
123
|
+
const rows = read && typeof read === 'object' ? read.approvals : null;
|
|
124
|
+
const b = builder.toLowerCase();
|
|
125
|
+
for (const r of Array.isArray(rows) ? rows : []) {
|
|
126
|
+
if (r && typeof r === 'object' && String(r.builder ?? '').toLowerCase() === b) {
|
|
127
|
+
const v = r.max_fee_cbps;
|
|
128
|
+
return typeof v === 'number' && Number.isSafeInteger(v) ? v : null;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
/** What the network checks for an order that names a builder, from the two reads. */
|
|
134
|
+
export function checkOrderBuilder(owner, builder, feeCbps, approvals, stats) {
|
|
135
|
+
requireActive(approvals);
|
|
136
|
+
requireActive(stats);
|
|
137
|
+
if (owner.toLowerCase() === builder.toLowerCase())
|
|
138
|
+
throw new BuilderError('builder_self', 'An account cannot be its own builder: name another builder.');
|
|
139
|
+
const approved = approvalOf(approvals, builder);
|
|
140
|
+
if (approved === null || approved === 0)
|
|
141
|
+
throw new BuilderError('builder_not_approved', `The account has not approved builder ${builder}: approve it first (approveBuilderFee).`);
|
|
142
|
+
if (feeCbps > approved)
|
|
143
|
+
throw new BuilderError('builder_fee_above_approved', `A builder fee of ${feeCbps} cbps is above the ${approved} cbps the account approved: use a lower fee, or approve a higher one.`);
|
|
144
|
+
if (stats.account_value_ok === false) {
|
|
145
|
+
const need = stats.min_account_value_6dec;
|
|
146
|
+
const floor = typeof need === 'string' && /^\d+$/.test(need) ? `the ${usdc(BigInt(need))} USDC minimum` : "the network's minimum account value";
|
|
147
|
+
throw new BuilderError('builder_too_small', `The builder's account value is below ${floor}, so the network refuses orders that name it. The builder must fund its account first.`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
/** 6-decimal USDC as a plain decimal: 100000000n → "100", 1500000n → "1.5". */
|
|
151
|
+
function usdc(v) {
|
|
152
|
+
const whole = v / 1000000n, frac = v % 1000000n;
|
|
153
|
+
return whole.toLocaleString('en-US') + (frac ? '.' + frac.toString().padStart(6, '0').replace(/0+$/, '') : '');
|
|
154
|
+
}
|
|
155
|
+
/** `{address, feeCbps}` checked: a checksummed address and a whole fee. */
|
|
156
|
+
export function parseBuilder(b) {
|
|
157
|
+
if (!b || typeof b !== 'object')
|
|
158
|
+
throw new BuilderError('invalid_body', 'builder is { address: "0x…", feeCbps: 500 }.');
|
|
159
|
+
const o = b;
|
|
160
|
+
if (o.feeCbps === undefined || o.feeCbps === null)
|
|
161
|
+
throw new BuilderError('invalid_body', 'The builder fee is missing (feeCbps).');
|
|
162
|
+
return { address: addr(o.address, 'The builder'), feeCbps: Number(whole(o.feeCbps, U32, 'The builder fee')) };
|
|
163
|
+
}
|
|
164
|
+
export function approveBuilderFeeValue(user, builder, maxFeeCbps, nonce, validUntil) {
|
|
165
|
+
const u = addr(user, 'The account'), b = addr(builder, 'The builder');
|
|
166
|
+
if (u.toLowerCase() === b.toLowerCase())
|
|
167
|
+
throw new BuilderError('builder_self', 'An account cannot be its own builder: name another builder.');
|
|
168
|
+
return { user: u, builder: b, maxFeeCbps: Number(whole(maxFeeCbps, U32, 'The approved fee')), nonce: whole(nonce, U64, 'nonce'), validUntil: whole(validUntil, U64, 'validUntil') };
|
|
169
|
+
}
|
|
170
|
+
export function approveBuilderFeeBody(v, signature) {
|
|
171
|
+
return { user: v.user, builder: v.builder, maxFeeCbps: v.maxFeeCbps, nonce: Number(v.nonce), validUntil: Number(v.validUntil), signature };
|
|
172
|
+
}
|
|
173
|
+
export function claimBuilderFeesValue(builder, nonce, validUntil) {
|
|
174
|
+
return { builder: addr(builder, 'The builder'), nonce: whole(nonce, U64, 'nonce'), validUntil: whole(validUntil, U64, 'validUntil') };
|
|
175
|
+
}
|
|
176
|
+
export function claimBuilderFeesBody(v, signature) {
|
|
177
|
+
return { builder: v.builder, nonce: Number(v.nonce), validUntil: Number(v.validUntil), signature };
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* The signed PlaceOrderV3 value from a checked PlaceOrder value, its deadline and its builder. Market and limit only.
|
|
181
|
+
* A market order's `worstPrice8` (already on the market's price step) is signed as both `price` (the slippage cap: the
|
|
182
|
+
* network refuses `slippage` when the market is past it) and `worstPrice8dec` (refused `worst_price`); 0 = no limit.
|
|
183
|
+
* A limit order signs its price and a worst price of 0.
|
|
184
|
+
*/
|
|
185
|
+
export function placeOrderV3Value(v, validUntil, b, source = ORDER_SOURCE, worstPrice8 = 0n) {
|
|
186
|
+
if (v.orderType !== 0 && v.orderType !== 1)
|
|
187
|
+
throw new BuilderError('builder_on_stop', 'Only market and limit orders can carry a builder fee: place a stop without a builder.');
|
|
188
|
+
if (v.triggerPrice8dec !== 0n || v.triggerDir !== 0)
|
|
189
|
+
throw new BuilderError('builder_on_stop', 'A stop order cannot carry a builder fee.');
|
|
190
|
+
const worst = whole(worstPrice8, U64, 'The worst price');
|
|
191
|
+
if (v.orderType === 0 && v.price !== 0n && v.price !== worst)
|
|
192
|
+
throw new BuilderError('invalid_body', "A market order's price is its worst price: give it as worstPrice.");
|
|
193
|
+
if (v.orderType === 1 && worst !== 0n)
|
|
194
|
+
throw new BuilderError('invalid_body', "A worst price applies to a market order; a limit order's price is its limit.");
|
|
195
|
+
const price = v.orderType === 0 ? worst : whole(v.price, U64, 'price');
|
|
196
|
+
return {
|
|
197
|
+
owner: addr(v.owner, 'The account'), pairId: v.pairId, side: v.side, size: whole(v.size, U128, 'size'), price,
|
|
198
|
+
leverageBps: v.leverageBps, orderType: v.orderType, reduceOnly: v.reduceOnly, postOnly: v.postOnly, triggerPrice8dec: 0n, triggerDir: 0,
|
|
199
|
+
timeInForce: 0, expiresAt: 0n, clientNonce: whole(v.clientNonce, U64, 'clientNonce'), validUntil: whole(validUntil, U64, 'validUntil'),
|
|
200
|
+
worstPrice8dec: v.orderType === 0 ? worst : 0n, source, builder: addr(b.address, 'The builder'), builderFeeCbps: Number(whole(b.feeCbps, U32, 'builderFeeCbps')),
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
const SIDE_NAME = ['buy', 'sell'];
|
|
204
|
+
const TYPE_NAME = ['Market', 'Limit'];
|
|
205
|
+
/** The `/order/place` body of a signed PlaceOrderV3 (`signature` = the 65-byte form). A market order carries no price. */
|
|
206
|
+
export function placeOrderV3Body(v, signature) {
|
|
207
|
+
const body = {
|
|
208
|
+
owner: v.owner, pairId: v.pairId, side: SIDE_NAME[v.side], size_18dec: v.size.toString(), orderType: TYPE_NAME[v.orderType],
|
|
209
|
+
leverageBps: v.leverageBps, clientNonce: v.clientNonce.toString(), validUntil: v.validUntil.toString(), worstPrice8dec: v.worstPrice8dec.toString(),
|
|
210
|
+
source: v.source, builder: v.builder, builderFeeCbps: v.builderFeeCbps, signature,
|
|
211
|
+
};
|
|
212
|
+
if (v.price !== 0n)
|
|
213
|
+
body.price_8dec = v.price.toString();
|
|
214
|
+
if (v.reduceOnly)
|
|
215
|
+
body.reduceOnly = true;
|
|
216
|
+
if (v.postOnly)
|
|
217
|
+
body.postOnly = true;
|
|
218
|
+
return body;
|
|
219
|
+
}
|
|
220
|
+
/** The level fields of a `/referral/stats` read, or null when it carries no `level` (levels not in force). */
|
|
221
|
+
export function referralLevel(read) {
|
|
222
|
+
if (!read || typeof read !== 'object')
|
|
223
|
+
return null;
|
|
224
|
+
const r = read;
|
|
225
|
+
if (typeof r.level !== 'string')
|
|
226
|
+
return null;
|
|
227
|
+
const n = (k) => (typeof r[k] === 'number' && Number.isSafeInteger(r[k]) ? r[k] : null);
|
|
228
|
+
const s = (k) => (typeof r[k] === 'string' ? r[k] : null);
|
|
229
|
+
const o = (k) => (r[k] && typeof r[k] === 'object' ? { ...r[k] } : {});
|
|
230
|
+
return {
|
|
231
|
+
level: r.level, shareBps: n('share_bps'), discountBps: n('discount_bps'), l2Progress: o('l2_progress'), nextCheckMs: n('next_check_ms'),
|
|
232
|
+
ownVolume6dec: s('own_volume_6dec'), minOwnVolume6dec: s('min_own_volume_6dec'), meetsMin: typeof r.meets_min === 'boolean' ? r.meets_min : null,
|
|
233
|
+
limits: o('limits'), refereeVolume6dec: s('referee_volume_6dec'),
|
|
234
|
+
};
|
|
235
|
+
}
|
package/dist/esm/client.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ import { type BatchAction, type OrderInput, type Submitted } from './orders.js';
|
|
|
9
9
|
import { type AgentRefusal, type Equity, type OpenOrder, type Position } from './reads.js';
|
|
10
10
|
import { type TypedSigner } from './sign.js';
|
|
11
11
|
import { type MarketRules, type PairSpec } from './rounding.js';
|
|
12
|
+
import { type BuilderApprovals, type BuilderStats, type OrderBuilder, type ReferralLevel } from './builder.js';
|
|
12
13
|
/**
|
|
13
14
|
* The result of a write. `applied` is true only once the chain's own read shows the change. A submission (HTTP 202) is
|
|
14
15
|
* not an application: `applied: false` with the chain's answer means the chain has not shown it (yet, or at all).
|
|
@@ -103,6 +104,18 @@ export declare class Vordium {
|
|
|
103
104
|
crossMargin(owner: string): Promise<unknown>;
|
|
104
105
|
/** The dead-man switch's committed state and limits. */
|
|
105
106
|
scheduleCancelState(owner: string): Promise<unknown>;
|
|
107
|
+
/** `GET /builder/approvals/<user>`: the builders `user` approved, the count, the most allowed, its nonce, `active`. */
|
|
108
|
+
builderApprovals(user: string): Promise<BuilderApprovals>;
|
|
109
|
+
/** `GET /builder/stats/<builder>`: volume, fees (= unclaimed + claimed), users in 30 days, caps, claim nonce, `active`. */
|
|
110
|
+
builderStats(builder: string): Promise<BuilderStats>;
|
|
111
|
+
/** `GET /referral/stats/<address>` as the network answers it. */
|
|
112
|
+
referralStats(address: string): Promise<unknown>;
|
|
113
|
+
/** The account's referral level (`L1`, `L2`, `PARTNER`) and its period progress; null where levels are not shown. */
|
|
114
|
+
referralLevel(address: string): Promise<ReferralLevel | null>;
|
|
115
|
+
/** `GET /referral/tiers`: the levels table, the L2 rule, the partner bounds and the referee caps. */
|
|
116
|
+
referralTiers(): Promise<unknown>;
|
|
117
|
+
/** The network's block time in whole seconds, else this computer's clock. */
|
|
118
|
+
nowS(): Promise<number>;
|
|
106
119
|
/** The TWAP route and limits from the network spec (refused when not published). */
|
|
107
120
|
twapRules(): TwapRules;
|
|
108
121
|
/** A trading account: `owner` signs account operations; `sessionSigner` (a session or agent key) signs orders. */
|
|
@@ -168,12 +181,15 @@ export declare class Account {
|
|
|
168
181
|
*/
|
|
169
182
|
placeOrder(o: Omit<OrderInput, 'owner' | 'clientNonce'> & {
|
|
170
183
|
clientNonce?: number;
|
|
184
|
+
builder?: OrderBuilder | null;
|
|
185
|
+
worstPrice?: string | number | null;
|
|
171
186
|
}): Promise<Submitted & {
|
|
172
187
|
clientNonce: number;
|
|
173
188
|
attempts: number;
|
|
174
189
|
alreadyReceived: boolean;
|
|
175
190
|
unknown: boolean;
|
|
176
191
|
found?: Record<string, unknown> | null;
|
|
192
|
+
builder?: OrderBuilder;
|
|
177
193
|
}>;
|
|
178
194
|
/**
|
|
179
195
|
* The account's order with `clientNonce` as the network lists it: resting (open orders), else its record; null when
|
|
@@ -256,6 +272,24 @@ export declare class Account {
|
|
|
256
272
|
cancelTwap(twapId: bigint | number, waitMs?: number): Promise<Result>;
|
|
257
273
|
/** The market's margin mode as the chain reads it ("cross" | "isolated"), or null. */
|
|
258
274
|
marginMode(pairId: number): Promise<MarginMode | null>;
|
|
275
|
+
/** An order's builder after the network's reads allow it (rule active, fee within the perps cap, approved, funded). */
|
|
276
|
+
private checkedBuilder;
|
|
277
|
+
/** The builders this account approved (`approvals`, `count`, `max`, `nonce`, `active`). */
|
|
278
|
+
builderApprovals(): Promise<BuilderApprovals>;
|
|
279
|
+
/** This account's stats as a builder. */
|
|
280
|
+
builderStats(): Promise<BuilderStats>;
|
|
281
|
+
/**
|
|
282
|
+
* Let `builder` charge this account up to `maxFeeCbps` per order (500 cbps = 5 bps = 0.05 %); approving a builder
|
|
283
|
+
* again replaces its fee. Signed with the account's own key. Refused before signing when builder fees are not active,
|
|
284
|
+
* the fee is 0 or above the highest cap (`caps_cbps`), the builder is this account, or the account already approved
|
|
285
|
+
* the most builders it may (`max`).
|
|
286
|
+
*/
|
|
287
|
+
approveBuilderFee(builder: string, maxFeeCbps: number, waitMs?: number): Promise<Result>;
|
|
288
|
+
/** Withdraw the approval of `builder` (an approval of 0). Orders that name it are refused from then on. */
|
|
289
|
+
revokeBuilderFee(builder: string, waitMs?: number): Promise<Result>;
|
|
290
|
+
private approve;
|
|
291
|
+
/** As a builder: move the unclaimed fees to this account's balance. Refused before signing when there is nothing. */
|
|
292
|
+
claimBuilderFees(waitMs?: number): Promise<Result>;
|
|
259
293
|
/** Cross or isolated for one market — only while it holds no open position and no resting order of the account. */
|
|
260
294
|
setMarginMode(pairId: number, mode: MarginMode, waitMs?: number): Promise<Result>;
|
|
261
295
|
private submitAgentOp;
|