This commit is contained in:
Georges KABBOUCHI 2021-09-04 19:50:56 +03:00
parent d79fe602b2
commit e052cbe70d
4 changed files with 128 additions and 23 deletions

View File

@ -42,13 +42,9 @@ export function useStrategy(defineStrategy: DefineStrategy) {
const error = ref(""); const error = ref("");
const pending = ref(false); const pending = ref(false);
strategy.onUpdated(async () => { // strategy.onUpdated(async () => {
await nextTick(); // await nextTick();
// });
// inputs.value = strategy.inputs;
console.log("onUpdated");
});
const submit = async () => { const submit = async () => {
error.value = ""; error.value = "";

View File

@ -49,7 +49,7 @@ export class Strategy {
continue; continue;
} }
if(input.defaulted){ if (input.defaulted) {
continue; continue;
} }

View File

@ -17,6 +17,8 @@ export default defineStrategy({
<li>Borrow Debt</li> <li>Borrow Debt</li>
</ul>`, </ul>`,
submitText: 'Deposit & Borrow',
author: "Instadapp Team", author: "Instadapp Team",
variables: { variables: {
@ -117,6 +119,7 @@ export default defineStrategy({
stats.totalMaxBorrowLimitInEth = toBN(priceInEth) stats.totalMaxBorrowLimitInEth = toBN(priceInEth)
.times(factor) .times(factor)
.times(supply)
.plus(stats.totalMaxBorrowLimitInEth) .plus(stats.totalMaxBorrowLimitInEth)
.toFixed(); .toFixed();

View File

@ -1,3 +1,4 @@
import BigNumber from "bignumber.js";
import tokens from "~/constant/tokens"; import tokens from "~/constant/tokens";
import { import {
defineStrategy, defineStrategy,
@ -12,6 +13,8 @@ export default defineStrategy({
description: "Payback debt & withdraw collateral in a single txn.", description: "Payback debt & withdraw collateral in a single txn.",
author: "Instadapp Team", author: "Instadapp Team",
submitText: "Payback & Withdraw",
details: `<p class="text-center">This strategy executes:</p> details: `<p class="text-center">This strategy executes:</p>
<ul> <ul>
<li>Payback debt</li> <li>Payback debt</li>
@ -24,13 +27,15 @@ export default defineStrategy({
name: "Debt", name: "Debt",
placeholder: ({ input }) => placeholder: ({ input }) =>
input.token ? `${input.token.symbol} to Payback` : "", input.token ? `${input.token.symbol} to Payback` : "",
validate: ({ input }) => { validate: ({ input, toBN, dsaBalances }) => {
if (!input.token) { if (!input.token) {
return "Token is required"; return "Debt token is required";
} }
if (input.token.balance < input.value) { const balance = toBN(dsaBalances[input.token.address]?.balance);
return "Your amount exceeds your maximum limit.";
if (toBN(balance).lt(input.value)) {
return "You don't have enough balance to payback.";
} }
}, },
defaults: ({ getTokenByKey }) => ({ defaults: ({ getTokenByKey }) => ({
@ -42,12 +47,119 @@ export default defineStrategy({
name: "Collateral", name: "Collateral",
placeholder: ({ input }) => placeholder: ({ input }) =>
input.token ? `${input.token.symbol} to Withdraw` : "", input.token ? `${input.token.symbol} to Withdraw` : "",
validate: ({ input, position, toBN }) => {
if (!input.token) {
return "Collateral token is required";
}
if (!input.value) {
return "Collateral amount is required";
}
if (position) {
const collateralPosition = position.data.find(
item => item.key === input.token.key
);
if (collateralPosition) {
const collateralBalance = toBN(collateralPosition.supply);
if (collateralBalance.lt(input.value)) {
const collateralBalanceFormatted = collateralBalance.toFixed(2);
return `Your amount exceeds your maximum limit of ${collateralBalanceFormatted} ${input.token.symbol}`;
}
}
}
},
defaults: ({ getTokenByKey }) => ({ defaults: ({ getTokenByKey }) => ({
token: getTokenByKey?.("eth") token: getTokenByKey?.("eth")
}) })
}) })
], ],
validate: async ({ position, inputs, toBN }) => {
if (toBN(inputs[0].value).isZero() && toBN(inputs[1].value).isZero()) {
return;
}
const newPositionData = position.data.map(position => {
const changedPosition = { ...position };
if (inputs[0].token.key === position.key) {
changedPosition.borrow = BigNumber.max(
toBN(position.borrow).minus(inputs[0].value),
"0"
).toFixed();
}
if (inputs[1].token.key === position.key) {
changedPosition.supply = BigNumber.max(
toBN(position.supply).minus(inputs[1].value),
"0"
).toFixed();
}
return changedPosition;
});
const stats = newPositionData.reduce(
(
stats,
{ key, supply, borrow, borrowStable, priceInEth, factor, liquidation }
) => {
if (key === "eth") {
stats.ethSupplied = supply;
}
const borrowTotal = toBN(borrow).plus(borrowStable);
stats.totalSupplyInEth = toBN(supply)
.times(priceInEth)
.plus(stats.totalSupplyInEth)
.toFixed();
stats.totalBorrowInEth = toBN(borrowTotal)
.times(priceInEth)
.plus(stats.totalBorrowInEth)
.toFixed();
stats.totalMaxBorrowLimitInEth = toBN(priceInEth)
.times(factor)
.times(supply)
.plus(stats.totalMaxBorrowLimitInEth)
.toFixed();
stats.totalMaxLiquidationLimitInEth = toBN(priceInEth)
.times(liquidation)
.times(supply)
.plus(stats.totalMaxLiquidationLimitInEth)
.toFixed();
return stats;
},
{
totalSupplyInEth: "0",
totalBorrowInEth: "0",
totalMaxBorrowLimitInEth: "0",
totalMaxLiquidationLimitInEth: "0"
}
);
let maxLiquidation = "0";
if (!toBN(stats.totalSupplyInEth).isZero()) {
maxLiquidation = BigNumber.max(
toBN(stats.totalMaxLiquidationLimitInEth).div(stats.totalSupplyInEth),
"0"
).toFixed();
}
const status = BigNumber.max(
toBN(stats.totalBorrowInEth).div(stats.totalSupplyInEth),
"0"
);
if (status.gt(toBN(maxLiquidation).minus("0.0001"))) {
return "Position will liquidate.";
}
},
spells: async ({ inputs, convertTokenAmountToWei }) => { spells: async ({ inputs, convertTokenAmountToWei }) => {
return [ return [
{ {
@ -55,11 +167,8 @@ export default defineStrategy({
method: "payback", method: "payback",
args: [ args: [
inputs[0].token.address, inputs[0].token.address,
convertTokenAmountToWei( convertTokenAmountToWei(inputs[0].value, inputs[0].token.decimals),
inputs[0].value, 2,
inputs[0].token.decimals
),
12,
0, 0,
0 0
] ]
@ -69,10 +178,7 @@ export default defineStrategy({
method: "withdraw", method: "withdraw",
args: [ args: [
inputs[1].token.address, inputs[1].token.address,
convertTokenAmountToWei( convertTokenAmountToWei(inputs[1].value, inputs[1].token.decimals),
inputs[1].value,
inputs[1].token.decimals
),
0, 0,
0 0
] ]