@parchment-trade/sdk 0.0.0-stage → 1.0.0

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.
Files changed (95) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +192 -2
  3. package/REFERENCE.md +338 -0
  4. package/SECURITY.md +23 -0
  5. package/dist/general/providers/index.d.ts +2 -0
  6. package/dist/general/providers/index.js +2 -0
  7. package/dist/general/providers/parchment.d.ts +1 -0
  8. package/dist/general/providers/parchment.js +1 -0
  9. package/dist/general/providers/parchmentSdk.d.ts +23 -0
  10. package/dist/general/providers/parchmentSdk.js +11 -0
  11. package/dist/general/types/configTypes.d.ts +62 -0
  12. package/dist/general/types/configTypes.js +1 -0
  13. package/dist/general/types/index.d.ts +1 -0
  14. package/dist/general/types/index.js +1 -0
  15. package/dist/general/utils/address.d.ts +49 -0
  16. package/dist/general/utils/address.js +110 -0
  17. package/dist/general/utils/caller.d.ts +58 -0
  18. package/dist/general/utils/caller.js +182 -0
  19. package/dist/general/utils/errors.d.ts +16 -0
  20. package/dist/general/utils/errors.js +26 -0
  21. package/dist/general/utils/index.d.ts +4 -0
  22. package/dist/general/utils/index.js +4 -0
  23. package/dist/general/utils/jsBigInt.d.ts +26 -0
  24. package/dist/general/utils/jsBigInt.js +74 -0
  25. package/dist/general/utils/txShape.d.ts +8 -0
  26. package/dist/general/utils/txShape.js +39 -0
  27. package/dist/index.d.ts +4 -0
  28. package/dist/index.js +4 -0
  29. package/dist/packages/index.d.ts +11 -0
  30. package/dist/packages/index.js +5 -0
  31. package/dist/packages/parchment/abortCodes.d.ts +196 -0
  32. package/dist/packages/parchment/abortCodes.js +522 -0
  33. package/dist/packages/parchment/batchClose.d.ts +268 -0
  34. package/dist/packages/parchment/batchClose.js +524 -0
  35. package/dist/packages/parchment/eventGuard.d.ts +106 -0
  36. package/dist/packages/parchment/eventGuard.js +119 -0
  37. package/dist/packages/parchment/gameCounterEvents.d.ts +59 -0
  38. package/dist/packages/parchment/gameCounterEvents.js +56 -0
  39. package/dist/packages/parchment/index.d.ts +52 -0
  40. package/dist/packages/parchment/index.js +30 -0
  41. package/dist/packages/parchment/liquidationCredit.d.ts +28 -0
  42. package/dist/packages/parchment/liquidationCredit.js +25 -0
  43. package/dist/packages/parchment/lockedMintEvents.d.ts +42 -0
  44. package/dist/packages/parchment/lockedMintEvents.js +46 -0
  45. package/dist/packages/parchment/monitoring.d.ts +369 -0
  46. package/dist/packages/parchment/monitoring.js +259 -0
  47. package/dist/packages/parchment/notes.d.ts +6 -0
  48. package/dist/packages/parchment/notes.js +1 -0
  49. package/dist/packages/parchment/onboarding.d.ts +50 -0
  50. package/dist/packages/parchment/onboarding.js +48 -0
  51. package/dist/packages/parchment/orderEffects.d.ts +207 -0
  52. package/dist/packages/parchment/orderEffects.js +268 -0
  53. package/dist/packages/parchment/orderParams.d.ts +72 -0
  54. package/dist/packages/parchment/orderParams.js +71 -0
  55. package/dist/packages/parchment/packageIds.d.ts +53 -0
  56. package/dist/packages/parchment/packageIds.js +149 -0
  57. package/dist/packages/parchment/parchment.d.ts +442 -0
  58. package/dist/packages/parchment/parchment.js +975 -0
  59. package/dist/packages/parchment/parchmentAccount.d.ts +189 -0
  60. package/dist/packages/parchment/parchmentAccount.js +273 -0
  61. package/dist/packages/parchment/parchmentMarket.d.ts +58 -0
  62. package/dist/packages/parchment/parchmentMarket.js +86 -0
  63. package/dist/packages/parchment/parchmentTypes.d.ts +2031 -0
  64. package/dist/packages/parchment/parchmentTypes.js +65 -0
  65. package/dist/packages/parchment/publicInts.d.ts +45 -0
  66. package/dist/packages/parchment/publicInts.js +383 -0
  67. package/dist/packages/parchment/reconcileEvents.d.ts +24 -0
  68. package/dist/packages/parchment/reconcileEvents.js +30 -0
  69. package/dist/packages/parchment/retainedFee.d.ts +43 -0
  70. package/dist/packages/parchment/retainedFee.js +68 -0
  71. package/dist/packages/parchment/sessionClose.d.ts +68 -0
  72. package/dist/packages/parchment/sessionClose.js +50 -0
  73. package/dist/packages/parchment/sessionKey.d.ts +78 -0
  74. package/dist/packages/parchment/sessionKey.js +154 -0
  75. package/dist/packages/parchment/slippage.d.ts +23 -0
  76. package/dist/packages/parchment/slippage.js +27 -0
  77. package/dist/packages/parchment/sponsor.d.ts +39 -0
  78. package/dist/packages/parchment/sponsor.js +48 -0
  79. package/dist/packages/parchment/stakingCalls.d.ts +33 -0
  80. package/dist/packages/parchment/stakingCalls.js +21 -0
  81. package/dist/packages/parchment/stakingEvents.d.ts +53 -0
  82. package/dist/packages/parchment/stakingEvents.js +71 -0
  83. package/dist/packages/parchment/triggerCalls.d.ts +46 -0
  84. package/dist/packages/parchment/triggerCalls.js +36 -0
  85. package/dist/packages/parchment/triggerEvents.d.ts +22 -0
  86. package/dist/packages/parchment/triggerEvents.js +33 -0
  87. package/dist/packages/parchment/triggers.d.ts +113 -0
  88. package/dist/packages/parchment/triggers.js +183 -0
  89. package/dist/packages/parchment/txBodies.d.ts +111 -0
  90. package/dist/packages/parchment/txBodies.js +683 -0
  91. package/dist/packages/parchment/txIntent.d.ts +99 -0
  92. package/dist/packages/parchment/txIntent.js +611 -0
  93. package/dist/types.d.ts +2 -0
  94. package/dist/types.js +1 -0
  95. package/package.json +55 -4
@@ -0,0 +1,189 @@
1
+ import type { CallerConfig } from "../../general/types/configTypes.js";
2
+ import { Caller } from "../../general/utils/caller.js";
3
+ import type { ApiParchmentHistoryBody, ApiParchmentAccountResponse, ApiParchmentBindReferrerTxBody, ApiParchmentBindTradingReferrerTxBody, ApiParchmentTransferAccountTxBody, ApiParchmentClaimQuestTxBody, ApiParchmentClaimTxBody, ApiParchmentExitEarlyBoundedTxBody, ApiParchmentExitEarlyTxBody, ApiParchmentReleaseUnlockedTxBody, ApiParchmentClosePreviewBody, ApiParchmentClosePreviewResponse, ApiParchmentCancelOrderAsDelegateTxBody, ApiParchmentCancelOrderTxBody, ApiParchmentCloseRetiredAsDelegateTxBody, ApiParchmentCloseRetiredTxBody, ApiParchmentEmergencyCloseAsDelegateTxBody, ApiParchmentEmergencyCloseTxBody, ApiParchmentExpireOrderTxBody, ApiParchmentRequestCloseAsDelegateTxBody, ApiParchmentRequestCloseTxBody, ApiParchmentRequestOpenAsDelegateTxBody, ApiParchmentRequestOpenTxBody, ApiParchmentDepositTxBody, ApiParchmentGameTxBody, ApiParchmentOpenCratesTxBody, ApiParchmentGrantDelegateTxBody, ApiParchmentLiquidateTxBody, ApiParchmentClaimPendingMintTxBody, ApiParchmentHistoryResponse, ApiParchmentEquityHistoryResponse, ParchmentEquityRange, ApiParchmentLpHarvestTxBody, ApiParchmentOpenPreviewBody, ApiParchmentOpenPreviewResponse, ApiParchmentRevokeDelegateTxBody, ApiParchmentStakeTxBody, ApiParchmentUnstakeTxBody, ApiParchmentWithdrawTxBody, ParchmentBuiltTx, ParchmentClaim, ParchmentPosition } from "./parchmentTypes.js";
4
+ /** Account view plus the transactions that owner signs. */
5
+ export declare class ParchmentAccount extends Caller {
6
+ readonly account: ApiParchmentAccountResponse;
7
+ readonly owner: string;
8
+ constructor(config: CallerConfig, account: ApiParchmentAccountResponse);
9
+ /** Positions already on this view. Does not call the network. */
10
+ getPositions(): ParchmentPosition[];
11
+ getQueue(): {
12
+ claims: ParchmentClaim[];
13
+ aheadCount: bigint;
14
+ aheadNet: bigint;
15
+ };
16
+ /** Re-reads the first page (service default limit) of accounts, positions and claims; follow the `*NextCursor` fields with `ParchmentClient.getAccount` for more. */
17
+ refresh(): Promise<ParchmentAccount>;
18
+ /** First page of open positions only (service default 50); the next cursor is dropped. Use `ParchmentClient.getPositions` and follow `nextCursor` before totalling exposure. */
19
+ refreshPositions(): Promise<ParchmentPosition[]>;
20
+ getHistory(input?: Omit<ApiParchmentHistoryBody, "walletAddress">): Promise<ApiParchmentHistoryResponse>;
21
+ /** This owner's account equity over time (hourly snapshots). */
22
+ getEquityHistory(range: ParchmentEquityRange): Promise<ApiParchmentEquityHistoryResponse>;
23
+ getPreviewOpen(input: Omit<ApiParchmentOpenPreviewBody, "marketId"> & {
24
+ marketId: string;
25
+ }): Promise<ApiParchmentOpenPreviewResponse>;
26
+ getPreviewClose(input: ApiParchmentClosePreviewBody): Promise<ApiParchmentClosePreviewResponse>;
27
+ getDepositTx(input: Omit<ApiParchmentDepositTxBody, "walletAddress" | "accountId"> & {
28
+ walletAddress?: string;
29
+ accountId?: string;
30
+ }): Promise<ParchmentBuiltTx>;
31
+ getWithdrawTx(input: Omit<ApiParchmentWithdrawTxBody, "walletAddress" | "accountId"> & {
32
+ walletAddress?: string;
33
+ accountId?: string;
34
+ }): Promise<ParchmentBuiltTx>;
35
+ /** Two-step open request; the keeper fills it. */
36
+ getRequestOpenTx(input: Omit<ApiParchmentRequestOpenTxBody, "walletAddress" | "accountId"> & {
37
+ walletAddress?: string;
38
+ accountId?: string;
39
+ }): Promise<ParchmentBuiltTx>;
40
+ /**
41
+ * Delegate open request. No AccountCap. `walletAddress` is the delegate (the signer); it never
42
+ * defaults to the owner, since `account::assert_delegate` checks the sender's delegate entry.
43
+ */
44
+ getRequestOpenAsDelegateTx(input: Omit<ApiParchmentRequestOpenAsDelegateTxBody, "accountId"> & {
45
+ accountId?: string;
46
+ }): Promise<ParchmentBuiltTx>;
47
+ /** Two-step close request; the keeper fills it. */
48
+ getRequestCloseTx(input: Omit<ApiParchmentRequestCloseTxBody, "walletAddress" | "accountId"> & {
49
+ walletAddress?: string;
50
+ accountId?: string;
51
+ }): Promise<ParchmentBuiltTx>;
52
+ /** Delegate close request. No AccountCap. */
53
+ getRequestCloseAsDelegateTx(input: Omit<ApiParchmentRequestCloseAsDelegateTxBody, "accountId"> & {
54
+ accountId?: string;
55
+ }): Promise<ParchmentBuiltTx>;
56
+ /** Cancel a pending order after its cancel delay. */
57
+ getCancelOrderTx(input: Omit<ApiParchmentCancelOrderTxBody, "walletAddress" | "accountId"> & {
58
+ walletAddress?: string;
59
+ accountId?: string;
60
+ }): Promise<ParchmentBuiltTx>;
61
+ /** Delegate cancel. No AccountCap. */
62
+ getCancelOrderAsDelegateTx(input: Omit<ApiParchmentCancelOrderAsDelegateTxBody, "accountId"> & {
63
+ accountId?: string;
64
+ }): Promise<ParchmentBuiltTx>;
65
+ /** Permissionless expiry, allowed after `orderExpiresAfterMs(order)`. */
66
+ getExpireOrderTx(input: Omit<ApiParchmentExpireOrderTxBody, "walletAddress" | "accountId"> & {
67
+ walletAddress?: string;
68
+ accountId?: string;
69
+ }): Promise<ParchmentBuiltTx>;
70
+ /** Emergency close of a pending close order after `emergencyAfterMs`. */
71
+ getEmergencyCloseTx(input: Omit<ApiParchmentEmergencyCloseTxBody, "walletAddress" | "accountId"> & {
72
+ walletAddress?: string;
73
+ accountId?: string;
74
+ }): Promise<ParchmentBuiltTx>;
75
+ /** Delegate emergency close. No AccountCap. */
76
+ getEmergencyCloseAsDelegateTx(input: Omit<ApiParchmentEmergencyCloseAsDelegateTxBody, "accountId"> & {
77
+ accountId?: string;
78
+ }): Promise<ParchmentBuiltTx>;
79
+ /**
80
+ * Owner one-step close of this account's position on a retired market. Runs under every
81
+ * pause; aborts `trade::EOracleKilled` (20) only while the market's oracle kill switch is on.
82
+ */
83
+ getCloseRetiredTx(input: Omit<ApiParchmentCloseRetiredTxBody, "walletAddress" | "accountId"> & {
84
+ walletAddress?: string;
85
+ accountId?: string;
86
+ }): Promise<ParchmentBuiltTx>;
87
+ /** Delegate close on a retired market. No AccountCap. Same pause aborts as `getCloseRetiredTx`. */
88
+ getCloseRetiredAsDelegateTx(input: Omit<ApiParchmentCloseRetiredAsDelegateTxBody, "accountId"> & {
89
+ accountId?: string;
90
+ }): Promise<ParchmentBuiltTx>;
91
+ /**
92
+ * Account-free liquidation of any crossed position; the bounty goes to this wallet. Order keepers only: any
93
+ * other sender aborts `trade::EKeeperOnlyLiquidation` (36). Runs under every pause; aborts
94
+ * `trade::EOracleKilled` (20) only under the market's kill switch (`liquidationPaused`).
95
+ */
96
+ getLiquidateTx(input: Omit<ApiParchmentLiquidateTxBody, "walletAddress"> & {
97
+ walletAddress?: string;
98
+ }): Promise<ParchmentBuiltTx>;
99
+ /** Mint PARCHMENT against fee-mint basis parked by account-free liquidations of this account. */
100
+ getClaimPendingMintTx(input: Omit<ApiParchmentClaimPendingMintTxBody, "walletAddress" | "accountId"> & {
101
+ walletAddress?: string;
102
+ accountId?: string;
103
+ }): Promise<ParchmentBuiltTx>;
104
+ /**
105
+ * User-triggered FIFO crank. Credits the exchange-side free balance and does
106
+ * not transfer a coin to the wallet. `maxEntries` defaults to 32 and must be
107
+ * from 1 to 128. This call takes no account objects.
108
+ */
109
+ getHarvestTx(input?: Omit<ApiParchmentLpHarvestTxBody, "walletAddress"> & {
110
+ walletAddress?: string;
111
+ }): Promise<ParchmentBuiltTx>;
112
+ getBindReferrerTx(input: Omit<ApiParchmentBindReferrerTxBody, "walletAddress"> & {
113
+ walletAddress?: string;
114
+ }): Promise<ParchmentBuiltTx>;
115
+ /**
116
+ * Bind a trading referrer for this account. Requires `accountCapId` and `referrer`.
117
+ * Version and digest are sent only when the caller provides them.
118
+ */
119
+ getBindTradingReferrerTx(input: Omit<ApiParchmentBindTradingReferrerTxBody, "walletAddress" | "accountId"> & {
120
+ walletAddress?: string;
121
+ accountId?: string;
122
+ }): Promise<ParchmentBuiltTx>;
123
+ /** Transfer this account to `recipient` in one step. Requires `accountCapId`. */
124
+ getTransferAccountTx(input: Omit<ApiParchmentTransferAccountTxBody, "walletAddress" | "accountId"> & {
125
+ walletAddress?: string;
126
+ accountId?: string;
127
+ }): Promise<ParchmentBuiltTx>;
128
+ getStakeTx(input: Omit<ApiParchmentStakeTxBody, "walletAddress"> & {
129
+ walletAddress?: string;
130
+ }): Promise<ParchmentBuiltTx>;
131
+ getUnstakeTx(input: Omit<ApiParchmentUnstakeTxBody, "walletAddress"> & {
132
+ walletAddress?: string;
133
+ }): Promise<ParchmentBuiltTx>;
134
+ /** Staking reward claim. The staker keeps 100%; a referred claim records the referrers' bonus as owed, paid from the treasury share. */
135
+ getClaimTx(input?: Omit<ApiParchmentClaimTxBody, "walletAddress"> & {
136
+ walletAddress?: string;
137
+ }): Promise<ParchmentBuiltTx>;
138
+ /** `release-unlocked` for this owner (or `who`): pays the vested locked PARCHMENT to its owner. */
139
+ getReleaseUnlockedTx(input?: Omit<ApiParchmentReleaseUnlockedTxBody, "walletAddress"> & {
140
+ walletAddress?: string;
141
+ }): Promise<ParchmentBuiltTx>;
142
+ /** Vest `exit-early` for this owner. Unbounded across a bucket's unlock: see `Parchment.getExitEarlyTx`. */
143
+ getExitEarlyTx(input?: Omit<ApiParchmentExitEarlyTxBody, "walletAddress"> & {
144
+ walletAddress?: string;
145
+ }): Promise<ParchmentBuiltTx>;
146
+ /** Vest `exit-early-bounded` for this owner: see `Parchment.getExitEarlyBoundedTx`. */
147
+ getExitEarlyBoundedTx(input: Omit<ApiParchmentExitEarlyBoundedTxBody, "walletAddress"> & {
148
+ walletAddress?: string;
149
+ }): Promise<ParchmentBuiltTx>;
150
+ /** Delegate expiry uses the shared on-chain Clock (0x6); legacy `nowMs` is ignored. */
151
+ getGrantDelegateTx(input: Omit<ApiParchmentGrantDelegateTxBody, "walletAddress" | "accountId"> & {
152
+ walletAddress?: string;
153
+ accountId?: string;
154
+ }): Promise<ParchmentBuiltTx>;
155
+ getRevokeDelegateTx(input: Omit<ApiParchmentRevokeDelegateTxBody, "walletAddress" | "accountId"> & {
156
+ walletAddress?: string;
157
+ accountId?: string;
158
+ }): Promise<ParchmentBuiltTx>;
159
+ /** Daily streak XP only; no coin result. Omits `questId`. Version and digest are sent only when provided. */
160
+ getClaimDailyTx(input: Omit<ApiParchmentGameTxBody, "walletAddress" | "accountId"> & {
161
+ walletAddress?: string;
162
+ accountId?: string;
163
+ }): Promise<ParchmentBuiltTx>;
164
+ /** XP/cosmetic crate via a private entry MoveCall using on-chain randomness; no result. Omits `questId`. */
165
+ getOpenCrateTx(input: Omit<ApiParchmentGameTxBody, "walletAddress" | "accountId"> & {
166
+ walletAddress?: string;
167
+ accountId?: string;
168
+ }): Promise<ParchmentBuiltTx>;
169
+ /**
170
+ * Open `count` crates (at least 1, no app-level cap) in one private entry MoveCall using on-chain randomness; no
171
+ * result, one `CrateOpened` event per crate. `count` is checked locally (`count_required` / `bad_count`).
172
+ * The batch's tier-ups and XP crate grants are deferred to the player's next open or XP grant.
173
+ */
174
+ getOpenCratesTx(input: Omit<ApiParchmentOpenCratesTxBody, "walletAddress" | "accountId"> & {
175
+ walletAddress?: string;
176
+ accountId?: string;
177
+ }): Promise<ParchmentBuiltTx>;
178
+ /** Claim one quest for XP/cosmetics; no coin result. Version and digest are sent only when provided. */
179
+ getClaimQuestTx(input: Omit<ApiParchmentClaimQuestTxBody, "walletAddress" | "accountId"> & {
180
+ walletAddress?: string;
181
+ accountId?: string;
182
+ }): Promise<ParchmentBuiltTx>;
183
+ /** Issue pending fee-backed achievement PARCHMENT using Clock and daily caps; returns a coin to transfer. */
184
+ getCollectMintTx(input: Omit<ApiParchmentGameTxBody, "walletAddress" | "accountId"> & {
185
+ walletAddress?: string;
186
+ accountId?: string;
187
+ }): Promise<ParchmentBuiltTx>;
188
+ private accountId;
189
+ }
@@ -0,0 +1,273 @@
1
+ import { equityRange, historyCursor, limitParam, normalizeAddress } from "../../general/utils/address.js";
2
+ import { Caller } from "../../general/utils/caller.js";
3
+ import { ParchmentApiError } from "../../general/utils/errors.js";
4
+ import { PUBLIC_INTS } from "./publicInts.js";
5
+ import { bindReferrerTxBody, bindTradingReferrerTxBody, transferAccountTxBody, claimQuestTxBody, claimTxBody, exitEarlyBoundedTxBody, exitEarlyTxBody, releaseUnlockedTxBody, closePreviewBody, cancelOrderTxBody, closeRetiredAsDelegateTxBody, closeRetiredTxBody, emergencyCloseAsDelegateTxBody, emergencyCloseTxBody, expireOrderTxBody, orderRefTxBody, requestCloseAsDelegateTxBody, requestCloseTxBody, requestOpenAsDelegateTxBody, requestOpenTxBody, depositTxBody, gameTxBody, openCratesTxBody, grantDelegateTxBody, liquidateTxBody, claimPendingMintTxBody, lpHarvestTxBody, openPreviewBody, revokeDelegateTxBody, stakeTxBody, unstakeTxBody, withdrawTxBody, } from "./txBodies.js";
6
+ function query(pairs) {
7
+ const params = new URLSearchParams();
8
+ for (const [key, value] of Object.entries(pairs)) {
9
+ if (value !== undefined)
10
+ params.set(key, value);
11
+ }
12
+ const text = params.toString();
13
+ return text ? `?${text}` : "";
14
+ }
15
+ export class ParchmentAccount extends Caller {
16
+ account;
17
+ owner;
18
+ constructor(config, account) {
19
+ super(config, "parchment");
20
+ this.account = account;
21
+ this.owner = account.owner;
22
+ }
23
+ getPositions() {
24
+ return this.account.positions;
25
+ }
26
+ getQueue() {
27
+ return this.account.queue;
28
+ }
29
+ async refresh() {
30
+ const data = await this.fetchApi(`accounts/${normalizeAddress(this.owner)}`, PUBLIC_INTS.account);
31
+ return new ParchmentAccount(this.config, data);
32
+ }
33
+ async refreshPositions() {
34
+ return this.fetchApiField(`accounts/${normalizeAddress(this.owner)}/positions`, "positions", PUBLIC_INTS.positions);
35
+ }
36
+ async getHistory(input = {}) {
37
+ const qs = query({
38
+ cursor: historyCursor(input.cursor),
39
+ limit: limitParam(input.limit),
40
+ });
41
+ return this.fetchApi(`accounts/${normalizeAddress(this.owner)}/history${qs}`, PUBLIC_INTS.history);
42
+ }
43
+ async getEquityHistory(range) {
44
+ return this.fetchApi(`accounts/${normalizeAddress(this.owner)}/equity?range=${equityRange(range)}`, PUBLIC_INTS.equityHistory);
45
+ }
46
+ getPreviewOpen(input) {
47
+ const only = this.account.accounts.length === 1 ? this.account.accounts[0].accountId : undefined;
48
+ return this.fetchApi("previews/open", PUBLIC_INTS.previewOpen, openPreviewBody({ ...input, accountId: input.accountId ?? only }));
49
+ }
50
+ getPreviewClose(input) {
51
+ return this.fetchApi("previews/close", PUBLIC_INTS.previewClose, closePreviewBody(input));
52
+ }
53
+ async getDepositTx(input) {
54
+ return this.fetchApiTxObject("transactions/deposit", depositTxBody({
55
+ ...input,
56
+ walletAddress: input.walletAddress ?? this.owner,
57
+ accountId: this.accountId(input.accountId),
58
+ }));
59
+ }
60
+ async getWithdrawTx(input) {
61
+ return this.fetchApiTxObject("transactions/withdraw", withdrawTxBody({
62
+ ...input,
63
+ walletAddress: input.walletAddress ?? this.owner,
64
+ accountId: this.accountId(input.accountId),
65
+ }));
66
+ }
67
+ async getRequestOpenTx(input) {
68
+ return this.fetchApiTxObject("transactions/request-open", requestOpenTxBody({
69
+ ...input,
70
+ walletAddress: input.walletAddress ?? this.owner,
71
+ accountId: this.accountId(input.accountId),
72
+ }));
73
+ }
74
+ async getRequestOpenAsDelegateTx(input) {
75
+ return this.fetchApiTxObject("transactions/request-open-as-delegate", requestOpenAsDelegateTxBody({
76
+ ...input,
77
+ walletAddress: delegateSender(input.walletAddress),
78
+ accountId: this.accountId(input.accountId),
79
+ }));
80
+ }
81
+ async getRequestCloseTx(input) {
82
+ return this.fetchApiTxObject("transactions/request-close", requestCloseTxBody({
83
+ ...input,
84
+ walletAddress: input.walletAddress ?? this.owner,
85
+ accountId: this.accountId(input.accountId),
86
+ }));
87
+ }
88
+ async getRequestCloseAsDelegateTx(input) {
89
+ return this.fetchApiTxObject("transactions/request-close-as-delegate", requestCloseAsDelegateTxBody({
90
+ ...input,
91
+ walletAddress: delegateSender(input.walletAddress),
92
+ accountId: this.accountId(input.accountId),
93
+ }));
94
+ }
95
+ async getCancelOrderTx(input) {
96
+ return this.fetchApiTxObject("transactions/cancel-order", cancelOrderTxBody({
97
+ ...input,
98
+ walletAddress: input.walletAddress ?? this.owner,
99
+ accountId: this.accountId(input.accountId),
100
+ }));
101
+ }
102
+ async getCancelOrderAsDelegateTx(input) {
103
+ return this.fetchApiTxObject("transactions/cancel-order-as-delegate", orderRefTxBody({
104
+ ...input,
105
+ walletAddress: delegateSender(input.walletAddress),
106
+ accountId: this.accountId(input.accountId),
107
+ }));
108
+ }
109
+ async getExpireOrderTx(input) {
110
+ return this.fetchApiTxObject("transactions/expire-order", expireOrderTxBody({
111
+ ...input,
112
+ walletAddress: input.walletAddress ?? this.owner,
113
+ accountId: this.accountId(input.accountId),
114
+ }));
115
+ }
116
+ async getEmergencyCloseTx(input) {
117
+ return this.fetchApiTxObject("transactions/emergency-close", emergencyCloseTxBody({
118
+ ...input,
119
+ walletAddress: input.walletAddress ?? this.owner,
120
+ accountId: this.accountId(input.accountId),
121
+ }));
122
+ }
123
+ async getEmergencyCloseAsDelegateTx(input) {
124
+ return this.fetchApiTxObject("transactions/emergency-close-as-delegate", emergencyCloseAsDelegateTxBody({
125
+ ...input,
126
+ walletAddress: delegateSender(input.walletAddress),
127
+ accountId: this.accountId(input.accountId),
128
+ }));
129
+ }
130
+ async getCloseRetiredTx(input) {
131
+ return this.fetchApiTxObject("transactions/close-retired", closeRetiredTxBody({
132
+ ...input,
133
+ walletAddress: input.walletAddress ?? this.owner,
134
+ accountId: this.accountId(input.accountId),
135
+ }));
136
+ }
137
+ async getCloseRetiredAsDelegateTx(input) {
138
+ return this.fetchApiTxObject("transactions/close-retired-as-delegate", closeRetiredAsDelegateTxBody({
139
+ ...input,
140
+ walletAddress: delegateSender(input.walletAddress),
141
+ accountId: this.accountId(input.accountId),
142
+ }));
143
+ }
144
+ async getLiquidateTx(input) {
145
+ const path = input.retired ? "transactions/liquidate-retired" : "transactions/liquidate";
146
+ return this.fetchApiTxObject(path, liquidateTxBody({
147
+ ...input,
148
+ walletAddress: input.walletAddress ?? this.owner,
149
+ }));
150
+ }
151
+ async getClaimPendingMintTx(input) {
152
+ return this.fetchApiTxObject("transactions/claim-pending-mint", claimPendingMintTxBody({
153
+ ...input,
154
+ walletAddress: input.walletAddress ?? this.owner,
155
+ accountId: this.accountId(input.accountId),
156
+ }));
157
+ }
158
+ getHarvestTx(input = {}) {
159
+ return this.fetchApiTxObject("transactions/harvest", lpHarvestTxBody({
160
+ ...input,
161
+ walletAddress: input.walletAddress ?? this.owner,
162
+ }));
163
+ }
164
+ getBindReferrerTx(input) {
165
+ return this.fetchApiTxObject("transactions/bind-referrer", bindReferrerTxBody({
166
+ ...input,
167
+ walletAddress: input.walletAddress ?? this.owner,
168
+ }));
169
+ }
170
+ async getBindTradingReferrerTx(input) {
171
+ return this.fetchApiTxObject("transactions/bind-trading-referrer", bindTradingReferrerTxBody({
172
+ ...input,
173
+ walletAddress: input.walletAddress ?? this.owner,
174
+ accountId: this.accountId(input.accountId),
175
+ }));
176
+ }
177
+ async getTransferAccountTx(input) {
178
+ return this.fetchApiTxObject("transactions/transfer-account", transferAccountTxBody({
179
+ ...input,
180
+ walletAddress: input.walletAddress ?? this.owner,
181
+ accountId: this.accountId(input.accountId),
182
+ }));
183
+ }
184
+ getStakeTx(input) {
185
+ return this.fetchApiTxObject("transactions/stake", stakeTxBody({ ...input, walletAddress: input.walletAddress ?? this.owner }));
186
+ }
187
+ getUnstakeTx(input) {
188
+ return this.fetchApiTxObject("transactions/unstake", unstakeTxBody({
189
+ ...input,
190
+ walletAddress: input.walletAddress ?? this.owner,
191
+ }));
192
+ }
193
+ getClaimTx(input = {}) {
194
+ return this.fetchApiTxObject("transactions/claim", claimTxBody({ walletAddress: input.walletAddress ?? this.owner, shared: input.shared }));
195
+ }
196
+ async getReleaseUnlockedTx(input = {}) {
197
+ const walletAddress = input.walletAddress ?? this.owner;
198
+ const built = await this.fetchApiTxObject("transactions/release-unlocked", releaseUnlockedTxBody({ ...input, walletAddress }));
199
+ return input.syncLocked ? this.appendSyncLocked(built, input.who ?? walletAddress) : built;
200
+ }
201
+ async getExitEarlyTx(input = {}) {
202
+ const walletAddress = input.walletAddress ?? this.owner;
203
+ const built = await this.fetchApiTxObject("transactions/exit-early", exitEarlyTxBody({ ...input, walletAddress }));
204
+ return input.syncLocked ? this.appendSyncLocked(built, walletAddress) : built;
205
+ }
206
+ async getExitEarlyBoundedTx(input) {
207
+ const walletAddress = input.walletAddress ?? this.owner;
208
+ const built = await this.fetchApiTxObject("transactions/exit-early-bounded", exitEarlyBoundedTxBody({ ...input, walletAddress }));
209
+ return input.syncLocked ? this.appendSyncLocked(built, walletAddress) : built;
210
+ }
211
+ async getGrantDelegateTx(input) {
212
+ return this.fetchApiTxObject("transactions/grant-delegate", grantDelegateTxBody({
213
+ ...input,
214
+ walletAddress: input.walletAddress ?? this.owner,
215
+ accountId: this.accountId(input.accountId),
216
+ }));
217
+ }
218
+ async getRevokeDelegateTx(input) {
219
+ return this.fetchApiTxObject("transactions/revoke-delegate", revokeDelegateTxBody({
220
+ ...input,
221
+ walletAddress: input.walletAddress ?? this.owner,
222
+ accountId: this.accountId(input.accountId),
223
+ }));
224
+ }
225
+ async getClaimDailyTx(input) {
226
+ return this.fetchApiTxObject("transactions/claim-daily", gameTxBody({
227
+ ...input,
228
+ walletAddress: input.walletAddress ?? this.owner,
229
+ accountId: this.accountId(input.accountId),
230
+ }));
231
+ }
232
+ async getOpenCrateTx(input) {
233
+ return this.fetchApiTxObject("transactions/open-crate", gameTxBody({
234
+ ...input,
235
+ walletAddress: input.walletAddress ?? this.owner,
236
+ accountId: this.accountId(input.accountId),
237
+ }));
238
+ }
239
+ async getOpenCratesTx(input) {
240
+ return this.fetchApiTxObject("transactions/open-crates", openCratesTxBody({
241
+ ...input,
242
+ walletAddress: input.walletAddress ?? this.owner,
243
+ accountId: this.accountId(input.accountId),
244
+ }));
245
+ }
246
+ async getClaimQuestTx(input) {
247
+ return this.fetchApiTxObject("transactions/claim-quest", claimQuestTxBody({
248
+ ...input,
249
+ walletAddress: input.walletAddress ?? this.owner,
250
+ accountId: this.accountId(input.accountId),
251
+ }));
252
+ }
253
+ async getCollectMintTx(input) {
254
+ return this.fetchApiTxObject("transactions/collect-mint", gameTxBody({
255
+ ...input,
256
+ walletAddress: input.walletAddress ?? this.owner,
257
+ accountId: this.accountId(input.accountId),
258
+ }));
259
+ }
260
+ accountId(explicit) {
261
+ if (explicit)
262
+ return normalizeAddress(explicit);
263
+ if (this.account.accounts.length === 1)
264
+ return this.account.accounts[0].accountId;
265
+ throw new ParchmentApiError(0, "account_id_required", "accountId is required when this owner does not have exactly one account");
266
+ }
267
+ }
268
+ function delegateSender(walletAddress) {
269
+ if (!walletAddress) {
270
+ throw new Error("as-delegate txs need walletAddress: the delegate's wallet, not the account owner");
271
+ }
272
+ return walletAddress;
273
+ }
@@ -0,0 +1,58 @@
1
+ import type { CallerConfig } from "../../general/types/configTypes.js";
2
+ import { Caller } from "../../general/utils/caller.js";
3
+ import type { ApiParchmentOpenPreviewBody, ApiParchmentOpenPreviewResponse, ApiParchmentPriceHistoryBody, ApiParchmentPriceHistoryResponse, ParchmentMarketView, ParchmentPrice } from "./parchmentTypes.js";
4
+ /** `constants::status_active()`: opens and closes allowed. */
5
+ export declare const MARKET_STATUS_ACTIVE = 1n;
6
+ /**
7
+ * `constants::status_reduce_only()`: `request_open` aborts `order::ERequestsPaused` (15) and a pending
8
+ * open filled after the change refunds outcome 7 (`closed`); closes still go through. (One-step
9
+ * `open` aborts `trade::EMarketNotActive`.)
10
+ */
11
+ export declare const MARKET_STATUS_REDUCE_ONLY = 2n;
12
+ /** `constants::status_retired()`: exit with `close_retired` / `liquidate_retired`; emergency close aborts `EMarketNotActive`. */
13
+ export declare const MARKET_STATUS_RETIRED = 3n;
14
+ /**
15
+ * The market's `TimingParamsV1.min_hold_ms` as a number, or null when the service has not indexed
16
+ * timing params (or the value is past `Number.MAX_SAFE_INTEGER`).
17
+ */
18
+ export declare function marketMinHoldMs(market: Pick<ParchmentMarketView, "timing">): number | null;
19
+ /** Served `sessionEndsAtMs` as a number; null when absent. */
20
+ export declare function marketSessionEndsAtMs(market: Pick<ParchmentMarketView, "sessionEndsAtMs">): number | null;
21
+ /** Served `nextSessionOpenMs` as a number; null when absent. */
22
+ export declare function marketNextSessionOpenMs(market: Pick<ParchmentMarketView, "nextSessionOpenMs">): number | null;
23
+ /** True when `status` is `MARKET_STATUS_RETIRED`. False for unindexed (null) status. */
24
+ export declare function marketIsRetired(market: Pick<ParchmentMarketView, "status">): boolean;
25
+ /** True when the service flags the market `delisted` (missing from the Registry's `market_ids`). False when absent. */
26
+ export declare function marketIsDelisted(market: Pick<ParchmentMarketView, "delisted">): boolean;
27
+ /** True when `status` is `MARKET_STATUS_ACTIVE`. False for unindexed (null) status. */
28
+ export declare function marketIsActive(market: Pick<ParchmentMarketView, "status">): boolean;
29
+ /**
30
+ * Cause-specific copy for `order::ERequestsPaused` (15), which `request_open` / `request_close` raise
31
+ * for several causes, from market state the caller already has (no fetch, no new fields). Checks, in
32
+ * order: retired (opens and closes; exit with close-retired, which runs under every pause and stops only while
33
+ * the market's oracle is off), oracle off, reduce-only (opens only).
34
+ * Anything else (a paused market or exchange, a disabled market, unindexed state, no `action`) gets the
35
+ * generic `ORDER_ABORT_MESSAGES.ERequestsPaused`. The market view can lag the chain, so this is copy
36
+ * only: never use it to decide whether to send.
37
+ */
38
+ export declare function requestsPausedMessage(market: Pick<ParchmentMarketView, "status" | "oracleKilled"> | null | undefined, action?: "open" | "close"): string;
39
+ /**
40
+ * True when liquidation is stopped on this market by state the caller already has.
41
+ * `liquidate`, `liquidate-retired` and `sweep` run under every pause, including `pause_all` and the exchange or
42
+ * market close pause; only the market's oracle kill switch stops them (`trade::EOracleKilled` 20). Reads the
43
+ * market's `oracleKilled` and the served `liquidationPaused` (which is the same kill switch). Unindexed (null) or
44
+ * missing flags count as not paused. Views can lag the chain, so use this for copy and to skip pointless sends,
45
+ * not as a guarantee; price freshness and validity still decide on chain.
46
+ */
47
+ export declare function liquidationPaused(market: (Pick<ParchmentMarketView, "oracleKilled"> & {
48
+ liquidationPaused?: boolean | null;
49
+ }) | null | undefined): boolean;
50
+ /** Market view plus reads. Does not read a storage object itself. */
51
+ export declare class ParchmentMarket extends Caller {
52
+ readonly market: ParchmentMarketView;
53
+ constructor(config: CallerConfig, market: ParchmentMarketView);
54
+ get price(): ParchmentPrice;
55
+ refresh(): Promise<ParchmentMarket>;
56
+ getPriceHistory(input?: Omit<ApiParchmentPriceHistoryBody, "marketId">): Promise<ApiParchmentPriceHistoryResponse>;
57
+ getPreviewOpen(input: Omit<ApiParchmentOpenPreviewBody, "marketId">): Promise<ApiParchmentOpenPreviewResponse>;
58
+ }
@@ -0,0 +1,86 @@
1
+ import { ORDER_ABORT_MESSAGES } from "./abortCodes.js";
2
+ import { i64Ms, limitParam, normalizeAddress } from "../../general/utils/address.js";
3
+ import { Caller } from "../../general/utils/caller.js";
4
+ import { PUBLIC_INTS } from "./publicInts.js";
5
+ import { openPreviewBody } from "./txBodies.js";
6
+ function query(pairs) {
7
+ const params = new URLSearchParams();
8
+ for (const [key, value] of Object.entries(pairs)) {
9
+ if (value !== undefined)
10
+ params.set(key, value);
11
+ }
12
+ const text = params.toString();
13
+ return text ? `?${text}` : "";
14
+ }
15
+ export const MARKET_STATUS_ACTIVE = 1n;
16
+ export const MARKET_STATUS_REDUCE_ONLY = 2n;
17
+ export const MARKET_STATUS_RETIRED = 3n;
18
+ export function marketMinHoldMs(market) {
19
+ return msOrNull(market.timing?.minHoldMs);
20
+ }
21
+ function msOrNull(raw) {
22
+ if (raw === null || raw === undefined || raw < 0n || raw > BigInt(Number.MAX_SAFE_INTEGER))
23
+ return null;
24
+ return Number(raw);
25
+ }
26
+ export function marketSessionEndsAtMs(market) {
27
+ return msOrNull(market.sessionEndsAtMs);
28
+ }
29
+ export function marketNextSessionOpenMs(market) {
30
+ return msOrNull(market.nextSessionOpenMs);
31
+ }
32
+ export function marketIsRetired(market) {
33
+ return market.status === MARKET_STATUS_RETIRED;
34
+ }
35
+ export function marketIsDelisted(market) {
36
+ return market.delisted === true;
37
+ }
38
+ export function marketIsActive(market) {
39
+ return market.status === MARKET_STATUS_ACTIVE;
40
+ }
41
+ export function requestsPausedMessage(market, action) {
42
+ const generic = ORDER_ABORT_MESSAGES.ERequestsPaused;
43
+ if (!market || (action !== "open" && action !== "close"))
44
+ return generic;
45
+ if (market.status === MARKET_STATUS_RETIRED) {
46
+ if (market.oracleKilled === true) {
47
+ return "This market is retired and its price feed is off; positions can exit with close-retired once it is back";
48
+ }
49
+ return "This market is retired; close positions with close-retired";
50
+ }
51
+ if (market.oracleKilled === true) {
52
+ return "This market's price feed (oracle) is off; orders are not accepted until it is back";
53
+ }
54
+ if (market.status === MARKET_STATUS_REDUCE_ONLY && action === "open") {
55
+ return "This market is reduce-only: you can close positions but not open new ones";
56
+ }
57
+ return generic;
58
+ }
59
+ export function liquidationPaused(market) {
60
+ return market?.liquidationPaused === true || market?.oracleKilled === true;
61
+ }
62
+ export class ParchmentMarket extends Caller {
63
+ market;
64
+ constructor(config, market) {
65
+ super(config, "parchment");
66
+ this.market = market;
67
+ }
68
+ get price() {
69
+ return this.market.price;
70
+ }
71
+ async refresh() {
72
+ const market = await this.fetchApiField(`markets/${normalizeAddress(this.market.marketId)}`, "market", PUBLIC_INTS.market);
73
+ return new ParchmentMarket(this.config, market);
74
+ }
75
+ async getPriceHistory(input = {}) {
76
+ const qs = query({
77
+ fromMs: input.fromMs === undefined ? undefined : i64Ms(input.fromMs, "fromMs"),
78
+ toMs: input.toMs === undefined ? undefined : i64Ms(input.toMs, "toMs"),
79
+ limit: limitParam(input.limit),
80
+ });
81
+ return this.fetchApi(`markets/${normalizeAddress(this.market.marketId)}/prices${qs}`, PUBLIC_INTS.priceHistory);
82
+ }
83
+ getPreviewOpen(input) {
84
+ return this.fetchApi("previews/open", PUBLIC_INTS.previewOpen, openPreviewBody({ ...input, marketId: this.market.marketId }));
85
+ }
86
+ }