Skip to content

Commit

Permalink
feat: cleaning up pr
Browse files Browse the repository at this point in the history
  • Loading branch information
SissonJ committed Mar 28, 2024
1 parent b1775c1 commit afe0170
Show file tree
Hide file tree
Showing 11 changed files with 275 additions and 36 deletions.
56 changes: 56 additions & 0 deletions docs/calculations/apy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# APY Calculations

This page demonstrates how to calculate outputs the shade derivative contracts

### stkd-SCRT APY
Calculates the apy expected for the stkd-SCRT token
```ts
/**
* Will calculate APY for the stkd secret derivative contract
*
* returns a number that is the decimal form of the percent APY
*/
function calculateDerivativeScrtApy({
queryRouterContractAddress,
queryRouterCodeHash,
contractAddress,
codeHash,
lcdEndpoint,
chainId,
}: {
queryRouterContractAddress: string,
queryRouterCodeHash?: string,
contractAddress: string,
codeHash: string,
lcdEndpoint: string,
chainId?: string,
}): Promise<number>
```

### dSHD APY
Calculates the apy expected for the stkd-SCRT token
```ts
/**
* Calculates the dSHD expected APY by querying the staking contract
* TESTNET ONLY NOT READY FOR PRODUCTION
*
* returns a number that is the decimal form of the percent APY
*/
async function calculateDerivativeShdApy({
shadeTokenContractAddress,
shadeStakingContractAddress,
shadeStakingCodeHash,
decimals,
price,
lcdEndpoint,
chainId,
}:{
shadeTokenContractAddress: string,
shadeStakingContractAddress: string,
shadeStakingCodeHash?: string,
decimals: number,
price: string,
lcdEndpoint?: string,
chainId?: string,
}): Promise<number>
```
2 changes: 2 additions & 0 deletions docs/contracts.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This page contains a list of deployed contracts.
| Batch Query Router | secret17gnlxnwux0szd7qhl90ym8lw22qvedjz4v09dm | 72a09535b77b76862f7b568baf1ddbe158a2e4bbd0f0879c69ada9b398e31c1f |
| Oracle | secret10n2xl5jmez6r9umtdrth78k0vwmce0l5m9f5dm | 32c4710842b97a526c243a68511b15f58d6e72a388af38a7221ff3244c754e91 |
| stkd-scrt | secret1k6u0cy4feepm6pehnz804zmwakuwdapm69tuc4 | f6be719b3c6feb498d3554ca0398eb6b7e7db262acb33f84a8f12106da6bbb09 |
| Shade Staking | secret1y6px5x7jzrk8hyvy67f06ytn8v0jwculypwxws | 2a1ae7fd2be82931cb11d0ce82b2e243507f2006074e2f316da661beb1abe3c3 |

::: tip
The ShadeSwap pairs contracts are accessible via the factory registered pairs query.
Expand All @@ -24,3 +25,4 @@ The ShadeSwap pairs contracts are accessible via the factory registered pairs qu
| Batch Query Router | secret10cxkxmspt44mp2wym6dcguk5wqqkm5a9ydw3du | 72a09535b77b76862f7b568baf1ddbe158a2e4bbd0f0879c69ada9b398e31c1f |
| Oracle | secret17z47r9u4nqytpdgvewxq4jqd965sfj2wpsnlak | 113c47c016667817b315dde03b4ee9774edf1fb293a7ea3f02d983c6b1fa1cf1 |
| stkd-scrt | secret14svk0x3sztxwta9kv9dv6fwzqlc26mmjfyypc2 | 680fbb3c8f8eb1c920da13d857daaedaa46ab8f9a8e26e892bb18a16985ec29e |
| Shade Staking | secret1f9ph34ydnnqg0rzs6h39ucswspllutp9c4ch4k | 5e1e1b0c2a8e2114d29725002be7206598ec68f4bdb28718b082fd84748d416f |
46 changes: 46 additions & 0 deletions docs/queries/shadeStaking.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Shade Staking Examples

This page demonstrates how to query the shade staking contracts

## Staking Info

**input**

```ts
/**
* query the staking info from the shade staking contract
*/
async function queryShadeStakingOpportunity({
shadeStakingContractAddress,
shadeStakingCodeHash,
lcdEndpoint,
chainId,
}: {
shadeStakingContractAddress: string,
shadeStakingCodeHash?: string,
lcdEndpoint?: string,
chainId?: string,
}): Promise<StakingInfoServiceModel>
```

**output**

```ts
type StakingInfoServiceModel = {
stakeTokenAddress: string,
totalStakedRaw: string,
unbondingPeriod: number,
rewardPools: StakingRewardPoolServiceModel[],
}
// type references below
type StakingRewardPoolServiceModel = {
id: string,
amountRaw: string,
startDate: Date,
endDate: Date,
tokenAddress: string,
rateRaw: string,
}
```
10 changes: 10 additions & 0 deletions src/contracts/definitions/shadeStaking.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {
test,
expect,
} from 'vitest';
import { msgQueryShadeStakingOpportunity } from './shadeStaking';

test('it tests the form of the query staking info msg', () => {
const output = { staking_info: {} };
expect(msgQueryShadeStakingOpportunity()).toStrictEqual(output);
});
6 changes: 6 additions & 0 deletions src/contracts/services/shadeStaking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import {
StakingInfoServiceModel,
} from '~/types/contracts/shadeStaking/index';

/**
* parses the response from the shade staking contract into a model
*/
function parseStakingOpportunity(data: StakingInfoServiceResponse): StakingInfoServiceModel {
const stakeTokenAddress = data.staking_info.info.stake_token;
const totalStakedRaw = data.staking_info.info.total_staked;
Expand Down Expand Up @@ -61,6 +64,9 @@ const queryShadeStakingOpportunity$ = ({
first(),
);

/**
* query the staking info from the shade staking contract
*/
async function queryShadeStakingOpportunity({
shadeStakingContractAddress,
shadeStakingCodeHash,
Expand Down
17 changes: 12 additions & 5 deletions src/lib/apy/derivativeScrt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ const calcAPY = (periodRate:number, apr:number):number => (1 + apr / periodRate)

/**
* Will calculate APY for the stkd secret derivative contract
*
* returns a number that is the decimal form of the percent APY
*/
function calculateDerivativeScrtApy$({
queryRouterContractAddress,
Expand All @@ -99,7 +101,7 @@ function calculateDerivativeScrtApy$({
}) {
const queries = Object.values(SecretQueryOptions);
return forkJoin({
chainParameters: secretChainQueries$(lcdEndpoint, queries),
chainParameters: secretChainQueries$<SecretChainDataQueryModel>(lcdEndpoint, queries),
derivativeInfo: queryDerivativeScrtInfo$({
queryRouterContractAddress,
queryRouterCodeHash,
Expand All @@ -114,15 +116,15 @@ function calculateDerivativeScrtApy$({
derivativeInfo: DerivativeScrtInfo,
}) => {
const apr = calcAggregateAPR({
networkValidatorList: response.chainParameters.secretValidators!,
networkValidatorList: response.chainParameters.secretValidators,
validatorSet: response.derivativeInfo.validators,
inflationRate: response.chainParameters.secretInflationPercent!,
inflationRate: response.chainParameters.secretInflationPercent,
totalScrtStaked: convertCoinFromUDenom(
response.chainParameters.secretTotalStakedRaw!,
response.chainParameters.secretTotalStakedRaw,
SECRET_DECIMALS,
).toNumber(),
totalScrtSupply: convertCoinFromUDenom(
response.chainParameters.secretTotalSupplyRaw!,
response.chainParameters.secretTotalSupplyRaw,
SECRET_DECIMALS,
).toNumber(),
foundationTax: response.chainParameters.secretTaxes!.foundationTaxPercent,
Expand All @@ -133,6 +135,11 @@ function calculateDerivativeScrtApy$({
);
}

/**
* Will calculate APY for the stkd secret derivative contract
*
* returns a number that is the decimal form of the percent APY
*/
function calculateDerivativeScrtApy({
queryRouterContractAddress,
queryRouterCodeHash,
Expand Down
10 changes: 4 additions & 6 deletions src/lib/apy/derivativeShd.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ import {
import { of } from 'rxjs';
import { stakingOpportunityResponseParsed } from '~/test/mocks/shadeStaking/response';
import { queryShadeStakingOpportunity$ } from '~/contracts/services/shadeStaking';
import { calculateDerivativeShdApy$, calculateDerivativeShdApy } from './derivativeShd';
import {
calculateDerivativeShdApy$,
calculateDerivativeShdApy,
} from './derivativeShd';

beforeAll(() => {
vi.setSystemTime(new Date('2024-03-26T18:00:00.000Z'));
vi.mock('~/lib/apy/secretQueries', () => ({
secretChainQueries$: vi.fn(() => of({
})),
}));

vi.mock('~/contracts/services/shadeStaking', () => ({
queryShadeStakingOpportunity$: vi.fn(() => of(stakingOpportunityResponseParsed)),
}));
Expand Down
4 changes: 4 additions & 0 deletions src/lib/apy/derivativeShd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ function calculateRewardPoolAPY({
/**
* Calculates the dSHD expected APY by querying the staking contract
* TESTNET ONLY NOT READY FOR PRODUCTION
*
* returns a number that is the decimal form of the percent APY
*/
function calculateDerivativeShdApy$({
shadeTokenContractAddress,
Expand Down Expand Up @@ -84,6 +86,8 @@ function calculateDerivativeShdApy$({
/**
* Calculates the dSHD expected APY by querying the staking contract
* TESTNET ONLY NOT READY FOR PRODUCTION
*
* returns a number that is the decimal form of the percent APY
*/
async function calculateDerivativeShdApy({
shadeTokenContractAddress,
Expand Down
79 changes: 79 additions & 0 deletions src/lib/apy/secretQueries.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import {
test,
expect,
vi,
beforeAll,
} from 'vitest';
import {
SecretQueryOptions,
} from '~/types/apy';
import { of } from 'rxjs';
import {
secretChainQuery$,
secretChainQuery,
secretChainQueries$,
secretChainQueries,
parseSecretQueryResponse,
} from './secretQueries';

beforeAll(async () => {
vi.mock('~/client/services/createFetch', () => ({
createFetch: vi.fn(() => of({ response: 'MOCK_API_RESPONSE' })),
}));
});

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

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

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

expect(parseSecretQueryResponse(
{ result: { 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' }] },
SecretQueryOptions.VALIDATORS,
)).toStrictEqual({ secretValidators: [{ ratePercent: 10, validatorAddress: 'MOCK_ADDRESS' }] });

expect(parseSecretQueryResponse(
'nonsence',
'/nonsence/api',
)).toStrictEqual('nonsence');
});

test('it can do a single chain query', async () => {
let output;
secretChainQuery$('MOCK_URL', 'MOCK_ENDPOINT').subscribe({
next: (response) => {
output = response;
},
});
expect(output).toStrictEqual({ response: 'MOCK_API_RESPONSE' });
const output2 = await secretChainQuery('MOCK_URL', 'MOCK_ENDPOINT');
expect(output2).toStrictEqual({ response: 'MOCK_API_RESPONSE' });
});

test('it can do many chain queries', async () => {
let output;
secretChainQueries$('MOCK_URL', ['MOCK_ENDPOINT']).subscribe({
next: (response) => {
output = response;
},
});
expect(output).toStrictEqual({ response: 'MOCK_API_RESPONSE' });
const output2 = await secretChainQueries('MOCK_URL', ['MOCK_ENDPOINT']);
expect(output2).toStrictEqual({ response: 'MOCK_API_RESPONSE' });
});
Loading

0 comments on commit afe0170

Please sign in to comment.