Skip to content

Commit

Permalink
feat: fix a decimal issue for the derivative
Browse files Browse the repository at this point in the history
  • Loading branch information
SissonJ committed Jan 28, 2025
1 parent ca7e65f commit af26e1b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/contracts/services/derivativeShd.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ beforeAll(() => {
test('it can parse the staking info', () => {
expect(parseDerivativeShdStakingInfo(
stakingInfoResponse as StakingInfoResponse,
8,
)).toStrictEqual(stakingInfoResponseParsed);
});

Expand All @@ -41,6 +42,7 @@ test('it can call the query staking info service', async () => {
codeHash: 'CODE_HASH',
lcdEndpoint: 'LCD_ENDPOINT',
chainId: 'CHAIN_ID',
decimals: 8,
};

let output;
Expand Down
13 changes: 8 additions & 5 deletions src/contracts/services/derivativeShd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,21 @@ import {
import { convertCoinFromUDenom } from '~/lib/utils';
import { msgQueryShdDerivativeStakingInfo } from '~/contracts/definitions/derivativeShd';

// Contract returns price as a rate of dSHD/SHD with 8 decimals
const DERIVATE_PRICE_DECIMALS = 8;

/**
* Parses the staking info query into a cleaner data model
* NOT FOR PRODUCTION USE, CONTRACT IS IN DEVELOPMENT ON TESTNET ONLY
*/
const parseDerivativeShdStakingInfo = (
response: StakingInfoResponse,
decimals: number,
): ParsedStakingInfoResponse => ({
unbondingTime: response.staking_info.unbonding_time,
bondedShd: response.staking_info.bonded_shd,
rewards: response.staking_info.rewards,
totalDerivativeTokenSupply: response.staking_info.total_derivative_token_supply,
price: convertCoinFromUDenom(
response.staking_info.price,
DERIVATE_PRICE_DECIMALS,
decimals,
).toNumber(),
feeInfo: {
stakingFee: response.staking_info.fee_info.staking.rate / (
Expand All @@ -54,19 +52,21 @@ const queryDerivativeShdStakingInfo$ = ({
codeHash,
lcdEndpoint,
chainId,
decimals,
}: {
contractAddress: string,
codeHash?: string,
lcdEndpoint?: string,
chainId?: string,
decimals: number,
}) => getActiveQueryClient$(lcdEndpoint, chainId).pipe(
switchMap(({ client }) => sendSecretClientContractQuery$({
queryMsg: msgQueryShdDerivativeStakingInfo(),
client,
contractAddress,
codeHash,
})),
map((response) => parseDerivativeShdStakingInfo(response as StakingInfoResponse)),
map((response) => parseDerivativeShdStakingInfo(response as StakingInfoResponse, decimals)),
first(),
);

Expand All @@ -79,17 +79,20 @@ async function queryDerivativeShdStakingInfo({
codeHash,
lcdEndpoint,
chainId,
decimals,
}: {
contractAddress: string,
codeHash?: string,
lcdEndpoint?: string,
chainId?: string,
decimals: number,
}) {
return lastValueFrom(queryDerivativeShdStakingInfo$({
contractAddress,
codeHash,
lcdEndpoint,
chainId,
decimals,
}));
}

Expand Down

0 comments on commit af26e1b

Please sign in to comment.