Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
MilGard91 committed Aug 23, 2023
1 parent 91cbc92 commit 67bfe36
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/lib/usageEngine.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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;

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);
Expand Down
27 changes: 15 additions & 12 deletions src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -37,4 +39,5 @@ module.exports = {
selectProduct,
selectProductPools,
selectPoolProducts,
selectPoolIds,
};
2 changes: 1 addition & 1 deletion test/mocks/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const store = {
},
'0_3': {
productId: 0,
poolId: 2,
poolId: 3,
allocations: [
BigNumber.from(0),
BigNumber.from(0),
Expand Down
1 change: 1 addition & 0 deletions test/unit/capacityEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Expand Down
31 changes: 24 additions & 7 deletions test/unit/responses.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,31 +60,31 @@ const capacities = [

const quote = {
quote: {
premiumInNXM: '2718347967479674796',
premiumInAsset: '27944726667418476',
premiumInNXM: '1945600000000000000',
premiumInAsset: '20000846416486563',
poolAllocationRequests: [
{
poolId: '1',
poolId: '2',
coverAmountInAsset: '1000042320824328192',
skip: false,
},
],
},
capacities: [
{
poolId: '1',
poolId: '2',
capacity: [
{
assetId: '0',
amount: '1011555965965397760',
amount: '3750158703091230720',
},
{
assetId: '1',
amount: '2826484798959880487553',
amount: '10478675352241508148979',
},
{
assetId: '255',
amount: '98400000000000000000',
amount: '364800000000000000000',
},
],
},
Expand Down Expand Up @@ -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: [
Expand Down
4 changes: 2 additions & 2 deletions test/unit/usageEngine.js
Original file line number Diff line number Diff line change
@@ -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 () {
Expand Down

0 comments on commit 67bfe36

Please sign in to comment.