Skip to content

Commit

Permalink
Add usageEngine and usage routes
Browse files Browse the repository at this point in the history
  • Loading branch information
MilGard91 committed Aug 23, 2023
1 parent e0a89f5 commit 8dc95d7
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const createEventsApi = require('./lib/eventsApi');
const createSynchronizer = require('./lib/synchronizer');

const capacityRouter = require('./routes/capacity');
const usageRouter = require('./routes/usage');
const quoteRouter = require('./routes/quote');

const main = async () => {
Expand Down Expand Up @@ -48,6 +49,7 @@ const main = async () => {

// initiate routes
app.use('/v2', capacityRouter);
app.use('/v2', usageRouter);
app.use('/v2', quoteRouter);

const port = config.get('port');
Expand Down
35 changes: 35 additions & 0 deletions src/lib/usageEngine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const { ethers } = require('ethers');

const { selectPoolProducts } = 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];

for (const poolId of ids) {
const poolProducts = selectPoolProducts(store, poolId);

const poolCapacities = poolProducts.map(pool => {
const { productId, allocations } = pool;
const used = allocations.reduce((total, allocation) => total.add(allocation), Zero);
const capacityUsedNXM = used.mul(NXM_PER_ALLOCATION_UNIT);
return {
productId,
capacityUsed: Object.values(assets).map(assetId => ({
assetId,
amount: capacityUsedNXM.mul(assetRates[assetId]).div(WeiPerEther),
})),
};
});

usage.push({ poolId, products: poolCapacities });
}

return usage;
}

module.exports = usageEngine;
41 changes: 41 additions & 0 deletions src/routes/usage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const express = require('express');
const usageEngine = require('../lib/usageEngine');
const { asyncRoute } = require('../lib/helpers');

const router = express.Router();

const formatUsageResult = ({ poolId, products }) => ({
poolId: poolId.toString(),
products: products.map(({ productId, capacityUsed }) => ({
productId,
capacityUsed: capacityUsed.map(({ assetId, amount }) => ({ assetId, amount: amount.toString() })),
})),
});

router.get(
'/usage',
asyncRoute(async (req, res) => {
const store = req.app.get('store');

const response = usageEngine(store, []);
res.json(response.map(usage => formatUsageResult(usage)));
}),
);

router.get(
'/usage/:poolId',
asyncRoute(async (req, res) => {
const poolId = Number(req.params.poolId);
const store = req.app.get('store');

const [usage] = usageEngine(store, [poolId]);

if (!usage) {
return res.status(400).send({ error: 'Invalid Pool Id', response: null });
}

res.json(formatUsageResult(usage));
}),
);

module.exports = router;
15 changes: 11 additions & 4 deletions src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const initialState = {
globalCapacityRatio: 0,
poolProducts: {}, // {productId}_{poolId} -> { allocations, trancheCapacities }
productPoolIds: {}, // productId -> [ poolIds ]
poolProductIds: {}, // productId -> [ poolIds ]
products: {}, // productId -> { product }
trancheId: 0,
};
Expand All @@ -29,10 +30,16 @@ function reducer(state = initialState, { type, payload }) {
const key = `${productId}_${poolId}`;
const newPoolProduct = { productId, poolId, ...poolProduct };
const poolProducts = { ...state.poolProducts, [key]: newPoolProduct };
const previousIds = state.productPoolIds[productId] || [];
const newIds = [...new Set([...previousIds, poolId])];
const productPoolIds = { ...state.productPoolIds, [productId]: newIds };
return { ...state, poolProducts, productPoolIds };

const previousPoolIds = state.productPoolIds[productId] || [];
const newPoolIds = [...new Set([...previousPoolIds, poolId])];
const productPoolIds = { ...state.productPoolIds, [productId]: newPoolIds };

const previousProductIds = state.poolProductIds[poolId] || [];
const newProductIds = [...new Set([...previousProductIds, productId])];
const poolProductIds = { ...state.poolProductIds, [poolId]: newProductIds };

return { ...state, poolProducts, productPoolIds, poolProductIds };
}

if (type === SET_ASSET_RATE) {
Expand Down
10 changes: 10 additions & 0 deletions src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ const selectProductPools = (store, 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 selectAssetSymbol = (store, assetId) => {
const { assets } = store.getState();
return Object.keys(assets).find(key => assets[key] === assetId);
Expand All @@ -27,4 +36,5 @@ module.exports = {
selectAssetSymbol,
selectProduct,
selectProductPools,
selectPoolProducts,
};

0 comments on commit 8dc95d7

Please sign in to comment.