2021-08-22 17:35:46 +00:00
|
|
|
import DSA, { Spell } from "dsa-connect";
|
|
|
|
import Web3 from "web3";
|
|
|
|
import slugify from "slugify";
|
|
|
|
import { Strategy } from "./strategy";
|
|
|
|
export interface IStrategyContext {
|
|
|
|
dsa: DSA;
|
|
|
|
web3: Web3;
|
|
|
|
inputs: IStrategyInput<StrategyInputType>[];
|
2021-08-25 19:42:41 +00:00
|
|
|
dsadsaBalances?: { [address: string]: IStrategyToken };
|
|
|
|
userdsaBalances?: { [address: string]: IStrategyToken };
|
|
|
|
tokens?: { [address: string]: IStrategyToken };
|
2021-08-25 11:13:23 +00:00
|
|
|
convertTokenAmountToBigNumber?: (value: any, decimals: any) => string;
|
|
|
|
getTokenByKey?: (key: string) => IStrategyToken;
|
2021-08-25 19:42:41 +00:00
|
|
|
position?: any;
|
2021-08-22 17:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 type StrategyInputParameterMap = {
|
|
|
|
[StrategyInputType.INPUT]: {};
|
|
|
|
|
|
|
|
[StrategyInputType.INPUT_WITH_TOKEN]: {
|
|
|
|
token?: IStrategyToken;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export interface IStrategyInput<InputType extends StrategyInputType> {
|
|
|
|
type: InputType;
|
|
|
|
name: string;
|
2021-08-25 19:42:41 +00:00
|
|
|
|
|
|
|
variables?: object;
|
|
|
|
|
2021-08-22 17:35:46 +00:00
|
|
|
placeholder?: (
|
|
|
|
context: IStrategyContext & {
|
|
|
|
input: IStrategyInput<InputType> & StrategyInputParameterMap[InputType];
|
|
|
|
}
|
|
|
|
) => string;
|
|
|
|
validate?: (
|
|
|
|
context: IStrategyContext & {
|
|
|
|
input: IStrategyInput<InputType> & StrategyInputParameterMap[InputType];
|
|
|
|
}
|
|
|
|
) => string | void;
|
2021-08-23 23:07:53 +00:00
|
|
|
|
2021-08-25 11:13:23 +00:00
|
|
|
defaults?: (context: Omit<IStrategyContext, "inputs">) => object;
|
|
|
|
|
2021-08-22 17:35:46 +00:00
|
|
|
value?: any;
|
|
|
|
|
|
|
|
[key: string]: any;
|
|
|
|
}
|
|
|
|
|
2021-08-25 11:13:23 +00:00
|
|
|
export enum StrategyProtocol {
|
2021-08-25 19:42:41 +00:00
|
|
|
AAVE_V2 = "aaveV2",
|
|
|
|
COMPOUND = "compound",
|
|
|
|
MAKERDAO = "makerdao",
|
|
|
|
LIQUITY = "liquity",
|
2021-08-25 11:13:23 +00:00
|
|
|
}
|
2021-08-22 17:35:46 +00:00
|
|
|
export interface IStrategy {
|
2021-08-25 11:13:23 +00:00
|
|
|
protocol: StrategyProtocol;
|
2021-08-22 17:35:46 +00:00
|
|
|
id?: string;
|
|
|
|
name: string;
|
|
|
|
description: string;
|
2021-08-25 11:13:23 +00:00
|
|
|
details?: string;
|
2021-08-22 17:35:46 +00:00
|
|
|
author?: string;
|
|
|
|
|
|
|
|
inputs: IStrategyInput<StrategyInputType>[];
|
|
|
|
|
|
|
|
spells: (context: IStrategyContext) => Promise<Spell[]> | Spell[];
|
|
|
|
|
|
|
|
submitText?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function defineInput<InputType extends StrategyInputType>(
|
|
|
|
input: IStrategyInput<InputType>
|
|
|
|
) {
|
|
|
|
return input as IStrategyInput<InputType>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function defineStrategy(strategy: IStrategy) {
|
|
|
|
return {
|
|
|
|
...strategy,
|
|
|
|
id: strategy.id ? strategy.id : slugify(strategy.name).toLowerCase()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function buildStrategy(schema: DefineStrategy) {
|
2021-08-23 23:07:53 +00:00
|
|
|
return new Strategy(schema);
|
2021-08-22 17:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export type DefineStrategy = ReturnType<typeof defineStrategy>;
|