Skip to content

Commit

Permalink
fix: dao fee parse bug, add docs and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AustinWoetzel committed Nov 30, 2023
1 parent 9b246b0 commit 3cead3a
Show file tree
Hide file tree
Showing 5 changed files with 350 additions and 4 deletions.
134 changes: 131 additions & 3 deletions docs/queries/swap.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ console.log(output)
```

## Pair Config
query the configuration of the pair
query the configuration of a single pair

**input**

Expand Down Expand Up @@ -281,14 +281,142 @@ console.log(output)
};
```

## Pairs Info
Query the info for multiple pairs
## Pairs Config
Query the pair config for multiple pairs
::: info
This query uses a smart contract batch query router to allow you to query many pairs in a single http request. This is a highly efficient method of interacting with the chain an minimizes the load on the LCD endpoint.
:::

**input**

```ts
async function batchQueryPairsConfig({
queryRouterContractAddress,
queryRouterCodeHash,
lcdEndpoint,
chainId,
pairsContracts,
}:{
queryRouterContractAddress: string,
queryRouterCodeHash?: string,
lcdEndpoint?: string,
chainId?: string,
pairsContracts: Contract[]
}): Promise<BatchPairsConfig>
```

**output**

```ts
type BatchPairsConfig = BatchPairConfig[]
type BatchPairConfig = {
pairContractAddress: string,
pairConfig: PairConfig,
}
type CustomFee = {
daoFee: number,
lpFee: number,
}
type PairConfig = {
factoryContract: Contract | null,
lpTokenContract: Contract | null,
stakingContract: Contract | null,
token0Contract: Contract,
token1Contract: Contract,
isStable: boolean,
fee: CustomFee | null,
}
```

**example use**

```ts
const output = await batchQueryPairsConfig({
queryRouterContractAddress: '[QUERY_ROUTER_CONTRACT_ADDRESS]',
queryRouterCodeHash: '[QUERY_ROUTER_CODE_HASH]',
pairsContracts: [{
address: '[PAIR_1_ADDRESS]',
codeHash: '[PAIR_1_CODE_HASH]',
},
{
address: '[PAIR_2_ADDRESS]',
codeHash: '[PAIR_2_CODE_HASH]',
}]
})
console.log(output)
```
***console***
```md
[
{
"pairContractAddress": "secret1qyt4l47yq3x43ezle4nwlh5q0sn6f9sesat7ap",
"pairConfig": {
"factoryContract": {
"address": "secret1ja0hcwvy76grqkpgwznxukgd7t8a8anmmx05pp",
"codeHash": "2ad4ed2a4a45fd6de3daca9541ba82c26bb66c76d1c3540de39b509abd26538e"
},
"lpTokenContract": {
"address": "secret10egcg03euavu336fzed87m4zdx8jkgzzz7zgmh",
"codeHash": "b0c2048d28a0ca0b92274549b336703622ecb24a8c21f417e70c03aa620fcd7b"
},
"stakingContract": {
"address": "secret1vgtmfvzdn7ztn7kcrqd7p6f2z97wvauavp3udh",
"codeHash": "a83f0fdc6e5bcdb1f59e39200a084401309fc5338dbb2e54a2bcdc08fa3eaf49"
},
"token0Contract": {
"address": "secret153wu605vvp934xhd4k9dtd640zsep5jkesstdm",
"codeHash": "638a3e1d50175fbcb8373cf801565283e3eb23d88a9b7b7f99fcc5eb1e6b561e"
},
"token1Contract": {
"address": "secret1k6u0cy4feepm6pehnz804zmwakuwdapm69tuc4",
"codeHash": "f6be719b3c6feb498d3554ca0398eb6b7e7db262acb33f84a8f12106da6bbb09"
},
"isStable": false,
"fee": null
}
},
{
"pairContractAddress": "secret1l34fyc9g23fnlk896693nw57phevnyha7pt6gj",
"pairConfig": {
"factoryContract": {
"address": "secret1ja0hcwvy76grqkpgwznxukgd7t8a8anmmx05pp",
"codeHash": "2ad4ed2a4a45fd6de3daca9541ba82c26bb66c76d1c3540de39b509abd26538e"
},
"lpTokenContract": {
"address": "secret1zw9gwj6kx7vd3xax7wf45y6dmawkj3pd3dk7wt",
"codeHash": "b0c2048d28a0ca0b92274549b336703622ecb24a8c21f417e70c03aa620fcd7b"
},
"stakingContract": {
"address": "secret13j4n5gj8857h2j4cnempdkfygrw9snasx4yzw2",
"codeHash": "a83f0fdc6e5bcdb1f59e39200a084401309fc5338dbb2e54a2bcdc08fa3eaf49"
},
"token0Contract": {
"address": "secret1fl449muk5yq8dlad7a22nje4p5d2pnsgymhjfd",
"codeHash": "638a3e1d50175fbcb8373cf801565283e3eb23d88a9b7b7f99fcc5eb1e6b561e"
},
"token1Contract": {
"address": "secret153wu605vvp934xhd4k9dtd640zsep5jkesstdm",
"codeHash": "638a3e1d50175fbcb8373cf801565283e3eb23d88a9b7b7f99fcc5eb1e6b561e"
},
"isStable": false,
"fee": {
"lpFee": 0,
"daoFee": 0.003
}
}
}
]
```

## Pairs Info
Query the info for multiple pairs

**input**

```ts
async function batchQueryPairsInfo({
queryRouterContractAddress,
Expand Down
69 changes: 69 additions & 0 deletions src/contracts/services/swap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ import {
parseSwapResponse,
parseBatchQueryPairInfoResponse,
parseBatchQueryStakingInfoResponse,
parseBatchQueryPairConfigResponse,
batchQueryPairsInfo,
batchQueryPairsInfo$,
batchQueryStakingInfo,
batchQueryStakingInfo$,
batchQueryPairsConfig,
batchQueryPairsConfig$,
} from '~/contracts/services/swap';
import factoryConfigResponse from '~/test/mocks/swap/factoryConfig.json';
import factoryPairsResponse from '~/test/mocks/swap/factoryPairs.json';
Expand All @@ -49,6 +52,8 @@ import { pairsInfoResponseUnparsed } from '~/test/mocks/swap/batchPairsInfoRespo
import { pairsInfoParsed } from '~/test/mocks/swap/batchPairsInfoParsed';
import { batchStakingConfigUnparsed } from '~/test/mocks/swap/batchStakingConfigUnparsed';
import { batchStakingConfigParsed } from '~/test/mocks/swap/batchStakingConfigParsed';
import { batchPairsConfigResponseUnparsed } from '~/test/mocks/swap/batchPairsConfigResponseUnparsed';
import { batchPairsConfigParsed } from '~/test/mocks/swap/batchPairsConfigParsed';

const sendSecretClientContractQuery$ = vi.hoisted(() => vi.fn());
const batchQuery$ = vi.hoisted(() => vi.fn());
Expand Down Expand Up @@ -121,6 +126,12 @@ test('it can parse the batch staking response', () => {
)).toStrictEqual(batchStakingConfigParsed);
});

test('it can parse the batch pair config response', () => {
expect(parseBatchQueryPairConfigResponse(
batchPairsConfigResponseUnparsed,
)).toStrictEqual(batchPairsConfigParsed);
});

test('it can call the query factory config service', async () => {
// observables function
sendSecretClientContractQuery$.mockReturnValueOnce(of(factoryConfigResponse));
Expand Down Expand Up @@ -354,3 +365,61 @@ test('it can call the batch staking info query service', async () => {
});
expect(response).toStrictEqual(batchStakingConfigParsed);
});

test('it can call the batch pair config query service', async () => {
const input = {
queryRouterContractAddress: 'CONTRACT_ADDRESS',
queryRouterCodeHash: 'CODE_HASH',
lcdEndpoint: 'LCD_ENDPOINT',
chainId: 'CHAIN_ID',
pairsContracts: [{
address: 'PAIR_ADDRESS',
codeHash: 'PAIR_CODE_HASH',
}],
};

// observables function
batchQuery$.mockReturnValueOnce(of(batchPairsConfigResponseUnparsed));
let output;
batchQueryPairsConfig$(input).subscribe({
next: (response) => {
output = response;
},
});

expect(batchQuery$).toHaveBeenCalledWith({
contractAddress: input.queryRouterContractAddress,
codeHash: input.queryRouterCodeHash,
lcdEndpoint: input.lcdEndpoint,
chainId: input.chainId,
queries: [{
id: input.pairsContracts[0].address,
contract: {
address: input.pairsContracts[0].address,
codeHash: input.pairsContracts[0].codeHash,
},
queryMsg: 'PAIR_CONFIG_MSG',
}],
});

expect(output).toStrictEqual(batchPairsConfigParsed);

// async/await function
batchQuery$.mockReturnValueOnce(of(batchPairsConfigResponseUnparsed));
const response = await batchQueryPairsConfig(input);
expect(batchQuery$).toHaveBeenCalledWith({
contractAddress: input.queryRouterContractAddress,
codeHash: input.queryRouterCodeHash,
lcdEndpoint: input.lcdEndpoint,
chainId: input.chainId,
queries: [{
id: input.pairsContracts[0].address,
contract: {
address: input.pairsContracts[0].address,
codeHash: input.pairsContracts[0].codeHash,
},
queryMsg: 'PAIR_CONFIG_MSG',
}],
});
expect(response).toStrictEqual(batchPairsConfigParsed);
});
3 changes: 2 additions & 1 deletion src/contracts/services/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ function parsePairConfig(response: PairConfigResponse): PairConfig {
isStable: pair[2],
fee: customFee ? {
lpFee: customFee.lp_fee.nom / customFee.lp_fee.denom,
daoFee: customFee.lp_fee.nom / customFee.lp_fee.denom,
daoFee: customFee.shade_dao_fee.nom / customFee.shade_dao_fee.denom,
} : null,
};
}
Expand Down Expand Up @@ -701,4 +701,5 @@ export {
parseSwapResponse,
parseBatchQueryPairInfoResponse,
parseBatchQueryStakingInfoResponse,
parseBatchQueryPairConfigResponse,
};
63 changes: 63 additions & 0 deletions src/test/mocks/swap/batchPairsConfigParsed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const batchPairsConfigParsed = [
{
pairContractAddress: 'secret1qyt4l47yq3x43ezle4nwlh5q0sn6f9sesat7ap',
pairConfig: {
factoryContract: {
address: 'secret1ja0hcwvy76grqkpgwznxukgd7t8a8anmmx05pp',
codeHash: '2ad4ed2a4a45fd6de3daca9541ba82c26bb66c76d1c3540de39b509abd26538e',
},
lpTokenContract: {
address: 'secret10egcg03euavu336fzed87m4zdx8jkgzzz7zgmh',
codeHash: 'b0c2048d28a0ca0b92274549b336703622ecb24a8c21f417e70c03aa620fcd7b',
},
stakingContract: {
address: 'secret1vgtmfvzdn7ztn7kcrqd7p6f2z97wvauavp3udh',
codeHash: 'a83f0fdc6e5bcdb1f59e39200a084401309fc5338dbb2e54a2bcdc08fa3eaf49',
},
token0Contract: {
address: 'secret153wu605vvp934xhd4k9dtd640zsep5jkesstdm',
codeHash: '638a3e1d50175fbcb8373cf801565283e3eb23d88a9b7b7f99fcc5eb1e6b561e',
},
token1Contract: {
address: 'secret1k6u0cy4feepm6pehnz804zmwakuwdapm69tuc4',
codeHash: 'f6be719b3c6feb498d3554ca0398eb6b7e7db262acb33f84a8f12106da6bbb09',
},
isStable: false,
fee: null,
},
},
{
pairContractAddress: 'secret1l34fyc9g23fnlk896693nw57phevnyha7pt6gj',
pairConfig: {
factoryContract: {
address: 'secret1ja0hcwvy76grqkpgwznxukgd7t8a8anmmx05pp',
codeHash: '2ad4ed2a4a45fd6de3daca9541ba82c26bb66c76d1c3540de39b509abd26538e',
},
lpTokenContract: {
address: 'secret1zw9gwj6kx7vd3xax7wf45y6dmawkj3pd3dk7wt',
codeHash: 'b0c2048d28a0ca0b92274549b336703622ecb24a8c21f417e70c03aa620fcd7b',
},
stakingContract: {
address: 'secret13j4n5gj8857h2j4cnempdkfygrw9snasx4yzw2',
codeHash: 'a83f0fdc6e5bcdb1f59e39200a084401309fc5338dbb2e54a2bcdc08fa3eaf49',
},
token0Contract: {
address: 'secret1fl449muk5yq8dlad7a22nje4p5d2pnsgymhjfd',
codeHash: '638a3e1d50175fbcb8373cf801565283e3eb23d88a9b7b7f99fcc5eb1e6b561e',
},
token1Contract: {
address: 'secret153wu605vvp934xhd4k9dtd640zsep5jkesstdm',
codeHash: '638a3e1d50175fbcb8373cf801565283e3eb23d88a9b7b7f99fcc5eb1e6b561e',
},
isStable: false,
fee: {
lpFee: 0,
daoFee: 0.003,
},
},
},
];

export {
batchPairsConfigParsed,
};
Loading

0 comments on commit 3cead3a

Please sign in to comment.