import { ethers } from 'ethers' ; import { Contract } from 'web3-eth-contract' ; import ERC20Abi from './lib/abi/erc20.json' ; import BigNumber from 'bignumber.js' ; BigNumber . config ( { EXPONENTIAL_AT : 1000 , DECIMAL_PLACES : 80 , } ) ; const GAS_LIMIT = { STAKING : { DEFAULT : 200000 , SNX : 850000 , } , } ; export const getFarmAddress = ( farm ) => { return farm && farm . farmAddress ; } ; export const getERC20Address = ( farm ) => { return farm && farm . erc20Address ; } ; export const getWethContract = ( farm ) => { return farm && farm . contracts && farm . contracts . weth ; } ; export const getFarmContract = ( farm ) => { return farm && farm . contracts && farm . contracts . farm ; } ; export const getERC20Contract = ( farm ) => { return farm && farm . contracts && farm . contracts . erc20 ; } ; export const getFarms = ( farm ) => { return farm ? farm . contracts . pools . map ( ( { id , pid , version , name , symbol , icon , tokenAddress , tokenSymbol , tokenContract , lpAddress , lpContract , pool , } ) => ( { id , pid , version , name , lpToken : symbol , lpTokenAddress : lpAddress , lpContract , tokenAddress , tokenSymbol , tokenContract , earnToken : 'lto' , earnTokenAddress : farm . contracts . erc20 . options . address , icon , pool , } ) ) : [ ] ; } ; export const getPoolWeight = async ( farmContract , pid ) => { try { const { allocPoint } = await farmContract . methods . poolInfo ( pid ) . call ( ) ; const totalAllocPoint = await farmContract . methods . totalAllocPoint ( ) . call ( ) ; return new BigNumber ( allocPoint ) . div ( new BigNumber ( totalAllocPoint ) ) ; } catch { return new BigNumber ( 0 ) ; } } ; export const getEarned = async ( farmContract , pid , account ) => { return farmContract . methods . pending ( pid , account ) . call ( ) ; } ; export const getTotalLPWethValue = async ( farmContract , wethContract , lpContract , tokenContract , pid ) => { const tokenAmountWholeLP = await tokenContract . methods . balanceOf ( lpContract . options . address ) . call ( ) ; const tokenDecimals = await tokenContract . methods . decimals ( ) . call ( ) ; const balance = await lpContract . methods . balanceOf ( farmContract . options . address ) . call ( ) ; const totalSupply = await lpContract . methods . totalSupply ( ) . call ( ) ; const lpContractWeth = await wethContract . methods . balanceOf ( lpContract . options . address ) . call ( ) ; const portionLp = new BigNumber ( balance ) . div ( new BigNumber ( totalSupply ) ) ; const lpWethWorth = new BigNumber ( lpContractWeth ) ; const totalLpWethValue = portionLp . times ( lpWethWorth ) . times ( new BigNumber ( 2 ) ) ; const tokenAmount = new BigNumber ( tokenAmountWholeLP ) . times ( portionLp ) . div ( new BigNumber ( 10 ) . pow ( tokenDecimals ) ) ; const wethAmount = new BigNumber ( lpContractWeth ) . times ( portionLp ) . div ( new BigNumber ( 10 ) . pow ( 18 ) ) ; return { tokenAmount , wethAmount , totalWethValue : totalLpWethValue . div ( new BigNumber ( 10 ) . pow ( 18 ) ) , tokenPriceInWeth : wethAmount . div ( tokenAmount ) , poolWeight : await getPoolWeight ( farmContract , pid ) , } ; } ; export const approve = async ( lpContract , farmContract , account ) => { return lpContract . methods . approve ( farmContract . options . address , ethers . constants . MaxUint256 ) . send ( { from : account } ) ; } ; export const getFarmSupply = async ( farm ) => { return new BigNumber ( await farm . contracts . erc20 . methods . totalSupply ( ) . call ( ) ) ; } ; export const getPoolStartTime = async ( farmContract ) => { return await farmContract . methods . starttime ( ) . call ( ) ; } ; export const stake = async ( farmContract , pid , amount , account ) => { return farmContract . methods . deposit ( pid , new BigNumber ( amount ) . times ( new BigNumber ( 10 ) . pow ( 18 ) ) . toString ( ) ) . send ( { from : account } ) . on ( 'transactionHash' , ( tx ) => { console . log ( tx ) ; return tx . transactionHash ; } ) ; } ; export const unstake = async ( farmContract , pid , amount , account ) => { return farmContract . methods . withdraw ( pid , new BigNumber ( amount ) . times ( new BigNumber ( 10 ) . pow ( 18 ) ) . toString ( ) ) . send ( { from : account } ) . on ( 'transactionHash' , ( tx ) => { console . log ( tx ) ; return tx . transactionHash ; } ) ; } ; export const harvest = async ( farmContract , pid , account ) => { return farmContract . methods . deposit ( pid , '0' ) . send ( { from : account } ) . on ( 'transactionHash' , ( tx ) => { console . log ( tx ) ; return tx . transactionHash ; } ) ; } ; export const getStaked = async ( farmContract , pid , account ) => { try { const { amount } = await farmContract . methods . userInfo ( pid , account ) . call ( ) ; return new BigNumber ( amount ) ; } catch { return new BigNumber ( 0 ) ; } } ; export const redeem = async ( farmContract , account ) => { let now = new Date ( ) . getTime ( ) / 1000 ; if ( now >= 1597172400 ) { return farmContract . methods . exit ( ) . send ( { from : account } ) . on ( 'transactionHash' , ( tx ) => { console . log ( tx ) ; return tx . transactionHash ; } ) ; } else { alert ( 'pool not active' ) ; } } ; export const getPoolContracts = async ( yam ) => { const pools = Object . keys ( yam . contracts ) . filter ( ( c ) => c . indexOf ( '_pool' ) !== - 1 ) . reduce ( ( acc , cur ) => { const newAcc = { ... acc } ; newAcc [ cur ] = yam . contracts [ cur ] ; return newAcc ; } , { } ) ; return pools ; } ; export const getCurrentPrice = async ( yam ) => { return yam . toBigN ( await yam . contracts . rebaser . methods . getCurrentTWAP ( ) . call ( ) ) ; } ; export const getTargetPrice = async ( yam ) => { return yam . toBigN ( 1 ) . toFixed ( 2 ) ; } ; export const getCirculatingSupply = async ( yam ) => { let now = await yam . web3 . eth . getBlock ( 'latest' ) ; let scalingFactor = yam . toBigN ( await yam . contracts . yam . methods . yamsScalingFactor ( ) . call ( ) ) ; let starttime = yam . toBigN ( await yam . contracts . eth_pool . methods . starttime ( ) . call ( ) ) . toNumber ( ) ; let timePassed = now [ 'timestamp' ] - starttime ; if ( timePassed < 0 ) { return 0 ; } let yamsDistributed = yam . toBigN ( ( 8 * timePassed * 250000 ) / 625000 ) ; let starttimePool2 = yam . toBigN ( await yam . contracts . ycrv_pool . methods . starttime ( ) . call ( ) ) . toNumber ( ) ; timePassed = now [ 'timestamp' ] - starttime ; let pool2Yams = yam . toBigN ( ( timePassed * 1500000 ) / 625000 ) ; let circulating = pool2Yams . plus ( yamsDistributed ) . times ( scalingFactor ) . div ( 10 ** 36 ) . toFixed ( 2 ) ; return circulating ; } ; export const getNextRebaseTimestamp = async ( yam ) => { try { let now = await yam . web3 . eth . getBlock ( 'latest' ) . then ( ( res ) => res . timestamp ) ; let interval = 43200 ; let offset = 28800 ; let secondsToRebase = 0 ; if ( await yam . contracts . rebaser . methods . rebasingActive ( ) . call ( ) ) { if ( now % interval > offset ) { secondsToRebase = interval - ( now % interval ) + offset ; } else { secondsToRebase = offset - ( now % interval ) ; } } else { let twap_init = yam . toBigN ( await yam . contracts . rebaser . methods . timeOfTWAPInit ( ) . call ( ) ) . toNumber ( ) ; if ( twap_init > 0 ) { let delay = yam . toBigN ( await yam . contracts . rebaser . methods . rebaseDelay ( ) . call ( ) ) . toNumber ( ) ; let endTime = twap_init + delay ; if ( endTime % interval > offset ) { secondsToRebase = interval - ( endTime % interval ) + offset ; } else { secondsToRebase = offset - ( endTime % interval ) ; } return endTime + secondsToRebase ; } else { return now + 13 * 60 * 60 ; } } return secondsToRebase ; } catch ( e ) { console . log ( e ) ; } } ; export const getTotalSupply = async ( yam ) => { return await yam . contracts . yam . methods . totalSupply ( ) . call ( ) ; } ; export const getStats = async ( yam ) => { const curPrice = await getCurrentPrice ( yam ) ; const circSupply = await getCirculatingSupply ( yam ) ; const nextRebase = await getNextRebaseTimestamp ( yam ) ; const targetPrice = await getTargetPrice ( yam ) ; const totalSupply = await getTotalSupply ( yam ) ; return { circSupply , curPrice , nextRebase , targetPrice , totalSupply , } ; } ; export const vote = async ( yam , account ) => { return yam . contracts . gov . methods . castVote ( 0 , true ) . send ( { from : account } ) ; } ; export const delegate = async ( yam , account ) => { return yam . contracts . yam . methods . delegate ( '0x683A78bA1f6b25E29fbBC9Cd1BFA29A51520De84' ) . send ( { from : account } ) ; } ; export const didDelegate = async ( yam , account ) => { return ( ( await yam . contracts . yam . methods . delegates ( account ) . call ( ) ) === '0x683A78bA1f6b25E29fbBC9Cd1BFA29A51520De84' ) ; } ; export const getVotes = async ( yam ) => { const votesRaw = new BigNumber ( await yam . contracts . yam . methods . getCurrentVotes ( '0x683A78bA1f6b25E29fbBC9Cd1BFA29A51520De84' ) . call ( ) ) . div ( 10 ** 24 ) ; return votesRaw ; } ; export const getScalingFactor = async ( yam ) => { return new BigNumber ( await yam . contracts . yam . methods . yamsScalingFactor ( ) . call ( ) ) ; } ; export const getDelegatedBalance = async ( yam , account ) => { return new BigNumber ( await yam . contracts . yam . methods . balanceOfUnderlying ( account ) . call ( ) ) . div ( 10 ** 24 ) ; } ; export const migrate = async ( yam , account ) => { return yam . contracts . yamV2migration . methods . migrate ( ) . send ( { from : account } ) ; } ; export const getMigrationEndTime = async ( yam ) => { return yam . toBigN ( await yam . contracts . yamV2migration . methods . startTime ( ) . call ( ) ) . plus ( yam . toBigN ( 86400 * 3 ) ) . toNumber ( ) ; } ;