assembly/composables/useStrategy.ts

143 lines
3.8 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,
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";
2021-09-04 23:12:21 +00:00
import { trove as liquityPosition, troveTypes, troveOverallDetails } 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";
2021-08-26 18:34:27 +00:00
import { useBigNumber } from "./useBigNumber";
2021-09-04 17:39:05 +00:00
import tokenIdMapping from "~/constant/tokenIdMapping";
2021-09-05 11:01:19 +00:00
import { useFormatting } from "./useFormatting";
2021-08-26 18:34:27 +00:00
2021-08-22 17:35:46 +00:00
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-26 18:34:27 +00:00
const { toBN } = useBigNumber();
2021-09-05 11:01:19 +00:00
const formatting = useFormatting();
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-09-05 11:46:39 +00:00
const components = ref(strategy.components);
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
2021-09-04 16:50:56 +00:00
// strategy.onUpdated(async () => {
// await nextTick();
// });
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) {
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;
2021-09-04 23:12:21 +00:00
let positionExtra = {}
2021-08-25 19:42:41 +00:00
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;
2021-09-04 23:12:21 +00:00
positionExtra["troveTypes"] = troveTypes.value;
positionExtra["troveOverallDetails"] = troveOverallDetails.value;
2021-08-25 19:42:41 +00:00
}
strategy.setProps({
2021-08-26 16:38:31 +00:00
convertTokenAmountToWei: valInt,
2021-08-25 19:42:41 +00:00
getTokenByKey,
2021-08-26 18:34:27 +00:00
toBN,
2021-09-04 17:39:05 +00:00
position,
2021-09-04 23:12:21 +00:00
positionExtra,
2021-09-05 11:01:19 +00:00
tokenIdMapping,
formatting,
2021-08-25 19:42:41 +00:00
});
});
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,
2021-09-05 11:46:39 +00:00
components,
2021-08-22 17:35:46 +00:00
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
};
}