diff --git a/src/lib/usageEngine.js b/src/lib/usageEngine.js index 08646af8..7b86dffe 100644 --- a/src/lib/usageEngine.js +++ b/src/lib/usageEngine.js @@ -1,6 +1,6 @@ const { ethers } = require('ethers'); -const { selectPoolProducts } = require('../store/selectors'); +const { selectPoolProducts, selectPoolIds } = require('../store/selectors'); const { NXM_PER_ALLOCATION_UNIT } = require('./constants'); const { WeiPerEther, Zero } = ethers.constants; @@ -8,7 +8,7 @@ const { WeiPerEther, Zero } = ethers.constants; function usageEngine(store, poolIds) { const { assets, assetRates } = store.getState(); const usage = []; - const ids = poolIds.length === 0 ? Object.keys(store.getState().poolProductIds) : [...poolIds]; + const ids = poolIds.length === 0 ? selectPoolIds(store) : [...poolIds]; for (const poolId of ids) { const poolProducts = selectPoolProducts(store, poolId); diff --git a/src/store/selectors.js b/src/store/selectors.js index f4b0332f..c4cc9701 100644 --- a/src/store/selectors.js +++ b/src/store/selectors.js @@ -9,21 +9,23 @@ const selectProduct = (store, productId) => { }; const selectProductPools = (store, productId) => { - const { poolProducts, productPoolIds } = store.getState(); - const poolIds = productPoolIds[productId] || []; - return poolIds.map(poolId => { - const key = `${productId}_${poolId}`; - return poolProducts[key]; - }); + const { poolProducts } = store.getState(); + return Object.values(poolProducts).filter(item => `${item.productId}` === `${productId}`); }; const selectPoolProducts = (store, poolId) => { - const { poolProducts, poolProductIds } = store.getState(); - const productIds = poolProductIds[poolId] || []; - return productIds.map(productId => { - const key = `${productId}_${poolId}`; - return poolProducts[key]; - }); + const { poolProducts } = store.getState(); + return Object.values(poolProducts).filter(item => `${item.poolId}` === `${poolId}`); +}; + +const selectPoolIds = store => { + const { poolProducts } = store.getState(); + return Object.values(poolProducts).reduce((acc, item) => { + if (!acc.includes(`${item.poolId}`)) { + acc.push(`${item.poolId}`); + } + return acc; + }, []); }; const selectAssetSymbol = (store, assetId) => { @@ -37,4 +39,5 @@ module.exports = { selectProduct, selectProductPools, selectPoolProducts, + selectPoolIds, }; diff --git a/test/mocks/store.js b/test/mocks/store.js index ed18a12d..1ae1b127 100644 --- a/test/mocks/store.js +++ b/test/mocks/store.js @@ -73,7 +73,7 @@ const store = { }, '0_3': { productId: 0, - poolId: 2, + poolId: 3, allocations: [ BigNumber.from(0), BigNumber.from(0), diff --git a/test/unit/capacityEngine.js b/test/unit/capacityEngine.js index 88036818..1ce6795a 100644 --- a/test/unit/capacityEngine.js +++ b/test/unit/capacityEngine.js @@ -5,6 +5,7 @@ const { BigNumber } = require('ethers'); const capacityEngine = require('../../src/lib/capacityEngine'); const mockStore = require('../mocks/store'); const { capacities } = require('./responses'); + describe('Capacity Engine tests', () => { const store = { getState: () => null }; diff --git a/test/unit/responses.js b/test/unit/responses.js index b8e85683..9c670d64 100644 --- a/test/unit/responses.js +++ b/test/unit/responses.js @@ -60,11 +60,11 @@ const capacities = [ const quote = { quote: { - premiumInNXM: '2718347967479674796', - premiumInAsset: '27944726667418476', + premiumInNXM: '1945600000000000000', + premiumInAsset: '20000846416486563', poolAllocationRequests: [ { - poolId: '1', + poolId: '2', coverAmountInAsset: '1000042320824328192', skip: false, }, @@ -72,19 +72,19 @@ const quote = { }, capacities: [ { - poolId: '1', + poolId: '2', capacity: [ { assetId: '0', - amount: '1011555965965397760', + amount: '3750158703091230720', }, { assetId: '1', - amount: '2826484798959880487553', + amount: '10478675352241508148979', }, { assetId: '255', - amount: '98400000000000000000', + amount: '364800000000000000000', }, ], }, @@ -151,6 +151,23 @@ const usage = [ { poolId: '2', products: [ + { + productId: 0, + capacityUsed: [ + { + assetId: 0, + amount: '0', + }, + { + assetId: 1, + amount: '0', + }, + { + assetId: 255, + amount: '0', + }, + ], + }, { productId: 1, capacityUsed: [ diff --git a/test/unit/usageEngine.js b/test/unit/usageEngine.js index f9ff0887..2d275d61 100644 --- a/test/unit/usageEngine.js +++ b/test/unit/usageEngine.js @@ -1,11 +1,11 @@ const sinon = require('sinon'); const { expect } = require('chai'); -const { BigNumber } = require('ethers'); const usageEngine = require('../../src/lib/usageEngine'); const mockStore = require('../mocks/store'); const { usage } = require('./responses'); -describe.only('Capacity Engine tests', () => { + +describe('Usage Engine tests', () => { const store = { getState: () => null }; afterEach(function () {