-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #137 from securesecrets/develop
lend stability pool public query
- Loading branch information
Showing
28 changed files
with
689 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@shadeprotocol/shadejs": patch | ||
--- | ||
|
||
lend stability pool query |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@shadeprotocol/shadejs": patch | ||
--- | ||
|
||
lend contracts in docs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './vaultRegistry'; | ||
export * from './stabilityPool'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { | ||
test, | ||
expect, | ||
} from 'vitest'; | ||
import { msgGetStabilityPoolInfo } from './stabilityPool'; | ||
|
||
test('it tests the form of the stability pool info message', () => { | ||
const output = { | ||
get_pool_info: {}, | ||
}; | ||
expect(msgGetStabilityPoolInfo()).toStrictEqual(output); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* message for the getting the stability pool data | ||
*/ | ||
const msgGetStabilityPoolInfo = () => ({ | ||
get_pool_info: {}, | ||
}); | ||
|
||
export { | ||
msgGetStabilityPoolInfo, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './vaultRegistry'; | ||
export * from './stabilityPool'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { | ||
test, | ||
expect, | ||
vi, | ||
beforeAll, | ||
} from 'vitest'; | ||
import stabilityPoolInfoResponse from '~/test/mocks/lend/stabilityPool/stabilityPoolInfoResponse.json'; | ||
import { stabilityPoolInfoParsed } from '~/test/mocks/lend/stabilityPool/stabilityPoolInfoParsed'; | ||
import { batchStabilityPoolResponseUnparsed } from '~/test/mocks/lend/stabilityPool/stabilityPoolInfoBatchResponse'; | ||
import { of } from 'rxjs'; | ||
import { MinBlockHeightValidationOptions } from '~/types'; | ||
import { | ||
parseStabilityPoolResponse, | ||
parseStabilityPoolResponseFromQueryRouter, | ||
queryStabilityPoolInfo$, | ||
queryStabilityPoolInfo, | ||
} from './stabilityPool'; | ||
|
||
const batchQuery$ = vi.hoisted(() => vi.fn()); | ||
|
||
beforeAll(() => { | ||
vi.mock('~/contracts/definitions/lend/stabilityPool', () => ({ | ||
msgGetStabilityPoolInfo: vi.fn(() => 'GET_STABILITY_POOL_MSG'), | ||
})); | ||
|
||
vi.mock('~/contracts/services/batchQuery', () => ({ | ||
batchQuery$, | ||
})); | ||
}); | ||
|
||
test('it can parse the stability pool response', () => { | ||
expect(parseStabilityPoolResponse( | ||
stabilityPoolInfoResponse, | ||
1, | ||
)).toStrictEqual(stabilityPoolInfoParsed); | ||
}); | ||
|
||
test('it can parse the stability pool response via the query router', () => { | ||
expect(parseStabilityPoolResponseFromQueryRouter( | ||
batchStabilityPoolResponseUnparsed, | ||
)).toStrictEqual(stabilityPoolInfoParsed); | ||
}); | ||
|
||
test('it can call stability pool info service', async () => { | ||
const input = { | ||
queryRouterContractAddress: 'QUERY_ROUTER_CONTRACT_ADDRESS', | ||
queryRouterCodeHash: 'QUERY_ROUTER_CODE_HASH', | ||
lcdEndpoint: 'LCD_ENDPOINT', | ||
chainId: 'CHAIN_ID', | ||
stabilityPoolContractAddress: 'STABILITY_POOL_CONTRACT_ADDRESS', | ||
stabilityPoolCodeHash: 'STABILITY_POOL_CODE_HASH', | ||
minBlockHeightValidationOptions: 'BLOCK_HEIGHT_VALIDATION_OPTIONS' as unknown as MinBlockHeightValidationOptions, | ||
}; | ||
// observables function | ||
batchQuery$.mockReturnValueOnce(of(batchStabilityPoolResponseUnparsed)); | ||
let output; | ||
queryStabilityPoolInfo$(input).subscribe({ | ||
next: (response) => { | ||
output = response; | ||
}, | ||
}); | ||
|
||
const batchQueryInput = { | ||
contractAddress: input.queryRouterContractAddress, | ||
codeHash: input.queryRouterCodeHash, | ||
lcdEndpoint: input.lcdEndpoint, | ||
chainId: input.chainId, | ||
queries: [{ | ||
id: input.stabilityPoolContractAddress, | ||
contract: { | ||
address: input.stabilityPoolContractAddress, | ||
codeHash: input.stabilityPoolCodeHash, | ||
}, | ||
queryMsg: 'GET_STABILITY_POOL_MSG', | ||
}], // array of length 1 for single query | ||
minBlockHeightValidationOptions: input.minBlockHeightValidationOptions, | ||
}; | ||
expect(batchQuery$).toHaveBeenCalledWith(batchQueryInput); | ||
|
||
expect(output).toStrictEqual(stabilityPoolInfoParsed); | ||
|
||
// async/await function | ||
batchQuery$.mockReturnValueOnce(of(batchStabilityPoolResponseUnparsed)); | ||
const response = await queryStabilityPoolInfo(input); | ||
expect(batchQuery$).toHaveBeenCalledWith(batchQueryInput); | ||
expect(response).toStrictEqual(stabilityPoolInfoParsed); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import { | ||
MinBlockHeightValidationOptions, | ||
BatchQueryParams, | ||
NormalizationFactor, | ||
BatchQueryParsedResponse, | ||
BatchItemResponseStatus, | ||
} from '~/types'; | ||
import { msgGetStabilityPoolInfo } from '~/contracts/definitions'; | ||
import { | ||
map, | ||
first, | ||
firstValueFrom, | ||
} from 'rxjs'; | ||
import { StabilityPoolInfo, StabilityPoolResponse } from '~/types/contracts/lend'; | ||
import { convertCoinFromUDenom } from '~/lib/utils'; | ||
import { batchQuery$ } from '../batchQuery'; | ||
|
||
/** | ||
* parses the direct contract query into the data model | ||
*/ | ||
const parseStabilityPoolResponse = ( | ||
response: StabilityPoolResponse, | ||
blockHeight: number, | ||
): StabilityPoolInfo => ({ | ||
silkDeposited: convertCoinFromUDenom( | ||
response.pool_info.total_silk_deposited, | ||
NormalizationFactor.LEND, | ||
).toString(), | ||
bondAmount: convertCoinFromUDenom( | ||
response.pool_info.total_bond_amount, | ||
NormalizationFactor.LEND, | ||
).toString(), | ||
blockHeight, | ||
}); | ||
|
||
/** | ||
* parses the query router response into the data model | ||
*/ | ||
function parseStabilityPoolResponseFromQueryRouter( | ||
batchQueryResponse: BatchQueryParsedResponse, | ||
): StabilityPoolInfo { | ||
const responseCount = batchQueryResponse.length; | ||
if (responseCount !== 1) { | ||
throw new Error(`${responseCount} responses found, one response is expected`); | ||
} | ||
|
||
const response = batchQueryResponse[0]; | ||
|
||
// handle error state | ||
if (response.status === BatchItemResponseStatus.ERROR) { | ||
throw new Error(response.response); | ||
} | ||
|
||
return { | ||
...parseStabilityPoolResponse( | ||
response.response, | ||
response.blockHeight, | ||
), | ||
}; | ||
} | ||
|
||
/** | ||
* query the stability pool info | ||
*/ | ||
function queryStabilityPoolInfo$({ | ||
queryRouterContractAddress, | ||
queryRouterCodeHash, | ||
lcdEndpoint, | ||
chainId, | ||
stabilityPoolContractAddress, | ||
stabilityPoolCodeHash, | ||
minBlockHeightValidationOptions, | ||
}:{ | ||
queryRouterContractAddress: string, | ||
queryRouterCodeHash?: string, | ||
lcdEndpoint?: string, | ||
chainId?: string, | ||
stabilityPoolContractAddress: string, | ||
stabilityPoolCodeHash: string, | ||
minBlockHeightValidationOptions?: MinBlockHeightValidationOptions, | ||
}) { | ||
const query:BatchQueryParams = { | ||
id: stabilityPoolContractAddress, | ||
contract: { | ||
address: stabilityPoolContractAddress, | ||
codeHash: stabilityPoolCodeHash, | ||
}, | ||
queryMsg: msgGetStabilityPoolInfo(), | ||
}; | ||
|
||
return batchQuery$({ | ||
contractAddress: queryRouterContractAddress, | ||
codeHash: queryRouterCodeHash, | ||
lcdEndpoint, | ||
chainId, | ||
queries: [query], // array of length 1 for single query | ||
minBlockHeightValidationOptions, | ||
}).pipe( | ||
map(parseStabilityPoolResponseFromQueryRouter), | ||
first(), | ||
); | ||
} | ||
|
||
/** | ||
* query the stability pool info | ||
*/ | ||
function queryStabilityPoolInfo({ | ||
queryRouterContractAddress, | ||
queryRouterCodeHash, | ||
lcdEndpoint, | ||
chainId, | ||
stabilityPoolContractAddress, | ||
stabilityPoolCodeHash, | ||
minBlockHeightValidationOptions, | ||
}:{ | ||
queryRouterContractAddress: string, | ||
queryRouterCodeHash?: string, | ||
lcdEndpoint?: string, | ||
chainId?: string, | ||
stabilityPoolContractAddress: string, | ||
stabilityPoolCodeHash: string, | ||
minBlockHeightValidationOptions?: MinBlockHeightValidationOptions, | ||
}) { | ||
return firstValueFrom(queryStabilityPoolInfo$({ | ||
queryRouterContractAddress, | ||
queryRouterCodeHash, | ||
lcdEndpoint, | ||
chainId, | ||
stabilityPoolContractAddress, | ||
stabilityPoolCodeHash, | ||
minBlockHeightValidationOptions, | ||
})); | ||
} | ||
|
||
export { | ||
queryStabilityPoolInfo, | ||
queryStabilityPoolInfo$, | ||
parseStabilityPoolResponse, | ||
parseStabilityPoolResponseFromQueryRouter, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.