Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: silk basket query #139

Merged
merged 3 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/spicy-boats-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@shadeprotocol/shadejs": minor
---

silk basket query
12 changes: 12 additions & 0 deletions src/contracts/definitions/silkBasket.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {
test,
expect,
} from 'vitest';
import { msgGetSilkBasket } from './silkBasket';

test('it tests the form of the silk basket message', () => {
const output = {
get_index_data: {},
};
expect(msgGetSilkBasket()).toStrictEqual(output);
});
10 changes: 10 additions & 0 deletions src/contracts/definitions/silkBasket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* message for the getting the silk basket data
*/
const msgGetSilkBasket = () => ({
get_index_data: {},
});

export {
msgGetSilkBasket,
};
257 changes: 257 additions & 0 deletions src/contracts/services/silkBasket.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
import {
test,
expect,
vi,
beforeAll,
afterEach,
} from 'vitest';
import silkBasketResponse from '~/test/mocks/silkBasket/silkBasketResponse.json';
import { of } from 'rxjs';
import { MinBlockHeightValidationOptions } from '~/types';
import {
silkBasketParsed,
silkBasketParsedWithStatus,
} from '~/test/mocks/silkBasket/silkBasketParsed';
import { batchSilkBasketOraclePricesResponse } from '~/test/mocks/silkBasket/batchSilkBasketOraclePricesResponse';
import { batchSilkBasketWithMissingOraclePricesResponse } from '~/test/mocks/silkBasket/batchSilkBasketWithMissingOraclePricesResponse';
import { silkBasketWithMissingPricesParsed } from '~/test/mocks/silkBasket/silkBasketWithMissingPricesParsed';
import { batchSilkBasketWithOracleErrorResponse } from '~/test/mocks/silkBasket/batchSilkBasketWithOracleErrorResponse';
import { silkBasketWithOracleErrorParsed } from '~/test/mocks/silkBasket/silkBasketWithOracleErrorParsed';
import { batchSilkBasketWithBasketErrorResponse } from '~/test/mocks/silkBasket/batchSilkBasketWithBasketErrorResponse';
import { SilkBasketBatchResponseItem } from '~/types/contracts/silkBasket/service';
import {
parseSilkBasketAndPricesResponse,
parseSilkBasketAndPricesResponseFromQueryRouter,
querySilkBasket$,
querySilkBasket,
} from './silkBasket';

const batchQuery$ = vi.hoisted(() => vi.fn());

beforeAll(() => {
vi.mock('~/contracts/definitions/silkBasket', () => ({
msgGetSilkBasket: vi.fn(() => 'MSG_GET_SILK_BASKET'),
}));

vi.mock('~/contracts/services/batchQuery', () => ({
batchQuery$,
}));
});

afterEach(() => {
vi.clearAllMocks();
});

test('it can parse the silk basket response', () => {
expect(parseSilkBasketAndPricesResponse({
silkBasketResponse,
batchBasketPricesResponse: batchSilkBasketOraclePricesResponse[1],
silkBasketResponseBlockHeight: 1,
basketPricesResponseBlockHeight: 1,
})).toStrictEqual(silkBasketParsedWithStatus);
});

test('it can parse the silk basket response via the query router', () => {
expect(parseSilkBasketAndPricesResponseFromQueryRouter(
batchSilkBasketOraclePricesResponse,
)).toStrictEqual(silkBasketParsedWithStatus);
});

test('it can parse the silk basket response when prices are missing', () => {
expect(parseSilkBasketAndPricesResponseFromQueryRouter(
batchSilkBasketWithMissingOraclePricesResponse,
)).toStrictEqual(silkBasketWithMissingPricesParsed);
});

test('it can parse the silk basket response when the price query has an error', () => {
expect(parseSilkBasketAndPricesResponseFromQueryRouter(
batchSilkBasketWithOracleErrorResponse,
)).toStrictEqual(silkBasketWithOracleErrorParsed);
});

test('it can detect and throw error for silk basket info error', () => {
expect(() => parseSilkBasketAndPricesResponseFromQueryRouter(
batchSilkBasketWithBasketErrorResponse,
)).toThrowError('BASKET_ERROR_MESSAGE');
});
test('it can call silk basket info service', async () => {
const input = {
queryRouterContractAddress: 'QUERY_ROUTER_CONTRACT_ADDRESS',
queryRouterCodeHash: 'QUERY_ROUTER_CODE_HASH',
lcdEndpoint: 'LCD_ENDPOINT',
chainId: 'CHAIN_ID',
oracleContractAddress: 'ORACLE_CONTRACT_ADDRESS',
oracleCodeHash: 'ORACLE_CODE_HASH',
silkBasketExpectedOracleKeys: ['KEY_1, KEY_2'],
silkIndexOracleContractAddress: 'SILK_INDEX_ORACLE_CONTRACT_ADDRESS',
silkIndexOracleCodeHash: 'SILK_INDEX_ORACLE_CODE_HASH',
minBlockHeightValidationOptions: 'BLOCK_HEIGHT_VALIDATION_OPTIONS' as unknown as MinBlockHeightValidationOptions,
};
// observables function
batchQuery$.mockReturnValueOnce(of(batchSilkBasketOraclePricesResponse));

let output;
querySilkBasket$(input).subscribe({
next: (response) => {
output = response;
},
});

const batchQueryInput = {
contractAddress: input.queryRouterContractAddress,
codeHash: input.queryRouterCodeHash,
lcdEndpoint: input.lcdEndpoint,
chainId: input.chainId,
queries: [{
id: SilkBasketBatchResponseItem.BASKET,
contract: {
address: input.silkIndexOracleContractAddress,
codeHash: input.silkIndexOracleCodeHash,
},
queryMsg: 'MSG_GET_SILK_BASKET',
},
{
id: SilkBasketBatchResponseItem.PRICES,
contract: {
address: input.oracleContractAddress,
codeHash: input.oracleCodeHash,
},
queryMsg: {
get_prices: {
keys: input.silkBasketExpectedOracleKeys,
},
},
},
],
minBlockHeightValidationOptions: input.minBlockHeightValidationOptions,
};
expect(batchQuery$).toHaveBeenCalledWith(batchQueryInput);

expect(output).toStrictEqual(silkBasketParsed);

// async/await function
batchQuery$.mockReturnValueOnce(of(batchSilkBasketOraclePricesResponse));
const response = await querySilkBasket(input);
expect(batchQuery$).toHaveBeenCalledWith(batchQueryInput);
expect(response).toStrictEqual(silkBasketParsed);
});

test('it can handle missing prices with a query retry', async () => {
const input = {
queryRouterContractAddress: 'QUERY_ROUTER_CONTRACT_ADDRESS',
queryRouterCodeHash: 'QUERY_ROUTER_CODE_HASH',
lcdEndpoint: 'LCD_ENDPOINT',
chainId: 'CHAIN_ID',
oracleContractAddress: 'ORACLE_CONTRACT_ADDRESS',
oracleCodeHash: 'ORACLE_CODE_HASH',
silkBasketExpectedOracleKeys: ['KEY_1, KEY_2'],
silkIndexOracleContractAddress: 'SILK_INDEX_ORACLE_CONTRACT_ADDRESS',
silkIndexOracleCodeHash: 'SILK_INDEX_ORACLE_CODE_HASH',
minBlockHeightValidationOptions: 'BLOCK_HEIGHT_VALIDATION_OPTIONS' as unknown as MinBlockHeightValidationOptions,
};
// observables function
batchQuery$
.mockReturnValueOnce(of(batchSilkBasketWithMissingOraclePricesResponse))
.mockReturnValueOnce(of(batchSilkBasketOraclePricesResponse));

let output;
querySilkBasket$(input).subscribe({
next: (response) => {
output = response;
},
});

const firstPriceQueryKeys = ['KEY_1, KEY_2'];
const retryPriceQueryKeys = ['CAD', 'USD', 'JPY', 'BTC', 'EUR', 'XAU'];

const batchQueryInput1 = {
contractAddress: input.queryRouterContractAddress,
codeHash: input.queryRouterCodeHash,
lcdEndpoint: input.lcdEndpoint,
chainId: input.chainId,
queries: [
{
id: SilkBasketBatchResponseItem.BASKET,
contract: {
address: input.silkIndexOracleContractAddress,
codeHash: input.silkIndexOracleCodeHash,
},
queryMsg: 'MSG_GET_SILK_BASKET',
},
{
id: SilkBasketBatchResponseItem.PRICES,
contract: {
address: input.oracleContractAddress,
codeHash: input.oracleCodeHash,
},
queryMsg: {
get_prices: {
keys: firstPriceQueryKeys,
},
},
},
],
minBlockHeightValidationOptions: input.minBlockHeightValidationOptions,
};

const batchQueryInput2 = {
contractAddress: input.queryRouterContractAddress,
codeHash: input.queryRouterCodeHash,
lcdEndpoint: input.lcdEndpoint,
chainId: input.chainId,
queries: [
{
id: SilkBasketBatchResponseItem.BASKET,
contract: {
address: input.silkIndexOracleContractAddress,
codeHash: input.silkIndexOracleCodeHash,
},
queryMsg: 'MSG_GET_SILK_BASKET',
},
{
id: SilkBasketBatchResponseItem.PRICES,
contract: {
address: input.oracleContractAddress,
codeHash: input.oracleCodeHash,
},
queryMsg: {
get_prices: {
keys: retryPriceQueryKeys,
},
},
},
],
minBlockHeightValidationOptions: input.minBlockHeightValidationOptions,
};

expect(batchQuery$).toHaveBeenNthCalledWith(1, batchQueryInput1);
expect(batchQuery$).toHaveBeenNthCalledWith(2, batchQueryInput2);

expect(output).toStrictEqual(silkBasketParsed);
});

test('it can properly catch the silk basket error', async () => {
const input = {
queryRouterContractAddress: 'QUERY_ROUTER_CONTRACT_ADDRESS',
queryRouterCodeHash: 'QUERY_ROUTER_CODE_HASH',
lcdEndpoint: 'LCD_ENDPOINT',
chainId: 'CHAIN_ID',
oracleContractAddress: 'ORACLE_CONTRACT_ADDRESS',
oracleCodeHash: 'ORACLE_CODE_HASH',
silkBasketExpectedOracleKeys: ['KEY_1, KEY_2'],
silkIndexOracleContractAddress: 'SILK_INDEX_ORACLE_CONTRACT_ADDRESS',
silkIndexOracleCodeHash: 'SILK_INDEX_ORACLE_CODE_HASH',
minBlockHeightValidationOptions: 'BLOCK_HEIGHT_VALIDATION_OPTIONS' as unknown as MinBlockHeightValidationOptions,
};
// observables function
batchQuery$.mockReturnValueOnce(of(batchSilkBasketWithBasketErrorResponse));

let error;
querySilkBasket$(input).subscribe({
error: (err) => {
error = err.message;
},
});

expect(error).toStrictEqual('BASKET_ERROR_MESSAGE');
});
Loading
Loading