-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
99 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters