const sdk = require("@defillama/sdk");
const getEntireSystemCollAbi = require("./getEntireSystemColl.abi.json");
const vestaFarmingAbi = require("./vestaFarming.abi.json");
const { sumBalancerLps, unwrapCrv } = require("../helper/unwrapLPs.js");
const { transformArbitrumAddress } = require("../helper/portedTokens");

const VaultTokens = {
  gOHM: "0x8d9ba570d6cb60c7e3e0f31343efe75ab8e65fb1",
  ETH: "0x0000000000000000000000000000000000000000",
  renBTC: "0xdbf31df14b66535af65aac99c32e9ea844e14501",
  DPX: "0x6c2c06790b3e3e3c38e12ee22f8183b37a13ee55",
  GMX: "0xfc5a1a6eb076a2c7ad06ed22c90d7e710e35ad0a"
}

const VSTA_FARMING_ADDRESS = "0x65207da01293C692a37f59D1D9b1624F0f21177c";
const LP_VSTA_ETH_ADDRESS = "0xc61ff48f94d801c1ceface0289085197b5ec44f0";

const VST_FARMING_ADDRESS = "0xB3667b3d1b3D4ed3d451dF68C9C12A686227Bada";
const LP_VST_FRAX_ADDRESS = "0x59bF0545FCa0E5Ad48E13DA269faCD2E8C886Ba4";

// TroveManager holds total system collateral for each individual vaults
const TROVE_MANAGER_ADDRESS = "0x100EC08129e0FD59959df93a8b914944A3BbD5df";

const chain = "arbitrum";

async function tvl(_, block, chainBlocks) {
  block = chainBlocks.arbitrum;
  const transform = await transformArbitrumAddress()
  const balances = {}
  const calls = Object.values(VaultTokens).map(token => ({ params: [token] }))
  const { output } = await sdk.api.abi.multiCall({
    calls, block, chain: 'arbitrum', target: TROVE_MANAGER_ADDRESS, abi: getEntireSystemCollAbi,
  })

  output.forEach(({ input: { params: [token] }, output }) => {
    if (token.toLowerCase() === '0xdbf31df14b66535af65aac99c32e9ea844e14501') // fix renBTC balance
      output /= 1e10
    sdk.util.sumSingleBalance(balances, transform(token), output)
  })

  return balances;
};

async function pool2(_timestamp, block, chainBlocks) {
  const balances = {};
  const transform = await transformArbitrumAddress();
  await sumBalancerLps(balances, [[LP_VSTA_ETH_ADDRESS, VSTA_FARMING_ADDRESS]], chainBlocks.arbitrum, chain, transform);

  const curveBalances = (
    await sdk.api.abi.call({ target: VST_FARMING_ADDRESS, abi: vestaFarmingAbi, block, params: [], chain, })
  ).output;

  await unwrapCrv(balances, LP_VST_FRAX_ADDRESS, curveBalances, block, chain, transform);
  return balances;
};

module.exports = {
  arbitrum: {
    tvl,
    pool2,
  },
  start: 1644339600,
  timetravel: true,
  methodology:
    "Total Value Locked includes all stability pools, troves, and vst pairs",
};
