import {BigNumber, Contract} from 'ethers'; import {Signer} from '@ethersproject/abstract-signer'; import {Provider} from '@ethersproject/abstract-provider'; import {TransactionResponse} from '@ethersproject/providers'; import {formatUnits} from 'ethers/lib/utils'; class ERC20 { private contract: Contract; address: string; symbol: string; decimal: number; constructor(address: string, provider: Signer | Provider, symbol: string, decimal = 18) { this.contract = new Contract(address, ABI, provider); this.address = address; this.symbol = symbol; this.decimal = decimal; } connect(signerOrProvider: Signer | Provider) { this.contract = new Contract(this.address, ABI, signerOrProvider); } get estimateGas() { return this.contract.estimateGas; } totalSupply(): Promise { return this.contract.totalSupply(); } balanceOf(account: string): Promise { return this.contract.balanceOf(account); } transfer(recipient: string, amount: BigNumber): Promise { return this.contract.transfer(recipient, amount); } allowance(owner: string, spender: string): Promise { return this.contract.allowance(owner, spender); } approve(spender: string, amount: BigNumber): Promise { return this.contract.approve(spender, amount); } transferFrom(sender: string, recipient: string, amount: BigNumber): Promise { return this.contract.transferFrom(sender, recipient, amount); } async displayedBalanceOf(account: string): Promise { const balance = await this.balanceOf(account); return formatUnits(balance, this.decimal); } async displayedTotalSupply(): Promise { const supply = await this.totalSupply(); return Number(formatUnits(supply, this.decimal)).toFixed(0); } } export class LPERC20 { pairTokens: [ERC20, ERC20]; pairTokenAddresses: [string, string]; token: ERC20; constructor(token: ERC20, pair: [ERC20, ERC20]) { this.token = token; this.pairTokens = pair; this.pairTokenAddresses = [pair[0].address.toLowerCase(), pair[1].address.toLowerCase()]; } } export default ERC20; const ABI = [ { inputs: [ {internalType: 'string', name: 'name', type: 'string'}, { internalType: 'string', name: 'symbol', type: 'string', }, ], stateMutability: 'nonpayable', type: 'constructor', }, { anonymous: false, inputs: [ { indexed: true, internalType: 'address', name: 'owner', type: 'address', }, { indexed: true, internalType: 'address', name: 'spender', type: 'address', }, { indexed: false, internalType: 'uint256', name: 'value', type: 'uint256', }, ], name: 'Approval', type: 'event', }, { anonymous: false, inputs: [ { indexed: true, internalType: 'address', name: 'from', type: 'address', }, { indexed: true, internalType: 'address', name: 'to', type: 'address', }, { indexed: false, internalType: 'uint256', name: 'value', type: 'uint256', }, ], name: 'Transfer', type: 'event', }, { inputs: [], name: 'name', outputs: [ { internalType: 'string', name: '', type: 'string', }, ], stateMutability: 'view', type: 'function', }, { inputs: [], name: 'symbol', outputs: [ { internalType: 'string', name: '', type: 'string', }, ], stateMutability: 'view', type: 'function', }, { inputs: [], name: 'decimals', outputs: [ { internalType: 'uint8', name: '', type: 'uint8', }, ], stateMutability: 'view', type: 'function', }, { inputs: [], name: 'totalSupply', outputs: [ { internalType: 'uint256', name: '', type: 'uint256', }, ], stateMutability: 'view', type: 'function', }, { inputs: [ { internalType: 'address', name: 'account', type: 'address', }, ], name: 'balanceOf', outputs: [ { internalType: 'uint256', name: '', type: 'uint256', }, ], stateMutability: 'view', type: 'function', }, { inputs: [ { internalType: 'address', name: 'recipient', type: 'address', }, { internalType: 'uint256', name: 'amount', type: 'uint256', }, ], name: 'transfer', outputs: [ { internalType: 'bool', name: '', type: 'bool', }, ], stateMutability: 'nonpayable', type: 'function', }, { inputs: [ { internalType: 'address', name: 'owner', type: 'address', }, { internalType: 'address', name: 'spender', type: 'address', }, ], name: 'allowance', outputs: [ { internalType: 'uint256', name: '', type: 'uint256', }, ], stateMutability: 'view', type: 'function', }, { inputs: [ { internalType: 'address', name: 'spender', type: 'address', }, { internalType: 'uint256', name: 'amount', type: 'uint256', }, ], name: 'approve', outputs: [ { internalType: 'bool', name: '', type: 'bool', }, ], stateMutability: 'nonpayable', type: 'function', }, { inputs: [ { internalType: 'address', name: 'sender', type: 'address', }, { internalType: 'address', name: 'recipient', type: 'address', }, { internalType: 'uint256', name: 'amount', type: 'uint256', }, ], name: 'transferFrom', outputs: [ { internalType: 'bool', name: '', type: 'bool', }, ], stateMutability: 'nonpayable', type: 'function', }, { inputs: [ { internalType: 'address', name: 'spender', type: 'address', }, { internalType: 'uint256', name: 'addedValue', type: 'uint256', }, ], name: 'increaseAllowance', outputs: [ { internalType: 'bool', name: '', type: 'bool', }, ], stateMutability: 'nonpayable', type: 'function', }, { inputs: [ { internalType: 'address', name: 'spender', type: 'address', }, { internalType: 'uint256', name: 'subtractedValue', type: 'uint256', }, ], name: 'decreaseAllowance', outputs: [ { internalType: 'bool', name: '', type: 'bool', }, ], stateMutability: 'nonpayable', type: 'function', }, ];