assembly/core/strategies/helpers/strategy.ts

131 lines
2.4 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: {},
userTokens: {},
};
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 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: {
...input,
token: {
2021-08-22 17:35:46 +00:00
// todo
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
});
}
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 17:35:46 +00:00
async submit() {
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
console.log(spells);
for (const spell of allSpells) {
spells.add(spell);
}
return await this.context.dsa.cast({
spells,
onReceipt: this.onReceipt
});
}
async validate() {
const inputs = this.getInputs();
for (const input of inputs) {
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
}
onReceipt(txHash: string, txReceipt: any) {
// do something
}
setWeb3(web3: Web3) {
console.log(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);
}
}