78 lines
2.2 KiB
TypeScript
78 lines
2.2 KiB
TypeScript
import { ref, reactive, computed, type ComputedRef } from 'vue';
|
|
import * as StakeContract from '@/contracts/stake';
|
|
import { waitForTransactionReceipt, type Config } from '@wagmi/core';
|
|
import { config } from '@/wagmi';
|
|
// import { compactNumber, formatBigIntDivision } from "@/utils/helper";
|
|
import { useContractToast } from './useContractToast';
|
|
import { TAX_RATE_OPTIONS } from 'kraiken-lib/taxRates';
|
|
|
|
const contractToast = useContractToast();
|
|
|
|
enum State {
|
|
SignTransaction = 'SignTransaction',
|
|
Waiting = 'Waiting',
|
|
Action = 'Action',
|
|
}
|
|
|
|
export const taxRates = TAX_RATE_OPTIONS.map(({ index, year, daily, decimal }) => ({
|
|
index,
|
|
year,
|
|
daily,
|
|
decimal,
|
|
}));
|
|
|
|
const taxRateIndexByDecimal = new Map<number, number>();
|
|
|
|
for (const option of taxRates) {
|
|
taxRateIndexByDecimal.set(option.decimal, option.index);
|
|
}
|
|
|
|
export function getTaxRateOptionByIndex(index: number) {
|
|
return taxRates[index];
|
|
}
|
|
|
|
export function getTaxRateIndexByDecimal(decimal: number) {
|
|
return taxRateIndexByDecimal.get(decimal);
|
|
}
|
|
|
|
export function useAdjustTaxRate() {
|
|
const loading = ref();
|
|
const waiting = ref();
|
|
|
|
const state: ComputedRef<State> = computed(() => {
|
|
if (loading.value) {
|
|
return State.SignTransaction;
|
|
} else if (waiting.value) {
|
|
return State.Waiting;
|
|
} else {
|
|
return State.Action;
|
|
}
|
|
});
|
|
|
|
async function changeTax(positionId: bigint, taxRateIndex: number) {
|
|
try {
|
|
loading.value = true;
|
|
const option = getTaxRateOptionByIndex(taxRateIndex);
|
|
if (!option) {
|
|
throw new Error(`Invalid tax rate index: ${taxRateIndex}`);
|
|
}
|
|
|
|
const hash = await StakeContract.changeTax(positionId, option.index);
|
|
loading.value = false;
|
|
waiting.value = true;
|
|
await waitForTransactionReceipt(config as Config, {
|
|
hash: hash,
|
|
});
|
|
|
|
contractToast.showSuccessToast(option.year.toString(), 'Success!', 'You adjusted your position tax to', '', '%');
|
|
waiting.value = false;
|
|
} catch (error: unknown) {
|
|
contractToast.showFailToast((error as { shortMessage?: string })?.shortMessage ?? 'Transaction failed');
|
|
} finally {
|
|
loading.value = false;
|
|
waiting.value = false;
|
|
}
|
|
}
|
|
|
|
return reactive({ state, taxRates, changeTax });
|
|
}
|