diff --git a/core/strategies/protocols/aave-v2/deposit-and-borrow.ts b/core/strategies/protocols/aave-v2/deposit-and-borrow.ts index 7fd4034..8ede76d 100644 --- a/core/strategies/protocols/aave-v2/deposit-and-borrow.ts +++ b/core/strategies/protocols/aave-v2/deposit-and-borrow.ts @@ -17,7 +17,7 @@ export default defineStrategy({
  • Borrow Debt
  • `, - submitText: 'Deposit & Borrow', + submitText: "Deposit & Borrow", author: "Instadapp Team", @@ -73,6 +73,75 @@ export default defineStrategy({ defaults: ({ getTokenByKey, variables }) => ({ token: getTokenByKey?.(variables.debtTokenKey) }) + }), + 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; + } + + if (!position) { + return; + } + + const newPositionData = changedPositionData(position, components); + const stats = calculateStats(newPositionData); + + component.liquidation = BigNumber.max( + toBN(stats.totalMaxBorrowLimitInEth).div(stats.totalSupplyInEth), + "0" + ).toFixed(); + component.status = BigNumber.max( + toBN(stats.totalBorrowInEth).div(stats.totalSupplyInEth), + "0" + ).toFixed(); + } + }), + defineStrategyComponent({ + type: StrategyComponentType.VALUE, + name: "LIQUIDATION PRICE (IN ETH)", + value: "-", + update: ({ position, component, components, toBN, formatting }) => { + if (!position) { + return; + } + + const newPositionData = changedPositionData(position, components); + const initialStats = calculateStats(position.data); + const newStats = calculateStats(newPositionData); + + const stats = + toBN(components[0].value).isZero() && + toBN(components[1].value).isZero() + ? initialStats + : newStats; + + console.log(stats); + + let liquidationPrice = "0"; + if (!toBN(stats.ethSupplied).isZero()) { + liquidationPrice = BigNumber.max( + toBN(stats.totalBorrowInEth) + .div(stats.totalMaxLiquidationLimitInEth) + .times(position.ethPriceInUsd), + "0" + ).toFixed(); + } + + component.value = `${formatting.formatUsdMax( + liquidationPrice, + position.ethPriceInUsd + )} / ${formatting.formatUsd(position.ethPriceInUsd)}`; + } }) ], @@ -81,56 +150,8 @@ export default defineStrategy({ return; } - const newPositionData = position.data.map(position => { - const changedPosition = { ...position }; - if (inputs[1].token.key === position.key) { - changedPosition.borrow = BigNumber.max( - toBN(position.borrow).plus(inputs[1].value), - "0" - ).toFixed(); - } - - if (inputs[0].token.key === position.key) { - changedPosition.supply = BigNumber.max( - toBN(position.supply).plus(inputs[0].value), - "0" - ).toFixed(); - } - - return changedPosition; - }); - - const stats = newPositionData.reduce( - (stats, { key, supply, borrow, borrowStable, priceInEth, factor }) => { - 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(); - - return stats; - }, - { - totalSupplyInEth: "0", - totalBorrowInEth: "0", - totalMaxBorrowLimitInEth: "0" - } - ); + const newPositionData = changedPositionData(position, inputs); + const stats = calculateStats(newPositionData); let liquidation = "0"; if (!toBN(stats.totalSupplyInEth).isZero()) { @@ -150,7 +171,11 @@ export default defineStrategy({ } }, - spells: async ({ components: inputs, convertTokenAmountToWei, variables }) => { + spells: async ({ + components: inputs, + convertTokenAmountToWei, + variables + }) => { return [ { connector: "aave_v2", @@ -176,3 +201,68 @@ export default defineStrategy({ ]; } }); + +const changedPositionData = (position, inputs) => { + return position.data.map(position => { + const changedPosition = { ...position }; + if (inputs[1].token.key === position.key) { + changedPosition.borrow = BigNumber.max( + new BigNumber(position.borrow).plus(inputs[1].value || "0"), + "0" + ).toFixed(); + } + + if (inputs[0].token.key === position.key) { + changedPosition.supply = BigNumber.max( + new BigNumber(position.supply).plus(inputs[0].value || "0"), + "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" + } + ); +}; diff --git a/core/strategies/protocols/aave-v2/payback-and-withdraw.ts b/core/strategies/protocols/aave-v2/payback-and-withdraw.ts index 2f5e033..fff0bfd 100644 --- a/core/strategies/protocols/aave-v2/payback-and-withdraw.ts +++ b/core/strategies/protocols/aave-v2/payback-and-withdraw.ts @@ -122,16 +122,17 @@ export default defineStrategy({ toBN(components[0].value).isZero() && toBN(components[1].value).isZero() ? initialStats - : newStats; + : newStats; - const liquidationPrice = BigNumber.max( - toBN(stats.totalBorrowInEth) - .div(stats.totalMaxLiquidationLimitInEth) - .times(position.ethPriceInUsd), - "0" - ).toFixed(); - - console.log(liquidationPrice); + let liquidationPrice = "0"; + if (!toBN(stats.ethSupplied).isZero()) { + liquidationPrice = BigNumber.max( + toBN(stats.totalBorrowInEth) + .div(stats.totalMaxLiquidationLimitInEth) + .times(position.ethPriceInUsd), + "0" + ).toFixed(); + } component.value = `${formatting.formatUsdMax( liquidationPrice,