2025-10-03 16:51:44 +02:00
|
|
|
import { ref, reactive, computed } from 'vue';
|
|
|
|
|
import { getAccount, getBalance, watchAccount, watchChainId, type Config } from '@wagmi/core';
|
|
|
|
|
import { type WatchAccountReturnType, type GetAccountReturnType, type GetBalanceReturnType } from '@wagmi/core';
|
|
|
|
|
import { config } from '@/wagmi';
|
|
|
|
|
import { getAllowance, HarbContract, getNonce } from '@/contracts/harb';
|
|
|
|
|
import logger from '@/utils/logger';
|
|
|
|
|
import { setHarbContract } from '@/contracts/harb';
|
|
|
|
|
import { setStakeContract } from '@/contracts/stake';
|
|
|
|
|
import { chainsData, DEFAULT_CHAIN_ID } from '@/config';
|
2025-09-23 14:18:04 +02:00
|
|
|
|
|
|
|
|
const balance = ref<GetBalanceReturnType>({
|
2025-10-03 16:51:44 +02:00
|
|
|
value: 0n,
|
|
|
|
|
decimals: 0,
|
|
|
|
|
symbol: '',
|
|
|
|
|
formatted: '',
|
2025-09-23 14:18:04 +02:00
|
|
|
});
|
2025-10-03 16:51:44 +02:00
|
|
|
const account = ref<GetAccountReturnType>(getAccount(config as Config));
|
2025-09-24 09:41:28 +02:00
|
|
|
if (!account.value.chainId) {
|
2025-10-03 16:51:44 +02:00
|
|
|
account.value.chainId = DEFAULT_CHAIN_ID;
|
2025-09-24 09:41:28 +02:00
|
|
|
}
|
2025-09-23 14:18:04 +02:00
|
|
|
|
2025-09-23 19:24:05 +02:00
|
|
|
const selectedChainId = computed(() => account.value.chainId ?? DEFAULT_CHAIN_ID);
|
|
|
|
|
|
2025-09-23 14:18:04 +02:00
|
|
|
export const chainData = computed(() => {
|
2025-10-03 16:51:44 +02:00
|
|
|
return chainsData.find(obj => obj.id === selectedChainId.value);
|
|
|
|
|
});
|
2025-09-23 14:18:04 +02:00
|
|
|
|
2025-10-03 16:51:44 +02:00
|
|
|
let unwatch: WatchAccountReturnType | null = null;
|
|
|
|
|
let unwatchChain: WatchAccountReturnType | null = null;
|
2025-09-23 14:18:04 +02:00
|
|
|
export function useWallet() {
|
2025-10-03 16:51:44 +02:00
|
|
|
async function loadBalance() {
|
|
|
|
|
logger.contract('loadBalance');
|
|
|
|
|
if (account.value.address) {
|
|
|
|
|
// console.log("HarbContract",HarbContract );
|
2025-09-23 14:18:04 +02:00
|
|
|
|
2025-10-03 16:51:44 +02:00
|
|
|
balance.value = await getBalance(config as Config, {
|
|
|
|
|
address: account.value.address,
|
|
|
|
|
token: HarbContract.contractAddress,
|
|
|
|
|
});
|
|
|
|
|
// console.log("balance.value", balance.value);
|
2025-09-23 14:18:04 +02:00
|
|
|
|
2025-10-03 16:51:44 +02:00
|
|
|
return balance.value;
|
|
|
|
|
} else {
|
|
|
|
|
return 0n;
|
2025-09-23 14:18:04 +02:00
|
|
|
}
|
2025-10-03 16:51:44 +02:00
|
|
|
}
|
2025-09-23 14:18:04 +02:00
|
|
|
|
2025-10-03 16:51:44 +02:00
|
|
|
if (!unwatch) {
|
|
|
|
|
// console.log("useWallet function");
|
2025-09-23 14:18:04 +02:00
|
|
|
|
2025-10-03 16:51:44 +02:00
|
|
|
unwatch = watchAccount(config as Config, {
|
|
|
|
|
async onChange(data) {
|
|
|
|
|
// console.log("watchaccount-useWallet", data);
|
2025-09-23 14:18:04 +02:00
|
|
|
|
2025-10-03 16:51:44 +02:00
|
|
|
if (!data.address) {
|
|
|
|
|
logger.info(`disconnected`);
|
|
|
|
|
balance.value = {
|
|
|
|
|
value: 0n,
|
|
|
|
|
decimals: 0,
|
|
|
|
|
symbol: '',
|
|
|
|
|
formatted: '',
|
|
|
|
|
};
|
|
|
|
|
} else if (account.value.address !== data.address || account.value.chainId !== data.chainId) {
|
|
|
|
|
logger.info(`Account changed!:`, data.address);
|
|
|
|
|
account.value = data;
|
|
|
|
|
await loadBalance();
|
|
|
|
|
await getAllowance();
|
|
|
|
|
// await loadPositions();
|
|
|
|
|
await getNonce();
|
|
|
|
|
setHarbContract();
|
|
|
|
|
setStakeContract();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//funzt nicht mehr-> library Änderung?
|
|
|
|
|
if (!unwatchChain) {
|
|
|
|
|
// console.log("unwatchChain");
|
|
|
|
|
|
|
|
|
|
unwatchChain = watchChainId(config as Config, {
|
|
|
|
|
async onChange(_chainId) {
|
|
|
|
|
await loadBalance();
|
|
|
|
|
await getAllowance();
|
|
|
|
|
await getNonce();
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-09-23 14:18:04 +02:00
|
|
|
|
2025-10-03 16:51:44 +02:00
|
|
|
return reactive({ balance, account, loadBalance });
|
2025-09-23 14:18:04 +02:00
|
|
|
}
|