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

Money market reward pools #175

Merged
merged 8 commits into from
Nov 21, 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
5 changes: 5 additions & 0 deletions .changeset/brown-laws-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@shadeprotocol/shadejs": patch
---

Money Market Reward Pools
112 changes: 106 additions & 6 deletions src/contracts/services/moneyMarket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import {
map,
lastValueFrom,
} from 'rxjs';

import {
BatchQueryParsedResponse,
BatchQueryParams,
} from '~/types/contracts/batchQuery/model';
import { sendSecretClientContractQuery$ } from '~/client/services/clientServices';
import { ConfigResponse, GetCollateralResponse, GetMarketsResponse } from '~/types/contracts/moneyMarket/response';
import {
Expand All @@ -13,11 +18,9 @@ import {
BatchMoneyMarketGetMarkets,
ContractAndPagination,
Pagination, ParsedConfigResponse, ParsedGetCollateralResponse, ParsedGetMarketsResponse,
ParsedRewardPoolsResponse,
} from '~/types/contracts/moneyMarket/model';
import { Contract } from '~/types/contracts/shared/index';
import {
BatchQueryParams, BatchQueryParsedResponse,
} from '~/types/contracts/batchQuery/model';
import { MinBlockHeightValidationOptions } from '~/types';
import { batchQuery$ } from './batchQuery';
import { msgQueryMoneyMarketCollaterals, msgQueryMoneyMarketConfig, msgQueryMoneyMarketMarkets } from '../definitions/moneyMarket';
Expand Down Expand Up @@ -123,7 +126,8 @@ const parseMoneyMarketGetCollateral = (
collateralAmount: cur.amount,
decimals: cur.decimals,
maxInitialLtv: cur.max_initial_ltv,
liquidationThreshold: cur.liquidation_threshold,
publicLiquidationThreshold: cur.public_liquidation_threshold,
privateLiquidationThreshold: cur.private_liquidation_threshold,
liquidationDiscount: cur.liquidation_discount,
oracleKey: cur.oracle_key,
depositEnabled: cur.status.deposit_enabled,
Expand Down Expand Up @@ -598,7 +602,7 @@ function queryMoneyMarketPublicLogs$({
pagination,
}: {
contractAddress: string,
codeHash: string,
codeHash?: string,
lcdEndpoint?: string,
chainId?: string,
pagination?: Pagination,
Expand Down Expand Up @@ -630,7 +634,7 @@ async function queryMoneyMarketPublicLogs({
page,
}: {
contractAddress: string,
codeHash: string,
codeHash?: string,
lcdEndpoint?: string,
chainId?: string,
pageSize?: number,
Expand Down Expand Up @@ -728,6 +732,100 @@ async function batchQueryMoneyMarketPublicLogs({
}));
}

const parseBatchQueryMoneyMarketRewardPools = (
responses: BatchQueryParsedResponse,
): ParsedRewardPoolsResponse[] => (
responses.map((response: any) => ({
debtMarket: response.id,
blockHeight: response.blockHeight,
rewardPools: response.response.map((pool: any) => ({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any?

rewardPoolId: pool.id,
amount: pool.amount,
token: pool.token,
start: pool.start,
end: pool.end,
rate: pool.rate,
})),
}))
);

function batchQueryMoneyMarketRewardPools$({
queryRouterContractAddress,
queryRouterCodeHash,
lcdEndpoint,
chainId,
moneyMarket,
debtMarkets,
batchSize,
minBlockHeightValidationOptions,
blockHeight,
}: {
queryRouterContractAddress: string,
queryRouterCodeHash?: string,
lcdEndpoint?: string,
chainId?: string,
moneyMarket: Contract,
debtMarkets: string[],
batchSize?: number,
minBlockHeightValidationOptions?: MinBlockHeightValidationOptions,
blockHeight?: number,
}) {
const queries = debtMarkets.map((debtMarket) => ({
id: debtMarket,
contract: {
address: moneyMarket.address,
codeHash: moneyMarket.codeHash,
},
queryMsg: {
reward_pools: {
market: debtMarket,
},
},
}));

return batchQuery$({
contractAddress: queryRouterContractAddress,
codeHash: queryRouterCodeHash,
lcdEndpoint,
chainId,
queries,
batchSize,
minBlockHeightValidationOptions,
blockHeight,
}).pipe(
map((response) => parseBatchQueryMoneyMarketRewardPools(response)),
first(),
);
}

async function batchQueryMoneyMarketRewardPools({
queryRouterContractAddress,
queryRouterCodeHash,
lcdEndpoint,
chainId,
moneyMarket,
debtMarkets,
minBlockHeightValidationOptions,
}: {
queryRouterContractAddress: string,
queryRouterCodeHash?: string,
lcdEndpoint?: string,
chainId?: string,
moneyMarket: Contract,
debtMarkets: string[],
minBlockHeightValidationOptions?: MinBlockHeightValidationOptions,
}) {
return lastValueFrom(batchQueryMoneyMarketRewardPools$({
queryRouterContractAddress,
queryRouterCodeHash,
lcdEndpoint,
chainId,
moneyMarket,
debtMarkets,
minBlockHeightValidationOptions,
}));
}

export {
queryMoneyMarketConfig,
queryMoneyMarketGetMarkets,
Expand All @@ -742,4 +840,6 @@ export {
queryMoneyMarketPublicLogs,
batchQueryMoneyMarketPublicLogs$,
batchQueryMoneyMarketPublicLogs,
batchQueryMoneyMarketRewardPools$,
batchQueryMoneyMarketRewardPools,
};
20 changes: 19 additions & 1 deletion src/types/contracts/moneyMarket/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ type ParsedCollateralReponse = {
collateralAmount: string,
decimals: number,
maxInitialLtv: string,
liquidationThreshold: string,
publicLiquidationThreshold: string,
privateLiquidationThreshold: string,
liquidationDiscount: string,
oracleKey: string,
depositEnabled: boolean,
Expand Down Expand Up @@ -164,6 +165,21 @@ type PaginatedPublicLogs = {
data: PublicLog[],
}

type RewardPool = {
rewardPoolId: string,
amount: string,
token: string,
start: string,
end: string,
rate: string,
}

type ParsedRewardPoolsResponse = {
debtMarket: string,
blockHeight: string,
rewardPools: RewardPool[],
}

export type {
Pagination,
ContractAndPagination,
Expand All @@ -179,4 +195,6 @@ export type {
BatchMoneyMarketGetCollaterals,
PublicLog,
PaginatedPublicLogs,
RewardPool,
ParsedRewardPoolsResponse,
};
3 changes: 2 additions & 1 deletion src/types/contracts/moneyMarket/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ type CollateralReponse = {
amount: string,
decimals: number,
max_initial_ltv: string,
liquidation_threshold: string,
public_liquidation_threshold: string,
private_liquidation_threshold: string,
liquidation_discount: string,
oracle_key: string,
status: {
Expand Down
Loading