2021-08-10 14:17:20 +00:00
|
|
|
import { computed, Ref, ref, watch } from "@nuxtjs/composition-api";
|
2021-08-08 11:34:44 +00:00
|
|
|
import BigNumber from "bignumber.js";
|
|
|
|
BigNumber.config({ POW_PRECISION: 200 });
|
|
|
|
import abis from "~/constant/abis";
|
|
|
|
import addresses from "~/constant/addresses";
|
|
|
|
import makerVaults from "~/constant/tokens/vaults";
|
2021-08-13 20:58:50 +00:00
|
|
|
import { useBigNumber } from "~/composables/useBigNumber";
|
|
|
|
import { useDSA } from "~/composables/useDSA";
|
|
|
|
import { useToken } from "~/composables/useToken";
|
|
|
|
import { useWeb3 } from "~/composables/useWeb3";
|
2021-08-08 11:34:44 +00:00
|
|
|
import { AbiItem } from "web3-utils";
|
|
|
|
|
|
|
|
const defaultVault = {
|
|
|
|
id: null,
|
|
|
|
tokenKey: "eth",
|
|
|
|
token: "ETH",
|
|
|
|
collateralName: "ETH-A",
|
|
|
|
collateral: "0",
|
|
|
|
debt: "0",
|
|
|
|
liquidatedCollateral: "0",
|
|
|
|
status: "0",
|
|
|
|
rate: "0",
|
|
|
|
liquidation: "0",
|
|
|
|
price: "0",
|
|
|
|
netvalue: "0"
|
|
|
|
};
|
|
|
|
|
2021-08-10 16:11:15 +00:00
|
|
|
const vaultId = ref(null);
|
2021-08-08 11:34:44 +00:00
|
|
|
const vaults = ref([]);
|
|
|
|
const isNewVault = ref(false);
|
|
|
|
const vaultTypes = ref([]);
|
|
|
|
const vaultType = ref("");
|
|
|
|
|
|
|
|
const vault = computed(() => {
|
|
|
|
const vlt = vaults.value.find(v => v.id === vaultId.value);
|
|
|
|
if (!isNewVault.value && !!vlt) {
|
|
|
|
return vlt;
|
|
|
|
}
|
|
|
|
|
|
|
|
const vt = vaultTypes.value.find(vt => vt.type === vaultType.value);
|
|
|
|
if (vt) {
|
|
|
|
return { ...defaultVault, ...vt };
|
|
|
|
}
|
|
|
|
|
|
|
|
const defaultVaultType = vaultTypes.value[0];
|
|
|
|
if (defaultVaultType) {
|
|
|
|
return { ...defaultVault, ...defaultVaultType };
|
|
|
|
}
|
|
|
|
|
|
|
|
return defaultVault;
|
|
|
|
});
|
|
|
|
|
2021-08-10 16:11:15 +00:00
|
|
|
export function useMakerdaoPosition(
|
|
|
|
collateralAmountRef: Ref = null,
|
|
|
|
debtAmountRef: Ref = null
|
|
|
|
) {
|
2021-08-08 11:34:44 +00:00
|
|
|
const { web3, chainId, networkName } = useWeb3();
|
|
|
|
const { activeAccount } = useDSA();
|
|
|
|
const { isZero, ensureValue, times, div, max, gt } = useBigNumber();
|
|
|
|
const { getTokenByKey } = useToken();
|
|
|
|
|
|
|
|
const vaultTokenType = computed(() => vault.value.vaultTokenType);
|
|
|
|
|
|
|
|
const price = computed(() => ensureValue(vault.value.price).toFixed());
|
|
|
|
|
|
|
|
const collateralUsd = computed(() =>
|
|
|
|
times(collateral.value, price.value).toFixed()
|
|
|
|
);
|
|
|
|
const collateral = computed(() =>
|
|
|
|
ensureValue(vault.value.collateral).toFixed()
|
|
|
|
);
|
|
|
|
|
|
|
|
const liquidation = computed(() =>
|
|
|
|
ensureValue(vault.value.liquidation).toFixed()
|
|
|
|
);
|
|
|
|
const tokenKey = computed(() => vault.value.tokenKey);
|
|
|
|
|
|
|
|
const token = computed(() => getTokenByKey(tokenKey.value));
|
|
|
|
const symbol = computed(() => token.value.symbol ?? "ETH");
|
|
|
|
const rate = computed(() => ensureValue(vault.value.rate).toFixed());
|
|
|
|
const netValue = computed(() => ensureValue(vault.value.netValue).toFixed());
|
|
|
|
|
2021-08-10 14:17:20 +00:00
|
|
|
const status = computed(() => {
|
2021-08-10 16:11:15 +00:00
|
|
|
if (!collateralAmountRef || !debtAmountRef)
|
|
|
|
return ensureValue(vault.value.status).toFixed();
|
2021-08-10 14:17:20 +00:00
|
|
|
return isZero(collateralAmountRef.value) && !isZero(debtAmountRef.value)
|
2021-08-10 16:11:15 +00:00
|
|
|
? "1.1"
|
|
|
|
: div(
|
|
|
|
debtAmountRef.value,
|
|
|
|
times(collateralAmountRef.value, price.value)
|
|
|
|
).toFixed();
|
|
|
|
});
|
2021-08-08 11:34:44 +00:00
|
|
|
|
|
|
|
const liquidationPrice = computed(() => {
|
2021-08-10 14:17:20 +00:00
|
|
|
if (!collateralAmountRef || !debtAmountRef)
|
2021-08-10 16:11:15 +00:00
|
|
|
return max(
|
|
|
|
div(div(debt.value, collateral.value), liquidation.value),
|
|
|
|
"0"
|
|
|
|
).toFixed();
|
2021-08-10 14:17:20 +00:00
|
|
|
return isZero(collateralAmountRef.value) && !isZero(debtAmountRef.value)
|
2021-08-10 16:11:15 +00:00
|
|
|
? times(price.value, "1.1").toFixed()
|
|
|
|
: max(
|
|
|
|
div(
|
|
|
|
div(debtAmountRef.value, collateralAmountRef.value),
|
|
|
|
liquidation.value
|
|
|
|
),
|
|
|
|
"0"
|
|
|
|
).toFixed();
|
|
|
|
});
|
2021-08-08 11:34:44 +00:00
|
|
|
|
|
|
|
const debt = computed(() => ensureValue(vault.value.debt).toFixed());
|
2021-08-23 19:57:11 +00:00
|
|
|
const minDebt = computed(() => vaultTypes.value[0]?.totalFloor.toString() || "5000");
|
2021-08-08 11:34:44 +00:00
|
|
|
const debtCeilingReached = computed(() =>
|
2021-08-08 20:34:05 +00:00
|
|
|
vaultTypes.value?.some(v =>
|
|
|
|
gt(v.overallTotalDebt, v.overallTotalDebtCeiling)
|
2021-08-08 11:34:44 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
const fetchPosition = async () => {
|
|
|
|
if (!web3.value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
vaultTypes.value = await getVaultTypes(web3.value);
|
|
|
|
|
|
|
|
if (!activeAccount.value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
vaults.value = await getVaults(activeAccount.value.address, web3.value);
|
2021-08-08 11:57:23 +00:00
|
|
|
if (vaults.value.length > 0) {
|
|
|
|
vaultId.value = vaults.value[0].id;
|
|
|
|
}
|
2021-08-08 11:34:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
watch(
|
|
|
|
web3,
|
|
|
|
async val => {
|
|
|
|
if (val) {
|
|
|
|
fetchPosition();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ immediate: true }
|
|
|
|
);
|
|
|
|
|
|
|
|
watch(
|
|
|
|
activeAccount,
|
|
|
|
async val => {
|
|
|
|
if (val) {
|
|
|
|
fetchPosition();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ immediate: true }
|
|
|
|
);
|
|
|
|
|
2021-08-10 16:11:15 +00:00
|
|
|
const selectVault = vid => {
|
|
|
|
if (vid === vaultId.value && !isNewVault.value) return;
|
|
|
|
vaultId.value = vid;
|
|
|
|
isNewVault.value = false;
|
|
|
|
};
|
|
|
|
|
2021-08-08 11:34:44 +00:00
|
|
|
return {
|
|
|
|
fetchPosition,
|
2021-08-10 16:11:15 +00:00
|
|
|
vaultId: computed(() => (isNewVault.value ? "0" : vaultId.value || "0")),
|
|
|
|
selectVault,
|
2021-08-08 11:34:44 +00:00
|
|
|
vaultTokenType,
|
|
|
|
vault,
|
|
|
|
vaults,
|
|
|
|
vaultType,
|
|
|
|
vaultTypes,
|
|
|
|
isNewVault,
|
|
|
|
collateralUsd,
|
|
|
|
collateral,
|
|
|
|
price,
|
|
|
|
liquidation,
|
|
|
|
tokenKey,
|
|
|
|
token,
|
|
|
|
symbol,
|
|
|
|
rate,
|
|
|
|
netValue,
|
|
|
|
status,
|
|
|
|
liquidationPrice,
|
|
|
|
liquidationMaxPrice: price,
|
|
|
|
debt,
|
|
|
|
minDebt,
|
|
|
|
debtCeilingReached
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getVaultTypes(web3) {
|
|
|
|
const makerResolveABI = abis.resolver.maker;
|
|
|
|
const makerResolveAddr = addresses.mainnet.resolver.maker;
|
|
|
|
|
|
|
|
const makerResolverInstance = new web3.eth.Contract(
|
|
|
|
makerResolveABI as AbiItem[],
|
|
|
|
makerResolveAddr
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const rawData: any[] = await makerResolverInstance.methods
|
|
|
|
.getColInfo(makerVaults.types)
|
|
|
|
.call();
|
|
|
|
|
|
|
|
return makerVaults.allVaults.map(
|
|
|
|
({ type, token, key: tokenKey, disabled, vaultTokenType }, i) => {
|
|
|
|
const [
|
|
|
|
rate,
|
|
|
|
price,
|
|
|
|
ratioCbyD,
|
|
|
|
debtCeiling,
|
|
|
|
totalFloor,
|
|
|
|
totalDebt,
|
|
|
|
overallTotalDebtCeiling,
|
|
|
|
overallTotalDebt
|
|
|
|
] = rawData[i];
|
|
|
|
|
|
|
|
return {
|
|
|
|
type,
|
|
|
|
token,
|
|
|
|
tokenKey,
|
|
|
|
disabled,
|
|
|
|
vaultTokenType,
|
|
|
|
rate: calRate(rate),
|
|
|
|
price: new BigNumber(price).dividedBy(1e27).toFixed(),
|
|
|
|
liquidation: new BigNumber(1)
|
|
|
|
.dividedBy(new BigNumber(ratioCbyD).dividedBy(1e27))
|
|
|
|
.toFixed(),
|
|
|
|
debtCeiling: debtCeiling,
|
|
|
|
totalDebt: new BigNumber(totalFloor)
|
|
|
|
.dividedBy(1e18)
|
|
|
|
.multipliedBy(1.00002)
|
|
|
|
.toFixed(),
|
|
|
|
totalFloor: new BigNumber(totalDebt).dividedBy(1e45),
|
|
|
|
overallTotalDebtCeiling: new BigNumber(
|
|
|
|
overallTotalDebtCeiling
|
|
|
|
).dividedBy(1e45),
|
|
|
|
overallTotalDebt: new BigNumber(overallTotalDebt).dividedBy(1e45)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async function getVaults(user, web3) {
|
|
|
|
try {
|
|
|
|
const makerResolveABI = abis.resolver.maker;
|
|
|
|
const makerResolveAddr = addresses.mainnet.resolver.maker;
|
|
|
|
|
|
|
|
const makerResolverInstance = new web3.eth.Contract(
|
|
|
|
makerResolveABI as AbiItem[],
|
|
|
|
makerResolveAddr
|
|
|
|
);
|
|
|
|
|
|
|
|
const rawData: any[] = await makerResolverInstance.methods
|
|
|
|
.getVaults(user)
|
|
|
|
.call();
|
|
|
|
|
|
|
|
return rawData.map(
|
|
|
|
([
|
|
|
|
id,
|
|
|
|
owner,
|
|
|
|
type,
|
|
|
|
collInWei,
|
2021-08-08 11:57:23 +00:00
|
|
|
,
|
2021-08-08 11:34:44 +00:00
|
|
|
debtInWei,
|
|
|
|
liquidatedColInWei,
|
|
|
|
ratePerBlock,
|
|
|
|
priceInWei,
|
|
|
|
liquidationRatioCbyD,
|
2021-08-10 16:11:15 +00:00
|
|
|
urn
|
2021-08-08 11:34:44 +00:00
|
|
|
]) => {
|
|
|
|
const collateral = new BigNumber(collInWei).dividedBy(1e18);
|
|
|
|
const debt = new BigNumber(debtInWei).dividedBy(1e18);
|
|
|
|
const price = new BigNumber(priceInWei).dividedBy(1e27);
|
|
|
|
|
|
|
|
const vault = makerVaults.getVaultByType(type);
|
|
|
|
|
|
|
|
return {
|
|
|
|
id,
|
|
|
|
owner,
|
|
|
|
type,
|
|
|
|
tokenKey: vault.key,
|
|
|
|
token: vault.token,
|
|
|
|
vaultTokenType: vault.vaultTokenType,
|
|
|
|
collateral: collateral.toFixed(),
|
|
|
|
debt: debt.toFixed(),
|
|
|
|
liquidatedCollateral: new BigNumber(liquidatedColInWei)
|
|
|
|
.dividedBy(1e18)
|
|
|
|
.toFixed(),
|
|
|
|
rate: calRate(ratePerBlock),
|
|
|
|
price: price.toFixed(),
|
|
|
|
liquidation: new BigNumber(1)
|
|
|
|
.dividedBy(new BigNumber(liquidationRatioCbyD).dividedBy(1e27))
|
|
|
|
.toFixed(),
|
|
|
|
urn,
|
|
|
|
netValue: collateral
|
|
|
|
.multipliedBy(price)
|
|
|
|
.minus(debt)
|
|
|
|
.toFixed(),
|
|
|
|
status: collateral.isZero()
|
|
|
|
? "0"
|
|
|
|
: debt.dividedBy(collateral.multipliedBy(price)).toFixed()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function calRate(ilkRate) {
|
|
|
|
try {
|
|
|
|
return new BigNumber(ilkRate)
|
|
|
|
.dividedBy(1e27)
|
|
|
|
.pow(31545000)
|
|
|
|
.minus(1)
|
|
|
|
.toFixed(18);
|
|
|
|
} catch (error) {
|
|
|
|
console.log("error", error);
|
|
|
|
}
|
|
|
|
}
|