import { ContractInterface } from "ethers" ; import { Bond , BondOpts } from "./bond" ; import { BondType } from "./constants" ; import { Networks } from "../../constants/blockchain" ; import { StaticJsonRpcProvider } from "@ethersproject/providers" ; import { getBondCalculator } from "../bond-calculator" ; import { getAddresses } from "../../constants/addresses" ; export interface LPBondOpts extends BondOpts { readonly reserveContractAbi : ContractInterface ; readonly lpUrl : string ; } export class LPBond extends Bond { readonly isLP = true ; readonly lpUrl : string ; readonly reserveContractAbi : ContractInterface ; readonly displayUnits : string ; constructor ( lpBondOpts ) { super ( BondType . LP , lpBondOpts ) ; this . lpUrl = lpBondOpts . lpUrl ; this . reserveContractAbi = lpBondOpts . reserveContractAbi ; this . displayUnits = "LP" ; } async getTreasuryBalance ( networkID , provider ) { const addresses = getAddresses ( networkID ) ; const token = this . getContractForReserve ( networkID , provider ) ; const tokenAddress = this . getAddressForReserve ( networkID ) ; const bondCalculator = getBondCalculator ( networkID , provider ) ; const tokenAmount = await token . balanceOf ( addresses . TREASURY_ADDRESS ) ; const valuation = await bondCalculator . valuation ( tokenAddress , tokenAmount ) ; const markdown = await bondCalculator . markdown ( tokenAddress ) ; const tokenUSD = ( valuation / Math . pow ( 10 , 9 ) ) * ( markdown / Math . pow ( 10 , 18 ) ) ; return tokenUSD ; } public getTokenAmount ( networkID , provider ) { return this . getReserves ( networkID , provider , true ) ; } public getTimeAmount ( networkID , provider ) { return this . getReserves ( networkID , provider , false ) ; } private async getReserves ( networkID , provider , isToken ) { const addresses = getAddresses ( networkID ) ; const token = this . getContractForReserve ( networkID , provider ) ; let [ reserve0 , reserve1 ] = await token . getReserves ( ) ; const token1 = await token . token1 ( ) ; const isTime = token1 . toLowerCase ( ) === addresses . TIME_ADDRESS . toLowerCase ( ) ; return isToken ? this . toTokenDecimal ( false , isTime ? reserve0 : reserve1 ) : this . toTokenDecimal ( true , isTime ? reserve1 : reserve0 ) ; } private toTokenDecimal ( isTime , reserve ) { return isTime ? reserve / Math . pow ( 10 , 9 ) : reserve / Math . pow ( 10 , 18 ) ; } } export interface CustomLPBondOpts extends LPBondOpts { } export class CustomLPBond extends LPBond { constructor ( customBondOpts ) { super ( customBondOpts ) ; this . getTreasuryBalance = async ( networkID , provider ) => { const tokenAmount = await super . getTreasuryBalance ( networkID , provider ) ; const tokenPrice = this . getTokenPrice ( ) ; return tokenAmount * tokenPrice ; } ; this . getTokenAmount = async ( networkID , provider ) => { const tokenAmount = await super . getTokenAmount ( networkID , provider ) ; const tokenPrice = this . getTokenPrice ( ) ; return tokenAmount * tokenPrice ; } ; } }