Skip to content

Commit

Permalink
Merge pull request #9 from Adamant-im/chore/js-docs
Browse files Browse the repository at this point in the history
add JS-docs
  • Loading branch information
gost1k337 authored Aug 16, 2022
2 parents 9ae14d6 + 25045d1 commit dbdf0f5
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
81 changes: 81 additions & 0 deletions helpers/cryptos/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ const Store = require('../../modules/Store');
const helpers = require('../utils');

module.exports = {
/**
* Get address from ADM address
* @param {String} coin
* @param {String} admAddress
* @return {Promise<*>}
*/
async getAddressCryptoFromAdmAddressADM(coin, admAddress) {
try {
if (this.isERC20(coin)) {
Expand All @@ -28,6 +34,10 @@ module.exports = {
}
},

/**
* Update all balances
* @return {Promise<void>}
*/
async updateAllBalances() {
try {
await this.ETH.updateBalance();
Expand All @@ -41,6 +51,10 @@ module.exports = {
}
},

/**
* Get last blocks numbers
* @return {Promise<Object>}
*/
async getLastBlocksNumbers() {
try {
const data = {
Expand All @@ -58,26 +72,56 @@ module.exports = {
}
},

/**
* Returns true if coin is in known_crypto list in config
* @param {String} coin
* @return {Boolean}
*/
isKnown(coin) {
return config.known_crypto.includes(coin);
},
/**
* Returns true if coin is in accepted_crypto list in config
* @param {String} coin
* @return {Boolean}
*/
isAccepted(coin) {
return config.accepted_crypto.includes(coin);
},
/**
* Returns true if coin is in exchange_crypto list in config
* @param {String} coin
* @return {Boolean}
*/
isExchanged(coin) {
return config.exchange_crypto.includes(coin);
},
/**
* Returns true if coin is fiat money
* @param {String} coin
* @return {Boolean}
*/
isFiat(coin) {
return ['USD', 'RUB', 'EUR', 'CNY', 'JPY'].includes(coin);
},
isHasTicker(coin) { // if coin has ticker like COIN/OTHERCOIN or OTHERCOIN/COIN
const pairs = Object.keys(Store.currencies).toString();
return pairs.includes(',' + coin + '/') || pairs.includes('/' + coin);
},
/**
* Returns true if coin is ERC-20 coin
* @param {String} coin
* @return {Boolean}
*/
isERC20(coin) {
return config.erc20.includes(coin.toUpperCase());
},

/**
* Get Twitter accounts
* @param {String} message
* @return {void}
*/
getAccounts(message) {
const userAccounts = {};
try {
Expand Down Expand Up @@ -105,6 +149,11 @@ module.exports = {
return userAccounts;
},

/**
* Get Twitter account by matching regex and message
* @param {String} message
* @return {Object}
*/
findTwitterAccount(message) {
const pattern = /(?<=^|(?<=[^a-zA-Z0-9-_.]))@([A-Za-z]+[A-Za-z0-9-_]+)/gi;
const accounts = message.match(pattern);
Expand All @@ -113,6 +162,12 @@ module.exports = {
}
},

/**
* Get Twitter account by matching regex and message
* @param {String} message
* @param {String} link
* @return {String}
*/
findLink(message, link) {
const kLINK_DETECTION_REGEX = /(([a-z]+:\/\/)?(([a-z0-9-]+\.)+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel|local|internal))(:[0-9]{1,5})?(\/[a-z0-9_\-.~]+)*(\/([a-z0-9_\-.]*)(\?[a-z0-9+_\-.%=&amp;]*)?)?(#[a-zA-Z0-9!$&'()*+.=-_~:@/?]*)?)(\s+|$)/gi;
const links = message.match(kLINK_DETECTION_REGEX);
Expand All @@ -127,6 +182,12 @@ module.exports = {
return found.trim().toLowerCase();
},

/**
* Trims char
* @param {String} s
* @param {String} mask
* @return {String}
*/
trimChar(s, mask) {
while (~mask.indexOf(s[0])) {
s = s.slice(1);
Expand All @@ -137,10 +198,20 @@ module.exports = {
return s;
},

/**
* Trims @ symbol in twitter name
* @param {String} account
* @return {String}
*/
getTwitterScreenName(account) {
return this.trimChar(account, '@').toLowerCase();
},

/**
* Parses twitter name from link
* @param {String} link
* @return {String}
*/
parseTwitterAccountFromLink(link) {
link = this.trimChar(link, '/');
const n = link.lastIndexOf('/');
Expand All @@ -151,6 +222,11 @@ module.exports = {
}
},

/**
* Get twitter ID from link
* @param {String} link
* @return {String}
*/
getTweetIdFromLink(link) {
link = this.trimChar(link, '/');
const n = link.lastIndexOf('/');
Expand All @@ -161,6 +237,11 @@ module.exports = {
}
},

/**
* Get twitter hashtags
* @param {String }tags
* @return {Array<String>}
*/
getTwitterHashtags(tags) {
for (let i = 0; i < tags.length; i++) {
tags[i] = this.trimChar(tags[i], '#');
Expand Down
14 changes: 14 additions & 0 deletions helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@ const constants = require('./const');
const log = require('./log');

module.exports = {
/**
* Returns current time in milliseconds since Unix Epoch
* @return {number}
*/
unix() {
return new Date().getTime();
},

/**
* Returns module name from its ID
* @param {string} id Module name, module.id
* @return {string}
*/
getModuleName(id) {
try {
let n = id.lastIndexOf('\\');
Expand All @@ -22,6 +31,11 @@ module.exports = {
}
},

/**
* Converts ADAMANT's epoch timestamp to a Unix timestamp
* @param {number} epochTime Timestamp to convert
* @return {number}
*/
toTimestamp(epochTime) {
return epochTime * 1000 + constants.EPOCH;
},
Expand Down

0 comments on commit dbdf0f5

Please sign in to comment.