Skip to content

Commit

Permalink
feat: money market reward pools batch query
Browse files Browse the repository at this point in the history
  • Loading branch information
DrPresident committed Nov 12, 2024
1 parent 330ce08 commit 531500b
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 3 deletions.
104 changes: 101 additions & 3 deletions src/contracts/services/moneyMarket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
BatchMoneyMarketGetMarkets,
ContractAndPagination,
Pagination, ParsedConfigResponse, ParsedGetCollateralResponse, ParsedGetMarketsResponse,
RewardPool, PaginatedRewardPools,
} from '~/types/contracts/moneyMarket/model';
import { Contract } from '~/types/contracts/shared/index';
import {
Expand Down Expand Up @@ -598,15 +599,15 @@ function queryMoneyMarketPublicLogs$({
pagination,
}: {
contractAddress: string,
codeHash: string,
codeHash?: string,
lcdEndpoint?: string,
chainId?: string,
pagination?: Pagination,
}) {
return getActiveQueryClient$(lcdEndpoint, chainId).pipe(
switchMap(({ client }) => sendSecretClientContractQuery$({
queryMsg: {
public_events: { pagination },
public_logs: { pagination },
},
client,
contractAddress,
Expand All @@ -630,7 +631,7 @@ async function queryMoneyMarketPublicLogs({
page,
}: {
contractAddress: string,
codeHash: string,
codeHash?: string,
lcdEndpoint?: string,
chainId?: string,
pageSize?: number,
Expand Down Expand Up @@ -728,6 +729,101 @@ async function batchQueryMoneyMarketPublicLogs({
}));
}

const parseBatchQueryMoneyMarketRewardPools = (response: any): PaginatedRewardPools => ({
page: response.page,
pageSize: response.page_size,
totalPages: response.total_pages,
totalItems: response.total_items,
debtMarket: response.id,
blockHeight: response.blockHeight,
data: response.data ? response.data.map((rewardPool: RewardPool) => ({
id: rewardPool.id,
amount: rewardPool.amount,
token: rewardPool.token,
start: rewardPool.start,
end: rewardPool.end,
rate: rewardPool.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 +838,6 @@ export {
queryMoneyMarketPublicLogs,
batchQueryMoneyMarketPublicLogs$,
batchQueryMoneyMarketPublicLogs,
batchQueryMoneyMarketRewardPools$,
batchQueryMoneyMarketRewardPools,
};
21 changes: 21 additions & 0 deletions src/types/contracts/moneyMarket/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,25 @@ type PaginatedPublicLogs = {
data: PublicLog[],
}

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

type PaginatedRewardPools = {
page: number,
pageSize: number,
totalPages: number,
totalItems: number,
blockHeight: number,
debtMarket: string,
data: RewardPool[],
}

export type {
Pagination,
ContractAndPagination,
Expand All @@ -179,4 +198,6 @@ export type {
BatchMoneyMarketGetCollaterals,
PublicLog,
PaginatedPublicLogs,
RewardPool,
PaginatedRewardPools,
};

0 comments on commit 531500b

Please sign in to comment.