assembly/core/strategies/helpers/strategy.ts

139 lines
2.6 KiB
TypeScript
Raw Normal View History

2021-08-22 17:35:46 +00:00
import DSA from "dsa-connect";
2021-08-22 11:27:02 +00:00
import Web3 from "web3";
2021-08-22 17:35:46 +00:00
import { DefineStrategy, IStrategyContext } from ".";
2021-08-22 12:54:23 +00:00
2021-08-22 17:35:46 +00:00
export class Strategy {
schema: DefineStrategy;
inputs = [];
context = {
web3: null,
dsa: null
2021-08-22 15:45:37 +00:00
};
2021-08-22 11:27:02 +00:00
2021-08-22 17:35:46 +00:00
listeners = [];
2021-08-22 11:27:02 +00:00
2021-08-22 17:35:46 +00:00
props: object = {
prices: {},
dsaTokens: {},
2021-08-22 18:32:28 +00:00
userTokens: {}
2021-08-22 17:35:46 +00:00
};
2021-08-22 11:27:02 +00:00
2021-08-22 17:35:46 +00:00
constructor(schema: DefineStrategy) {
this.schema = schema;
2021-08-22 12:54:23 +00:00
2021-08-22 17:35:46 +00:00
this.inputs = this.schema.inputs;
}
2021-08-22 11:27:02 +00:00
2021-08-22 17:35:46 +00:00
getContext(): IStrategyContext {
return {
...this.context,
...this.props,
inputs: this.getInputs()
};
}
2021-08-22 15:45:37 +00:00
2021-08-22 17:35:46 +00:00
setProps(props: object) {
Object.assign(this.props, props);
2021-08-22 19:34:59 +00:00
this.notifyListeners()
2021-08-22 17:35:46 +00:00
}
2021-08-22 15:45:37 +00:00
2021-08-22 17:35:46 +00:00
getInputs() {
return this.inputs.map(input => ({
2021-08-22 13:49:31 +00:00
...input,
2021-08-22 17:35:46 +00:00
value: input.value || "",
error: input.error || "",
2021-08-22 15:45:37 +00:00
placeholder: () =>
input.placeholder
? input.placeholder({
2021-08-22 17:35:46 +00:00
...this.context,
inputs: this.inputs,
2021-08-22 15:45:37 +00:00
input: {
token: {
2021-08-22 17:35:46 +00:00
// todo
2021-08-22 18:32:28 +00:00
},
...input
2021-08-22 15:45:37 +00:00
}
})
: null,
2021-08-22 13:49:31 +00:00
onInput: (val: any) => {
2021-08-22 17:35:46 +00:00
input.error = "";
2021-08-22 13:49:31 +00:00
input.value = val;
2021-08-22 17:35:46 +00:00
if (val) {
input.error = input.validate({
...this.getContext(),
input
});
}
2021-08-22 18:32:28 +00:00
this.notifyListeners();
},
onCustomInput: (values: object) => {
2021-08-22 19:34:59 +00:00
input = Object.assign(input, values);
2021-08-22 18:32:28 +00:00
input.error = input.validate({
...this.getContext(),
input
});
2021-08-22 17:35:46 +00:00
this.notifyListeners();
2021-08-22 13:49:31 +00:00
}
2021-08-22 17:35:46 +00:00
}));
}
2021-08-22 12:54:23 +00:00
2021-08-22 18:32:28 +00:00
async submit(options) {
2021-08-22 17:35:46 +00:00
await this.validate();
2021-08-22 12:54:23 +00:00
2021-08-22 17:35:46 +00:00
const allSpells = await this.schema.spells(this.getContext());
2021-08-22 15:45:37 +00:00
2021-08-22 17:35:46 +00:00
const spells = this.context.dsa.Spell();
2021-08-22 15:45:37 +00:00
2021-08-22 17:35:46 +00:00
for (const spell of allSpells) {
spells.add(spell);
}
return await this.context.dsa.cast({
spells,
2021-08-22 18:32:28 +00:00
onReceipt: options?.onReceipt,
from: options?.from
2021-08-22 17:35:46 +00:00
});
}
async validate() {
const inputs = this.getInputs();
for (const input of inputs) {
2021-08-22 18:32:28 +00:00
if (typeof input.validate !== "function") {
continue;
}
2021-08-22 17:35:46 +00:00
const result = await input.validate({
...this.getContext(),
input
2021-08-22 13:49:31 +00:00
});
2021-08-22 17:35:46 +00:00
if (typeof result === "string") {
throw new Error(result || "Error has occurred");
2021-08-22 13:49:31 +00:00
}
}
2021-08-22 17:35:46 +00:00
}
setWeb3(web3: Web3) {
this.context.web3 = web3;
}
2021-08-22 13:49:31 +00:00
2021-08-22 17:35:46 +00:00
setDSA(dsa: DSA) {
this.context.dsa = dsa;
}
async notifyListeners() {
for (const listener of this.listeners) {
await listener(this);
}
}
onUpdated(cb) {
this.listeners.push(cb);
}
}