import ABI from '../abi/UniswapV2Library.json';
import { ethers } from 'ethers'
import Address from'../Address.js'

let Web3 = require('web3')
let _web3;
let walletMgr;
let codeEnum;
let contract;
function init (provider,_walletMgr) {
    walletMgr = _walletMgr;
    codeEnum = walletMgr.ReturnCodeEnum;
    _web3 = new Web3(provider);
    contract = new _web3.eth.Contract(ABI, Address.address.UniswapV2Library)
}

async function pairFor(tokenA,tokenB,fun){
    let pair = await contract.methods.pairFor(Address.address.UniswapV2Factory,tokenA,tokenB ).call();
    fun({ code: codeEnum.SUCCESS, data: pair })
}

async function getReserves(tokenA,tokenB,fun){
    let reserves = await contract.methods.getReserves(Address.address.UniswapV2Factory,tokenA,tokenB ).call();
    console.log(reserves.reserveA,reserves.reserveB);
    let a = ethers.BigNumber.from(reserves.reserveA)
    let b= ethers.BigNumber.from(reserves.reserveB)
    console.log(a.toString(),b.toString());
    fun({ code: codeEnum.SUCCESS, data: {tokenAAmount:a,tokenBAmount:b} })
}

async function quote(amountA,reserveA,reserveB,fun){
    let reserves = await contract.methods.quote(amountA,reserveA,reserveB ).call();
    console.log(reserves);
    fun({ code: codeEnum.SUCCESS, data:reserves })
}

export default {
    init,
    pairFor,
    getReserves,
    quote,
}