Skip to content

Commit

Permalink
Merge pull request #109 from Polymarket/fix/rewards
Browse files Browse the repository at this point in the history
Including new endpoints and types of the rewards API
  • Loading branch information
poly-rodr authored Apr 11, 2024
2 parents c5913ad + c846ff2 commit edd55ef
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 17 deletions.
16 changes: 14 additions & 2 deletions examples/rewards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,28 @@ async function main() {

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

main();
77 changes: 73 additions & 4 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ import {
BookParams,
UserEarning,
RewardsPercentages,
CurrentReward,
MarketReward,
UserRewardsEarning,
TotalUserEarning,
} from "./types";
import { createL1Headers, createL2Headers } from "./headers";
import {
Expand Down Expand Up @@ -97,6 +98,8 @@ import {
GET_LIQUIDITY_REWARD_PERCENTAGES,
GET_REWARDS_MARKETS_CURRENT,
GET_REWARDS_MARKETS,
GET_REWARDS_EARNINGS_PERCENTAGES,
GET_TOTAL_EARNINGS_FOR_USER_FOR_DAY,
} from "./endpoints";
import { OrderBuilder } from "./order-builder/builder";
import { END_CURSOR, INITIAL_CURSOR } from "./constants";
Expand Down Expand Up @@ -703,7 +706,73 @@ export class ClobClient {
return results;
}

public async getLRewardPercentages(): Promise<RewardsPercentages> {
public async getTotalEarningsForUserForDay(date: string): Promise<TotalUserEarning[]> {
this.canL2Auth();

const endpoint = GET_TOTAL_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,
);

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

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

public async getUserEarningsAndMarketsConfig(
date: string,
order_by = "",
position = "",
): Promise<UserRewardsEarning[]> {
this.canL2Auth();

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

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

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

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

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

const endpoint = GET_LIQUIDITY_REWARD_PERCENTAGES;
Expand All @@ -725,8 +794,8 @@ export class ClobClient {
return this.get(`${this.host}${endpoint}`, { headers, params: _params });
}

public async getCurrentRewards(): Promise<CurrentReward[]> {
let results: CurrentReward[] = [];
public async getCurrentRewards(): 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_CURRENT}`, {
Expand Down
2 changes: 2 additions & 0 deletions src/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export const GET_MARKET_TRADES_EVENTS = "/live-activity/events/";

// Rewards
export const GET_EARNINGS_FOR_USER_FOR_DAY = "/rewards/user";
export const GET_TOTAL_EARNINGS_FOR_USER_FOR_DAY = "/rewards/user/total";
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/";
export const GET_REWARDS_EARNINGS_PERCENTAGES = "/rewards/user/markets";
66 changes: 55 additions & 11 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,15 @@ export interface BookParams {

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

export interface TotalUserEarning {
date: string;
asset_address: string;
maker_address: string;
earnings: number;
Expand All @@ -404,22 +412,58 @@ export interface RewardsPercentages {
[market: string]: number;
}

export interface CurrentReward {
market: string;
export interface Token {
token_id: string;
outcome: string;
price: number;
}

export interface RewardsConfig {
asset_address: string;
start_date: string;
end_date: string;
current_rewards_per_day: number;
total_reward_amount: number;
rate_per_day: number;
total_rewards: number;
remaining_reward_amount: number;
}

export interface MarketReward {
market: string;
condition_id: string;
market_id: number;
question: string;
market_slug: string;
event_slug: string;
active: boolean;
archived: boolean;
closed: boolean;
image: string;
rewards_max_spread: number;
rewards_min_size: number;
tokens: Token[];
rewards_config: RewardsConfig[];
}

export interface Earning {
asset_address: string;
start_date: string;
end_date: string;
rate_per_day: number;
total_rewards: number;
total_days: number;
earnings: number;
asset_rate: number;
}

export interface UserRewardsEarning {
condition_id: string;
market_id: number;
question: string;
market_slug: string;
event_slug: string;
active: boolean;
archived: boolean;
closed: boolean;
image: string;
rewards_max_spread: number;
rewards_min_size: number;
tokens: Token[];
rewards_config: RewardsConfig[];
maker_address: string;
earning_percentage: number;
earnings: Earning[];
}

0 comments on commit edd55ef

Please sign in to comment.