@lightconexyz/lightcone-sdk 0.1.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.
- package/README.md +232 -0
- package/dist/api/client.d.ts +225 -0
- package/dist/api/client.d.ts.map +1 -0
- package/dist/api/client.js +452 -0
- package/dist/api/client.js.map +1 -0
- package/dist/api/error.d.ts +58 -0
- package/dist/api/error.d.ts.map +1 -0
- package/dist/api/error.js +98 -0
- package/dist/api/error.js.map +1 -0
- package/dist/api/index.d.ts +23 -0
- package/dist/api/index.d.ts.map +1 -0
- package/dist/api/index.js +51 -0
- package/dist/api/index.js.map +1 -0
- package/dist/api/types/admin.d.ts +49 -0
- package/dist/api/types/admin.d.ts.map +1 -0
- package/dist/api/types/admin.js +13 -0
- package/dist/api/types/admin.js.map +1 -0
- package/dist/api/types/index.d.ts +14 -0
- package/dist/api/types/index.d.ts.map +1 -0
- package/dist/api/types/index.js +13 -0
- package/dist/api/types/index.js.map +1 -0
- package/dist/api/types/market.d.ts +186 -0
- package/dist/api/types/market.d.ts.map +1 -0
- package/dist/api/types/market.js +6 -0
- package/dist/api/types/market.js.map +1 -0
- package/dist/api/types/order.d.ts +190 -0
- package/dist/api/types/order.d.ts.map +1 -0
- package/dist/api/types/order.js +6 -0
- package/dist/api/types/order.js.map +1 -0
- package/dist/api/types/orderbook.d.ts +36 -0
- package/dist/api/types/orderbook.d.ts.map +1 -0
- package/dist/api/types/orderbook.js +6 -0
- package/dist/api/types/orderbook.js.map +1 -0
- package/dist/api/types/position.d.ts +60 -0
- package/dist/api/types/position.d.ts.map +1 -0
- package/dist/api/types/position.js +6 -0
- package/dist/api/types/position.js.map +1 -0
- package/dist/api/types/price_history.d.ts +68 -0
- package/dist/api/types/price_history.d.ts.map +1 -0
- package/dist/api/types/price_history.js +13 -0
- package/dist/api/types/price_history.js.map +1 -0
- package/dist/api/types/trade.d.ts +67 -0
- package/dist/api/types/trade.d.ts.map +1 -0
- package/dist/api/types/trade.js +13 -0
- package/dist/api/types/trade.js.map +1 -0
- package/dist/api/validation.d.ts +24 -0
- package/dist/api/validation.d.ts.map +1 -0
- package/dist/api/validation.js +53 -0
- package/dist/api/validation.js.map +1 -0
- package/dist/auth.d.ts +80 -0
- package/dist/auth.d.ts.map +1 -0
- package/dist/auth.js +149 -0
- package/dist/auth.js.map +1 -0
- package/dist/index.d.ts +55 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +107 -0
- package/dist/index.js.map +1 -0
- package/dist/network.d.ts +5 -0
- package/dist/network.d.ts.map +1 -0
- package/dist/network.js +8 -0
- package/dist/network.js.map +1 -0
- package/dist/program/accounts.d.ts +98 -0
- package/dist/program/accounts.d.ts.map +1 -0
- package/dist/program/accounts.js +319 -0
- package/dist/program/accounts.js.map +1 -0
- package/dist/program/builder.d.ts +94 -0
- package/dist/program/builder.d.ts.map +1 -0
- package/dist/program/builder.js +175 -0
- package/dist/program/builder.js.map +1 -0
- package/dist/program/client.d.ts +56 -0
- package/dist/program/client.d.ts.map +1 -0
- package/dist/program/client.js +288 -0
- package/dist/program/client.js.map +1 -0
- package/dist/program/constants.d.ts +108 -0
- package/dist/program/constants.d.ts.map +1 -0
- package/dist/program/constants.js +112 -0
- package/dist/program/constants.js.map +1 -0
- package/dist/program/index.d.ts +14 -0
- package/dist/program/index.d.ts.map +1 -0
- package/dist/program/index.js +149 -0
- package/dist/program/index.js.map +1 -0
- package/dist/program/instructions.d.ts +248 -0
- package/dist/program/instructions.d.ts.map +1 -0
- package/dist/program/instructions.js +692 -0
- package/dist/program/instructions.js.map +1 -0
- package/dist/program/orders.d.ts +151 -0
- package/dist/program/orders.d.ts.map +1 -0
- package/dist/program/orders.js +417 -0
- package/dist/program/orders.js.map +1 -0
- package/dist/program/pda.d.ts +73 -0
- package/dist/program/pda.d.ts.map +1 -0
- package/dist/program/pda.js +131 -0
- package/dist/program/pda.js.map +1 -0
- package/dist/program/types.d.ts +380 -0
- package/dist/program/types.d.ts.map +1 -0
- package/dist/program/types.js +27 -0
- package/dist/program/types.js.map +1 -0
- package/dist/program/utils.d.ts +91 -0
- package/dist/program/utils.d.ts.map +1 -0
- package/dist/program/utils.js +219 -0
- package/dist/program/utils.js.map +1 -0
- package/dist/shared/index.d.ts +8 -0
- package/dist/shared/index.d.ts.map +1 -0
- package/dist/shared/index.js +18 -0
- package/dist/shared/index.js.map +1 -0
- package/dist/shared/price.d.ts +41 -0
- package/dist/shared/price.d.ts.map +1 -0
- package/dist/shared/price.js +57 -0
- package/dist/shared/price.js.map +1 -0
- package/dist/shared/scaling.d.ts +45 -0
- package/dist/shared/scaling.d.ts.map +1 -0
- package/dist/shared/scaling.js +84 -0
- package/dist/shared/scaling.js.map +1 -0
- package/dist/shared/types.d.ts +19 -0
- package/dist/shared/types.d.ts.map +1 -0
- package/dist/shared/types.js +23 -0
- package/dist/shared/types.js.map +1 -0
- package/dist/websocket/client.d.ts +238 -0
- package/dist/websocket/client.d.ts.map +1 -0
- package/dist/websocket/client.js +580 -0
- package/dist/websocket/client.js.map +1 -0
- package/dist/websocket/error.d.ts +47 -0
- package/dist/websocket/error.d.ts.map +1 -0
- package/dist/websocket/error.js +83 -0
- package/dist/websocket/error.js.map +1 -0
- package/dist/websocket/handlers.d.ts +97 -0
- package/dist/websocket/handlers.d.ts.map +1 -0
- package/dist/websocket/handlers.js +277 -0
- package/dist/websocket/handlers.js.map +1 -0
- package/dist/websocket/index.d.ts +38 -0
- package/dist/websocket/index.d.ts.map +1 -0
- package/dist/websocket/index.js +75 -0
- package/dist/websocket/index.js.map +1 -0
- package/dist/websocket/state/index.d.ts +7 -0
- package/dist/websocket/state/index.d.ts.map +1 -0
- package/dist/websocket/state/index.js +14 -0
- package/dist/websocket/state/index.js.map +1 -0
- package/dist/websocket/state/orderbook.d.ts +107 -0
- package/dist/websocket/state/orderbook.d.ts.map +1 -0
- package/dist/websocket/state/orderbook.js +293 -0
- package/dist/websocket/state/orderbook.js.map +1 -0
- package/dist/websocket/state/price.d.ts +108 -0
- package/dist/websocket/state/price.d.ts.map +1 -0
- package/dist/websocket/state/price.js +243 -0
- package/dist/websocket/state/price.js.map +1 -0
- package/dist/websocket/state/user.d.ts +83 -0
- package/dist/websocket/state/user.d.ts.map +1 -0
- package/dist/websocket/state/user.js +228 -0
- package/dist/websocket/state/user.js.map +1 -0
- package/dist/websocket/subscriptions.d.ts +143 -0
- package/dist/websocket/subscriptions.d.ts.map +1 -0
- package/dist/websocket/subscriptions.js +244 -0
- package/dist/websocket/subscriptions.js.map +1 -0
- package/dist/websocket/types.d.ts +417 -0
- package/dist/websocket/types.d.ts.map +1 -0
- package/dist/websocket/types.js +195 -0
- package/dist/websocket/types.js.map +1 -0
- package/package.json +75 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OrderSide = exports.MarketStatus = void 0;
|
|
4
|
+
// ============================================================================
|
|
5
|
+
// ENUMS
|
|
6
|
+
// ============================================================================
|
|
7
|
+
/**
|
|
8
|
+
* Market status enum matching on-chain representation
|
|
9
|
+
*/
|
|
10
|
+
var MarketStatus;
|
|
11
|
+
(function (MarketStatus) {
|
|
12
|
+
MarketStatus[MarketStatus["Pending"] = 0] = "Pending";
|
|
13
|
+
MarketStatus[MarketStatus["Active"] = 1] = "Active";
|
|
14
|
+
MarketStatus[MarketStatus["Resolved"] = 2] = "Resolved";
|
|
15
|
+
MarketStatus[MarketStatus["Cancelled"] = 3] = "Cancelled";
|
|
16
|
+
})(MarketStatus || (exports.MarketStatus = MarketStatus = {}));
|
|
17
|
+
/**
|
|
18
|
+
* Order side enum
|
|
19
|
+
* BID = buyer wants base tokens (pays quote)
|
|
20
|
+
* ASK = seller offers base tokens (receives quote)
|
|
21
|
+
*/
|
|
22
|
+
var OrderSide;
|
|
23
|
+
(function (OrderSide) {
|
|
24
|
+
OrderSide[OrderSide["BID"] = 0] = "BID";
|
|
25
|
+
OrderSide[OrderSide["ASK"] = 1] = "ASK";
|
|
26
|
+
})(OrderSide || (exports.OrderSide = OrderSide = {}));
|
|
27
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/program/types.ts"],"names":[],"mappings":";;;AAEA,+EAA+E;AAC/E,QAAQ;AACR,+EAA+E;AAE/E;;GAEG;AACH,IAAY,YAKX;AALD,WAAY,YAAY;IACtB,qDAAW,CAAA;IACX,mDAAU,CAAA;IACV,uDAAY,CAAA;IACZ,yDAAa,CAAA;AACf,CAAC,EALW,YAAY,4BAAZ,YAAY,QAKvB;AAED;;;;GAIG;AACH,IAAY,SAGX;AAHD,WAAY,SAAS;IACnB,uCAAO,CAAA;IACP,uCAAO,CAAA;AACT,CAAC,EAHW,SAAS,yBAAT,SAAS,QAGpB","sourcesContent":["import { PublicKey, Transaction } from \"@solana/web3.js\";\n\n// ============================================================================\n// ENUMS\n// ============================================================================\n\n/**\n * Market status enum matching on-chain representation\n */\nexport enum MarketStatus {\n Pending = 0,\n Active = 1,\n Resolved = 2,\n Cancelled = 3,\n}\n\n/**\n * Order side enum\n * BID = buyer wants base tokens (pays quote)\n * ASK = seller offers base tokens (receives quote)\n */\nexport enum OrderSide {\n BID = 0,\n ASK = 1,\n}\n\n// ============================================================================\n// ACCOUNT TYPES\n// ============================================================================\n\n/**\n * Exchange account - singleton central state\n * PDA: [\"central_state\"]\n * Size: 88 bytes\n */\nexport interface Exchange {\n discriminator: Buffer; // 8 bytes\n authority: PublicKey; // 32 bytes - initial admin\n operator: PublicKey; // 32 bytes - can perform operational tasks\n marketCount: bigint; // u64 - incremented for each market\n paused: boolean; // u8 - 0 = active, 1 = paused\n bump: number; // u8\n}\n\n/**\n * Market account\n * PDA: [\"market\", market_id (u64)]\n * Size: 120 bytes\n */\nexport interface Market {\n discriminator: Buffer; // 8 bytes\n marketId: bigint; // u64 - auto-assigned, sequential\n numOutcomes: number; // u8 - 2-6 outcomes supported\n status: MarketStatus; // u8\n winningOutcome: number; // u8\n hasWinningOutcome: boolean; // u8\n bump: number; // u8\n oracle: PublicKey; // 32 bytes - who can settle the market\n questionId: Buffer; // 32 bytes\n conditionId: Buffer; // 32 bytes - derived from oracle + questionId + numOutcomes\n}\n\n/**\n * Order status account - tracks partial fills and cancellations\n * PDA: [\"order_status\", order_hash (32 bytes)]\n * Size: 24 bytes\n */\nexport interface OrderStatus {\n discriminator: Buffer; // 8 bytes\n remaining: bigint; // u64 - maker_amount not yet filled\n isCancelled: boolean; // u8\n}\n\n/**\n * User nonce account - replay protection\n * PDA: [\"user_nonce\", user_pubkey (32 bytes)]\n * Size: 16 bytes\n */\nexport interface UserNonce {\n discriminator: Buffer; // 8 bytes\n nonce: bigint; // u64 - incremented per order\n}\n\n/**\n * Position account - user's state in a market\n * PDA: [\"position\", owner (32 bytes), market (32 bytes)]\n * Size: 80 bytes\n */\nexport interface Position {\n discriminator: Buffer; // 8 bytes\n owner: PublicKey; // 32 bytes\n market: PublicKey; // 32 bytes\n bump: number; // u8\n}\n\n/**\n * Orderbook account - links market to token pair and lookup table\n * PDA: [\"orderbook\", mint_a (32 bytes), mint_b (32 bytes)]\n * Size: 144 bytes\n */\nexport interface Orderbook {\n discriminator: Buffer; // 8 bytes\n market: PublicKey; // 32 bytes\n mintA: PublicKey; // 32 bytes\n mintB: PublicKey; // 32 bytes\n lookupTable: PublicKey; // 32 bytes\n bump: number; // u8\n}\n\n// ============================================================================\n// ORDER TYPES\n// ============================================================================\n\n/**\n * Signed order format (225 bytes)\n * Full order with all fields for submission, cancellation, and hashing\n */\nexport interface SignedOrder {\n nonce: number; // u32 - order ID + replay protection (serialized as u64 LE on wire)\n maker: PublicKey; // 32 bytes - signer\n market: PublicKey; // 32 bytes\n baseMint: PublicKey; // 32 bytes - token being bought/sold\n quoteMint: PublicKey; // 32 bytes - payment token\n side: OrderSide; // u8 - 0=BID, 1=ASK\n makerAmount: bigint; // u64 - what maker gives\n takerAmount: bigint; // u64 - what maker receives\n expiration: bigint; // i64 - unix timestamp, 0=no expiration\n signature: Buffer; // 64 bytes - Ed25519 signature\n}\n\n/**\n * Compact order format (29 bytes)\n * Transaction-optimized version: nonce is u32, no maker field (derived on-chain from Position PDA)\n */\nexport interface Order {\n nonce: number; // u32 (4 bytes) - truncated from SignedOrder's u64 nonce\n side: OrderSide; // u8\n makerAmount: bigint; // u64\n takerAmount: bigint; // u64\n expiration: bigint; // i64\n}\n\n// ============================================================================\n// PARAMETER TYPES\n// ============================================================================\n\n/**\n * Parameters for initialize instruction\n */\nexport interface InitializeParams {\n authority: PublicKey;\n}\n\n/**\n * Parameters for createMarket instruction\n */\nexport interface CreateMarketParams {\n authority: PublicKey;\n numOutcomes: number; // 2-6\n oracle: PublicKey;\n questionId: Buffer; // 32 bytes\n}\n\n/**\n * Metadata for a single outcome token\n */\nexport interface OutcomeMetadata {\n name: string;\n symbol: string;\n uri: string;\n}\n\n/**\n * Parameters for addDepositMint instruction\n */\nexport interface AddDepositMintParams {\n authority: PublicKey;\n marketId: bigint;\n depositMint: PublicKey;\n outcomeMetadata: OutcomeMetadata[];\n}\n\n/**\n * Parameters for mintCompleteSet instruction\n */\nexport interface MintCompleteSetParams {\n user: PublicKey;\n market: PublicKey;\n depositMint: PublicKey;\n amount: bigint;\n}\n\n/**\n * Parameters for mergeCompleteSet instruction\n */\nexport interface MergeCompleteSetParams {\n user: PublicKey;\n market: PublicKey;\n depositMint: PublicKey;\n amount: bigint;\n}\n\n/**\n * Parameters for cancelOrder instruction\n */\nexport interface CancelOrderParams {\n maker: PublicKey;\n order: SignedOrder;\n}\n\n/**\n * Parameters for incrementNonce instruction\n */\nexport interface IncrementNonceParams {\n user: PublicKey;\n}\n\n/**\n * Parameters for settleMarket instruction\n */\nexport interface SettleMarketParams {\n oracle: PublicKey;\n marketId: bigint;\n winningOutcome: number;\n}\n\n/**\n * Parameters for redeemWinnings instruction\n */\nexport interface RedeemWinningsParams {\n user: PublicKey;\n market: PublicKey;\n depositMint: PublicKey;\n amount: bigint;\n}\n\n/**\n * Parameters for setPaused instruction\n */\nexport interface SetPausedParams {\n authority: PublicKey;\n paused: boolean;\n}\n\n/**\n * Parameters for setOperator instruction\n */\nexport interface SetOperatorParams {\n authority: PublicKey;\n newOperator: PublicKey;\n}\n\n/**\n * Parameters for withdrawFromPosition instruction\n */\nexport interface WithdrawFromPositionParams {\n user: PublicKey;\n market: PublicKey;\n mint: PublicKey; // Can be deposit mint or conditional mint\n amount: bigint;\n outcomeIndex: number; // u8 outcome index\n}\n\n/**\n * Parameters for activateMarket instruction\n */\nexport interface ActivateMarketParams {\n authority: PublicKey;\n marketId: bigint;\n}\n\n/**\n * Parameters for matchOrdersMulti instruction\n */\nexport interface MatchOrdersMultiParams {\n operator: PublicKey;\n market: PublicKey;\n baseMint: PublicKey;\n quoteMint: PublicKey;\n takerOrder: SignedOrder;\n makerOrders: SignedOrder[];\n makerFillAmounts: bigint[]; // Per maker - what each maker gives\n takerFillAmounts: bigint[]; // Per maker - what taker gives to each maker\n fullFillBitmask: number; // u8 bitmask: bit 7 = taker, bits 0..n = makers\n}\n\n/**\n * Parameters for setAuthority instruction\n */\nexport interface SetAuthorityParams {\n currentAuthority: PublicKey;\n newAuthority: PublicKey;\n}\n\n/**\n * Parameters for createOrderbook instruction\n */\nexport interface CreateOrderbookParams {\n payer: PublicKey;\n market: PublicKey;\n mintA: PublicKey;\n mintB: PublicKey;\n recentSlot: bigint;\n}\n\n// ============================================================================\n// BUILDER RESULT TYPES\n// ============================================================================\n\n/**\n * Result from transaction builders\n */\nexport interface BuildResult<T = Record<string, PublicKey>> {\n /** Unsigned transaction ready for signing */\n transaction: Transaction;\n /** Key accounts involved in the transaction */\n accounts: T;\n /** Serialize transaction to base64 */\n serialize: () => string;\n}\n\n/**\n * Accounts returned from initialize\n */\nexport interface InitializeAccounts {\n exchange: PublicKey;\n}\n\n/**\n * Accounts returned from createMarket\n */\nexport interface CreateMarketAccounts {\n exchange: PublicKey;\n market: PublicKey;\n}\n\n/**\n * Accounts returned from addDepositMint\n */\nexport interface AddDepositMintAccounts {\n market: PublicKey;\n vault: PublicKey;\n mintAuthority: PublicKey;\n conditionalMints: PublicKey[];\n}\n\n/**\n * Accounts returned from mintCompleteSet\n */\nexport interface MintCompleteSetAccounts {\n position: PublicKey;\n vault: PublicKey;\n conditionalMints: PublicKey[];\n}\n\n/**\n * Accounts returned from mergeCompleteSet\n */\nexport interface MergeCompleteSetAccounts {\n position: PublicKey;\n vault: PublicKey;\n conditionalMints: PublicKey[];\n}\n\n/**\n * Accounts returned from cancelOrder\n */\nexport interface CancelOrderAccounts {\n orderStatus: PublicKey;\n}\n\n/**\n * Accounts returned from incrementNonce\n */\nexport interface IncrementNonceAccounts {\n userNonce: PublicKey;\n}\n\n/**\n * Accounts returned from settleMarket\n */\nexport interface SettleMarketAccounts {\n exchange: PublicKey;\n market: PublicKey;\n}\n\n/**\n * Accounts returned from redeemWinnings\n */\nexport interface RedeemWinningsAccounts {\n position: PublicKey;\n vault: PublicKey;\n winningMint: PublicKey;\n}\n\n/**\n * Accounts returned from activateMarket\n */\nexport interface ActivateMarketAccounts {\n exchange: PublicKey;\n market: PublicKey;\n}\n\n/**\n * Accounts returned from matchOrdersMulti\n */\nexport interface MatchOrdersMultiAccounts {\n takerOrderStatus: PublicKey;\n takerPosition: PublicKey;\n makerOrderStatuses: PublicKey[];\n makerPositions: PublicKey[];\n}\n\n// ============================================================================\n// ORDER CREATION TYPES\n// ============================================================================\n\n/**\n * Parameters for creating a bid order\n */\nexport interface BidOrderParams {\n nonce: number;\n maker: PublicKey;\n market: PublicKey;\n baseMint: PublicKey; // Token to buy\n quoteMint: PublicKey; // Token to pay with\n makerAmount: bigint; // Quote tokens to give\n takerAmount: bigint; // Base tokens to receive\n expiration?: bigint; // 0 = no expiration\n}\n\n/**\n * Parameters for creating an ask order\n */\nexport interface AskOrderParams {\n nonce: number;\n maker: PublicKey;\n market: PublicKey;\n baseMint: PublicKey; // Token to sell\n quoteMint: PublicKey; // Token to receive\n makerAmount: bigint; // Base tokens to give\n takerAmount: bigint; // Quote tokens to receive\n expiration?: bigint; // 0 = no expiration\n}\n"]}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { PublicKey } from "@solana/web3.js";
|
|
2
|
+
/**
|
|
3
|
+
* Convert a bigint to little-endian bytes
|
|
4
|
+
* @param value - The value to convert
|
|
5
|
+
* @param bytes - Number of bytes in the output
|
|
6
|
+
*/
|
|
7
|
+
export declare function toLeBytes(value: bigint, bytes: number): Buffer;
|
|
8
|
+
/**
|
|
9
|
+
* Convert little-endian bytes to bigint
|
|
10
|
+
* @param buffer - The buffer to convert
|
|
11
|
+
*/
|
|
12
|
+
export declare function fromLeBytes(buffer: Buffer): bigint;
|
|
13
|
+
/**
|
|
14
|
+
* Convert a bigint to big-endian bytes
|
|
15
|
+
* @param value - The value to convert
|
|
16
|
+
* @param bytes - Number of bytes in the output
|
|
17
|
+
*/
|
|
18
|
+
export declare function toBeBytes(value: bigint, bytes: number): Buffer;
|
|
19
|
+
/**
|
|
20
|
+
* Convert big-endian bytes to bigint
|
|
21
|
+
* @param buffer - The buffer to convert
|
|
22
|
+
*/
|
|
23
|
+
export declare function fromBeBytes(buffer: Buffer): bigint;
|
|
24
|
+
/**
|
|
25
|
+
* Convert a number to u8 buffer
|
|
26
|
+
*/
|
|
27
|
+
export declare function toU8(value: number): Buffer;
|
|
28
|
+
/**
|
|
29
|
+
* Convert a bigint to u64 little-endian buffer
|
|
30
|
+
*/
|
|
31
|
+
export declare function toU64Le(value: bigint): Buffer;
|
|
32
|
+
/**
|
|
33
|
+
* Convert a number to u32 little-endian buffer
|
|
34
|
+
*/
|
|
35
|
+
export declare function toU32Le(value: number): Buffer;
|
|
36
|
+
/**
|
|
37
|
+
* Convert a bigint to i64 little-endian buffer (signed)
|
|
38
|
+
*/
|
|
39
|
+
export declare function toI64Le(value: bigint): Buffer;
|
|
40
|
+
/**
|
|
41
|
+
* Convert a buffer to i64 (signed)
|
|
42
|
+
*/
|
|
43
|
+
export declare function fromI64Le(buffer: Buffer): bigint;
|
|
44
|
+
/**
|
|
45
|
+
* Compute keccak256 hash of data
|
|
46
|
+
* @param data - The data to hash
|
|
47
|
+
* @returns 32-byte hash buffer
|
|
48
|
+
*/
|
|
49
|
+
export declare function keccak256(data: Buffer): Buffer;
|
|
50
|
+
/**
|
|
51
|
+
* Derive condition ID from oracle, question ID, and number of outcomes
|
|
52
|
+
* This matches the on-chain derivation: keccak256(oracle || questionId || numOutcomes)
|
|
53
|
+
*/
|
|
54
|
+
export declare function deriveConditionId(oracle: PublicKey, questionId: Buffer, numOutcomes: number): Buffer;
|
|
55
|
+
/**
|
|
56
|
+
* Derive Associated Token Address
|
|
57
|
+
* @param mint - The token mint
|
|
58
|
+
* @param owner - The owner of the ATA
|
|
59
|
+
* @param token2022 - Whether to use Token-2022 program
|
|
60
|
+
*/
|
|
61
|
+
export declare function getAssociatedTokenAddress(mint: PublicKey, owner: PublicKey, token2022?: boolean): PublicKey;
|
|
62
|
+
/**
|
|
63
|
+
* Get ATA for a conditional token (always Token-2022)
|
|
64
|
+
*/
|
|
65
|
+
export declare function getConditionalTokenAta(mint: PublicKey, owner: PublicKey): PublicKey;
|
|
66
|
+
/**
|
|
67
|
+
* Get ATA for a deposit token (SPL Token)
|
|
68
|
+
*/
|
|
69
|
+
export declare function getDepositTokenAta(mint: PublicKey, owner: PublicKey): PublicKey;
|
|
70
|
+
/**
|
|
71
|
+
* Serialize a string with u16 length prefix (little-endian)
|
|
72
|
+
*/
|
|
73
|
+
export declare function serializeString(str: string): Buffer;
|
|
74
|
+
/**
|
|
75
|
+
* Deserialize a string with u16 length prefix
|
|
76
|
+
* @returns [string, bytesConsumed]
|
|
77
|
+
*/
|
|
78
|
+
export declare function deserializeString(buffer: Buffer, offset: number): [string, number];
|
|
79
|
+
/**
|
|
80
|
+
* Validate that a number is within the valid outcomes range
|
|
81
|
+
*/
|
|
82
|
+
export declare function validateOutcomes(numOutcomes: number): void;
|
|
83
|
+
/**
|
|
84
|
+
* Validate that an outcome index is valid for a market
|
|
85
|
+
*/
|
|
86
|
+
export declare function validateOutcomeIndex(outcomeIndex: number, numOutcomes: number): void;
|
|
87
|
+
/**
|
|
88
|
+
* Validate that a buffer is exactly 32 bytes
|
|
89
|
+
*/
|
|
90
|
+
export declare function validate32Bytes(buffer: Buffer, name: string): void;
|
|
91
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/program/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAY5C;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAQ9D;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAMlD;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAQ9D;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAMlD;AAED;;GAEG;AACH,wBAAgB,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAI1C;AAED;;GAEG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAE7C;AAED;;GAEG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAI7C;AAED;;GAEG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAO7C;AAED;;GAEG;AACH,wBAAgB,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAOhD;AAMD;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAE9C;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAC/B,MAAM,EAAE,SAAS,EACjB,UAAU,EAAE,MAAM,EAClB,WAAW,EAAE,MAAM,GAClB,MAAM,CAOR;AAMD;;;;;GAKG;AACH,wBAAgB,yBAAyB,CACvC,IAAI,EAAE,SAAS,EACf,KAAK,EAAE,SAAS,EAChB,SAAS,GAAE,OAAe,GACzB,SAAS,CASX;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CACpC,IAAI,EAAE,SAAS,EACf,KAAK,EAAE,SAAS,GACf,SAAS,CAEX;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,SAAS,EACf,KAAK,EAAE,SAAS,GACf,SAAS,CAEX;AAMD;;GAEG;AACH,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAKnD;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAC/B,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,GACb,CAAC,MAAM,EAAE,MAAM,CAAC,CAIlB;AAMD;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,WAAW,EAAE,MAAM,GAAG,IAAI,CAM1D;AAED;;GAEG;AACH,wBAAgB,oBAAoB,CAClC,YAAY,EAAE,MAAM,EACpB,WAAW,EAAE,MAAM,GAClB,IAAI,CAMN;AAED;;GAEG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAIlE"}
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toLeBytes = toLeBytes;
|
|
4
|
+
exports.fromLeBytes = fromLeBytes;
|
|
5
|
+
exports.toBeBytes = toBeBytes;
|
|
6
|
+
exports.fromBeBytes = fromBeBytes;
|
|
7
|
+
exports.toU8 = toU8;
|
|
8
|
+
exports.toU64Le = toU64Le;
|
|
9
|
+
exports.toU32Le = toU32Le;
|
|
10
|
+
exports.toI64Le = toI64Le;
|
|
11
|
+
exports.fromI64Le = fromI64Le;
|
|
12
|
+
exports.keccak256 = keccak256;
|
|
13
|
+
exports.deriveConditionId = deriveConditionId;
|
|
14
|
+
exports.getAssociatedTokenAddress = getAssociatedTokenAddress;
|
|
15
|
+
exports.getConditionalTokenAta = getConditionalTokenAta;
|
|
16
|
+
exports.getDepositTokenAta = getDepositTokenAta;
|
|
17
|
+
exports.serializeString = serializeString;
|
|
18
|
+
exports.deserializeString = deserializeString;
|
|
19
|
+
exports.validateOutcomes = validateOutcomes;
|
|
20
|
+
exports.validateOutcomeIndex = validateOutcomeIndex;
|
|
21
|
+
exports.validate32Bytes = validate32Bytes;
|
|
22
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
23
|
+
const js_sha3_1 = require("js-sha3");
|
|
24
|
+
const constants_1 = require("./constants");
|
|
25
|
+
// ============================================================================
|
|
26
|
+
// BUFFER UTILITIES - Little Endian
|
|
27
|
+
// ============================================================================
|
|
28
|
+
/**
|
|
29
|
+
* Convert a bigint to little-endian bytes
|
|
30
|
+
* @param value - The value to convert
|
|
31
|
+
* @param bytes - Number of bytes in the output
|
|
32
|
+
*/
|
|
33
|
+
function toLeBytes(value, bytes) {
|
|
34
|
+
const buffer = Buffer.alloc(bytes);
|
|
35
|
+
let remaining = value;
|
|
36
|
+
for (let i = 0; i < bytes; i++) {
|
|
37
|
+
buffer[i] = Number(remaining & 0xffn);
|
|
38
|
+
remaining = remaining >> 8n;
|
|
39
|
+
}
|
|
40
|
+
return buffer;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Convert little-endian bytes to bigint
|
|
44
|
+
* @param buffer - The buffer to convert
|
|
45
|
+
*/
|
|
46
|
+
function fromLeBytes(buffer) {
|
|
47
|
+
let result = 0n;
|
|
48
|
+
for (let i = buffer.length - 1; i >= 0; i--) {
|
|
49
|
+
result = (result << 8n) | BigInt(buffer[i]);
|
|
50
|
+
}
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Convert a bigint to big-endian bytes
|
|
55
|
+
* @param value - The value to convert
|
|
56
|
+
* @param bytes - Number of bytes in the output
|
|
57
|
+
*/
|
|
58
|
+
function toBeBytes(value, bytes) {
|
|
59
|
+
const buffer = Buffer.alloc(bytes);
|
|
60
|
+
let remaining = value;
|
|
61
|
+
for (let i = bytes - 1; i >= 0; i--) {
|
|
62
|
+
buffer[i] = Number(remaining & 0xffn);
|
|
63
|
+
remaining = remaining >> 8n;
|
|
64
|
+
}
|
|
65
|
+
return buffer;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Convert big-endian bytes to bigint
|
|
69
|
+
* @param buffer - The buffer to convert
|
|
70
|
+
*/
|
|
71
|
+
function fromBeBytes(buffer) {
|
|
72
|
+
let result = 0n;
|
|
73
|
+
for (let i = 0; i < buffer.length; i++) {
|
|
74
|
+
result = (result << 8n) | BigInt(buffer[i]);
|
|
75
|
+
}
|
|
76
|
+
return result;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Convert a number to u8 buffer
|
|
80
|
+
*/
|
|
81
|
+
function toU8(value) {
|
|
82
|
+
const buffer = Buffer.alloc(1);
|
|
83
|
+
buffer[0] = value & 0xff;
|
|
84
|
+
return buffer;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Convert a bigint to u64 little-endian buffer
|
|
88
|
+
*/
|
|
89
|
+
function toU64Le(value) {
|
|
90
|
+
return toLeBytes(value, 8);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Convert a number to u32 little-endian buffer
|
|
94
|
+
*/
|
|
95
|
+
function toU32Le(value) {
|
|
96
|
+
const buffer = Buffer.alloc(4);
|
|
97
|
+
buffer.writeUInt32LE(value, 0);
|
|
98
|
+
return buffer;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Convert a bigint to i64 little-endian buffer (signed)
|
|
102
|
+
*/
|
|
103
|
+
function toI64Le(value) {
|
|
104
|
+
// For negative values, we need to handle two's complement
|
|
105
|
+
if (value < 0n) {
|
|
106
|
+
// Add 2^64 to get two's complement representation
|
|
107
|
+
value = value + (1n << 64n);
|
|
108
|
+
}
|
|
109
|
+
return toLeBytes(value, 8);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Convert a buffer to i64 (signed)
|
|
113
|
+
*/
|
|
114
|
+
function fromI64Le(buffer) {
|
|
115
|
+
const unsigned = fromLeBytes(buffer);
|
|
116
|
+
// Check if the high bit is set (negative number)
|
|
117
|
+
if (unsigned >= 1n << 63n) {
|
|
118
|
+
return unsigned - (1n << 64n);
|
|
119
|
+
}
|
|
120
|
+
return unsigned;
|
|
121
|
+
}
|
|
122
|
+
// ============================================================================
|
|
123
|
+
// KECCAK256 HASHING
|
|
124
|
+
// ============================================================================
|
|
125
|
+
/**
|
|
126
|
+
* Compute keccak256 hash of data
|
|
127
|
+
* @param data - The data to hash
|
|
128
|
+
* @returns 32-byte hash buffer
|
|
129
|
+
*/
|
|
130
|
+
function keccak256(data) {
|
|
131
|
+
return Buffer.from(js_sha3_1.keccak_256.arrayBuffer(data));
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Derive condition ID from oracle, question ID, and number of outcomes
|
|
135
|
+
* This matches the on-chain derivation: keccak256(oracle || questionId || numOutcomes)
|
|
136
|
+
*/
|
|
137
|
+
function deriveConditionId(oracle, questionId, numOutcomes) {
|
|
138
|
+
const data = Buffer.concat([
|
|
139
|
+
oracle.toBuffer(),
|
|
140
|
+
questionId,
|
|
141
|
+
toU8(numOutcomes),
|
|
142
|
+
]);
|
|
143
|
+
return keccak256(data);
|
|
144
|
+
}
|
|
145
|
+
// ============================================================================
|
|
146
|
+
// ASSOCIATED TOKEN ADDRESS HELPERS
|
|
147
|
+
// ============================================================================
|
|
148
|
+
/**
|
|
149
|
+
* Derive Associated Token Address
|
|
150
|
+
* @param mint - The token mint
|
|
151
|
+
* @param owner - The owner of the ATA
|
|
152
|
+
* @param token2022 - Whether to use Token-2022 program
|
|
153
|
+
*/
|
|
154
|
+
function getAssociatedTokenAddress(mint, owner, token2022 = false) {
|
|
155
|
+
const tokenProgramId = token2022 ? constants_1.TOKEN_2022_PROGRAM_ID : constants_1.TOKEN_PROGRAM_ID;
|
|
156
|
+
const [address] = web3_js_1.PublicKey.findProgramAddressSync([owner.toBuffer(), tokenProgramId.toBuffer(), mint.toBuffer()], constants_1.ASSOCIATED_TOKEN_PROGRAM_ID);
|
|
157
|
+
return address;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Get ATA for a conditional token (always Token-2022)
|
|
161
|
+
*/
|
|
162
|
+
function getConditionalTokenAta(mint, owner) {
|
|
163
|
+
return getAssociatedTokenAddress(mint, owner, true);
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Get ATA for a deposit token (SPL Token)
|
|
167
|
+
*/
|
|
168
|
+
function getDepositTokenAta(mint, owner) {
|
|
169
|
+
return getAssociatedTokenAddress(mint, owner, false);
|
|
170
|
+
}
|
|
171
|
+
// ============================================================================
|
|
172
|
+
// STRING SERIALIZATION
|
|
173
|
+
// ============================================================================
|
|
174
|
+
/**
|
|
175
|
+
* Serialize a string with u16 length prefix (little-endian)
|
|
176
|
+
*/
|
|
177
|
+
function serializeString(str) {
|
|
178
|
+
const strBuffer = Buffer.from(str, "utf-8");
|
|
179
|
+
const lengthBuffer = Buffer.alloc(2);
|
|
180
|
+
lengthBuffer.writeUInt16LE(strBuffer.length, 0);
|
|
181
|
+
return Buffer.concat([lengthBuffer, strBuffer]);
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Deserialize a string with u16 length prefix
|
|
185
|
+
* @returns [string, bytesConsumed]
|
|
186
|
+
*/
|
|
187
|
+
function deserializeString(buffer, offset) {
|
|
188
|
+
const length = buffer.readUInt16LE(offset);
|
|
189
|
+
const str = buffer.toString("utf-8", offset + 2, offset + 2 + length);
|
|
190
|
+
return [str, 2 + length];
|
|
191
|
+
}
|
|
192
|
+
// ============================================================================
|
|
193
|
+
// VALIDATION HELPERS
|
|
194
|
+
// ============================================================================
|
|
195
|
+
/**
|
|
196
|
+
* Validate that a number is within the valid outcomes range
|
|
197
|
+
*/
|
|
198
|
+
function validateOutcomes(numOutcomes) {
|
|
199
|
+
if (numOutcomes < 2 || numOutcomes > 6) {
|
|
200
|
+
throw new Error(`Invalid number of outcomes: ${numOutcomes}. Must be between 2 and 6.`);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Validate that an outcome index is valid for a market
|
|
205
|
+
*/
|
|
206
|
+
function validateOutcomeIndex(outcomeIndex, numOutcomes) {
|
|
207
|
+
if (outcomeIndex < 0 || outcomeIndex >= numOutcomes) {
|
|
208
|
+
throw new Error(`Invalid outcome index: ${outcomeIndex}. Must be between 0 and ${numOutcomes - 1}.`);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Validate that a buffer is exactly 32 bytes
|
|
213
|
+
*/
|
|
214
|
+
function validate32Bytes(buffer, name) {
|
|
215
|
+
if (buffer.length !== 32) {
|
|
216
|
+
throw new Error(`${name} must be exactly 32 bytes, got ${buffer.length}`);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/program/utils.ts"],"names":[],"mappings":";;AAiBA,8BAQC;AAMD,kCAMC;AAOD,8BAQC;AAMD,kCAMC;AAKD,oBAIC;AAKD,0BAEC;AAKD,0BAIC;AAKD,0BAOC;AAKD,8BAOC;AAWD,8BAEC;AAMD,8CAWC;AAYD,8DAaC;AAKD,wDAKC;AAKD,gDAKC;AASD,0CAKC;AAMD,8CAOC;AASD,4CAMC;AAKD,oDASC;AAKD,0CAIC;AA7PD,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AAE,CAAC;QACpD,MAAM,IAAI,KAAK,CACb,0BAA0B,YAAY,2BAA2B,WAAW,GAAG,CAAC,GAAG,CACpF,CAAC;IACJ,CAAC;AACH,CAAC;AAED;;GAEG;AACH,SAAgB,eAAe,CAAC,MAAc,EAAE,IAAY;IAC1D,IAAI,MAAM,CAAC,MAAM,KAAK,EAAE,EAAE,CAAC;QACzB,MAAM,IAAI,KAAK,CAAC,GAAG,IAAI,kCAAkC,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC;IAC5E,CAAC;AACH,CAAC","sourcesContent":["import { PublicKey } from \"@solana/web3.js\";\nimport { keccak_256 } from \"js-sha3\";\nimport {\n TOKEN_PROGRAM_ID,\n TOKEN_2022_PROGRAM_ID,\n ASSOCIATED_TOKEN_PROGRAM_ID,\n} from \"./constants\";\n\n// ============================================================================\n// BUFFER UTILITIES - Little Endian\n// ============================================================================\n\n/**\n * Convert a bigint to little-endian bytes\n * @param value - The value to convert\n * @param bytes - Number of bytes in the output\n */\nexport function toLeBytes(value: bigint, bytes: number): Buffer {\n const buffer = Buffer.alloc(bytes);\n let remaining = value;\n for (let i = 0; i < bytes; i++) {\n buffer[i] = Number(remaining & 0xffn);\n remaining = remaining >> 8n;\n }\n return buffer;\n}\n\n/**\n * Convert little-endian bytes to bigint\n * @param buffer - The buffer to convert\n */\nexport function fromLeBytes(buffer: Buffer): bigint {\n let result = 0n;\n for (let i = buffer.length - 1; i >= 0; i--) {\n result = (result << 8n) | BigInt(buffer[i]);\n }\n return result;\n}\n\n/**\n * Convert a bigint to big-endian bytes\n * @param value - The value to convert\n * @param bytes - Number of bytes in the output\n */\nexport function toBeBytes(value: bigint, bytes: number): Buffer {\n const buffer = Buffer.alloc(bytes);\n let remaining = value;\n for (let i = bytes - 1; i >= 0; i--) {\n buffer[i] = Number(remaining & 0xffn);\n remaining = remaining >> 8n;\n }\n return buffer;\n}\n\n/**\n * Convert big-endian bytes to bigint\n * @param buffer - The buffer to convert\n */\nexport function fromBeBytes(buffer: Buffer): bigint {\n let result = 0n;\n for (let i = 0; i < buffer.length; i++) {\n result = (result << 8n) | BigInt(buffer[i]);\n }\n return result;\n}\n\n/**\n * Convert a number to u8 buffer\n */\nexport function toU8(value: number): Buffer {\n const buffer = Buffer.alloc(1);\n buffer[0] = value & 0xff;\n return buffer;\n}\n\n/**\n * Convert a bigint to u64 little-endian buffer\n */\nexport function toU64Le(value: bigint): Buffer {\n return toLeBytes(value, 8);\n}\n\n/**\n * Convert a number to u32 little-endian buffer\n */\nexport function toU32Le(value: number): Buffer {\n const buffer = Buffer.alloc(4);\n buffer.writeUInt32LE(value, 0);\n return buffer;\n}\n\n/**\n * Convert a bigint to i64 little-endian buffer (signed)\n */\nexport function toI64Le(value: bigint): Buffer {\n // For negative values, we need to handle two's complement\n if (value < 0n) {\n // Add 2^64 to get two's complement representation\n value = value + (1n << 64n);\n }\n return toLeBytes(value, 8);\n}\n\n/**\n * Convert a buffer to i64 (signed)\n */\nexport function fromI64Le(buffer: Buffer): bigint {\n const unsigned = fromLeBytes(buffer);\n // Check if the high bit is set (negative number)\n if (unsigned >= 1n << 63n) {\n return unsigned - (1n << 64n);\n }\n return unsigned;\n}\n\n// ============================================================================\n// KECCAK256 HASHING\n// ============================================================================\n\n/**\n * Compute keccak256 hash of data\n * @param data - The data to hash\n * @returns 32-byte hash buffer\n */\nexport function keccak256(data: Buffer): Buffer {\n return Buffer.from(keccak_256.arrayBuffer(data));\n}\n\n/**\n * Derive condition ID from oracle, question ID, and number of outcomes\n * This matches the on-chain derivation: keccak256(oracle || questionId || numOutcomes)\n */\nexport function deriveConditionId(\n oracle: PublicKey,\n questionId: Buffer,\n numOutcomes: number\n): Buffer {\n const data = Buffer.concat([\n oracle.toBuffer(),\n questionId,\n toU8(numOutcomes),\n ]);\n return keccak256(data);\n}\n\n// ============================================================================\n// ASSOCIATED TOKEN ADDRESS HELPERS\n// ============================================================================\n\n/**\n * Derive Associated Token Address\n * @param mint - The token mint\n * @param owner - The owner of the ATA\n * @param token2022 - Whether to use Token-2022 program\n */\nexport function getAssociatedTokenAddress(\n mint: PublicKey,\n owner: PublicKey,\n token2022: boolean = false\n): PublicKey {\n const tokenProgramId = token2022 ? TOKEN_2022_PROGRAM_ID : TOKEN_PROGRAM_ID;\n\n const [address] = PublicKey.findProgramAddressSync(\n [owner.toBuffer(), tokenProgramId.toBuffer(), mint.toBuffer()],\n ASSOCIATED_TOKEN_PROGRAM_ID\n );\n\n return address;\n}\n\n/**\n * Get ATA for a conditional token (always Token-2022)\n */\nexport function getConditionalTokenAta(\n mint: PublicKey,\n owner: PublicKey\n): PublicKey {\n return getAssociatedTokenAddress(mint, owner, true);\n}\n\n/**\n * Get ATA for a deposit token (SPL Token)\n */\nexport function getDepositTokenAta(\n mint: PublicKey,\n owner: PublicKey\n): PublicKey {\n return getAssociatedTokenAddress(mint, owner, false);\n}\n\n// ============================================================================\n// STRING SERIALIZATION\n// ============================================================================\n\n/**\n * Serialize a string with u16 length prefix (little-endian)\n */\nexport function serializeString(str: string): Buffer {\n const strBuffer = Buffer.from(str, \"utf-8\");\n const lengthBuffer = Buffer.alloc(2);\n lengthBuffer.writeUInt16LE(strBuffer.length, 0);\n return Buffer.concat([lengthBuffer, strBuffer]);\n}\n\n/**\n * Deserialize a string with u16 length prefix\n * @returns [string, bytesConsumed]\n */\nexport function deserializeString(\n buffer: Buffer,\n offset: number\n): [string, number] {\n const length = buffer.readUInt16LE(offset);\n const str = buffer.toString(\"utf-8\", offset + 2, offset + 2 + length);\n return [str, 2 + length];\n}\n\n// ============================================================================\n// VALIDATION HELPERS\n// ============================================================================\n\n/**\n * Validate that a number is within the valid outcomes range\n */\nexport function validateOutcomes(numOutcomes: number): void {\n if (numOutcomes < 2 || numOutcomes > 6) {\n throw new Error(\n `Invalid number of outcomes: ${numOutcomes}. Must be between 2 and 6.`\n );\n }\n}\n\n/**\n * Validate that an outcome index is valid for a market\n */\nexport function validateOutcomeIndex(\n outcomeIndex: number,\n numOutcomes: number\n): void {\n if (outcomeIndex < 0 || outcomeIndex >= numOutcomes) {\n throw new Error(\n `Invalid outcome index: ${outcomeIndex}. Must be between 0 and ${numOutcomes - 1}.`\n );\n }\n}\n\n/**\n * Validate that a buffer is exactly 32 bytes\n */\nexport function validate32Bytes(buffer: Buffer, name: string): void {\n if (buffer.length !== 32) {\n throw new Error(`${name} must be exactly 32 bytes, got ${buffer.length}`);\n }\n}\n"]}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared utilities used across API and WebSocket modules.
|
|
3
|
+
*/
|
|
4
|
+
export { Resolution } from "./types";
|
|
5
|
+
export { parseDecimal, formatDecimal, isZero } from "./price";
|
|
6
|
+
export { scalePriceSize, ScalingError, } from "./scaling";
|
|
7
|
+
export type { OrderbookDecimals, ScaledAmounts } from "./scaling";
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/shared/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAC9D,OAAO,EACL,cAAc,EACd,YAAY,GACb,MAAM,WAAW,CAAC;AACnB,YAAY,EAAE,iBAAiB,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Shared utilities used across API and WebSocket modules.
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.ScalingError = exports.scalePriceSize = exports.isZero = exports.formatDecimal = exports.parseDecimal = exports.Resolution = void 0;
|
|
7
|
+
var types_1 = require("./types");
|
|
8
|
+
Object.defineProperty(exports, "Resolution", { enumerable: true, get: function () { return types_1.Resolution; } });
|
|
9
|
+
var price_1 = require("./price");
|
|
10
|
+
Object.defineProperty(exports, "parseDecimal", { enumerable: true, get: function () { return price_1.parseDecimal; } });
|
|
11
|
+
Object.defineProperty(exports, "formatDecimal", { enumerable: true, get: function () { return price_1.formatDecimal; } });
|
|
12
|
+
Object.defineProperty(exports, "isZero", { enumerable: true, get: function () { return price_1.isZero; } });
|
|
13
|
+
var scaling_1 = require("./scaling");
|
|
14
|
+
Object.defineProperty(exports, "scalePriceSize", { enumerable: true, get: function () { return scaling_1.scalePriceSize; } });
|
|
15
|
+
Object.defineProperty(exports, "ScalingError", { enumerable: true, get: function () { return scaling_1.ScalingError; } });
|
|
16
|
+
// deriveOrderbookId is exported from the program module (orders.ts)
|
|
17
|
+
// Re-exported here for convenience via the shared module path
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/shared/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAEH,iCAAqC;AAA5B,mGAAA,UAAU,OAAA;AACnB,iCAA8D;AAArD,qGAAA,YAAY,OAAA;AAAE,sGAAA,aAAa,OAAA;AAAE,+FAAA,MAAM,OAAA;AAC5C,qCAGmB;AAFjB,yGAAA,cAAc,OAAA;AACd,uGAAA,YAAY,OAAA;AAId,oEAAoE;AACpE,8DAA8D","sourcesContent":["/**\n * Shared utilities used across API and WebSocket modules.\n */\n\nexport { Resolution } from \"./types\";\nexport { parseDecimal, formatDecimal, isZero } from \"./price\";\nexport {\n scalePriceSize,\n ScalingError,\n} from \"./scaling\";\nexport type { OrderbookDecimals, ScaledAmounts } from \"./scaling\";\n\n// deriveOrderbookId is exported from the program module (orders.ts)\n// Re-exported here for convenience via the shared module path\n"]}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Price utilities for the Lightcone SDK.
|
|
3
|
+
*
|
|
4
|
+
* This module provides helper functions for working with decimal string prices.
|
|
5
|
+
* The SDK uses String types for price/size/balance fields to preserve
|
|
6
|
+
* the exact decimal representation from the server, as different tokens
|
|
7
|
+
* have different decimal places (USDC=6, SOL=9, BTC=8, etc.).
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Parse a decimal string to number for calculations.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* parseDecimal("0.500000"); // returns 0.5
|
|
15
|
+
* parseDecimal("1.000000"); // returns 1.0
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare function parseDecimal(s: string): number;
|
|
19
|
+
/**
|
|
20
|
+
* Format a number as a decimal string with specified precision.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```typescript
|
|
24
|
+
* formatDecimal(0.5, 6); // returns "0.500000"
|
|
25
|
+
* formatDecimal(1.0, 6); // returns "1.000000"
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare function formatDecimal(value: number, precision: number): string;
|
|
29
|
+
/**
|
|
30
|
+
* Check if a decimal string represents zero.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* isZero("0"); // true
|
|
35
|
+
* isZero("0.0"); // true
|
|
36
|
+
* isZero("0.000000"); // true
|
|
37
|
+
* isZero("0.001"); // false
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export declare function isZero(s: string): boolean;
|
|
41
|
+
//# sourceMappingURL=price.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"price.d.ts","sourceRoot":"","sources":["../../src/shared/price.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH;;;;;;;;GAQG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAM9C;AAED;;;;;;;;GAQG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,CAEtE;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,MAAM,CAAC,CAAC,EAAE,MAAM,GAAG,OAAO,CAGzC"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Price utilities for the Lightcone SDK.
|
|
4
|
+
*
|
|
5
|
+
* This module provides helper functions for working with decimal string prices.
|
|
6
|
+
* The SDK uses String types for price/size/balance fields to preserve
|
|
7
|
+
* the exact decimal representation from the server, as different tokens
|
|
8
|
+
* have different decimal places (USDC=6, SOL=9, BTC=8, etc.).
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.parseDecimal = parseDecimal;
|
|
12
|
+
exports.formatDecimal = formatDecimal;
|
|
13
|
+
exports.isZero = isZero;
|
|
14
|
+
/**
|
|
15
|
+
* Parse a decimal string to number for calculations.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* parseDecimal("0.500000"); // returns 0.5
|
|
20
|
+
* parseDecimal("1.000000"); // returns 1.0
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
function parseDecimal(s) {
|
|
24
|
+
const result = parseFloat(s);
|
|
25
|
+
if (isNaN(result)) {
|
|
26
|
+
throw new Error(`Invalid decimal string: ${s}`);
|
|
27
|
+
}
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Format a number as a decimal string with specified precision.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* formatDecimal(0.5, 6); // returns "0.500000"
|
|
36
|
+
* formatDecimal(1.0, 6); // returns "1.000000"
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
function formatDecimal(value, precision) {
|
|
40
|
+
return value.toFixed(precision);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Check if a decimal string represents zero.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```typescript
|
|
47
|
+
* isZero("0"); // true
|
|
48
|
+
* isZero("0.0"); // true
|
|
49
|
+
* isZero("0.000000"); // true
|
|
50
|
+
* isZero("0.001"); // false
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
function isZero(s) {
|
|
54
|
+
const parsed = parseFloat(s);
|
|
55
|
+
return !isNaN(parsed) && parsed === 0;
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=price.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"price.js","sourceRoot":"","sources":["../../src/shared/price.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;AAWH,oCAMC;AAWD,sCAEC;AAaD,wBAGC;AA5CD;;;;;;;;GAQG;AACH,SAAgB,YAAY,CAAC,CAAS;IACpC,MAAM,MAAM,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC;IAC7B,IAAI,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC;QAClB,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,EAAE,CAAC,CAAC;IAClD,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;;;;;GAQG;AACH,SAAgB,aAAa,CAAC,KAAa,EAAE,SAAiB;IAC5D,OAAO,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;AAClC,CAAC;AAED;;;;;;;;;;GAUG;AACH,SAAgB,MAAM,CAAC,CAAS;IAC9B,MAAM,MAAM,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC;IAC7B,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,MAAM,KAAK,CAAC,CAAC;AACxC,CAAC","sourcesContent":["/**\n * Price utilities for the Lightcone SDK.\n *\n * This module provides helper functions for working with decimal string prices.\n * The SDK uses String types for price/size/balance fields to preserve\n * the exact decimal representation from the server, as different tokens\n * have different decimal places (USDC=6, SOL=9, BTC=8, etc.).\n */\n\n/**\n * Parse a decimal string to number for calculations.\n *\n * @example\n * ```typescript\n * parseDecimal(\"0.500000\"); // returns 0.5\n * parseDecimal(\"1.000000\"); // returns 1.0\n * ```\n */\nexport function parseDecimal(s: string): number {\n const result = parseFloat(s);\n if (isNaN(result)) {\n throw new Error(`Invalid decimal string: ${s}`);\n }\n return result;\n}\n\n/**\n * Format a number as a decimal string with specified precision.\n *\n * @example\n * ```typescript\n * formatDecimal(0.5, 6); // returns \"0.500000\"\n * formatDecimal(1.0, 6); // returns \"1.000000\"\n * ```\n */\nexport function formatDecimal(value: number, precision: number): string {\n return value.toFixed(precision);\n}\n\n/**\n * Check if a decimal string represents zero.\n *\n * @example\n * ```typescript\n * isZero(\"0\"); // true\n * isZero(\"0.0\"); // true\n * isZero(\"0.000000\"); // true\n * isZero(\"0.001\"); // false\n * ```\n */\nexport function isZero(s: string): boolean {\n const parsed = parseFloat(s);\n return !isNaN(parsed) && parsed === 0;\n}\n\n"]}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { OrderSide } from "../program/types";
|
|
2
|
+
/**
|
|
3
|
+
* Orderbook decimal configuration
|
|
4
|
+
*/
|
|
5
|
+
export interface OrderbookDecimals {
|
|
6
|
+
orderbookId: string;
|
|
7
|
+
baseDecimals: number;
|
|
8
|
+
quoteDecimals: number;
|
|
9
|
+
priceDecimals: number;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Scaled amounts result
|
|
13
|
+
*/
|
|
14
|
+
export interface ScaledAmounts {
|
|
15
|
+
makerAmount: bigint;
|
|
16
|
+
takerAmount: bigint;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Error thrown during price/size scaling
|
|
20
|
+
*/
|
|
21
|
+
export declare class ScalingError extends Error {
|
|
22
|
+
constructor(message: string);
|
|
23
|
+
static zeroPriceOrSize(): ScalingError;
|
|
24
|
+
static negativeValue(field: string): ScalingError;
|
|
25
|
+
static overflow(field: string): ScalingError;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Scale price and size to maker_amount and taker_amount using exact decimal arithmetic.
|
|
29
|
+
*
|
|
30
|
+
* For BID orders (buying base with quote):
|
|
31
|
+
* - maker_amount = price * size * 10^quoteDecimals (what maker gives in quote tokens)
|
|
32
|
+
* - taker_amount = size * 10^baseDecimals (what maker receives in base tokens)
|
|
33
|
+
*
|
|
34
|
+
* For ASK orders (selling base for quote):
|
|
35
|
+
* - maker_amount = size * 10^baseDecimals (what maker gives in base tokens)
|
|
36
|
+
* - taker_amount = price * size * 10^quoteDecimals (what maker receives in quote tokens)
|
|
37
|
+
*
|
|
38
|
+
* @param priceStr - Price as a decimal string (e.g., "0.75")
|
|
39
|
+
* @param sizeStr - Size as a decimal string (e.g., "100")
|
|
40
|
+
* @param side - Order side (BID or ASK)
|
|
41
|
+
* @param decimals - Orderbook decimal configuration
|
|
42
|
+
* @returns Scaled maker_amount and taker_amount as bigints
|
|
43
|
+
*/
|
|
44
|
+
export declare function scalePriceSize(priceStr: string, sizeStr: string, side: OrderSide, decimals: OrderbookDecimals): ScaledAmounts;
|
|
45
|
+
//# sourceMappingURL=scaling.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scaling.d.ts","sourceRoot":"","sources":["../../src/shared/scaling.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAE7C;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;IACrB,aAAa,EAAE,MAAM,CAAC;IACtB,aAAa,EAAE,MAAM,CAAC;CACvB;AAED;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,MAAM,CAAC;CACrB;AAED;;GAEG;AACH,qBAAa,YAAa,SAAQ,KAAK;gBACzB,OAAO,EAAE,MAAM;IAK3B,MAAM,CAAC,eAAe,IAAI,YAAY;IAItC,MAAM,CAAC,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,YAAY;IAIjD,MAAM,CAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,YAAY;CAG7C;AAID;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,cAAc,CAC5B,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,SAAS,EACf,QAAQ,EAAE,iBAAiB,GAC1B,aAAa,CAyCf"}
|