Skip to content

Commit

Permalink
refactor: return empty product capacity on pools with no products (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
rackstar authored Dec 4, 2024
2 parents 9432203 + 3f8121b commit 41add24
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
9 changes: 4 additions & 5 deletions src/lib/capacityEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,12 @@ function calculateProductCapacity(
);

if (i === firstUsableTrancheIndex) {
// use the capacity data on the firstUsableTrancheIndex
aggregatedData = trancheData;
capacityPerPool = trancheCapacityPerPool;
}

// continue iterating through the tranches to calculate the max annual price
const { capacityAvailableNXM, totalPremium } = trancheData;
const maxTrancheAnnualPrice = capacityAvailableNXM.isZero()
? Zero
Expand All @@ -115,7 +117,8 @@ function calculateProductCapacity(
}

const { capacityAvailableNXM, capacityUsedNXM, minPrice } = aggregatedData;
// The available capacity of a product across all pools

// The available (i.e. remaining) capacity of a product
const capacityInAssets = Object.keys(assets).map(assetId => ({
assetId: Number(assetId),
amount: capacityAvailableNXM.mul(assetRates[assetId]).div(WeiPerEther),
Expand Down Expand Up @@ -191,10 +194,6 @@ function getPoolCapacity(store, poolId, period) {
const now = BigNumber.from(Date.now()).div(1000);
const productIds = selectProductsInPool(store, poolId);

if (productIds.length === 0) {
return null;
}

const productsCapacity = productIds
.map(productId =>
calculateProductCapacity(store, productId, {
Expand Down
6 changes: 0 additions & 6 deletions src/routes/capacity.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,6 @@ router.get(
* ]
* 400:
* description: Invalid pool id or period
* 404:
* description: Pool not found
* 500:
* description: Internal Server Error
*/
Expand All @@ -281,10 +279,6 @@ router.get(
const store = req.app.get('store');
const poolCapacity = getPoolCapacity(store, poolId, period);

if (poolCapacity === null) {
return res.status(404).send({ error: 'Pool not found', response: null });
}

const response = {
poolId: poolCapacity.poolId,
utilizationRate: poolCapacity.utilizationRate.toNumber(),
Expand Down
23 changes: 18 additions & 5 deletions test/unit/routes/capacity.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,24 @@ describe('Capacity Routes', () => {
expect(response.error).to.be.equal('Invalid period: must be an integer between 28 and 365');
});

it('should return 404 Pool not found', async function () {
const nonExistentPoolId = 999;
const url = `/v2/capacity/pools/${nonExistentPoolId}`;
const { body: response } = await server.get(url).expect('Content-Type', /json/).expect(404);
expect(response.error).to.be.equal('Pool not found');
it('should return empty productsCapacity and zero utilizationRate for pool with no products', async function () {
const emptyPoolId = 28;
const url = `/v2/capacity/pools/${emptyPoolId}`;
const { body: response } = await server.get(url).expect('Content-Type', /json/).expect(200);

expect(response.poolId).to.equal(emptyPoolId);
expect(response.utilizationRate).to.equal(0);
expect(response.productsCapacity).to.have.lengthOf(0);
});

it('should return empty productsCapacity and zero utilizationRate for for non-existent poolId', async function () {
const invalidPoolId = 28;
const url = `/v2/capacity/pools/${invalidPoolId}`;
const { body: response } = await server.get(url).expect('Content-Type', /json/).expect(200);

expect(response.poolId).to.equal(invalidPoolId);
expect(response.utilizationRate).to.equal(0);
expect(response.productsCapacity).to.have.lengthOf(0);
});
});

Expand Down

0 comments on commit 41add24

Please sign in to comment.