This commit is contained in:
Georges KABBOUCHI 2021-08-22 18:45:37 +03:00
parent 2f28caa1de
commit b5b7141661
3 changed files with 81 additions and 48 deletions

View File

@ -14,6 +14,7 @@
:key="index" :key="index"
:value="input.value" :value="input.value"
@input="$event => input.onInput($event.target.value)" @input="$event => input.onInput($event.target.value)"
:placeholder="input.placeholder()"
/> />
</div> </div>
</div> </div>
@ -53,7 +54,7 @@ export default defineComponent({
strategies.find(strategy => strategy.id === props.strategy) strategies.find(strategy => strategy.id === props.strategy)
); );
watch(() => { watch([], () => {
selectedStrategy.value = strategies.find( selectedStrategy.value = strategies.find(
strategy => strategy.id === props.strategy strategy => strategy.id === props.strategy
); );

View File

@ -1,10 +1,10 @@
import DSA from "dsa-connect"; import DSA, { Spell } from "dsa-connect";
import Web3 from "web3"; import Web3 from "web3";
import slugify from "slugify"; import slugify from "slugify";
export interface IStrategyContext { export interface IStrategyContext {
dsa: typeof DSA; dsa: DSA;
web3: Web3; web3: Web3;
inputs: IStrategyInput[]; inputs: IStrategyInput<StrategyInputType>[];
} }
export interface IStrategyToken { export interface IStrategyToken {
@ -22,24 +22,27 @@ export enum StrategyInputType {
INPUT_WITH_TOKEN = "input-with-token" INPUT_WITH_TOKEN = "input-with-token"
} }
// type InputTypes = { export type StrategyInputParameterMap = {
// [StrategyInputType.INPUT] : { [StrategyInputType.INPUT]: {};
// token?: IStrategyToken;
// value?: any;
// };
// }
export interface IStrategyInput { [StrategyInputType.INPUT_WITH_TOKEN]: {
type: StrategyInputType; token?: IStrategyToken;
};
};
export interface IStrategyInput<InputType extends StrategyInputType> {
type: InputType;
name: string; name: string;
placeholder: placeholder: (
| string context: IStrategyContext & {
| ((context: IStrategyContext & { input: IStrategyInput }) => string); input: IStrategyInput<InputType> & StrategyInputParameterMap[InputType];
}
) => string;
validate?: ( validate?: (
context: IStrategyContext & { input: IStrategyInput } context: IStrategyContext & {
) => boolean | string; input: IStrategyInput<InputType> & StrategyInputParameterMap[InputType];
// If type is "input-with-token", this is the token }
token?: IStrategyToken; ) => string | void;
value?: any; value?: any;
[key: string]: any; [key: string]: any;
@ -51,41 +54,71 @@ export interface IStrategy {
description: string; description: string;
author?: string; author?: string;
inputs: IStrategyInput[]; inputs: IStrategyInput<any>[];
spells: (context: IStrategyContext) => any; spells: (context: IStrategyContext) => Promise<Spell[]> | Spell[];
submitText?: string; submitText?: string;
} }
export function defineInput<InputType extends StrategyInputType>(
input: IStrategyInput<InputType>
) {
return input as IStrategyInput<any>;
}
export function defineStrategy(strategy: IStrategy) { export function defineStrategy(strategy: IStrategy) {
const context = {
web3: null,
dsa: null
};
return { return {
...strategy, ...strategy,
id: strategy.id ? strategy.id : slugify(strategy.name).toLowerCase(), id: strategy.id ? strategy.id : slugify(strategy.name).toLowerCase(),
inputs: strategy.inputs.map(input => ({ inputs: strategy.inputs.map(input => ({
...input, ...input,
value: null, value: null,
placeholder: () =>
input.placeholder
? input.placeholder({
...context,
inputs: strategy.inputs,
input: {
...input,
token: {
// todo
}
}
})
: null,
onInput: (val: any) => { onInput: (val: any) => {
input.value = val; input.value = val;
} }
})), })),
submit: async (context: Pick<IStrategyContext, "web3" | "dsa">) => { submit: async () => {
await this.validate({ await this.validate({
...context, ...context,
inputs: strategy.inputs inputs: strategy.inputs
}); });
const spells = strategy.spells({ const allSpells = await strategy.spells({
...context, ...context,
inputs: strategy.inputs inputs: strategy.inputs
}); });
const spells = context.dsa.Spell();
for (const spell of allSpells) {
spells.add(spell);
}
return await context.dsa.cast({ return await context.dsa.cast({
spells, spells,
onReceipt: this.onReceipt onReceipt: this.onReceipt
}); });
}, },
validate: async (context: IStrategyContext) => { validate: async () => {
for (const input of this.inputs) { for (const input of this.inputs) {
const result = await input.validate({ const result = await input.validate({
...context, ...context,

View File

@ -1,4 +1,8 @@
import { defineStrategy, StrategyInputType } from "../../helpers/strategy"; import {
defineStrategy,
defineInput,
StrategyInputType
} from "../../helpers/strategy";
export default defineStrategy({ export default defineStrategy({
name: "Deposit & Borrow", name: "Deposit & Borrow",
@ -6,7 +10,7 @@ export default defineStrategy({
author: "Instadapp Team", author: "Instadapp Team",
inputs: [ inputs: [
{ defineInput({
type: StrategyInputType.INPUT_WITH_TOKEN, type: StrategyInputType.INPUT_WITH_TOKEN,
name: "Debt", name: "Debt",
placeholder: ({ input }) => `${input.token.symbol} to Payback`, placeholder: ({ input }) => `${input.token.symbol} to Payback`,
@ -18,32 +22,27 @@ export default defineStrategy({
if (input.token.balance < input.value) { if (input.token.balance < input.value) {
return "Your amount exceeds your maximum limit."; return "Your amount exceeds your maximum limit.";
} }
return true;
} }
}, }),
{ defineInput({
type: StrategyInputType.INPUT_WITH_TOKEN, type: StrategyInputType.INPUT_WITH_TOKEN,
name: "Collateral", name: "Collateral",
placeholder: ({ input }) => `${input.token.symbol} to Withdraw` placeholder: ({ input }) => `${input.token.symbol} to Withdraw`
} })
], ],
spells: async ({ dsa, inputs }) => { spells: async ({ inputs }) => {
const spells = dsa.Spells(); return [
{
spells.add({ connector: "aave_v2",
connector: "aave_v2", method: "deposit",
method: "deposit", args: [inputs[0].token.address, inputs[0].value, 0, 0]
args: [inputs[0].token.address, inputs[0].value, 0, 0] },
}); {
connector: "aave_v2",
spells.add({ method: "borrow",
connector: "aave_v2", args: [inputs[1].token.address, inputs[1].value, 0, 0, 0]
method: "borrow", }
args: [inputs[1].token.address, inputs[1].value, 0, 0, 0] ];
});
return spells;
} }
}); });