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 #176

Closed
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
fb39e25
feat: get_public_logs
mohammedpatla Oct 22, 2024
bc9388b
feat: build the moneymarketpublic events service
mohammedpatla Oct 23, 2024
d2f97a3
feat: mock typed and test to check outputs and parsed values match
mohammedpatla Oct 25, 2024
99e4dd4
chore: eslint fixes
mohammedpatla Oct 25, 2024
3c3251b
chore: docs
mohammedpatla Oct 31, 2024
5283621
fix: changeset
mohammedpatla Oct 31, 2024
ae9d4b4
fix: address comments
mohammedpatla Oct 31, 2024
8a5254a
chore: eslint
mohammedpatla Oct 31, 2024
bceb82a
fix: change to logs and fix test
mohammedpatla Oct 31, 2024
218c67d
fix: correct changeset
mohammedpatla Oct 31, 2024
d988eb0
feat: bump secretjs version
DrPresident Oct 31, 2024
98f687b
fix: patch secretnetwork upgrade
mohammedpatla Nov 1, 2024
0500c93
feat: create a batch query
mohammedpatla Nov 4, 2024
437872f
chore: eslint
mohammedpatla Nov 4, 2024
5977ce3
fix: change event to logs
mohammedpatla Nov 4, 2024
be9476b
fix: change query to reflect data onchain
mohammedpatla Nov 11, 2024
330ce08
chore: eslint
mohammedpatla Nov 11, 2024
f148579
fix: change public events to get_public_logs
mohammedpatla Nov 11, 2024
531500b
feat: money market reward pools batch query
DrPresident Nov 12, 2024
3cdb253
feat: format
DrPresident Nov 12, 2024
45d2e27
feat: functions working, should add types
DrPresident Nov 19, 2024
57e129c
feat: removing console.logs
DrPresident Nov 21, 2024
f259a3e
feat: types
DrPresident Nov 21, 2024
4762e5d
feat: added type
DrPresident Nov 21, 2024
a9b17c1
Merge branch 'moneymarket' into money-market-reward-pools
DrPresident Nov 21, 2024
65a9948
feat: changeset
DrPresident Nov 21, 2024
6c90022
feat: more types
DrPresident Nov 21, 2024
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
5 changes: 5 additions & 0 deletions .changeset/poor-apricots-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@shadeprotocol/shadejs": patch
---

Money Market Public Logs Querys
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ Docs will be available on a vitepress site. You can run the site locally with th

```
$ yarn docs:dev
```

## Building new version run
```bash
yarn changeset
```
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@shadeprotocol/shadejs",
"description": "The Typescript SDK for Shade Protocol",
"version": "1.0.9",
"version": "1.5.10-beta.1",
"author": "Secure Secrets",
"bugs": {
"url": "https://github.com/securesecrets/shadejs/issues"
Expand Down Expand Up @@ -42,7 +42,7 @@
"dependencies": {
"bignumber.js": "^9.1.2",
"rxjs": "^7.8.1",
"secretjs": "^1.12.1",
"secretjs": "1.15.0-beta.0",
"vite": "^4.5.0",
"whatwg-fetch": "^3.6.19"
},
Expand Down
13 changes: 13 additions & 0 deletions src/contracts/definitions/moneyMarket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,18 @@ function msgMoneyMarketRepay({
}).msg;
}

/**
* Query the Public Event Logs for momeymarket contract
* NOT FOR PRODUCTION USE, CONTRACT IS IN DEVELOPMENT ON TESTNET ONLY
*/
const msgQueryMoneyMarketPublicLogs = (
pagination?: Pagination,
) => ({
get_public_log: {
pagination,
},
});

export {
msgQueryMoneyMarketConfig,
msgQueryMoneyMarketCollaterals,
Expand All @@ -188,4 +200,5 @@ export {
msgMoneyMarketSupply,
msgMoneyMarketWithdrawSupply,
msgMoneyMarketRepay,
msgQueryMoneyMarketPublicLogs,
};
270 changes: 266 additions & 4 deletions src/contracts/services/moneyMarket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import {
map,
lastValueFrom,
} from 'rxjs';

import {
BatchQueryParsedResponse,
BatchQueryParams,
BatchQueryParsedResponseItem,
} 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 +19,10 @@ import {
BatchMoneyMarketGetMarkets,
ContractAndPagination,
Pagination, ParsedConfigResponse, ParsedGetCollateralResponse, ParsedGetMarketsResponse,
ParsedRewardPoolsResponse,
RewardPoolResponse,
} 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 +128,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 @@ -572,6 +578,256 @@ async function batchQueryMoneyMarketGetCollateral({
}));
}

// Parsing function for MoneyMarket Public Logs response
const parseMoneyMarketPublicLogs = (response: any) => ({
page: response.page,
pageSize: response.page_size,
totalPages: response.total_pages,
totalItems: response.total_items,
data: response.data
? response.data.map((event: any) => ({
timestamp: new Date(event.timestamp * 1000), // Convert UNIX timestamp to JS Date
action: event.action, // Pass the full action JSON object without further parsing
}))
: [],
});

/**
* Query the Public Logs for a single money market contract using RxJS
* NOT FOR PRODUCTION USE, CONTRACT IS IN DEVELOPMENT ON TESTNET ONLY
*/
function queryMoneyMarketPublicLogs$({
contractAddress,
codeHash,
lcdEndpoint,
chainId,
pagination,
}: {
contractAddress: string,
codeHash?: string,
lcdEndpoint?: string,
chainId?: string,
pagination?: Pagination,
}) {
return getActiveQueryClient$(lcdEndpoint, chainId).pipe(
switchMap(({ client }) => sendSecretClientContractQuery$({
queryMsg: {
get_public_logs: { pagination },
},
client,
contractAddress,
codeHash,
})),
map((response) => parseMoneyMarketPublicLogs(response)),
first(),
);
}

/**
* Query the Public Logs for a single money market contract
* NOT FOR PRODUCTION USE, CONTRACT IS IN DEVELOPMENT ON TESTNET ONLY
*/
async function queryMoneyMarketPublicLogs({
contractAddress,
codeHash,
lcdEndpoint,
chainId,
pageSize,
page,
}: {
contractAddress: string,
codeHash?: string,
lcdEndpoint?: string,
chainId?: string,
pageSize?: number,
page?: number,
}) {
return lastValueFrom(queryMoneyMarketPublicLogs$({
contractAddress,
codeHash,
lcdEndpoint,
chainId,
pagination: pageSize !== undefined && page !== undefined
? { page_size: pageSize, page }
: undefined,
}));
}

/**
* Batch query the Public Logs for multiple money market contracts
* NOT FOR PRODUCTION USE, CONTRACT IS IN DEVELOPMENT ON TESTNET ONLY
*/
function batchQueryMoneyMarketPublicLogs$({
queryPublicLogsContractAddress,
queryPublicLogsCodeHash,
lcdEndpoint,
chainId,
moneyMarketContracts,
batchSize,
minBlockHeightValidationOptions,
blockHeight,
}: {
queryPublicLogsContractAddress: string,
queryPublicLogsCodeHash?: string,
lcdEndpoint?: string,
chainId?: string,
moneyMarketContracts: ContractAndPagination[],
batchSize?: number,
minBlockHeightValidationOptions?: MinBlockHeightValidationOptions,
blockHeight?: number,
}) {
const queries: BatchQueryParams[] = moneyMarketContracts.map((contract) => ({
id: contract.address,
contract: {
address: contract.address,
codeHash: contract.codeHash,
},
queryMsg: {
get_public_logs: {
pagination: contract.pageSize && contract.page
? { page_size: contract.pageSize, page: contract.page } : undefined,
},
},
}));

return batchQuery$({
contractAddress: queryPublicLogsContractAddress,
codeHash: queryPublicLogsCodeHash,
lcdEndpoint,
chainId,
queries,
batchSize,
minBlockHeightValidationOptions,
blockHeight,
}).pipe(
map((response) => response.map((item) => ({
moneyMarketContractAddress: item.id as string,
publicLogs: parseMoneyMarketPublicLogs(item.response),
blockHeight: item.blockHeight,
}))),
first(),
);
}

async function batchQueryMoneyMarketPublicLogs({
queryPublicLogsContractAddress,
queryPublicLogsCodeHash,
lcdEndpoint,
chainId,
moneyMarketContracts,
minBlockHeightValidationOptions,
}: {
queryPublicLogsContractAddress: string,
queryPublicLogsCodeHash?: string,
lcdEndpoint?: string,
chainId?: string,
moneyMarketContracts: ContractAndPagination[],
minBlockHeightValidationOptions?: MinBlockHeightValidationOptions,
}) {
return lastValueFrom(batchQueryMoneyMarketPublicLogs$({
queryPublicLogsContractAddress,
queryPublicLogsCodeHash,
lcdEndpoint,
chainId,
moneyMarketContracts,
minBlockHeightValidationOptions,
}));
}

const parseBatchQueryMoneyMarketRewardPools = (
responses: BatchQueryParsedResponse,
): ParsedRewardPoolsResponse[] => (
responses.map((response: BatchQueryParsedResponseItem) => ({
debtMarket: response.id.toString(),
blockHeight: response.blockHeight,
rewardPools: response.response.map((pool: RewardPoolResponse) => ({
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 @@ -582,4 +838,10 @@ export {
batchQueryMoneyMarketConfig,
batchQueryMoneyMarketGetMarkets,
batchQueryMoneyMarketGetCollateral,
queryMoneyMarketPublicLogs$,
queryMoneyMarketPublicLogs,
batchQueryMoneyMarketPublicLogs$,
batchQueryMoneyMarketPublicLogs,
batchQueryMoneyMarketRewardPools$,
batchQueryMoneyMarketRewardPools,
};
Loading
Loading