2021-09-21 20:11:08 +00:00
|
|
|
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;
|
|
|
|
|
2021-09-24 22:19:32 +00:00
|
|
|
const allTokens = tokens.mainnet.allTokens.map(token => token.address);
|
|
|
|
|
|
|
|
const wantAddresses = allTokens;
|
2021-09-21 20:11:08 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
);
|
|
|
|
|
2021-09-24 22:19:32 +00:00
|
|
|
// 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))
|
|
|
|
)
|
|
|
|
];
|
|
|
|
|
2021-09-21 20:11:08 +00:00
|
|
|
|
|
|
|
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)
|
2021-09-24 22:19:32 +00:00
|
|
|
.dividedBy(10 ** decimals)
|
|
|
|
.toFixed();
|
2021-09-21 20:11:08 +00:00
|
|
|
|
|
|
|
newVaults.push({
|
|
|
|
...v,
|
|
|
|
priceInUsd: v.tvl.price.toString(),
|
|
|
|
position: {
|
|
|
|
vaultLatestVersion,
|
|
|
|
vault,
|
|
|
|
want,
|
|
|
|
pricePerShare,
|
|
|
|
availableDepositLimit,
|
|
|
|
totalAssets,
|
|
|
|
balanceOf,
|
|
|
|
wantBalanceOf,
|
|
|
|
expectedShareValue,
|
|
|
|
decimals,
|
|
|
|
isDeprecated,
|
|
|
|
emergencyShutdown,
|
|
|
|
supply,
|
2021-09-24 22:19:32 +00:00
|
|
|
supplyUsd: times(supply, v.tvl.price).toFixed()
|
2021-09-21 20:11:08 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
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
|
|
|
|
};
|
|
|
|
}
|