This repository has been archived by the owner on Jun 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Task/607 refactor currency supply endpoints (#610)
* create network currency plugin on catapult-sdk * moved all function to plugin * rename to cmc * added cmc plugin for REST * addded cmc utils * added pluges test * lint fixes * update test case to pass travis
- Loading branch information
1 parent
e59bb2c
commit 5f5d60b
Showing
21 changed files
with
625 additions
and
80 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,38 @@ | ||
/* | ||
* Copyright (c) 2016-2019, Jaguar0625, gimre, BloodyRookie, Tech Bureau, Corp. | ||
* Copyright (c) 2020-present, Jaguar0625, gimre, BloodyRookie. | ||
* All rights reserved. | ||
* | ||
* This file is part of Catapult. | ||
* | ||
* Catapult is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Catapult is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with Catapult. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
/** @module plugins/cmc */ | ||
|
||
// To ensure cmc able to load from the config, created empty plugins | ||
|
||
/* eslint-disable no-unused-vars */ | ||
|
||
/** | ||
* Creates a cmc plugin use for CoinMarketCap. | ||
* @type {module:plugins/CatapultPlugin} | ||
*/ | ||
const cmcPlugin = { | ||
registerSchema: builder => {}, | ||
|
||
registerCodecs: codecBuilder => {} | ||
}; | ||
|
||
module.exports = cmcPlugin; |
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,51 @@ | ||
/* | ||
* Copyright (c) 2016-2019, Jaguar0625, gimre, BloodyRookie, Tech Bureau, Corp. | ||
* Copyright (c) 2020-present, Jaguar0625, gimre, BloodyRookie. | ||
* All rights reserved. | ||
* | ||
* This file is part of Catapult. | ||
* | ||
* Catapult is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Catapult is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with Catapult. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
const { convertToLong } = require('../../db/dbUtils'); | ||
|
||
class CmcDb { | ||
/** | ||
* Creates NetworkCurrencyDb around CatapultDb. | ||
* @param {module:db/CatapultDb} db Catapult db instance. | ||
*/ | ||
constructor(db) { | ||
this.catapultDb = db; | ||
} | ||
|
||
// region NetworkCurrency retrieval | ||
/** | ||
* Retrieves mosaics given their ids. | ||
* @param {Array.<module:catapult.utils/uint64~uint64>} ids Mosaic ids. | ||
* @returns {Promise.<array>} Mosaics. | ||
*/ | ||
mosaicsByIds(ids) { | ||
const mosaicIds = ids.map(id => convertToLong(id)); | ||
const conditions = { 'mosaic.id': { $in: mosaicIds } }; | ||
const collection = this.catapultDb.database.collection('mosaics'); | ||
return collection.find(conditions) | ||
.sort({ _id: -1 }) | ||
.toArray() | ||
.then(entities => Promise.resolve(this.catapultDb.sanitizer.renameIds(entities))); | ||
} | ||
// endregion | ||
} | ||
|
||
module.exports = CmcDb; |
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,40 @@ | ||
/* | ||
* Copyright (c) 2016-2019, Jaguar0625, gimre, BloodyRookie, Tech Bureau, Corp. | ||
* Copyright (c) 2020-present, Jaguar0625, gimre, BloodyRookie. | ||
* All rights reserved. | ||
* | ||
* This file is part of Catapult. | ||
* | ||
* Catapult is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Catapult is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with Catapult. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
/** @module plugins/cmc */ | ||
const CmcDb = require('./CmcDb'); | ||
const cmcRoutes = require('./cmcRoutes'); | ||
|
||
/** | ||
* Creates a cmc plugin. | ||
* @type {module:plugins/CatapultRestPlugin} | ||
*/ | ||
module.exports = { | ||
createDb: db => new CmcDb(db), | ||
|
||
registerTransactionStates: () => {}, | ||
|
||
registerMessageChannels: () => {}, | ||
|
||
registerRoutes: (...args) => { | ||
cmcRoutes.register(...args); | ||
} | ||
}; |
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,85 @@ | ||
/* | ||
* Copyright (c) 2016-2019, Jaguar0625, gimre, BloodyRookie, Tech Bureau, Corp. | ||
* Copyright (c) 2020-present, Jaguar0625, gimre, BloodyRookie. | ||
* All rights reserved. | ||
* | ||
* This file is part of Catapult. | ||
* | ||
* Catapult is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Catapult is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with Catapult. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
const cmcUtils = require('./cmcUtils'); | ||
const uncirculatedAddresses = require('./unCirculatedAccounts'); | ||
const routeUtils = require('../../routes/routeUtils'); | ||
const errors = require('../../server/errors'); | ||
const AccountType = require('../AccountType'); | ||
const ini = require('ini'); | ||
const fs = require('fs'); | ||
const util = require('util'); | ||
|
||
module.exports = { | ||
register: (server, db, services) => { | ||
const sender = routeUtils.createSender('cmc'); | ||
|
||
const readAndParseNetworkPropertiesFile = () => { | ||
const readFile = util.promisify(fs.readFile); | ||
return readFile(services.config.apiNode.networkPropertyFilePath, 'utf8') | ||
.then(fileData => ini.parse(fileData)); | ||
}; | ||
|
||
server.get('/network/currency/supply/circulating', (req, res, next) => readAndParseNetworkPropertiesFile() | ||
.then(async propertiesObject => { | ||
/* eslint-disable global-require */ | ||
const accountIds = routeUtils.parseArgumentAsArray({ addresses: uncirculatedAddresses }, 'addresses', 'address'); | ||
const currencyId = propertiesObject.chain.currencyMosaicId.replace(/'/g, '').replace('0x', ''); | ||
const mosaicId = routeUtils.parseArgument({ mosaicId: currencyId }, 'mosaicId', 'uint64hex'); | ||
|
||
const mosaics = await db.mosaicsByIds([mosaicId]); | ||
const accounts = await db.catapultDb.accountsByIds(accountIds.map(accountId => ({ [AccountType.address]: accountId }))); | ||
|
||
const totalSupply = parseInt(mosaics[0].mosaic.supply.toString(), 10); | ||
const totalUncirculated = accounts.reduce((a, b) => a + parseInt(b.account.mosaics[0].amount.toString(), 10), 0); | ||
|
||
const circulatingSupply = (totalSupply - totalUncirculated).toString(); | ||
|
||
sender.sendPlainText(res, next)(cmcUtils.convertToRelative(circulatingSupply)); | ||
}).catch(() => { | ||
res.send(errors.createInvalidArgumentError('there was an error reading the network properties file')); | ||
next(); | ||
})); | ||
|
||
server.get('/network/currency/supply/total', (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 supply = response[0].mosaic.supply.toString(); | ||
|
||
sender.sendPlainText(res, next)(cmcUtils.convertToRelative(supply)); | ||
}).catch(() => { | ||
res.send(errors.createInvalidArgumentError('there was an error reading the network properties file')); | ||
next(); | ||
}); | ||
})); | ||
|
||
server.get('/network/currency/supply/max', (req, res, next) => readAndParseNetworkPropertiesFile() | ||
.then(propertiesObject => { | ||
const supply = propertiesObject.chain.maxMosaicAtomicUnits.replace(/'/g, '').replace('0x', ''); | ||
sender.sendPlainText(res, next)(cmcUtils.convertToRelative(supply)); | ||
}).catch(() => { | ||
res.send(errors.createInvalidArgumentError('there was an error reading the network properties file')); | ||
next(); | ||
})); | ||
} | ||
}; |
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,26 @@ | ||
/* | ||
* Copyright (c) 2016-2019, Jaguar0625, gimre, BloodyRookie, Tech Bureau, Corp. | ||
* Copyright (c) 2020-present, Jaguar0625, gimre, BloodyRookie. | ||
* All rights reserved. | ||
* | ||
* This file is part of Catapult. | ||
* | ||
* Catapult is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Catapult is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with Catapult. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
const cmcUtils = { | ||
convertToRelative: absolute => (Number(absolute) / (10 ** 6)).toFixed(6) | ||
}; | ||
|
||
module.exports = cmcUtils; |
File renamed without changes.
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
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
Oops, something went wrong.