|
| 1 | +import { EvmAddressSchema, type TickSizeValue } from '@polymarket/bindings'; |
| 2 | +import { |
| 3 | + OrderSide, |
| 4 | + OrderSideSchema, |
| 5 | + OrderType, |
| 6 | + type SignatureType, |
| 7 | +} from '@polymarket/bindings/clob'; |
| 8 | +import type { EvmAddress } from '@polymarket/types'; |
| 9 | +import { z } from 'zod'; |
| 10 | +import type { SecureClient } from '../../clients'; |
| 11 | +import { UserInputError } from '../../errors'; |
| 12 | +import { fetchNegRisk, fetchTickSize } from '../clob'; |
| 13 | +import { |
| 14 | + resolveExchangeAddress, |
| 15 | + resolveFeeRateBps, |
| 16 | + resolveFunderAddress, |
| 17 | + resolveRoundingConfig, |
| 18 | +} from './context'; |
| 19 | +import { |
| 20 | + decimalPlaces, |
| 21 | + parseAmount, |
| 22 | + roundDown, |
| 23 | + roundNormal, |
| 24 | + roundUp, |
| 25 | +} from './math'; |
| 26 | +import type { OrderDraft, PrepareLimitOrderRequest } from './types'; |
| 27 | + |
| 28 | +export const PrepareLimitOrderParamsSchema = z |
| 29 | + .object({ |
| 30 | + tokenId: z.string(), |
| 31 | + price: z.number().positive(), |
| 32 | + size: z.number().positive(), |
| 33 | + side: OrderSideSchema, |
| 34 | + taker: EvmAddressSchema.optional(), |
| 35 | + expiration: z.number().int().nonnegative().optional(), |
| 36 | + orderType: z |
| 37 | + .union([z.literal(OrderType.GTC), z.literal(OrderType.GTD)]) |
| 38 | + .default(OrderType.GTC), |
| 39 | + }) |
| 40 | + .superRefine((params, context) => { |
| 41 | + if (params.orderType === OrderType.GTD) { |
| 42 | + if (params.expiration === undefined) { |
| 43 | + context.addIssue({ |
| 44 | + code: 'custom', |
| 45 | + message: 'GTD orders require an expiration timestamp.', |
| 46 | + path: ['expiration'], |
| 47 | + }); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + const minimumExpiration = Math.floor(Date.now() / 1000) + 60; |
| 52 | + |
| 53 | + if (params.expiration <= minimumExpiration) { |
| 54 | + context.addIssue({ |
| 55 | + code: 'custom', |
| 56 | + message: 'GTD expiration must be at least 60 seconds in the future.', |
| 57 | + path: ['expiration'], |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + if (params.expiration !== undefined) { |
| 65 | + context.addIssue({ |
| 66 | + code: 'custom', |
| 67 | + message: 'Expiration is only supported for GTD orders.', |
| 68 | + path: ['expiration'], |
| 69 | + }); |
| 70 | + } |
| 71 | + }) satisfies z.ZodType<PrepareLimitOrderRequest>; |
| 72 | + |
| 73 | +export type PrepareLimitOrderDraftParams = z.output< |
| 74 | + typeof PrepareLimitOrderParamsSchema |
| 75 | +>; |
| 76 | + |
| 77 | +type ResolveLimitOrderContextParams = { |
| 78 | + price: number; |
| 79 | + tokenId: string; |
| 80 | +}; |
| 81 | + |
| 82 | +export async function prepareLimitOrderDraft( |
| 83 | + client: SecureClient, |
| 84 | + params: PrepareLimitOrderDraftParams, |
| 85 | +): Promise<OrderDraft> { |
| 86 | + const context = await resolveLimitOrderContext(client, { |
| 87 | + price: params.price, |
| 88 | + tokenId: params.tokenId, |
| 89 | + }); |
| 90 | + const amounts = computeLimitOrderAmounts({ |
| 91 | + price: context.price, |
| 92 | + side: params.side, |
| 93 | + size: params.size, |
| 94 | + tickSize: context.tickSize, |
| 95 | + }); |
| 96 | + |
| 97 | + return { |
| 98 | + chainId: client.environment.chainId, |
| 99 | + exchangeAddress: context.exchangeAddress, |
| 100 | + expiration: params.expiration ?? 0, |
| 101 | + feeRateBps: context.feeRateBps, |
| 102 | + funderAddress: context.funderAddress, |
| 103 | + offeredAmount: amounts.offeredAmount, |
| 104 | + orderType: params.orderType, |
| 105 | + side: params.side, |
| 106 | + signatureType: context.signatureType, |
| 107 | + signer: context.signerAddress, |
| 108 | + allowedTaker: params.taker, |
| 109 | + requestedAmount: amounts.requestedAmount, |
| 110 | + tokenId: params.tokenId, |
| 111 | + }; |
| 112 | +} |
| 113 | + |
| 114 | +type LimitOrderContext = { |
| 115 | + exchangeAddress: EvmAddress; |
| 116 | + feeRateBps: number; |
| 117 | + funderAddress: EvmAddress; |
| 118 | + negRisk: boolean; |
| 119 | + price: number; |
| 120 | + signatureType: SignatureType; |
| 121 | + signerAddress: EvmAddress; |
| 122 | + tickSize: TickSizeValue; |
| 123 | +}; |
| 124 | + |
| 125 | +async function resolveLimitOrderContext( |
| 126 | + client: SecureClient, |
| 127 | + params: ResolveLimitOrderContextParams, |
| 128 | +): Promise<LimitOrderContext> { |
| 129 | + const signerAddress = client.address; |
| 130 | + const signatureType = client.signatureType; |
| 131 | + const funderAddress = await resolveFunderAddress(client); |
| 132 | + const tickSize = await fetchTickSize(client, { |
| 133 | + tokenId: params.tokenId, |
| 134 | + }); |
| 135 | + const feeRateBps = await resolveFeeRateBps(client, params.tokenId); |
| 136 | + const negRisk = await fetchNegRisk(client, { |
| 137 | + tokenId: params.tokenId, |
| 138 | + }); |
| 139 | + |
| 140 | + return { |
| 141 | + exchangeAddress: resolveExchangeAddress(client, negRisk), |
| 142 | + feeRateBps, |
| 143 | + funderAddress, |
| 144 | + negRisk, |
| 145 | + price: resolvePrice(params.price, tickSize), |
| 146 | + signatureType, |
| 147 | + signerAddress, |
| 148 | + tickSize, |
| 149 | + }; |
| 150 | +} |
| 151 | + |
| 152 | +function computeLimitOrderAmounts(params: { |
| 153 | + price: number; |
| 154 | + side: OrderSide; |
| 155 | + size: number; |
| 156 | + tickSize: TickSizeValue; |
| 157 | +}): { |
| 158 | + offeredAmount: bigint; |
| 159 | + requestedAmount: bigint; |
| 160 | +} { |
| 161 | + const roundConfig = resolveRoundingConfig(params.tickSize); |
| 162 | + const rawPrice = roundNormal(params.price, roundConfig.price); |
| 163 | + |
| 164 | + if (params.side === OrderSide.BUY) { |
| 165 | + const rawTakerAmount = roundDown(params.size, roundConfig.size); |
| 166 | + let rawMakerAmount = rawTakerAmount * rawPrice; |
| 167 | + |
| 168 | + if (decimalPlaces(rawMakerAmount) > roundConfig.amount) { |
| 169 | + rawMakerAmount = roundUp(rawMakerAmount, roundConfig.amount + 4); |
| 170 | + |
| 171 | + if (decimalPlaces(rawMakerAmount) > roundConfig.amount) { |
| 172 | + rawMakerAmount = roundDown(rawMakerAmount, roundConfig.amount); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + return { |
| 177 | + offeredAmount: parseAmount(rawMakerAmount), |
| 178 | + requestedAmount: parseAmount(rawTakerAmount), |
| 179 | + }; |
| 180 | + } |
| 181 | + |
| 182 | + const rawMakerAmount = roundDown(params.size, roundConfig.size); |
| 183 | + let rawTakerAmount = rawMakerAmount * rawPrice; |
| 184 | + |
| 185 | + if (decimalPlaces(rawTakerAmount) > roundConfig.amount) { |
| 186 | + rawTakerAmount = roundUp(rawTakerAmount, roundConfig.amount + 4); |
| 187 | + |
| 188 | + if (decimalPlaces(rawTakerAmount) > roundConfig.amount) { |
| 189 | + rawTakerAmount = roundDown(rawTakerAmount, roundConfig.amount); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + return { |
| 194 | + offeredAmount: parseAmount(rawMakerAmount), |
| 195 | + requestedAmount: parseAmount(rawTakerAmount), |
| 196 | + }; |
| 197 | +} |
| 198 | + |
| 199 | +function resolvePrice(price: number, tickSize: TickSizeValue): number { |
| 200 | + const roundConfig = resolveRoundingConfig(tickSize); |
| 201 | + |
| 202 | + if (price < tickSize || price > 1 - tickSize) { |
| 203 | + throw new UserInputError( |
| 204 | + `Price must be between ${tickSize} and ${1 - tickSize} for tick size ${tickSize}.`, |
| 205 | + ); |
| 206 | + } |
| 207 | + |
| 208 | + if (decimalPlaces(price) > roundConfig.price) { |
| 209 | + throw new UserInputError( |
| 210 | + `Price must conform to tick size ${tickSize} with at most ${roundConfig.price} decimal places.`, |
| 211 | + ); |
| 212 | + } |
| 213 | + |
| 214 | + return roundNormal(price, roundConfig.price); |
| 215 | +} |
0 commit comments