assembly/composables/useStrategy.ts

89 lines
2.2 KiB
TypeScript
Raw Normal View History

2021-08-22 17:35:46 +00:00
import { nextTick, onMounted, ref, watch } from "@nuxtjs/composition-api";
2021-08-22 19:34:59 +00:00
import tokens from "~/constant/tokens";
2021-08-22 17:35:46 +00:00
import { buildStrategy, DefineStrategy, IStrategy } from "~/core/strategies";
import { useBalances } from "./useBalances";
import { useDSA } from "./useDSA";
2021-08-22 18:32:28 +00:00
import { useNotification } from "./useNotification";
import { useSidebar } from "./useSidebar";
2021-08-22 17:35:46 +00:00
import { useWeb3 } from "./useWeb3";
export function useStrategy(defineStrategy: DefineStrategy) {
2021-08-22 18:32:28 +00:00
const { web3, networkName, account } = useWeb3();
2021-08-22 17:35:46 +00:00
const { dsa } = useDSA();
2021-08-22 18:32:28 +00:00
const { prices, balances, fetchBalances } = useBalances();
const { close } = useSidebar();
const {
showPendingTransaction,
showConfirmedTransaction
} = useNotification();
2021-08-22 17:35:46 +00:00
const strategy = buildStrategy(defineStrategy);
const inputs = ref(strategy.getInputs());
const error = ref("");
strategy.onUpdated(async () => {
await nextTick();
inputs.value = strategy.getInputs();
2021-08-22 18:32:28 +00:00
console.log("onUpdated");
2021-08-22 17:35:46 +00:00
});
const submit = async () => {
2021-08-22 18:32:28 +00:00
error.value = "";
2021-08-22 17:35:46 +00:00
try {
2021-08-22 18:32:28 +00:00
const tx = await strategy.submit({
onReceipt: async () => {
showConfirmedTransaction(tx);
await fetchBalances(true);
},
from: account.value
});
showPendingTransaction(tx);
close();
2021-08-22 17:35:46 +00:00
} catch (e) {
error.value = e.message;
}
};
watch(web3, () => strategy.setWeb3(web3.value), { immediate: true });
watch(dsa, () => strategy.setDSA(dsa.value), { immediate: true });
watch(
prices,
() => strategy.setProps({ prices: prices[networkName.value] }),
{ immediate: true }
);
watch(
balances,
() => {
strategy.setProps({
dsaTokens: balances.dsa[networkName.value],
userTokens: balances.user[networkName.value]
});
},
{ immediate: true }
);
2021-08-22 19:34:59 +00:00
watch(
networkName,
() =>
strategy.setProps({
tokens: tokens[networkName.value].allTokens,
tokenKeys: tokens[networkName.value].tokenKeys
}),
{ immediate: true }
);
2021-08-22 17:35:46 +00:00
// testing
onMounted(() => {
//@ts-ignore
window.strategy = strategy;
});
return {
strategy,
inputs,
submit,
2021-08-22 18:32:28 +00:00
error
2021-08-22 17:35:46 +00:00
};
}