assembly/core/strategies/protocols/aave-v2/payback-and-withdraw.ts

263 lines
7.3 KiB
TypeScript
Raw Permalink Normal View History

2021-09-04 16:50:56 +00:00
import BigNumber from "bignumber.js";
2021-08-23 23:07:53 +00:00
import tokens from "~/constant/tokens";
2021-08-25 11:13:23 +00:00
import {
defineStrategy,
2021-09-05 11:46:39 +00:00
defineStrategyComponent,
StrategyComponentType,
2021-08-25 11:13:23 +00:00
StrategyProtocol
} from "../../helpers";
2021-08-23 23:07:53 +00:00
export default defineStrategy({
2021-08-25 11:13:23 +00:00
protocol: StrategyProtocol.AAVE_V2,
2021-08-23 23:07:53 +00:00
name: "Payback & Withdraw",
description: "Payback debt & withdraw collateral in a single txn.",
author: "Instadapp Team",
2021-09-04 16:50:56 +00:00
submitText: "Payback & Withdraw",
2021-08-25 11:13:23 +00:00
details: `<p class="text-center">This strategy executes:</p>
<ul>
<li>Payback debt</li>
<li>Withdraw collateral</li>
</ul>`,
2021-09-05 11:46:39 +00:00
components: [
defineStrategyComponent({
type: StrategyComponentType.INPUT_WITH_TOKEN,
2021-08-23 23:07:53 +00:00
name: "Debt",
2021-09-05 11:46:39 +00:00
placeholder: ({ component: input }) =>
2021-08-25 11:13:23 +00:00
input.token ? `${input.token.symbol} to Payback` : "",
2021-09-05 11:46:39 +00:00
validate: ({ component: input, toBN, dsaBalances }) => {
2021-08-23 23:07:53 +00:00
if (!input.token) {
2021-09-04 16:50:56 +00:00
return "Debt token is required";
2021-08-23 23:07:53 +00:00
}
2021-09-04 16:50:56 +00:00
const balance = toBN(dsaBalances[input.token.address]?.balance);
if (toBN(balance).lt(input.value)) {
return "You don't have enough balance to payback.";
2021-08-23 23:07:53 +00:00
}
},
2021-08-25 11:13:23 +00:00
defaults: ({ getTokenByKey }) => ({
token: getTokenByKey?.("dai")
})
2021-08-23 23:07:53 +00:00
}),
2021-09-05 11:46:39 +00:00
defineStrategyComponent({
type: StrategyComponentType.INPUT_WITH_TOKEN,
2021-08-23 23:07:53 +00:00
name: "Collateral",
2021-09-05 11:46:39 +00:00
placeholder: ({ component: input }) =>
2021-08-25 11:13:23 +00:00
input.token ? `${input.token.symbol} to Withdraw` : "",
2021-09-05 11:46:39 +00:00
validate: ({ component: input, position, toBN }) => {
2021-09-04 16:50:56 +00:00
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}`;
}
}
}
},
2021-08-25 11:13:23 +00:00
defaults: ({ getTokenByKey }) => ({
token: getTokenByKey?.("eth")
})
2021-09-05 15:28:08 +00:00
}),
defineStrategyComponent({
type: StrategyComponentType.HEADING,
name: "Projected Debt Position"
}),
defineStrategyComponent({
type: StrategyComponentType.STATUS,
name: "Status",
update: ({ position, component, components, toBN }) => {
if (
toBN(components[0].value).isZero() &&
toBN(components[1].value).isZero()
) {
return;
}
2021-08-23 23:07:53 +00:00
2021-09-05 15:28:08 +00:00
if (!position) {
return;
}
const newPositionData = changedPositionData(position, components);
const stats = calculateStats(newPositionData);
2021-09-04 16:50:56 +00:00
2021-09-05 15:28:08 +00:00
component.liquidation = BigNumber.max(
toBN(stats.totalMaxLiquidationLimitInEth).div(stats.totalSupplyInEth),
2021-09-04 16:50:56 +00:00
"0"
).toFixed();
2021-09-05 15:28:08 +00:00
component.status = BigNumber.max(
toBN(stats.totalBorrowInEth).div(stats.totalSupplyInEth),
2021-09-04 16:50:56 +00:00
"0"
).toFixed();
}
2021-09-05 15:28:08 +00:00
}),
defineStrategyComponent({
type: StrategyComponentType.VALUE,
name: "LIQUIDATION PRICE (IN ETH)",
value: "-",
update: ({ position, component, components, toBN, formatting }) => {
if (!position) {
return;
}
2021-09-04 16:50:56 +00:00
2021-09-05 15:28:08 +00:00
const newPositionData = changedPositionData(position, components);
const initialStats = calculateStats(position.data);
const newStats = calculateStats(newPositionData);
2021-09-04 16:50:56 +00:00
2021-09-05 15:28:08 +00:00
const stats =
toBN(components[0].value).isZero() &&
toBN(components[1].value).isZero()
? initialStats
2021-09-05 18:31:23 +00:00
: newStats;
let liquidationPrice = "0";
if (!toBN(stats.ethSupplied).isZero()) {
liquidationPrice = BigNumber.max(
toBN(stats.totalBorrowInEth)
.div(stats.totalMaxLiquidationLimitInEth)
.times(position.ethPriceInUsd),
"0"
).toFixed();
}
2021-09-05 15:28:08 +00:00
component.value = `${formatting.formatUsdMax(
liquidationPrice,
position.ethPriceInUsd
)} / ${formatting.formatUsd(position.ethPriceInUsd)}`;
2021-09-04 16:50:56 +00:00
}
2021-09-05 15:28:08 +00:00
})
],
validate: async ({ position, components: inputs, toBN }) => {
if (toBN(inputs[0].value).isZero() && toBN(inputs[1].value).isZero()) {
return;
}
const newPositionData = changedPositionData(position, inputs);
const stats = calculateStats(newPositionData);
2021-09-04 16:50:56 +00:00
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.";
}
},
2021-09-05 11:46:39 +00:00
spells: async ({ components: inputs, convertTokenAmountToWei }) => {
2021-08-23 23:07:53 +00:00
return [
{
connector: "aave_v2",
method: "payback",
2021-08-25 11:13:23 +00:00
args: [
inputs[0].token.address,
2021-09-04 16:50:56 +00:00
convertTokenAmountToWei(inputs[0].value, inputs[0].token.decimals),
2,
2021-08-25 11:13:23 +00:00
0,
0
]
2021-08-23 23:07:53 +00:00
},
{
connector: "aave_v2",
method: "withdraw",
2021-08-25 11:13:23 +00:00
args: [
inputs[1].token.address,
2021-09-04 16:50:56 +00:00
convertTokenAmountToWei(inputs[1].value, inputs[1].token.decimals),
2021-08-25 11:13:23 +00:00
0,
0
]
2021-08-23 23:07:53 +00:00
}
];
}
});
2021-09-05 15:28:08 +00:00
const changedPositionData = (position, inputs) => {
return position.data.map(position => {
const changedPosition = { ...position };
if (inputs[0].token.key === position.key) {
changedPosition.borrow = BigNumber.max(
new BigNumber(position.borrow).minus(inputs[0].value),
"0"
).toFixed();
}
if (inputs[1].token.key === position.key) {
changedPosition.supply = BigNumber.max(
new BigNumber(position.supply).minus(inputs[1].value),
"0"
).toFixed();
}
return changedPosition;
});
};
const calculateStats = positionData => {
return positionData.reduce(
(
stats,
{ key, supply, borrow, borrowStable, priceInEth, factor, liquidation }
) => {
if (key === "eth") {
stats.ethSupplied = supply;
}
const borrowTotal = new BigNumber(borrow).plus(borrowStable);
stats.totalSupplyInEth = new BigNumber(supply)
.times(priceInEth)
.plus(stats.totalSupplyInEth)
.toFixed();
stats.totalBorrowInEth = new BigNumber(borrowTotal)
.times(priceInEth)
.plus(stats.totalBorrowInEth)
.toFixed();
stats.totalMaxBorrowLimitInEth = new BigNumber(priceInEth)
.times(factor)
.times(supply)
.plus(stats.totalMaxBorrowLimitInEth)
.toFixed();
stats.totalMaxLiquidationLimitInEth = new BigNumber(priceInEth)
.times(liquidation)
.times(supply)
.plus(stats.totalMaxLiquidationLimitInEth)
.toFixed();
return stats;
},
{
totalSupplyInEth: "0",
totalBorrowInEth: "0",
totalMaxBorrowLimitInEth: "0",
totalMaxLiquidationLimitInEth: "0"
}
);
};