Skip to content

Commit

Permalink
Neg Risk Flag Resolution (#119)
Browse files Browse the repository at this point in the history
* neg risk flag resolution

* update createMarketBuyOrder

* update version
  • Loading branch information
mshrieve authored Jul 19, 2024
1 parent c267bd9 commit 45aa0d0
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
build:
@echo "Building ts code..."
rm -rf dist
tsc --module commonjs
yarn tsc --module commonjs

.PHONY: test
test:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@polymarket/clob-client",
"description": "Typescript client for Polymarket's CLOB",
"version": "4.8.0",
"version": "4.8.1",
"contributors": [
{
"name": "Jonathan Amenechi",
Expand Down
26 changes: 23 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
MarketReward,
UserRewardsEarning,
TotalUserEarning,
NegRisk,
} from "./types";
import { createL1Headers, createL2Headers } from "./headers";
import {
Expand Down Expand Up @@ -83,6 +84,7 @@ import {
GET_BALANCE_ALLOWANCE,
IS_ORDER_SCORING,
GET_TICK_SIZE,
GET_NEG_RISK,
ARE_ORDERS_SCORING,
GET_SIMPLIFIED_MARKETS,
GET_SAMPLING_SIMPLIFIED_MARKETS,
Expand Down Expand Up @@ -121,6 +123,8 @@ export class ClobClient {

readonly tickSizes: TickSizes;

readonly negRisk: NegRisk;

readonly geoBlockToken?: string;

constructor(
Expand Down Expand Up @@ -148,6 +152,7 @@ export class ClobClient {
funderAddress,
);
this.tickSizes = {};
this.negRisk = {};
this.geoBlockToken = geoBlockToken;
}

Expand Down Expand Up @@ -215,6 +220,19 @@ export class ClobClient {
return this.tickSizes[tokenID];
}

public async getNegRisk(tokenID: string): Promise<boolean> {
if (tokenID in this.negRisk) {
return this.negRisk[tokenID];
}

const result = await this.get(`${this.host}${GET_NEG_RISK}`, {
params: { token_id: tokenID },
});
this.negRisk[tokenID] = result.neg_risk as boolean;

return this.negRisk[tokenID];
}

/**
* Calculates the hash for the given orderbook
* @param orderbook
Expand Down Expand Up @@ -524,7 +542,6 @@ export class ClobClient {
const { tokenID } = userOrder;

const tickSize = await this._resolveTickSize(tokenID, options?.tickSize);
const negRisk = options?.negRisk ?? false;

if (!priceValid(userOrder.price, tickSize)) {
throw new Error(
Expand All @@ -534,6 +551,8 @@ export class ClobClient {
);
}

const negRisk = options?.negRisk ?? await this.getNegRisk(tokenID);

return this.orderBuilder.buildOrder(userOrder, {
tickSize,
negRisk,
Expand All @@ -542,14 +561,13 @@ export class ClobClient {

public async createMarketBuyOrder(
userMarketOrder: UserMarketOrder,
options?: CreateOrderOptions,
options?: Partial<CreateOrderOptions>,
): Promise<SignedOrder> {
this.canL1Auth();

const { tokenID } = userMarketOrder;

const tickSize = await this._resolveTickSize(tokenID, options?.tickSize);
const negRisk = options?.negRisk ?? false;

if (!userMarketOrder.price) {
userMarketOrder.price = await this.calculateMarketPrice(
Expand All @@ -567,6 +585,8 @@ export class ClobClient {
);
}

const negRisk = options?.negRisk ?? await this.getNegRisk(tokenID);

return this.orderBuilder.buildMarketOrder(userMarketOrder, {
tickSize,
negRisk,
Expand Down
1 change: 1 addition & 0 deletions src/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const GET_SPREADS = "/spreads";
export const GET_LAST_TRADE_PRICE = "/last-trade-price";
export const GET_LAST_TRADES_PRICES = "/last-trades-prices";
export const GET_TICK_SIZE = "/tick-size";
export const GET_NEG_RISK = "/neg-risk";

// Order endpoints
export const POST_ORDER = "/order";
Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,10 @@ export interface TickSizes {
[tokenId: string]: TickSize;
}

export interface NegRisk {
[tokenId: string]: boolean;
}

export interface PaginationPayload {
readonly limit: number;
readonly count: number;
Expand Down

0 comments on commit 45aa0d0

Please sign in to comment.