assembly/composables/protocols/useYearnV2Position.ts
Georges KABBOUCHI c6ceb9773f wip
2021-09-25 01:19:32 +03:00

139 lines
3.2 KiB
TypeScript

import { useWeb3 } from "@instadapp/vue-web3";
import { ref, useContext, watch } from "@nuxtjs/composition-api";
import addresses from "~/constant/addresses";
import tokens from "~/constant/tokens";
import { useDSA } from "../useDSA";
import yearnV2ABI from "~/abis/read/yearnV2.json";
import useEventBus from "../useEventBus";
import BigNumber from "bignumber.js";
import { useBigNumber } from "../useBigNumber";
const resolver = addresses.mainnet.resolver.yearnV2;
const allTokens = tokens.mainnet.allTokens.map(token => token.address);
const wantAddresses = allTokens;
const vaults = ref([]);
export function useYearnV2Position() {
const { $axios } = useContext();
const { times } = useBigNumber();
const { library } = useWeb3();
const { activeAccount } = useDSA();
const { onEvent } = useEventBus();
const fetchPosition = async () => {
const availableVaults = await $axios
.$get("https://api.yearn.finance/v1/chains/1/vaults/all")
.then(vs => vs.filter(v => v.type === "v2"));
if (!library.value) {
return;
}
if (!activeAccount.value) {
return;
}
const resolverInstance = new library.value.eth.Contract(
yearnV2ABI as any,
resolver
);
// const tokensArr = wantAddresses;
// allow user to add custom tokens
const tokensArr = [
...new Set(
availableVaults.filter(v => v.type === "v2").map(v => v.token.address)
.filter(a => allTokens.includes(a))
)
];
const rawData = await resolverInstance.methods
.getPositionsForLatest(activeAccount.value.address, tokensArr)
.call();
const newVaults = [];
rawData.forEach(
([
vaultLatestVersion,
vault,
want,
pricePerShare,
availableDepositLimit,
totalAssets,
balanceOf,
wantBalanceOf,
expectedShareValue,
decimals,
isDeprecated,
emergencyShutdown
]) => {
const v = availableVaults.find(v => v.address === vault);
if (v) {
const supply = new BigNumber(balanceOf)
.dividedBy(10 ** decimals)
.toFixed();
newVaults.push({
...v,
priceInUsd: v.tvl.price.toString(),
position: {
vaultLatestVersion,
vault,
want,
pricePerShare,
availableDepositLimit,
totalAssets,
balanceOf,
wantBalanceOf,
expectedShareValue,
decimals,
isDeprecated,
emergencyShutdown,
supply,
supplyUsd: times(supply, v.tvl.price).toFixed()
}
});
}
}
);
vaults.value = newVaults;
};
const refreshPosition = async () => {
await fetchPosition();
};
onEvent("protocol::compound::refresh", refreshPosition);
watch(
library,
async val => {
if (val) {
refreshPosition();
}
},
{ immediate: true }
);
watch(
activeAccount,
async val => {
if (val) {
refreshPosition();
}
},
{ immediate: true }
);
return {
vaults,
refreshPosition
};
}