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

test: oracle price services, and general code cleaning #7

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 6 additions & 3 deletions src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@ import { ClientData } from '~/types/client';

/**
* Create and returns Secret Network client
* @param walletAccount not required for making public queries
* @param lcdEndpoint LCD endpoint to make queries to
* @param chainId chainID string from the config of the chain
* @param walletAccount wallet account data - not required for making public queries
*/
const getSecretNetworkClient$ = ({
walletAccount,
lcdEndpoint,
chainId,
walletAccount,
}:{
walletAccount?: WalletAccount

lcdEndpoint: string,
chainId: string,
walletAccount?: WalletAccount
}): Observable<ClientData> => createFetchClient(defer(
() => {
if (walletAccount) {
Expand Down
94 changes: 66 additions & 28 deletions src/contracts/services/oracle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@ import {
import {
parsePriceFromContract,
parsePricesFromContract,
queryPrice$,
queryPrices$,
} from '~/contracts/services/oracle';
import priceResponse from '~/test/mocks/oracle/priceResponse.json';
import pricesResponse from '~/test/mocks/oracle/pricesResponse.json';
import BigNumber from 'bignumber.js';
import { of } from 'rxjs';
import {
priceParsed,
pricesParsed,
} from '~/test/mocks/oracle/pricesParsed';

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

beforeAll(() => {
vi.mock('~/contracts/definitions/oracle', () => ({
Expand All @@ -21,15 +28,11 @@ beforeAll(() => {
}));

vi.mock('~/client/index', () => ({
getActiveQueryClient$: vi.fn(() => of('CLIENT')),
getActiveQueryClient$: vi.fn(() => of({ client: 'CLIENT' })),
}));

vi.mock('~/client/services/clientServices', () => ({
sendSecretClientContractQuery$: vi.fn(() => of()),
}));

vi.mock('~/client/services/clientServices', () => ({
sendSecretClientContractQuery$: vi.fn(() => of()),
sendSecretClientContractQuery$,
}));
});

Expand All @@ -40,30 +43,65 @@ afterAll(() => {
test('it can parse the price response', () => {
expect(parsePriceFromContract(
priceResponse,
)).toStrictEqual({
oracleKey: 'BTC',
rate: BigNumber('27917.2071556'),
lastUpdatedBase: 1696644063,
lastUpdatedQuote: 18446744073709552000,
});
)).toStrictEqual(priceParsed);
});

test('it can parse the prices response', () => {
const parsedOutput = {
BTC: {
oracleKey: 'BTC',
rate: BigNumber('27917.2071556'),
lastUpdatedBase: 1696644063,
lastUpdatedQuote: 18446744073709552000,
},
ETH: {
oracleKey: 'ETH',
rate: BigNumber('1644.0836829'),
lastUpdatedBase: 1696644063,
lastUpdatedQuote: 18446744073709552000,
},
};
expect(parsePricesFromContract(
pricesResponse,
)).toStrictEqual(parsedOutput);
)).toStrictEqual(pricesParsed);
});

test('it can send the query single price service', () => {
sendSecretClientContractQuery$.mockReturnValue(of(priceResponse));

const input = {
contractAddress: 'CONTRACT_ADDRESS',
codeHash: 'CODE_HASH',
oracleKey: 'ORACLE_KEY',
lcdEndpoint: 'LCD_ENDPOINT',
chainId: 'CHAIN_ID',
};
let output;
queryPrice$(input).subscribe({
next: (response) => {
output = response;
},
});

expect(sendSecretClientContractQuery$).toHaveBeenCalledWith({
queryMsg: 'MSG_QUERY_ORACLE_PRICE',
client: 'CLIENT',
contractAddress: input.contractAddress,
codeHash: input.codeHash,
});

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

test('it can send the query multiple prices service', () => {
sendSecretClientContractQuery$.mockReturnValue(of(pricesResponse));

const input = {
contractAddress: 'CONTRACT_ADDRESS',
codeHash: 'CODE_HASH',
oracleKeys: ['ORACLE_KEY'],
lcdEndpoint: 'LCD_ENDPOINT',
chainId: 'CHAIN_ID',
};
let output;
queryPrices$(input).subscribe({
next: (response) => {
output = response;
},
});

expect(sendSecretClientContractQuery$).toHaveBeenCalledWith({
queryMsg: 'MSG_QUERY_ORACLE_PRICES',
client: 'CLIENT',
contractAddress: input.contractAddress,
codeHash: input.codeHash,
});

expect(output).toStrictEqual(pricesParsed);
});
6 changes: 4 additions & 2 deletions src/contracts/services/oracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ import { getActiveQueryClient$ } from '~/client';
import { convertCoinFromUDenom } from '~/lib/utils';
import { msgQueryOraclePrice, msgQueryOraclePrices } from '~/contracts/definitions/oracle';

const ORACLE_NORMALIZATION_FACTOR = 18;

/**
* Parses the contract price query into the app data model
*/
const parsePriceFromContract = (response: OraclePriceResponse): ParsedOraclePriceResponse => ({
oracleKey: response.key,
rate: convertCoinFromUDenom(response.data.rate, 18),
rate: convertCoinFromUDenom(response.data.rate, ORACLE_NORMALIZATION_FACTOR),
lastUpdatedBase: response.data.last_updated_base,
lastUpdatedQuote: response.data.last_updated_quote,
});
Expand All @@ -32,7 +34,7 @@ function parsePricesFromContract(pricesResponse: OraclePricesResponse) {
...prev,
[curr.key]: {
oracleKey: curr.key,
rate: convertCoinFromUDenom(curr.data.rate, 18),
rate: convertCoinFromUDenom(curr.data.rate, ORACLE_NORMALIZATION_FACTOR),
lastUpdatedBase: curr.data.last_updated_base,
lastUpdatedQuote: curr.data.last_updated_quote,
} as ParsedOraclePriceResponse,
Expand Down
28 changes: 28 additions & 0 deletions src/test/mocks/oracle/pricesParsed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import BigNumber from 'bignumber.js';

const priceParsed = {
oracleKey: 'BTC',
rate: BigNumber('27917.2071556'),
lastUpdatedBase: 1696644063,
lastUpdatedQuote: 18446744073709552000,
};

const pricesParsed = {
BTC: {
oracleKey: 'BTC',
rate: BigNumber('27917.2071556'),
lastUpdatedBase: 1696644063,
lastUpdatedQuote: 18446744073709552000,
},
ETH: {
oracleKey: 'ETH',
rate: BigNumber('1644.0836829'),
lastUpdatedBase: 1696644063,
lastUpdatedQuote: 18446744073709552000,
},
};

export {
priceParsed,
pricesParsed,
};
Loading