Skip to content
This repository has been archived by the owner on Jun 10, 2022. It is now read-only.

Commit

Permalink
CMC specific endpoints (#603)
Browse files Browse the repository at this point in the history
* - Added 2 new endpoints to return plain network currency supplies

* Use const mainnet mosaic id

* Linting

* another lint fix

* instead of trimming the fraction, add the dot

* Added parameter to currency circulating endpoint

* Changed to load from network properties
  • Loading branch information
rg911 authored Apr 19, 2021
1 parent fcd14dc commit 0e56ea0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
23 changes: 23 additions & 0 deletions rest/src/plugins/mosaic/mosaicRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
const merkleUtils = require('../../routes/merkleUtils');
const routeUtils = require('../../routes/routeUtils');
const catapult = require('catapult-sdk');
const ini = require('ini');
const fs = require('fs');
const util = require('util');

const { PacketType } = catapult.packet;

Expand Down Expand Up @@ -60,5 +63,25 @@ module.exports = {
next();
});
});

// CMC specific endpoint
const readAndParseNetworkPropertiesFile = () => {
const readFile = util.promisify(fs.readFile);
return readFile(services.config.apiNode.networkPropertyFilePath, 'utf8')
.then(fileData => ini.parse(fileData));
};

server.get('/network/currency/circulating', (req, res, next) => readAndParseNetworkPropertiesFile()
.then(propertiesObject => {
const currencyId = propertiesObject.chain.currencyMosaicId.replace(/'/g, '').replace('0x', '');
const mosaicId = routeUtils.parseArgument({ mosaicId: currencyId }, 'mosaicId', 'uint64hex');
return db.mosaicsByIds([mosaicId]).then(response => {
const s = response[0].mosaic.supply.toString();
const supply = `${s.substring(0, s.length - 6)}.${s.substring(s.length - 6, s.length)}`;
res.setHeader('content-type', 'text/plain');
res.send(supply);
next();
});
}));
}
};
12 changes: 12 additions & 0 deletions rest/src/routes/networkRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,17 @@ module.exports = {
next();
});
});

// CMC specific, only return the plain value of the max mosaic supply
server.get('/network/currency/total', (req, res, next) => readAndParseNetworkPropertiesFile()
.then(propertiesObject => {
const maxSupply = propertiesObject.chain.maxMosaicAtomicUnits.replace(/'/g, '').slice(0, -6);
res.setHeader('content-type', 'text/plain');
res.send(maxSupply);
next();
}).catch(() => {
res.send(errors.createInvalidArgumentError('there was an error reading the network properties file'));
next();
}));
}
};
3 changes: 2 additions & 1 deletion rest/test/plugins/mosaic/mosaic_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ describe('mosaic plugin', () => {
test.assert.assertRoutes(routes, [
'/mosaics',
'/mosaics/:mosaicId',
'/mosaics/:mosaicId/merkle'
'/mosaics/:mosaicId/merkle',
'/network/currency/circulating'
]);
});

Expand Down
4 changes: 3 additions & 1 deletion rest/test/routes/allRoutes_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ describe('all routes', () => {

'/transactions/:group/:transactionId',
'/transactions/:group',
'/transactionStatus/:hash'
'/transactionStatus/:hash',

'/network/currency/total'
]);
});

Expand Down

0 comments on commit 0e56ea0

Please sign in to comment.