diff --git a/composables/useStrategy.ts b/composables/useStrategy.ts index 4b38b3b..86c31bf 100644 --- a/composables/useStrategy.ts +++ b/composables/useStrategy.ts @@ -85,7 +85,7 @@ export function useStrategy(defineStrategy: DefineStrategy) { } strategy.setProps({ - convertTokenAmountToBigNumber: valInt, + convertTokenAmountToWei: valInt, getTokenByKey, position }); diff --git a/core/strategies/helpers/index.ts b/core/strategies/helpers/index.ts index 054fb43..f9d9bc1 100644 --- a/core/strategies/helpers/index.ts +++ b/core/strategies/helpers/index.ts @@ -6,13 +6,13 @@ export interface IStrategyContext { dsa: DSA; web3: Web3; inputs: IStrategyInput[]; - dsadsaBalances?: { [address: string]: IStrategyToken }; - userdsaBalances?: { [address: string]: IStrategyToken }; + dsaBalances?: { [address: string]: IStrategyToken }; + userBalances?: { [address: string]: IStrategyToken }; tokens?: { [address: string]: IStrategyToken }; - convertTokenAmountToBigNumber?: (value: any, decimals: any) => string; + convertTokenAmountToWei?: (value: any, decimals: any) => string; getTokenByKey?: (key: string) => IStrategyToken; position?: any; - variables?: object; + variables?: { [key: string]: any }; } export interface IStrategyToken { @@ -42,7 +42,7 @@ export interface IStrategyInput { type: InputType; name: string; - variables?: object; + variables?: { [key: string]: any }; placeholder?: ( context: IStrategyContext & { diff --git a/core/strategies/helpers/strategy.ts b/core/strategies/helpers/strategy.ts index 7673fb3..cf3507e 100644 --- a/core/strategies/helpers/strategy.ts +++ b/core/strategies/helpers/strategy.ts @@ -34,8 +34,7 @@ export class Strategy { getContext(): IStrategyContext { return { - ...this.context, - ...this.props, + ...this.getBaseContext(), inputs: this.inputs }; } diff --git a/core/strategies/protocols/aave-v2/deposit-and-borrow.ts b/core/strategies/protocols/aave-v2/deposit-and-borrow.ts index 61bde9b..37e6f93 100644 --- a/core/strategies/protocols/aave-v2/deposit-and-borrow.ts +++ b/core/strategies/protocols/aave-v2/deposit-and-borrow.ts @@ -1,3 +1,4 @@ +import BigNumber from "bignumber.js"; import { defineStrategy, defineInput, @@ -18,12 +19,46 @@ export default defineStrategy({ author: "Instadapp Team", + variables: { + collateralTokenKey: "eth", + debtTokenKey: "dai", + debtRateMode: 2, + }, + inputs: [ + defineInput({ + type: StrategyInputType.INPUT_WITH_TOKEN, + name: "Collateral", + placeholder: ({ input }) => + input.token ? `${input.token.symbol} to Deposit` : "", + validate: ({ input, dsaBalances }) => { + if (!input.token) { + return "Collateral token is required"; + } + + if (!input.value) { + return "Collateral amount is required"; + } + + const collateralBalance = new BigNumber( + dsaBalances[input.token.address]?.balance + ); + + if (new BigNumber(collateralBalance).lt(input.value)) { + const collateralBalanceFormatted = collateralBalance.toFixed(2); + return `Your amount exceeds your maximum limit of ${collateralBalanceFormatted} ${input.token.symbol}`; + } + }, + defaults: ({ getTokenByKey, variables }) => ({ + token: getTokenByKey?.(variables.collateralTokenKey) + }) + }), + defineInput({ type: StrategyInputType.INPUT_WITH_TOKEN, name: "Debt", placeholder: ({ input }) => - input.token ? `${input.token.symbol} to Payback` : "", + input.token ? `${input.token.symbol} to Borrow` : "", validate: ({ input }) => { if (!input.token) { return "Debt token is required"; @@ -32,46 +67,21 @@ export default defineStrategy({ if (!input.value) { return "Debt amount is required"; } - - // if (input.token.balance < input.value) { - // return "Your amount exceeds your maximum limit."; - // } }, - defaults: ({ getTokenByKey }) => ({ - token: getTokenByKey?.("eth") - }) - }), - defineInput({ - type: StrategyInputType.INPUT_WITH_TOKEN, - name: "Collateral", - placeholder: ({ input }) => - input.token ? `${input.token.symbol} to Withdraw` : "", - validate: ({ input }) => { - if (!input.token) { - return "Collateral token is required"; - } - - if (!input.value) { - return "Collateral amount is required"; - } - }, - defaults: ({ getTokenByKey }) => ({ - token: getTokenByKey?.("dai") + defaults: ({ getTokenByKey, variables }) => ({ + token: getTokenByKey?.(variables.debtTokenKey) }) }) ], - spells: async ({ inputs, convertTokenAmountToBigNumber }) => { + spells: async ({ inputs, convertTokenAmountToWei, variables }) => { return [ { connector: "aave_v2", method: "deposit", args: [ inputs[0].token.address, - convertTokenAmountToBigNumber( - inputs[0].value, - inputs[0].token.decimals - ), + convertTokenAmountToWei(inputs[0].value, inputs[0].token.decimals), 0, 0 ] @@ -81,11 +91,8 @@ export default defineStrategy({ method: "borrow", args: [ inputs[1].token.address, - convertTokenAmountToBigNumber( - inputs[1].value, - inputs[1].token.decimals - ), - 2, + convertTokenAmountToWei(inputs[1].value, inputs[1].token.decimals), + variables.debtRateMode, 0, 0 ] diff --git a/core/strategies/protocols/aave-v2/payback-and-withdraw.ts b/core/strategies/protocols/aave-v2/payback-and-withdraw.ts index 38e9174..5dcbae2 100644 --- a/core/strategies/protocols/aave-v2/payback-and-withdraw.ts +++ b/core/strategies/protocols/aave-v2/payback-and-withdraw.ts @@ -48,14 +48,14 @@ export default defineStrategy({ }) ], - spells: async ({ inputs, convertTokenAmountToBigNumber }) => { + spells: async ({ inputs, convertTokenAmountToWei }) => { return [ { connector: "aave_v2", method: "payback", args: [ inputs[0].token.address, - convertTokenAmountToBigNumber( + convertTokenAmountToWei( inputs[0].value, inputs[0].token.decimals ), @@ -69,7 +69,7 @@ export default defineStrategy({ method: "withdraw", args: [ inputs[1].token.address, - convertTokenAmountToBigNumber( + convertTokenAmountToWei( inputs[1].value, inputs[1].token.decimals ),