Skip to content

Commit

Permalink
fix: secret api had breaking changes after upgrade related to the lcd…
Browse files Browse the repository at this point in the history
… api
  • Loading branch information
AustinWoetzel committed Jan 3, 2025
1 parent 63b774e commit 23bbb4c
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .changeset/spicy-badgers-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@shadeprotocol/shadejs": patch
---

change secret endpoints to new api after upgrade
11 changes: 11 additions & 0 deletions src/client/services/clientServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ const sendSecretClientContractQuery$ = ({
first(),
);

// This function queries the total supply of the a token
const secretClientTokenSupplyQuery$ = (
client: SecretNetworkClient,
denom: string,
) => createFetchClient(defer(
() => from(client.query.bank.supplyOf({
denom,
})),
));

export {
sendSecretClientContractQuery$,
secretClientTokenSupplyQuery$,
};
1 change: 1 addition & 0 deletions src/lib/apy/derivativeScrt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ beforeAll(() => {
vi.mock('~/lib/apy/secretQueries', async (importOriginal: any) => ({
...(await importOriginal()),
secretChainQueries$: vi.fn(() => of(chainQueryParsedResponse)),
queryScrtTotalSupply$: vi.fn(() => of(292470737038201)),
}));

vi.mock('~/contracts/services/derivativeScrt', () => ({
Expand Down
7 changes: 5 additions & 2 deletions src/lib/apy/derivativeScrt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import {
import { convertCoinFromUDenom } from '~/lib/utils';
import { queryDerivativeScrtInfo$ } from '~/contracts/services/derivativeScrt';
import {
queryScrtTotalSupply$,
secretChainQueries$,
SecretQueryOptions,
} from './secretQueries';
} from '~/lib/apy/secretQueries';
import { calcAggregateAPR, calcAPY } from './utils';

const SECRET_DECIMALS = 6;
Expand Down Expand Up @@ -39,6 +40,7 @@ function calculateDerivativeScrtApy$({
const queries = Object.values(SecretQueryOptions);
return forkJoin({
chainParameters: secretChainQueries$<SecretChainDataQueryModel>(lcdEndpoint, queries),
scrtTotalSupplyRaw: queryScrtTotalSupply$(lcdEndpoint),
derivativeInfo: queryDerivativeScrtInfo$({
queryRouterContractAddress,
queryRouterCodeHash,
Expand All @@ -50,6 +52,7 @@ function calculateDerivativeScrtApy$({
}).pipe(
map((response: {
chainParameters: SecretChainDataQueryModel,
scrtTotalSupplyRaw: number,
derivativeInfo: DerivativeScrtInfo,
}) => {
const apr = calcAggregateAPR({
Expand All @@ -61,7 +64,7 @@ function calculateDerivativeScrtApy$({
SECRET_DECIMALS,
).toNumber(),
totalScrtSupply: convertCoinFromUDenom(
response.chainParameters.secretTotalSupplyRaw,
response.scrtTotalSupplyRaw,
SECRET_DECIMALS,
).toNumber(),
foundationTax: response.chainParameters.secretTaxes!.foundationTaxPercent,
Expand Down
13 changes: 4 additions & 9 deletions src/lib/apy/secretQueries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,22 @@ beforeAll(async () => {

test('it can parse chain queries', () => {
expect(parseSecretQueryResponse(
{ result: 10 },
{ inflation: 10 },
SecretQueryOptions.INFLATION,
)).toStrictEqual({ secretInflationPercent: 10 });

expect(parseSecretQueryResponse(
{ amount: { amount: 10 } },
SecretQueryOptions.TOTAL_SUPPLY,
)).toStrictEqual({ secretTotalSupplyRaw: 10 });

expect(parseSecretQueryResponse(
{ result: { bonded_tokens: 10 } },
{ pool: { bonded_tokens: 10 } },
SecretQueryOptions.TOTAL_STAKED,
)).toStrictEqual({ secretTotalStakedRaw: 10 });

expect(parseSecretQueryResponse(
{ result: { community_tax: '10', secret_foundation_tax: '11' } },
{ params: { community_tax: '10', secret_foundation_tax: '11' } },
SecretQueryOptions.TAXES,
)).toStrictEqual({ secretTaxes: { foundationTaxPercent: 11, communityTaxPercent: 10 } });

expect(parseSecretQueryResponse(
{ result: [{ commission: { commission_rates: { rate: '10' } }, operator_address: 'MOCK_ADDRESS' }] },
{ validators: [{ commission: { commission_rates: { rate: '10' } }, operator_address: 'MOCK_ADDRESS' }] },
SecretQueryOptions.VALIDATORS,
)).toStrictEqual({ secretValidators: [{ ratePercent: 10, validatorAddress: 'MOCK_ADDRESS' }] });

Expand Down
57 changes: 40 additions & 17 deletions src/lib/apy/secretQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@ import {
forkJoin,
lastValueFrom,
map,
first, switchMap,
} from 'rxjs';
import { fromFetch } from 'rxjs/fetch';
import { createFetch } from '~/client/services/createFetch';
import {
SecretValidatorItemResponse,
} from '~/types/apy';

import { QuerySupplyOfResponse } from 'secretjs/dist/grpc_gateway/cosmos/bank/v1beta1/query.pb';
import { getActiveQueryClient$ } from '~/client';
import { secretClientTokenSupplyQuery$ } from '~/client/services/clientServices';

enum SecretQueryOptions {
INFLATION = '/minting/inflation',
TOTAL_SUPPLY = '/cosmos/bank/v1beta1/supply/uscrt',
TOTAL_STAKED = '/staking/pool',
TAXES = '/distribution/parameters',
VALIDATORS = '/staking/validators',
INFLATION = '/cosmos/mint/v1beta1/inflation',
TOTAL_STAKED = '/cosmos/staking/v1beta1/pool',
TAXES = '/cosmos/distribution/v1beta1/params',
VALIDATORS = '/cosmos/staking/v1beta1/validators',
}

/**
Expand All @@ -29,31 +33,27 @@ function parseSecretQueryResponse<ResponseType>(
switch (query) {
case SecretQueryOptions.INFLATION:
return {
secretInflationPercent: response?.result,
secretInflationPercent: response?.inflation,
} as ResponseType;
case SecretQueryOptions.TOTAL_SUPPLY:
return {
secretTotalSupplyRaw: response?.amount?.amount,
}as ResponseType;
case SecretQueryOptions.TOTAL_STAKED:
return {
secretTotalStakedRaw: response?.result?.bonded_tokens,
secretTotalStakedRaw: response?.pool?.bonded_tokens,
}as ResponseType;
case SecretQueryOptions.TAXES:
if (response.result
&& response.result.community_tax
&& response.result.secret_foundation_tax
if (response.params
&& response.params.community_tax
&& response.params.secret_foundation_tax
) {
return {
secretTaxes: {
foundationTaxPercent: Number(response.result.secret_foundation_tax),
communityTaxPercent: Number(response.result.community_tax),
foundationTaxPercent: Number(response.params.secret_foundation_tax),
communityTaxPercent: Number(response.params.community_tax),
},
}as ResponseType;
}
return response as ResponseType;
case SecretQueryOptions.VALIDATORS: {
const parsedValidators = response?.result?.map((
const parsedValidators = response?.validators?.map((
nextValidator: SecretValidatorItemResponse,
) => ({
ratePercent: Number(nextValidator.commission.commission_rates.rate),
Expand Down Expand Up @@ -138,11 +138,34 @@ async function secretChainQueries<ResponseType>(
return lastValueFrom(secretChainQueries$(url, queries));
}

const parseTotalSupplyResponse = (response:QuerySupplyOfResponse) => {
if (response === undefined
|| response.amount === undefined
|| response.amount.amount === undefined
) {
throw new Error('No response from the total supply query');
}
return Number(response.amount.amount);
};

/**
* query the staking info from the shade staking contract
*/
const queryScrtTotalSupply$ = (
lcdEndpoint?: string,
chainId?: string,
) => getActiveQueryClient$(lcdEndpoint, chainId).pipe(
switchMap(({ client }) => secretClientTokenSupplyQuery$(client, 'uscrt')),
map((response) => parseTotalSupplyResponse(response)),
first(),
);

export {
SecretQueryOptions,
parseSecretQueryResponse,
secretChainQuery$,
secretChainQuery,
secretChainQueries$,
secretChainQueries,
queryScrtTotalSupply$,
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"secretInflationPercent": 0.09,
"secretTotalSupplyRaw": 292470737038201,
"secretTotalStakedRaw": 161156183048148,
"secretTaxes": {
"foundationTaxPercent": 0,
Expand Down

0 comments on commit 23bbb4c

Please sign in to comment.