You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.2 KiB
55 lines
1.2 KiB
const util = require('./util')
|
|
|
|
function tokenInfoGetter () {
|
|
const tokens = {}
|
|
|
|
return async (address) => {
|
|
if (tokens[address]) {
|
|
return tokens[address]
|
|
}
|
|
|
|
tokens[address] = await getSymbolAndDecimals(address)
|
|
|
|
return tokens[address]
|
|
}
|
|
}
|
|
|
|
async function getSymbolAndDecimals (tokenAddress, existingTokens = []) {
|
|
const existingToken = existingTokens.find(({ address }) => tokenAddress === address)
|
|
if (existingToken) {
|
|
return existingToken
|
|
}
|
|
|
|
let result = []
|
|
try {
|
|
const token = util.getContractAtAddress(tokenAddress)
|
|
|
|
result = await Promise.all([
|
|
token.symbol(),
|
|
token.decimals(),
|
|
])
|
|
} catch (err) {
|
|
console.log(`symbol() and decimal() calls for token at address ${tokenAddress} resulted in error:`, err)
|
|
}
|
|
|
|
const [ symbol = [], decimals = [] ] = result
|
|
|
|
return {
|
|
symbol: symbol[0] || null,
|
|
decimals: decimals[0] && decimals[0].toString() || null,
|
|
}
|
|
}
|
|
|
|
function calcTokenAmount (value, decimals) {
|
|
const multiplier = Math.pow(10, Number(decimals || 0))
|
|
const amount = Number(value / multiplier)
|
|
|
|
return amount
|
|
}
|
|
|
|
|
|
module.exports = {
|
|
tokenInfoGetter,
|
|
calcTokenAmount,
|
|
getSymbolAndDecimals,
|
|
}
|
|
|