Skip to content

Commit

Permalink
feat: add tests for new utils
Browse files Browse the repository at this point in the history
  • Loading branch information
marslavish committed Mar 18, 2024
1 parent b922c9d commit c4895a4
Show file tree
Hide file tree
Showing 4 changed files with 289 additions and 124 deletions.
124 changes: 0 additions & 124 deletions packages/utils/__tests__/asset-list-util.test.js

This file was deleted.

145 changes: 145 additions & 0 deletions packages/utils/__tests__/assets.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import assets from '../../../__fixtures__/assets.json';
import {
getAssetByDenom,
getDenomBySymbol,
getCoinGeckoIdByDenom,
getDenomByCoinGeckoId,
getExponentByDenom,
getSymbolByDenom,
getAssetBySymbol,
getChainLogo,
getChainNameByDenom,
getExponentBySymbol,
getNativeTokenByChainName,
getTokenLogoByDenom,
getTokenNameByDenom
} from '../src/assets';

describe('tests for assets utils', () => {
it('getAssetByDenom', () => {
expect(() => getAssetByDenom(assets, 'uosmo')).toThrow();
const asset = getAssetByDenom(assets, 'uosmo', 'osmosis');
expect(asset.base).toEqual('uosmo');
});

it('getAssetBySymbol', () => {
expect(getAssetBySymbol([], 'ATOM')).toBeUndefined();
const asset1 = getAssetBySymbol(assets, 'DVPN');
expect(asset1.base).toEqual('udvpn');
const asset2 = getAssetBySymbol(assets, 'SOMM');
expect(asset2.base).toEqual('usomm');
});

it('getChainLogo', () => {
const logo1 = getChainLogo(assets, 'comdex');
expect(logo1).toEqual(
'https://raw.githubusercontent.com/cosmos/chain-registry/master/comdex/images/cmdx.png'
);
const logo2 = getChainLogo(assets, 'cosmoshub');
expect(logo2).toEqual(
'https://raw.githubusercontent.com/cosmos/chain-registry/master/cosmoshub/images/atom.png'
);
});

it('getChainNameByDenom', () => {
const chain1 = getChainNameByDenom(assets, 'ucmdx');
expect(chain1).toEqual('comdex');
expect(() => getChainNameByDenom(assets, 'uatom')).toThrow();
});

it('getDenomByCoinGeckoId', () => {
const denom1 = getDenomByCoinGeckoId(assets, 'jackal');
expect(denom1).toEqual('ujkl');
const denom2 = getDenomByCoinGeckoId(assets, 'stargaze', 'stargaze');
expect(denom2).toEqual('ustars');
});

it('getSymbolByDenom', () => {
const denom1 = getSymbolByDenom(assets, 'swth');
expect(denom1).toEqual('SWTH');
const denom2 = getSymbolByDenom(assets, 'uusdc', 'axelar');
expect(denom2).toEqual('USDC');
});

it('getDenomBySymbol', () => {
const denom1 = getDenomBySymbol(assets, 'OCTA');
expect(denom1).toEqual('uocta');
const denom2 = getDenomBySymbol(assets, 'NOM', 'nomic');
expect(denom2).toEqual('unom');
});

it('getExponentByDenom', () => {
const exponent = getExponentByDenom(assets, 'uosmo', 'osmosis');
expect(exponent).toEqual(6);
});

it('getExponentBySymbol', () => {
const exponent = getExponentBySymbol(assets, 'OCTA');
expect(exponent).toEqual(6);
expect(() => getExponentBySymbol(assets, 'ATOM')).toThrow();
const exponent2 = getExponentBySymbol(assets, 'ATOM', 'cosmoshub');
expect(exponent2).toEqual(6);
});

it('getNativeTokenByChainName', () => {
const nativeToken = getNativeTokenByChainName(assets, 'osmosis');
expect(nativeToken.base).toEqual('uosmo');
const nativeToken2 = getNativeTokenByChainName(assets, 'juno');
expect(nativeToken2.base).toEqual('ujuno');
});

it('getTokenLogoByDenom', () => {
const logo = getTokenLogoByDenom(assets, 'uosmo', 'osmosis');
expect(logo).toEqual(
'https://raw.githubusercontent.com/cosmos/chain-registry/master/osmosis/images/osmo.png'
);
expect(() => getTokenLogoByDenom(assets, 'uatom')).toThrow();
const logo2 = getTokenLogoByDenom(assets, 'uatom', 'cosmoshub');
expect(logo2).toEqual(
'https://raw.githubusercontent.com/cosmos/chain-registry/master/cosmoshub/images/atom.png'
);
const logo3 = getTokenLogoByDenom(assets, 'ubcre');
expect(logo3).toEqual(
'https://raw.githubusercontent.com/cosmos/chain-registry/master/crescent/images/bcre.png'
);
});

it('getTokenNameByDenom', () => {
expect(() => getTokenNameByDenom(assets, 'uatom')).toThrow();
const name = getTokenNameByDenom(assets, 'uatom', 'cosmoshub');
expect(name).toEqual('Cosmos');
const name2 = getTokenNameByDenom(assets, 'ubcre');
expect(name2).toEqual('Bonded Crescent');
});
});

describe('getCoinGeckoIdByDenom', () => {
it('uosmo coingecko id', () => {
const id = getCoinGeckoIdByDenom(assets, 'uosmo');
expect(id).toEqual('osmosis');
});

it('ujkl coingecko id on testnet', () => {
const id = getCoinGeckoIdByDenom(assets, 'ujkl', {
allowTestnet: true,
excludedChainNames: ['jackal']
});
expect(id).toEqual('jackal');
});

it('uluna coingecko id on terra2', () => {
const id = getCoinGeckoIdByDenom(assets, 'uluna', {
excludedChainNames: ['terra']
});
expect(id).toEqual('terra-luna-2');
});

it('uusdc coingecko id without traces', () => {
const id = getCoinGeckoIdByDenom(assets, 'uusdcc', {
customAssetFilter(asset) {
return !asset.traces;
}
});
expect(id).toBeUndefined();
});
});
72 changes: 72 additions & 0 deletions packages/utils/__tests__/calc.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import assets from '../../../__fixtures__/assets.json';
import {
roundDown,
mapCoinGeckoPricesToDenoms,
convertBaseUnitToDisplayUnit,
convertBaseUnitToDollarValue,
convertDollarValueToBaseUnit,
convertDisplayUnitToBaseUnit
} from '../src/calc';

const osmosisAssets = assets.filter(
({ chain_name }) => chain_name === 'osmosis'
);

const coinGeckoPrices = {
osmosis: {
usd: 0.498124
},
ion: {
usd: 0.321456
}
};

describe('tests for calc utils', () => {
it('roundDown', () => {
const re1 = roundDown(1.12);
expect(re1).toEqual('1');
const re2 = roundDown(1.67);
expect(re2).toEqual('1');
});

it('mapCoinGeckoPricesToDenoms', () => {
const priceMap = mapCoinGeckoPricesToDenoms(osmosisAssets, coinGeckoPrices);
expect(priceMap.uosmo).toEqual(0.498124);
expect(priceMap.uion).toEqual(0.321456);
});

it('convertBaseUnitToDollarValue', () => {
const priceMap = mapCoinGeckoPricesToDenoms(osmosisAssets, coinGeckoPrices);
const value = convertBaseUnitToDollarValue(
osmosisAssets,
priceMap,
'OSMO',
5000000
);
expect(value).toEqual('2.49062');
});

it('convertDollarValueToBaseUnit', () => {
const priceMap = mapCoinGeckoPricesToDenoms(osmosisAssets, coinGeckoPrices);
const value = convertDollarValueToBaseUnit(
osmosisAssets,
priceMap,
'OSMO',
100
);
expect(value).toEqual('200753226');
});

it('convertBaseUnitToDisplayUnit', () => {
const value = convertBaseUnitToDisplayUnit(osmosisAssets, 'OSMO', 329823);
expect(value).toEqual('0.329823');
expect(() =>
convertBaseUnitToDisplayUnit(osmosisAssets, 'ATOM', 100)
).toThrow();
});

it('convertDisplayUnitToBaseUnit', () => {
const value = convertDisplayUnitToBaseUnit(osmosisAssets, 'OSMO', 0.00009944);
expect(value).toEqual('99');
});
});
Loading

0 comments on commit c4895a4

Please sign in to comment.