assembly/composables/useStrategy.ts

135 lines
3.4 KiB
TypeScript
Raw Normal View History

2021-08-25 19:42:41 +00:00
import {
nextTick,
onMounted,
ref,
watch,
watchEffect
} from "@nuxtjs/composition-api";
2021-08-22 19:34:59 +00:00
import tokens from "~/constant/tokens";
2021-08-25 19:42:41 +00:00
import {
buildStrategy,
DefineStrategy,
IStrategy,
StrategyProtocol
} from "~/core/strategies";
import { position as aaveV2Position } from "./protocols/useAaveV2Position";
import { position as compoundPosition } from "./protocols/useCompoundPosition";
import { vault as makerPosition } from "./protocols/useMakerdaoPosition";
import { trove as liquityPosition } from "./protocols/useLiquityPosition";
2021-08-22 17:35:46 +00:00
import { useBalances } from "./useBalances";
import { useDSA } from "./useDSA";
2021-08-25 11:13:23 +00:00
import useEventBus from "./useEventBus";
2021-08-22 18:32:28 +00:00
import { useNotification } from "./useNotification";
import { useSidebar } from "./useSidebar";
2021-08-25 11:13:23 +00:00
import { useToken } from "./useToken";
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();
2021-08-25 11:13:23 +00:00
const { valInt, getTokenByKey } = useToken();
const { emitEvent } = useEventBus();
2021-08-22 18:32:28 +00:00
const {
showPendingTransaction,
showConfirmedTransaction
} = useNotification();
2021-08-22 17:35:46 +00:00
const strategy = buildStrategy(defineStrategy);
2021-08-23 23:07:53 +00:00
const inputs = ref(strategy.inputs);
2021-08-22 17:35:46 +00:00
const error = ref("");
2021-08-25 11:13:23 +00:00
const pending = ref(false);
2021-08-22 17:35:46 +00:00
strategy.onUpdated(async () => {
await nextTick();
2021-08-23 23:07:53 +00:00
inputs.value = strategy.inputs;
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-25 11:13:23 +00:00
pending.value = true;
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);
2021-08-25 11:13:23 +00:00
2021-08-25 19:42:41 +00:00
emitEvent(`protocol::${strategy.schema.protocol}::refresh`, {});
2021-08-22 18:32:28 +00:00
},
from: account.value
});
showPendingTransaction(tx);
close();
2021-08-22 17:35:46 +00:00
} catch (e) {
2021-08-25 11:13:23 +00:00
console.error(e);
2021-08-25 19:42:41 +00:00
2021-08-22 17:35:46 +00:00
error.value = e.message;
}
2021-08-25 11:13:23 +00:00
pending.value = false;
2021-08-22 17:35:46 +00:00
};
2021-08-25 19:42:41 +00:00
watchEffect(() => {
let position = null;
if (strategy.schema.protocol == StrategyProtocol.AAVE_V2) {
position = aaveV2Position.value;
} else if (strategy.schema.protocol == StrategyProtocol.MAKERDAO) {
position = makerPosition.value;
} else if (strategy.schema.protocol == StrategyProtocol.COMPOUND) {
position = compoundPosition.value;
} else if (strategy.schema.protocol == StrategyProtocol.LIQUITY) {
position = liquityPosition.value;
}
strategy.setProps({
2021-08-26 16:38:31 +00:00
convertTokenAmountToWei: valInt,
2021-08-25 19:42:41 +00:00
getTokenByKey,
position
});
});
2021-08-25 11:13:23 +00:00
2021-08-22 17:35:46 +00:00
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({
2021-08-25 19:42:41 +00:00
dsaBalances: balances.dsa[networkName.value],
userBalances: balances.user[networkName.value]
2021-08-22 17:35:46 +00:00
});
},
{ 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-25 11:13:23 +00:00
error,
2021-08-25 19:42:41 +00:00
pending
2021-08-22 17:35:46 +00:00
};
}