From 88a00cb219a9a903f48ae588a1654d059f208d15 Mon Sep 17 00:00:00 2001 From: Ivan Violentov Date: Thu, 7 Nov 2019 17:41:39 +0300 Subject: [PATCH 1/5] Move addETHAsset & mergeDigitalAssets to utils scope --- src/store/sagas/digitalAssets/core.js | 157 ++++-------------- src/utils/digitalAssets/addETHAsset.js | 35 ++++ src/utils/digitalAssets/index.js | 3 + src/utils/digitalAssets/mergeDigitalAssets.js | 86 ++++++++++ 4 files changed, 156 insertions(+), 125 deletions(-) create mode 100644 src/utils/digitalAssets/addETHAsset.js create mode 100644 src/utils/digitalAssets/mergeDigitalAssets.js diff --git a/src/store/sagas/digitalAssets/core.js b/src/store/sagas/digitalAssets/core.js index 2de7add09..b7ec5c4cc 100644 --- a/src/store/sagas/digitalAssets/core.js +++ b/src/store/sagas/digitalAssets/core.js @@ -1,6 +1,4 @@ -// @flow - -import { mapKeys } from 'lodash-es' +// @flow strict import { delay } from 'redux-saga' @@ -13,136 +11,32 @@ import { } from 'redux-saga/effects' import { - ethereum, - getAssetsMainnet, -} from 'data/assets' + addETHAsset, + mergeDigitalAssets, +} from 'utils/digitalAssets' import { selectDigitalAsset, selectDigitalAssetsItems, } from 'store/selectors/digitalAssets' -import { getAddressChecksum } from 'utils/address' import * as blocks from 'store/modules/blocks' import * as ticker from 'store/modules/ticker' import * as digitalAssets from 'store/modules/digitalAssets' -function mergeItems( - items: DigitalAssets, - assetsMainnet: DigitalAsset[], -): DigitalAssets { - // FIXME: use current network id instead - const defaultItems: DigitalAssets = assetsMainnet.reduce(( - reduceResult: DigitalAssets, - item: DigitalAsset, - ): DigitalAssets => { - const { address }: DigitalAssetBlockchainParams = item.blockchainParams - - if (!address) { - return reduceResult - } - - const addressWithChecksum: AssetAddress = getAddressChecksum(address) - - reduceResult[addressWithChecksum] = { - ...item, - isActive: false, - blockchainParams: { - ...item.blockchainParams, - address: addressWithChecksum, - }, - } - - return reduceResult - }, {}) - - const itemsWithChecksum: DigitalAssets = mapKeys( - items, - (item, address) => getAddressChecksum(address), - ) - - const existingItems: DigitalAssets = Object.keys(itemsWithChecksum).reduce(( - reduceResult: DigitalAssets, - addressWithChecksum: AssetAddress, - ): DigitalAssets => { - const foundExistingItem: ?DigitalAsset = items[addressWithChecksum] - const foundDefaultItem: ?DigitalAsset = defaultItems[addressWithChecksum] - - // Ignore nonexistent items - if (!foundExistingItem) { - return reduceResult - } - - // Update from default item if it exists - if (foundDefaultItem) { - reduceResult[addressWithChecksum] = { - ...foundExistingItem, - ...foundDefaultItem, - isCustom: false, - isActive: !!foundExistingItem.isActive, - } - - return reduceResult - } - - // Remove (ignore) inactive items that are not in default list and not marked as custom - if (!foundExistingItem.isActive && !foundExistingItem.isCustom) { - return reduceResult - } - - // Active items that are not in default list are converted to custom - reduceResult[addressWithChecksum] = { - ...foundExistingItem, - isCustom: true, - } - - return reduceResult - }, {}) - - return { - ...defaultItems, - ...existingItems, - } -} - -function addETHAsset( - items: DigitalAssets, - assetsMainnet: DigitalAsset[], -): DigitalAssets { - const defaultETHAddress: AssetAddress = ethereum.blockchainParams.address - - const foundETHAsset: ?DigitalAsset = assetsMainnet - .find((item: DigitalAsset) => (item.blockchainParams.type === 'ethereum')) - - if (!foundETHAsset) { - return { - ...items, - [defaultETHAddress]: ethereum, - } - } - - const { blockchainParams }: DigitalAsset = foundETHAsset - - return { - ...items, - [defaultETHAddress]: { - ...foundETHAsset, - blockchainParams: { - ...blockchainParams, - address: defaultETHAddress, - }, - isActive: true, - }, - } -} - function* init(): Saga { - const existingItems: ExtractReturn = + const items: ExtractReturn = yield select(selectDigitalAssetsItems) - const assetsMainnet: DigitalAsset[] = yield call(getAssetsMainnet) - const mergedItems: DigitalAssets = mergeItems(existingItems, assetsMainnet) - const itemsWithETH: DigitalAssets = addETHAsset(mergedItems, assetsMainnet) + const mergedItems: DigitalAssets = yield call( + mergeDigitalAssets, + items, + ) + + const itemsWithETH: DigitalAssets = yield call( + addETHAsset, + mergedItems, + ) yield put(digitalAssets.setInitialItems(itemsWithETH)) } @@ -158,8 +52,10 @@ function* deleteCustomAsset( ): Saga { const { assetAddress } = action.payload - const foundAsset: ExtractReturn = - yield select(selectDigitalAsset, assetAddress) + const foundAsset: ExtractReturn = yield select( + selectDigitalAsset, + assetAddress, + ) if (!(foundAsset && foundAsset.isCustom)) { return @@ -169,7 +65,18 @@ function* deleteCustomAsset( } export function* digitalAssetsRootSaga(): Saga { - yield takeEvery(digitalAssets.INIT, init) - yield takeLatest(digitalAssets.SET_ASSET_IS_ACTIVE, setAssetIsActive) - yield takeEvery(digitalAssets.DELETE_CUSTOM_ASSET, deleteCustomAsset) + yield takeEvery( + digitalAssets.INIT, + init, + ) + + yield takeLatest( + digitalAssets.SET_ASSET_IS_ACTIVE, + setAssetIsActive, + ) + + yield takeEvery( + digitalAssets.DELETE_CUSTOM_ASSET, + deleteCustomAsset, + ) } diff --git a/src/utils/digitalAssets/addETHAsset.js b/src/utils/digitalAssets/addETHAsset.js new file mode 100644 index 000000000..ed501c090 --- /dev/null +++ b/src/utils/digitalAssets/addETHAsset.js @@ -0,0 +1,35 @@ +// @flow strict + +import { + ethereum, + getAssetsMainnet, +} from 'data/assets' + +export default async function addETHAsset(items: DigitalAssets): DigitalAssets { + const itemsMainnet: DigitalAsset[] = await getAssetsMainnet() + const defaultETHAddress: AssetAddress = ethereum.blockchainParams.address + + const foundETHAsset: ?DigitalAsset = itemsMainnet + .find((item: DigitalAsset) => (item.blockchainParams.type === 'ethereum')) + + if (!foundETHAsset) { + return { + ...items, + [defaultETHAddress]: ethereum, + } + } + + const { blockchainParams }: DigitalAsset = foundETHAsset + + return { + ...items, + [defaultETHAddress]: { + ...foundETHAsset, + blockchainParams: { + ...blockchainParams, + address: defaultETHAddress, + }, + isActive: true, + }, + } +} diff --git a/src/utils/digitalAssets/index.js b/src/utils/digitalAssets/index.js index fb5967539..1fcbd14fb 100644 --- a/src/utils/digitalAssets/index.js +++ b/src/utils/digitalAssets/index.js @@ -14,6 +14,9 @@ import compareDigitalAssetsByBalance from './compareDigitalAssetsByBalance' export { sortAssets } from './sortAssets' export { getFiatBalance } from './getFiatBalance' +export { default as addETHAsset } from './addETHAsset' +export { default as mergeDigitalAssets } from './mergeDigitalAssets' + export { checkETH, checkJNT, diff --git a/src/utils/digitalAssets/mergeDigitalAssets.js b/src/utils/digitalAssets/mergeDigitalAssets.js new file mode 100644 index 000000000..e21e720e7 --- /dev/null +++ b/src/utils/digitalAssets/mergeDigitalAssets.js @@ -0,0 +1,86 @@ +// @flow strict + +import { getAssetsMainnet } from 'data/assets' +import { getAddressChecksum } from 'utils/address' + +export default async function mergeDigitalAssets(items: DigitalAssets): DigitalAssets { + const itemsMainnet: DigitalAsset[] = await getAssetsMainnet() + + // FIXME: use current network id instead + const defaultItems: DigitalAssets = itemsMainnet.reduce(( + reduceResult: DigitalAssets, + item: DigitalAsset, + ): DigitalAssets => { + const { address }: DigitalAssetBlockchainParams = item.blockchainParams + + if (!address) { + return reduceResult + } + + const addressWithChecksum: AssetAddress = getAddressChecksum(address) + + reduceResult[addressWithChecksum] = { + ...item, + isActive: false, + blockchainParams: { + ...item.blockchainParams, + address: addressWithChecksum, + }, + } + + return reduceResult + }, {}) + + const itemsWithChecksum: DigitalAssets = Object.keys(items).reduce(( + reduceResult: DigitalAssets, + address: AssetAddress, + ): DigitalAssets => { + const addressWithChecksum: AssetAddress = getAddressChecksum(address) + reduceResult[addressWithChecksum] = items[address] + + return reduceResult + }, {}) + + const existingItems: DigitalAssets = Object.keys(itemsWithChecksum).reduce(( + reduceResult: DigitalAssets, + addressWithChecksum: AssetAddress, + ): DigitalAssets => { + const foundDefaultItem: ?DigitalAsset = defaultItems[addressWithChecksum] + const foundExistingItem: ?DigitalAsset = itemsWithChecksum[addressWithChecksum] + + // Ignore nonexistent items + if (!foundExistingItem) { + return reduceResult + } + + // Update from default item if it exists + if (foundDefaultItem) { + reduceResult[addressWithChecksum] = { + ...foundExistingItem, + ...foundDefaultItem, + isCustom: false, + isActive: !!foundExistingItem.isActive, + } + + return reduceResult + } + + // Remove (ignore) inactive items that are not in default list and not marked as custom + if (!foundExistingItem.isActive && !foundExistingItem.isCustom) { + return reduceResult + } + + // Active items that are not in default list are converted to custom + reduceResult[addressWithChecksum] = { + ...foundExistingItem, + isCustom: true, + } + + return reduceResult + }, {}) + + return { + ...defaultItems, + ...existingItems, + } +} From 45bc2b9da4a54d5e140b340b010aaaa5c1f33b8a Mon Sep 17 00:00:00 2001 From: Ivan Violentov Date: Thu, 7 Nov 2019 17:42:07 +0300 Subject: [PATCH 2/5] Add tests for mergeDigitalAssets --- .../digitalAssets/tests/addETHAsset.test.js | 0 .../tests/mergeDigitalAssets.test.js | 109 ++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 src/utils/digitalAssets/tests/addETHAsset.test.js create mode 100644 src/utils/digitalAssets/tests/mergeDigitalAssets.test.js diff --git a/src/utils/digitalAssets/tests/addETHAsset.test.js b/src/utils/digitalAssets/tests/addETHAsset.test.js new file mode 100644 index 000000000..e69de29bb diff --git a/src/utils/digitalAssets/tests/mergeDigitalAssets.test.js b/src/utils/digitalAssets/tests/mergeDigitalAssets.test.js new file mode 100644 index 000000000..ec1640040 --- /dev/null +++ b/src/utils/digitalAssets/tests/mergeDigitalAssets.test.js @@ -0,0 +1,109 @@ +// @flow strict + +import mergeDigitalAssets from '../mergeDigitalAssets' + +const JNT_SYMBOL: string = 'JNT' +const JNT_NAME: string = 'Jibrel Network Token' +const JNT_ADDRESS: AssetAddress = '0xa5Fd1A791C4dfcaacC963D4F73c6Ae5824149eA7' + +const AZBIT_ADDRESS: AssetAddress = '0xeccab39acb2caf9adba72c1cb92fdc106b993e0b' +const AZBIT_ADDRESS_CS: AssetAddress = '0xEccAB39acB2CAF9adBa72C1CB92FDC106B993E0b' + +const AZBIT_ASSET: DigitalAsset = { + blockchainParams: { + type: 'erc-20', + address: AZBIT_ADDRESS_CS, + decimals: 6, + }, + symbol: 'AZ', + name: 'Azbit', + isActive: true, + isCustom: true, +} + +describe('mergeDigitalAssets', () => { + test('defined', () => { + expect(mergeDigitalAssets).toBeDefined() + }) + + test('check that default items are merged correctly', async () => { + const result: DigitalAssets = await mergeDigitalAssets({}) + const jnt: ?DigitalAsset = result[JNT_ADDRESS] + + if (!jnt) { + throw new Error('JNT asset not found') + } + + expect(jnt).toBeDefined() + expect(jnt.name).toEqual(JNT_NAME) + expect(jnt.symbol).toEqual(JNT_SYMBOL) + expect(jnt.blockchainParams).toBeDefined() + expect(jnt.blockchainParams.address).toEqual(JNT_ADDRESS) + }) + + test('check that existed items are merged correctly', async () => { + const result: DigitalAssets = await mergeDigitalAssets({ + [AZBIT_ADDRESS]: AZBIT_ASSET, + }) + + const azbit: ?DigitalAsset = result[AZBIT_ADDRESS_CS] + + if (!azbit) { + throw new Error('Azbit asset not found') + } + + expect(azbit).toBeDefined() + expect(azbit.name).toEqual(AZBIT_ASSET.name) + expect(azbit.symbol).toEqual(AZBIT_ASSET.symbol) + expect(azbit.blockchainParams).toBeDefined() + expect(azbit.blockchainParams.address).toEqual(AZBIT_ADDRESS_CS) + }) + + test('check that existed non-custom item will be removed', async () => { + const result: DigitalAssets = await mergeDigitalAssets({ + [AZBIT_ADDRESS]: { + ...AZBIT_ASSET, + isActive: false, + isCustom: false, + }, + }) + + const azbit: ?DigitalAsset = result[AZBIT_ADDRESS_CS] + + expect(azbit).toBeUndefined() + }) + + test('check that existed non-custom item will not be removed', async () => { + const result: DigitalAssets = await mergeDigitalAssets({ + [AZBIT_ADDRESS]: { + ...AZBIT_ASSET, + isActive: true, + isCustom: false, + }, + }) + + const azbit: ?DigitalAsset = result[AZBIT_ADDRESS_CS] + + if (!azbit) { + throw new Error('Azbit asset not found') + } + + expect(azbit).toBeDefined() + expect(azbit.isCustom).toBeTruthy() + }) + + test('check that existed custom item will be converted to non-custom', async () => { + const result: DigitalAssets = await mergeDigitalAssets({ + [JNT_ADDRESS]: AZBIT_ASSET, + }) + + const jnt: ?DigitalAsset = result[JNT_ADDRESS] + + if (!jnt) { + throw new Error('JNT asset not found') + } + + expect(jnt).toBeDefined() + expect(jnt.isCustom).toBeFalsy() + }) +}) From f9e13af9c28b60eed7d96ef21c1d530ea926ff66 Mon Sep 17 00:00:00 2001 From: Ivan Violentov Date: Thu, 7 Nov 2019 17:53:32 +0300 Subject: [PATCH 3/5] Add addETHAsset tests --- .../digitalAssets/tests/addETHAsset.test.js | 26 +++++++++++++++++++ .../tests/mergeDigitalAssets.test.js | 10 +++---- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/utils/digitalAssets/tests/addETHAsset.test.js b/src/utils/digitalAssets/tests/addETHAsset.test.js index e69de29bb..2251fa840 100644 --- a/src/utils/digitalAssets/tests/addETHAsset.test.js +++ b/src/utils/digitalAssets/tests/addETHAsset.test.js @@ -0,0 +1,26 @@ +// @flow strict + +import addETHAsset from '../addETHAsset' + +const ETH_ADDRESS: AssetAddress = 'Ethereum' + +describe('addETHAsset', () => { + test('defined', () => { + expect(addETHAsset).toBeDefined() + }) + + test('ETH asset will be added correctly', async () => { + const result: DigitalAssets = await addETHAsset({}) + const eth: ?DigitalAsset = result[ETH_ADDRESS] + + if (!eth) { + throw new Error('ETH asset not found') + } + + expect(eth).toBeDefined() + expect(eth.symbol).toEqual('ETH') + expect(eth.name).toEqual('Ethereum') + expect(eth.blockchainParams).toBeDefined() + expect(eth.blockchainParams.address).toEqual(ETH_ADDRESS) + }) +}) diff --git a/src/utils/digitalAssets/tests/mergeDigitalAssets.test.js b/src/utils/digitalAssets/tests/mergeDigitalAssets.test.js index ec1640040..ee3acf7e3 100644 --- a/src/utils/digitalAssets/tests/mergeDigitalAssets.test.js +++ b/src/utils/digitalAssets/tests/mergeDigitalAssets.test.js @@ -26,7 +26,7 @@ describe('mergeDigitalAssets', () => { expect(mergeDigitalAssets).toBeDefined() }) - test('check that default items are merged correctly', async () => { + test('default items are merged correctly', async () => { const result: DigitalAssets = await mergeDigitalAssets({}) const jnt: ?DigitalAsset = result[JNT_ADDRESS] @@ -41,7 +41,7 @@ describe('mergeDigitalAssets', () => { expect(jnt.blockchainParams.address).toEqual(JNT_ADDRESS) }) - test('check that existed items are merged correctly', async () => { + test('existed items are merged correctly', async () => { const result: DigitalAssets = await mergeDigitalAssets({ [AZBIT_ADDRESS]: AZBIT_ASSET, }) @@ -59,7 +59,7 @@ describe('mergeDigitalAssets', () => { expect(azbit.blockchainParams.address).toEqual(AZBIT_ADDRESS_CS) }) - test('check that existed non-custom item will be removed', async () => { + test('existed non-custom item will be removed', async () => { const result: DigitalAssets = await mergeDigitalAssets({ [AZBIT_ADDRESS]: { ...AZBIT_ASSET, @@ -73,7 +73,7 @@ describe('mergeDigitalAssets', () => { expect(azbit).toBeUndefined() }) - test('check that existed non-custom item will not be removed', async () => { + test('existed non-custom item will not be removed', async () => { const result: DigitalAssets = await mergeDigitalAssets({ [AZBIT_ADDRESS]: { ...AZBIT_ASSET, @@ -92,7 +92,7 @@ describe('mergeDigitalAssets', () => { expect(azbit.isCustom).toBeTruthy() }) - test('check that existed custom item will be converted to non-custom', async () => { + test('existed custom item will be converted to non-custom', async () => { const result: DigitalAssets = await mergeDigitalAssets({ [JNT_ADDRESS]: AZBIT_ASSET, }) From aa0f16dd62548e6e4c0a0d12d30fd44e494267f2 Mon Sep 17 00:00:00 2001 From: Ivan Violentov Date: Mon, 25 Nov 2019 16:23:34 +0300 Subject: [PATCH 4/5] Update digitalAssets/addETHAsset tests --- .../digitalAssets/tests/addETHAsset.test.js | 37 ++++++++++++++++++- src/utils/digitalAssets/tests/data.js | 13 +++++++ .../tests/mergeDigitalAssets.test.js | 13 +------ 3 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 src/utils/digitalAssets/tests/data.js diff --git a/src/utils/digitalAssets/tests/addETHAsset.test.js b/src/utils/digitalAssets/tests/addETHAsset.test.js index 2251fa840..3874e373a 100644 --- a/src/utils/digitalAssets/tests/addETHAsset.test.js +++ b/src/utils/digitalAssets/tests/addETHAsset.test.js @@ -1,16 +1,21 @@ // @flow strict import addETHAsset from '../addETHAsset' +import { AZBIT_ASSET } from './data' const ETH_ADDRESS: AssetAddress = 'Ethereum' +const EXISTED_ASSET_LIST: DigitalAssets = { + [AZBIT_ASSET.blockchainParams.address]: AZBIT_ASSET, +} + describe('addETHAsset', () => { test('defined', () => { expect(addETHAsset).toBeDefined() }) test('ETH asset will be added correctly', async () => { - const result: DigitalAssets = await addETHAsset({}) + const result: DigitalAssets = await addETHAsset(EXISTED_ASSET_LIST) const eth: ?DigitalAsset = result[ETH_ADDRESS] if (!eth) { @@ -23,4 +28,34 @@ describe('addETHAsset', () => { expect(eth.blockchainParams).toBeDefined() expect(eth.blockchainParams.address).toEqual(ETH_ADDRESS) }) + + test('ETH asset will be added to empty container correctly', async () => { + const result: DigitalAssets = await addETHAsset({}) + const eth: ?DigitalAsset = result[ETH_ADDRESS] + + if (!eth) { + throw new Error('ETH asset not found') + } + + expect(eth).toBeDefined() + expect(eth.blockchainParams).toBeDefined() + expect(eth.blockchainParams.address).toEqual(ETH_ADDRESS) + }) + + test('ETH asset will be added twice correctly', async () => { + const result: DigitalAssets = await addETHAsset(EXISTED_ASSET_LIST) + const eth: ?DigitalAsset = result[ETH_ADDRESS] + + if (!eth) { + throw new Error('ETH asset not found') + } + + expect(eth).toBeDefined() + expect(eth.blockchainParams).toBeDefined() + expect(eth.blockchainParams.address).toEqual(ETH_ADDRESS) + + const resultAgain: DigitalAssets = await addETHAsset(result) + + expect(resultAgain).toEqual(result) + }) }) diff --git a/src/utils/digitalAssets/tests/data.js b/src/utils/digitalAssets/tests/data.js new file mode 100644 index 000000000..fd2aa754a --- /dev/null +++ b/src/utils/digitalAssets/tests/data.js @@ -0,0 +1,13 @@ +// @flow strict + +export const AZBIT_ASSET: DigitalAsset = { + blockchainParams: { + type: 'erc-20', + address: '0xEccAB39acB2CAF9adBa72C1CB92FDC106B993E0b', + decimals: 6, + }, + symbol: 'AZ', + name: 'Azbit', + isActive: true, + isCustom: true, +} diff --git a/src/utils/digitalAssets/tests/mergeDigitalAssets.test.js b/src/utils/digitalAssets/tests/mergeDigitalAssets.test.js index ee3acf7e3..7a028d1e2 100644 --- a/src/utils/digitalAssets/tests/mergeDigitalAssets.test.js +++ b/src/utils/digitalAssets/tests/mergeDigitalAssets.test.js @@ -1,6 +1,7 @@ // @flow strict import mergeDigitalAssets from '../mergeDigitalAssets' +import { AZBIT_ASSET } from './data' const JNT_SYMBOL: string = 'JNT' const JNT_NAME: string = 'Jibrel Network Token' @@ -9,18 +10,6 @@ const JNT_ADDRESS: AssetAddress = '0xa5Fd1A791C4dfcaacC963D4F73c6Ae5824149eA7' const AZBIT_ADDRESS: AssetAddress = '0xeccab39acb2caf9adba72c1cb92fdc106b993e0b' const AZBIT_ADDRESS_CS: AssetAddress = '0xEccAB39acB2CAF9adBa72C1CB92FDC106B993E0b' -const AZBIT_ASSET: DigitalAsset = { - blockchainParams: { - type: 'erc-20', - address: AZBIT_ADDRESS_CS, - decimals: 6, - }, - symbol: 'AZ', - name: 'Azbit', - isActive: true, - isCustom: true, -} - describe('mergeDigitalAssets', () => { test('defined', () => { expect(mergeDigitalAssets).toBeDefined() From 61b7c634fffec1063dc6d0f6fd598756db720926 Mon Sep 17 00:00:00 2001 From: Ivan Violentov Date: Mon, 25 Nov 2019 17:18:00 +0300 Subject: [PATCH 5/5] Add tests with getAssetsMainnet mocks --- src/data/assets/getAssets.js | 11 ++++++ src/data/assets/index.js | 17 ++-------- .../tests/addETHAsset_empty.test.js | 26 ++++++++++++++ .../tests/addETHAsset_withETH.test.js | 33 ++++++++++++++++++ .../tests/addETHAsset_withoutETH.test.js | 34 +++++++++++++++++++ 5 files changed, 107 insertions(+), 14 deletions(-) create mode 100644 src/data/assets/getAssets.js create mode 100644 src/utils/digitalAssets/tests/addETHAsset_empty.test.js create mode 100644 src/utils/digitalAssets/tests/addETHAsset_withETH.test.js create mode 100644 src/utils/digitalAssets/tests/addETHAsset_withoutETH.test.js diff --git a/src/data/assets/getAssets.js b/src/data/assets/getAssets.js new file mode 100644 index 000000000..6621552ad --- /dev/null +++ b/src/data/assets/getAssets.js @@ -0,0 +1,11 @@ +// @flow strict + +export function getAssetsMainnet(): Promise { + return import(/* webpackChunkName: "mainnet" */ '../../../assets/mainnet/assets.json') + .then(({ default: mainnet }) => mainnet) +} + +export function getAssetsRopsten(): Promise { + return import(/* webpackChunkName: "ropsten" */ '../../../assets/ropsten/assets.json') + .then(({ default: ropsten }) => ropsten) +} diff --git a/src/data/assets/index.js b/src/data/assets/index.js index 9f873cfb8..c18ef792a 100644 --- a/src/data/assets/index.js +++ b/src/data/assets/index.js @@ -1,19 +1,8 @@ -// @flow +// @flow strict -import ethereum from './ethereum' - -function getAssetsMainnet(): Promise { - return import(/* webpackChunkName: "mainnet" */ '../../../assets/mainnet/assets.json') - .then(({ default: mainnet }) => mainnet) -} - -function getAssetsRopsten(): Promise { - return import(/* webpackChunkName: "ropsten" */ '../../../assets/ropsten/assets.json') - .then(({ default: ropsten }) => ropsten) -} +export { default as ethereum } from './ethereum' export { - ethereum, getAssetsMainnet, getAssetsRopsten, -} +} from './getAssets' diff --git a/src/utils/digitalAssets/tests/addETHAsset_empty.test.js b/src/utils/digitalAssets/tests/addETHAsset_empty.test.js new file mode 100644 index 000000000..f4ffe904c --- /dev/null +++ b/src/utils/digitalAssets/tests/addETHAsset_empty.test.js @@ -0,0 +1,26 @@ +// @flow strict + +import addETHAsset from '../addETHAsset' + +const ETH_ADDRESS: AssetAddress = 'Ethereum' + +jest.mock('../../../data/assets/getAssets', () => ({ + getAssetsMainnet: () => [], +})) + +describe('addETHAsset with getAssetsMainnet empty result mock', () => { + test('ETH asset will be added correctly', async () => { + const result: DigitalAssets = await addETHAsset({}) + const eth: ?DigitalAsset = result[ETH_ADDRESS] + + if (!eth) { + throw new Error('ETH asset not found') + } + + expect(eth).toBeDefined() + expect(eth.symbol).toEqual('ETH') + expect(eth.name).toEqual('Ethereum') + expect(eth.blockchainParams).toBeDefined() + expect(eth.blockchainParams.address).toEqual(ETH_ADDRESS) + }) +}) diff --git a/src/utils/digitalAssets/tests/addETHAsset_withETH.test.js b/src/utils/digitalAssets/tests/addETHAsset_withETH.test.js new file mode 100644 index 000000000..d83627acf --- /dev/null +++ b/src/utils/digitalAssets/tests/addETHAsset_withETH.test.js @@ -0,0 +1,33 @@ +// @flow strict + +import addETHAsset from '../addETHAsset' + +const ETH_ADDRESS: AssetAddress = 'Ethereum' + +jest.mock('../../../data/assets/getAssets', () => ({ + getAssetsMainnet: () => [{ + blockchainParams: { + type: 'ethereum', + decimals: 18, + }, + symbol: 'ETH', + name: 'Ethereum', + }], +})) + +describe('addETHAsset with getAssetsMainnet ETH included result mock', () => { + test('ETH asset will be added correctly', async () => { + const result: DigitalAssets = await addETHAsset({}) + const eth: ?DigitalAsset = result[ETH_ADDRESS] + + if (!eth) { + throw new Error('ETH asset not found') + } + + expect(eth).toBeDefined() + expect(eth.symbol).toEqual('ETH') + expect(eth.name).toEqual('Ethereum') + expect(eth.blockchainParams).toBeDefined() + expect(eth.blockchainParams.address).toEqual(ETH_ADDRESS) + }) +}) diff --git a/src/utils/digitalAssets/tests/addETHAsset_withoutETH.test.js b/src/utils/digitalAssets/tests/addETHAsset_withoutETH.test.js new file mode 100644 index 000000000..d4a6637d5 --- /dev/null +++ b/src/utils/digitalAssets/tests/addETHAsset_withoutETH.test.js @@ -0,0 +1,34 @@ +// @flow strict + +import addETHAsset from '../addETHAsset' + +const ETH_ADDRESS: AssetAddress = 'Ethereum' + +jest.mock('../../../data/assets/getAssets', () => ({ + getAssetsMainnet: () => [{ + name: 'Jibrel Network Token', + symbol: 'JNT', + blockchainParams: { + 'type': 'erc-20', + 'address': '0xa5fd1a791c4dfcaacc963d4f73c6ae5824149ea7', + 'decimals': 18, + }, + }], +})) + +describe('addETHAsset with getAssetsMainnet ETH excluded result mock', () => { + test('ETH asset will be added correctly', async () => { + const result: DigitalAssets = await addETHAsset({}) + const eth: ?DigitalAsset = result[ETH_ADDRESS] + + if (!eth) { + throw new Error('ETH asset not found') + } + + expect(eth).toBeDefined() + expect(eth.symbol).toEqual('ETH') + expect(eth.name).toEqual('Ethereum') + expect(eth.blockchainParams).toBeDefined() + expect(eth.blockchainParams.address).toEqual(ETH_ADDRESS) + }) +})