Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reward service endpoints #108

Merged
merged 5 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions examples/rewards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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.MUMBAI}`) 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);

console.log(
"today earnings",
await clobClient.getEarningsForUserForDay("2024-04-03" /* UTC TIME */),
);
console.log("rewards percentages", await clobClient.getLRewardPercentages());
console.log("current rewards", await clobClient.getCurrentRewards());
console.log(
"rewards for market",
await clobClient.getRawRewardsForMarket(
"0xbd31dc8a20211944f6b70f31557f1001557b59905b7738480ca09bd4532f84af",
),
);
}

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.3.0",
"version": "4.4.0",
"contributors": [
{
"name": "Jonathan Amenechi",
Expand Down
102 changes: 98 additions & 4 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ import {
MarketTradeEvent,
DropNotificationParams,
BookParams,
UserEarning,
RewardsPercentages,
CurrentReward,
MarketReward,
} from "./types";
import { createL1Headers, createL2Headers } from "./headers";
import {
Expand Down Expand Up @@ -89,8 +93,13 @@ import {
GET_MIDPOINTS,
GET_PRICES,
GET_LAST_TRADES_PRICES,
GET_EARNINGS_FOR_USER_FOR_DAY,
GET_LIQUIDITY_REWARD_PERCENTAGES,
GET_REWARDS_MARKETS_CURRENT,
GET_REWARDS_MARKETS,
} from "./endpoints";
import { OrderBuilder } from "./order-builder/builder";
import { END_CURSOR, INITIAL_CURSOR } from "./constants";

export class ClobClient {
readonly host: string;
Expand Down Expand Up @@ -146,25 +155,27 @@ export class ClobClient {
return this.get(`${this.host}${TIME}`);
}

public async getSamplingSimplifiedMarkets(next_cursor = "MA=="): Promise<PaginationPayload> {
public async getSamplingSimplifiedMarkets(
next_cursor = INITIAL_CURSOR,
): Promise<PaginationPayload> {
return this.get(`${this.host}${GET_SAMPLING_SIMPLIFIED_MARKETS}`, {
params: { next_cursor },
});
}

public async getSamplingMarkets(next_cursor = "MA=="): Promise<PaginationPayload> {
public async getSamplingMarkets(next_cursor = INITIAL_CURSOR): Promise<PaginationPayload> {
return this.get(`${this.host}${GET_SAMPLING_MARKETS}`, {
params: { next_cursor },
});
}

public async getSimplifiedMarkets(next_cursor = "MA=="): Promise<PaginationPayload> {
public async getSimplifiedMarkets(next_cursor = INITIAL_CURSOR): Promise<PaginationPayload> {
return this.get(`${this.host}${GET_SIMPLIFIED_MARKETS}`, {
params: { next_cursor },
});
}

public async getMarkets(next_cursor = "MA=="): Promise<PaginationPayload> {
public async getMarkets(next_cursor = INITIAL_CURSOR): Promise<PaginationPayload> {
return this.get(`${this.host}${GET_MARKETS}`, {
params: { next_cursor },
});
Expand Down Expand Up @@ -657,6 +668,89 @@ export class ClobClient {
});
}

// Rewards
public async getEarningsForUserForDay(date: string): Promise<UserEarning[]> {
this.canL2Auth();

const endpoint = GET_EARNINGS_FOR_USER_FOR_DAY;
const headerArgs = {
method: GET,
requestPath: endpoint,
};

const headers = await createL2Headers(
this.signer as Wallet | JsonRpcSigner,
this.creds as ApiKeyCreds,
headerArgs,
);

let results: UserEarning[] = [];
let next_cursor = INITIAL_CURSOR;
while (next_cursor != END_CURSOR) {
const params = {
date,
signature_type: this.orderBuilder.signatureType,
next_cursor,
};

const response = await this.get(`${this.host}${endpoint}`, {
headers,
params,
});
next_cursor = response.next_cursor;
results = [...results, ...response.data];
}
return results;
}

public async getLRewardPercentages(): Promise<RewardsPercentages> {
this.canL2Auth();

const endpoint = GET_LIQUIDITY_REWARD_PERCENTAGES;
const headerArgs = {
method: GET,
requestPath: endpoint,
};

const headers = await createL2Headers(
this.signer as Wallet | JsonRpcSigner,
this.creds as ApiKeyCreds,
headerArgs,
);

const _params = {
signature_type: this.orderBuilder.signatureType,
};

return this.get(`${this.host}${endpoint}`, { headers, params: _params });
}

public async getCurrentRewards(): Promise<CurrentReward[]> {
let results: CurrentReward[] = [];
let next_cursor = INITIAL_CURSOR;
while (next_cursor != END_CURSOR) {
const response = await this.get(`${this.host}${GET_REWARDS_MARKETS_CURRENT}`, {
params: { next_cursor },
});
next_cursor = response.next_cursor;
results = [...results, ...response.data];
}
return results;
}

public async getRawRewardsForMarket(conditionId: string): Promise<MarketReward[]> {
let results: MarketReward[] = [];
let next_cursor = INITIAL_CURSOR;
while (next_cursor != END_CURSOR) {
const response = await this.get(`${this.host}${GET_REWARDS_MARKETS}${conditionId}`, {
params: { next_cursor },
});
next_cursor = response.next_cursor;
results = [...results, ...response.data];
}
return results;
}

public async getMarketTradesEvents(conditionID: string): Promise<MarketTradeEvent[]> {
return this.get(`${this.host}${GET_MARKET_TRADES_EVENTS}${conditionID}`);
}
Expand Down
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ export const CREDS_CREATION_WARNING = `🚨🚨🚨
Your credentials CANNOT be recovered after they've been created.
Be sure to store them safely!
🚨🚨🚨`;

export const INITIAL_CURSOR = "MA==";
export const END_CURSOR = "LTE=";
6 changes: 6 additions & 0 deletions src/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,9 @@ export const GET_BALANCE_ALLOWANCE = "/balance-allowance";

// Live activity
export const GET_MARKET_TRADES_EVENTS = "/live-activity/events/";

// Rewards
export const GET_EARNINGS_FOR_USER_FOR_DAY = "/rewards/user";
export const GET_LIQUIDITY_REWARD_PERCENTAGES = "/rewards/user/percentages";
export const GET_REWARDS_MARKETS_CURRENT = "/rewards/markets/current";
export const GET_REWARDS_MARKETS = "/rewards/markets/";
33 changes: 33 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,3 +390,36 @@ export interface BookParams {
token_id: string;
side: Side;
}

export interface UserEarning {
date: string;
market: string;
asset_address: string;
maker_address: string;
earnings: number;
asset_rate: number;
}

export interface RewardsPercentages {
[market: string]: number;
}

export interface CurrentReward {
market: string;
asset_address: string;
start_date: string;
end_date: string;
current_rewards_per_day: number;
total_reward_amount: number;
remaining_reward_amount: number;
}

export interface MarketReward {
market: string;
asset_address: string;
start_date: string;
end_date: string;
rate_per_day: number;
total_rewards: number;
total_days: number;
}
Loading