From 4951b6733b6a937e7e20c777bfa383791faab3bc Mon Sep 17 00:00:00 2001 From: Georges KABBOUCHI Date: Sun, 22 Aug 2021 14:27:02 +0300 Subject: [PATCH] wip --- core/strategies/helpers/strategy.ts | 49 +++++++++++++++++++ core/strategies/index.ts | 5 ++ .../protocols/aave-v2/deposit-and-borrow.ts | 49 +++++++++++++++++++ core/strategies/protocols/aave-v2/index.ts | 6 +++ 4 files changed, 109 insertions(+) create mode 100644 core/strategies/helpers/strategy.ts create mode 100644 core/strategies/index.ts create mode 100644 core/strategies/protocols/aave-v2/deposit-and-borrow.ts create mode 100644 core/strategies/protocols/aave-v2/index.ts diff --git a/core/strategies/helpers/strategy.ts b/core/strategies/helpers/strategy.ts new file mode 100644 index 0000000..79cf986 --- /dev/null +++ b/core/strategies/helpers/strategy.ts @@ -0,0 +1,49 @@ +import DSA from "dsa-connect"; +import Web3 from "web3"; + +export interface IStrategyContext { + dsa: typeof DSA; + web3: Web3; + inputs: IStrategyInput[]; +} + +export interface IStrategyToken { + address: string + key: string + symbol: string + balance: string + + supply: string + borrow: string +} + +export enum StrategyInputType { + INPUT = "input", + INPUT_WITH_TOKEN = "input-with-token" +} + +export interface IStrategyInput { + type: StrategyInputType; + name: string; + placeholder: + | string + | ((context: IStrategyContext & { input: IStrategyInput }) => string); + validate?: ((context: IStrategyContext & { input: IStrategyInput }) => boolean|string); + // If type is "input-with-token", this is the token + token?: IStrategyToken; + value?: any; +} + +export interface IStrategy { + name: string; + description: string; + author?: string; + + inputs: IStrategyInput[]; + + spells: (context: IStrategyContext) => any; +} + +export function defineStrategy(strategy: IStrategy): IStrategy { + return strategy; +} diff --git a/core/strategies/index.ts b/core/strategies/index.ts new file mode 100644 index 0000000..47e70d5 --- /dev/null +++ b/core/strategies/index.ts @@ -0,0 +1,5 @@ +import AaveV2 from "./protocols/aave-v2" + +export const strategies = { + aaveV2 : AaveV2, +} \ No newline at end of file diff --git a/core/strategies/protocols/aave-v2/deposit-and-borrow.ts b/core/strategies/protocols/aave-v2/deposit-and-borrow.ts new file mode 100644 index 0000000..76582c6 --- /dev/null +++ b/core/strategies/protocols/aave-v2/deposit-and-borrow.ts @@ -0,0 +1,49 @@ +import { defineStrategy, StrategyInputType } from "../../helpers/strategy"; + +export default defineStrategy({ + name: "Deposit & Borrow", + description: "Deposit collateral & borrow asset in a single txn.", + author: "Instadapp Team", + + inputs: [ + { + type: StrategyInputType.INPUT_WITH_TOKEN, + name: "Debt", + placeholder: ({ input }) => `${input.token.symbol} to Payback`, + validate: ({ input }) => { + if (!input.token) { + return "Token is required"; + } + + if (input.token.balance < input.value) { + return "Your amount exceeds your maximum limit."; + } + + return true; + } + }, + { + type: StrategyInputType.INPUT_WITH_TOKEN, + name: "Collateral", + placeholder: ({ input }) => `${input.token.symbol} to Withdraw` + } + ], + + spells: async ({ dsa, inputs }) => { + const spells = dsa.Spells(); + + spells.add({ + connector: "aave_v2", + method: "deposit", + args: [inputs[0].token.address, inputs[0].value, 0, 0] + }); + + spells.add({ + connector: "aave_v2", + method: "borrow", + args: [inputs[1].token.address, inputs[1].value, 0, 0, 0] + }); + + return spells; + } +}); diff --git a/core/strategies/protocols/aave-v2/index.ts b/core/strategies/protocols/aave-v2/index.ts new file mode 100644 index 0000000..f6bac2d --- /dev/null +++ b/core/strategies/protocols/aave-v2/index.ts @@ -0,0 +1,6 @@ +import depositAndBorrow from "./deposit-and-borrow" + +export default [ + depositAndBorrow +] + \ No newline at end of file