Skip to content

Commit

Permalink
Merge pull request #115 from Polymarket/feat/spread-calculation
Browse files Browse the repository at this point in the history
getting the orderbook spread
  • Loading branch information
poly-rodr authored May 27, 2024
2 parents 86088ef + 11a4a82 commit 1263177
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 1 deletion.
30 changes: 30 additions & 0 deletions examples/getSpread.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { ethers } from "ethers";
import { config as dotenvConfig } from "dotenv";
import { resolve } from "path";
import { ApiKeyCreds, Chain, ClobClient } from "../src";

dotenvConfig({ path: resolve(__dirname, "../.env") });

async function main() {
const wallet = new ethers.Wallet(`${process.env.PK}`);
const chainId = parseInt(`${process.env.CHAIN_ID || Chain.AMOY}`) as Chain;
console.log(`Address: ${await wallet.getAddress()}, chainId: ${chainId}`);

const host = process.env.CLOB_API_URL || "http://localhost:8080";
const creds: ApiKeyCreds = {
key: `${process.env.CLOB_API_KEY}`,
secret: `${process.env.CLOB_SECRET}`,
passphrase: `${process.env.CLOB_PASS_PHRASE}`,
};
const clobClient = new ClobClient(host, chainId, wallet, creds);

const YES_TOKEN_ID =
"71321045679252212594626385532706912750332728571942532289631379312455583992563";
const NO_TOKEN_ID =
"52114319501245915516055106046884209969926127482827954674443846427813813222426";

clobClient.getSpread(YES_TOKEN_ID).then((spread: any) => console.log("YES", spread));
clobClient.getSpread(NO_TOKEN_ID).then((spread: any) => console.log("NO", spread));
}

main();
32 changes: 32 additions & 0 deletions examples/getSpreads.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { ethers } from "ethers";
import { config as dotenvConfig } from "dotenv";
import { resolve } from "path";
import { ApiKeyCreds, BookParams, Chain, ClobClient } from "../src";

dotenvConfig({ path: resolve(__dirname, "../.env") });

async function main() {
const wallet = new ethers.Wallet(`${process.env.PK}`);
const chainId = parseInt(`${process.env.CHAIN_ID || Chain.AMOY}`) as Chain;
console.log(`Address: ${await wallet.getAddress()}, chainId: ${chainId}`);

const host = process.env.CLOB_API_URL || "http://localhost:8080";
const creds: ApiKeyCreds = {
key: `${process.env.CLOB_API_KEY}`,
secret: `${process.env.CLOB_SECRET}`,
passphrase: `${process.env.CLOB_PASS_PHRASE}`,
};
const clobClient = new ClobClient(host, chainId, wallet, creds);

const YES = "71321045679252212594626385532706912750332728571942532289631379312455583992563";
const NO = "52114319501245915516055106046884209969926127482827954674443846427813813222426";

const spreads = await clobClient.getSpreads([
{ token_id: YES },
{ token_id: NO },
] as BookParams[]);

console.log(spreads);
}

main();
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.6.0",
"version": "4.7.0",
"contributors": [
{
"name": "Jonathan Amenechi",
Expand Down
14 changes: 14 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ import {
GET_REWARDS_MARKETS,
GET_REWARDS_EARNINGS_PERCENTAGES,
GET_TOTAL_EARNINGS_FOR_USER_FOR_DAY,
GET_SPREAD,
GET_SPREADS,
} from "./endpoints";
import { OrderBuilder } from "./order-builder/builder";
import { END_CURSOR, INITIAL_CURSOR } from "./constants";
Expand Down Expand Up @@ -245,6 +247,18 @@ export class ClobClient {
});
}

public async getSpread(tokenID: string): Promise<any> {
return this.get(`${this.host}${GET_SPREAD}`, {
params: { token_id: tokenID },
});
}

public async getSpreads(params: BookParams[]): Promise<any> {
return this.post(`${this.host}${GET_SPREADS}`, {
data: params,
});
}

public async getLastTradePrice(tokenID: string): Promise<any> {
return this.get(`${this.host}${GET_LAST_TRADE_PRICE}`, {
params: { token_id: tokenID },
Expand Down
2 changes: 2 additions & 0 deletions src/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export const GET_MIDPOINT = "/midpoint";
export const GET_MIDPOINTS = "/midpoints";
export const GET_PRICE = "/price";
export const GET_PRICES = "/prices";
export const GET_SPREAD = "/spread";
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";
Expand Down

0 comments on commit 1263177

Please sign in to comment.