UNPKG

@dydxfoundation/governance

Version:
70 lines (69 loc) 3.08 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const utils_1 = require("ethers/lib/utils"); const types_1 = require("../../../types"); const types_2 = require("../types"); const BaseService_1 = __importDefault(require("./BaseService")); class ERC20Service extends BaseService_1.default { constructor(config) { super(config, types_1.ERC20__factory); this.approve = (token, user, spender, amount) => { const erc20Contract = this.getContractInstance(token); const txCallback = this.generateTxCallback({ rawTxMethod: () => erc20Contract.populateTransaction.approve(spender, amount), from: user, }); return { tx: txCallback, txType: types_2.eEthereumTxType.ERC20_APPROVAL, gas: this.generateTxPriceEstimation([], txCallback), }; }; this.allowance = async (token, userAddress, spender) => { const erc20Contract = this.getContractInstance(token); const [tokenApprovalAmount, tokenDecimals,] = await Promise.all([ erc20Contract.allowance(userAddress, spender), this.decimalsOf(token), ]); return (0, utils_1.formatUnits)(tokenApprovalAmount, tokenDecimals); }; this.decimalsOf = async (token) => { if (!this.tokenDecimals[token]) { const erc20Contract = this.getContractInstance(token); this.tokenDecimals[token] = await erc20Contract.decimals(); } return this.tokenDecimals[token]; }; this.getTokenData = async (token) => { const { name: nameGetter, symbol: symbolGetter, decimals: decimalsGetter, } = this.getContractInstance(token); const [name, symbol, decimals] = await Promise.all([nameGetter(), symbolGetter(), decimalsGetter()]); return { name, symbol, decimals, address: token, }; }; this.balanceOf = async (token, user) => { const erc20Contract = this.getContractInstance(token); const [tokenBalance, tokenDecimals,] = await Promise.all([ erc20Contract.balanceOf(user), this.decimalsOf(token), ]); return (0, utils_1.formatUnits)(tokenBalance, tokenDecimals.toString()); }; this.totalSupply = async (token) => { const erc20Contract = this.getContractInstance(token); const [tokenTotalSupply, tokenDecimals,] = await Promise.all([ erc20Contract.totalSupply(), this.decimalsOf(token), ]); return (0, utils_1.formatUnits)(tokenTotalSupply, tokenDecimals.toString()); }; this.tokenDecimals = {}; } } exports.default = ERC20Service;