2025-10-03 16:51:44 +02:00
|
|
|
import { ref } from 'vue';
|
|
|
|
|
import { config } from '@/wagmi';
|
|
|
|
|
import { getAccount, readContract, writeContract, waitForTransactionReceipt, getChainId } from '@wagmi/core';
|
|
|
|
|
import type { Config } from '@wagmi/core';
|
2025-11-20 18:54:53 +01:00
|
|
|
import { KraikenAbi } from 'kraiken-lib/abis';
|
2025-10-03 16:51:44 +02:00
|
|
|
import { type Abi, type Address, type Hash } from 'viem';
|
|
|
|
|
import { StakeContract } from '@/contracts/stake';
|
|
|
|
|
import { getChain } from '@/config';
|
|
|
|
|
import logger from '@/utils/logger';
|
2025-10-11 10:55:49 +00:00
|
|
|
import { getWalletPublicClient } from '@/services/walletRpc';
|
2025-09-23 14:18:04 +02:00
|
|
|
|
|
|
|
|
interface Contract {
|
2025-10-03 16:51:44 +02:00
|
|
|
abi: Abi;
|
|
|
|
|
contractAddress: Address;
|
2025-09-23 14:18:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const allowance = ref();
|
|
|
|
|
export const nonce = ref();
|
|
|
|
|
export const name = ref();
|
|
|
|
|
export const ubiDue = ref();
|
|
|
|
|
export const totalSupply = ref(0n);
|
|
|
|
|
|
|
|
|
|
// export const HarbContract = await getHarbJson()
|
|
|
|
|
export let HarbContract = getHarbJson();
|
|
|
|
|
|
2025-10-03 16:51:44 +02:00
|
|
|
function getHarbJson() {
|
|
|
|
|
// console.log("getHarbJson");
|
2025-09-30 20:02:43 +02:00
|
|
|
|
2025-10-03 16:51:44 +02:00
|
|
|
const chainId = getChainId(config as Config);
|
|
|
|
|
// console.log("chainId", chainId);
|
2025-09-30 20:02:43 +02:00
|
|
|
|
2025-10-03 16:51:44 +02:00
|
|
|
const chain = getChain(chainId);
|
2025-09-30 20:02:43 +02:00
|
|
|
|
2025-10-03 16:51:44 +02:00
|
|
|
return { abi: KraikenAbi as Abi, contractAddress: chain?.harb } as Contract;
|
2025-09-23 14:18:04 +02:00
|
|
|
}
|
|
|
|
|
|
2025-10-03 16:51:44 +02:00
|
|
|
export function setHarbContract() {
|
|
|
|
|
HarbContract = getHarbJson();
|
2025-09-23 14:18:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getAllowance() {
|
2025-10-03 16:51:44 +02:00
|
|
|
logger.contract('getAllowance');
|
|
|
|
|
|
|
|
|
|
const account = getAccount(config as Config);
|
|
|
|
|
if (!account.address) {
|
|
|
|
|
return 0n;
|
|
|
|
|
}
|
2025-10-11 10:55:49 +00:00
|
|
|
|
|
|
|
|
const publicClient = getWalletPublicClient();
|
|
|
|
|
if (!publicClient) {
|
|
|
|
|
throw new Error('Wallet public client unavailable');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const result = (await publicClient.readContract({
|
2025-10-03 16:51:44 +02:00
|
|
|
abi: HarbContract.abi,
|
|
|
|
|
address: HarbContract.contractAddress,
|
|
|
|
|
functionName: 'allowance',
|
|
|
|
|
args: [account.address, StakeContract.contractAddress],
|
2025-10-11 10:55:49 +00:00
|
|
|
})) as bigint;
|
2025-10-03 16:51:44 +02:00
|
|
|
allowance.value = result;
|
|
|
|
|
return result;
|
2025-09-23 14:18:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getNonce() {
|
2025-10-03 16:51:44 +02:00
|
|
|
logger.contract('getNonce');
|
|
|
|
|
|
|
|
|
|
const account = getAccount(config as Config);
|
|
|
|
|
if (!account.address) {
|
|
|
|
|
return 0n;
|
|
|
|
|
}
|
2025-10-11 10:55:49 +00:00
|
|
|
// console.log('HarbContract.contractAddress', HarbContract.contractAddress);
|
|
|
|
|
|
|
|
|
|
const publicClient = getWalletPublicClient();
|
|
|
|
|
if (!publicClient) {
|
|
|
|
|
throw new Error('Wallet public client unavailable');
|
|
|
|
|
}
|
2025-10-03 16:51:44 +02:00
|
|
|
|
2025-10-11 10:55:49 +00:00
|
|
|
const result = (await publicClient.readContract({
|
2025-10-03 16:51:44 +02:00
|
|
|
abi: HarbContract.abi,
|
|
|
|
|
address: HarbContract.contractAddress,
|
|
|
|
|
functionName: 'nonces',
|
|
|
|
|
args: [account.address],
|
2025-10-11 10:55:49 +00:00
|
|
|
})) as bigint;
|
2025-10-03 16:51:44 +02:00
|
|
|
nonce.value = result;
|
|
|
|
|
|
|
|
|
|
return result;
|
2025-09-23 14:18:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getName() {
|
2025-10-03 16:51:44 +02:00
|
|
|
logger.contract('getName');
|
2025-09-23 14:18:04 +02:00
|
|
|
|
2025-10-11 10:55:49 +00:00
|
|
|
const publicClient = getWalletPublicClient();
|
|
|
|
|
if (!publicClient) {
|
|
|
|
|
throw new Error('Wallet public client unavailable');
|
|
|
|
|
}
|
|
|
|
|
const result = (await publicClient.readContract({
|
2025-10-03 16:51:44 +02:00
|
|
|
abi: HarbContract.abi,
|
|
|
|
|
address: HarbContract.contractAddress,
|
|
|
|
|
functionName: 'name',
|
|
|
|
|
args: [],
|
2025-10-11 10:55:49 +00:00
|
|
|
})) as string;
|
2025-10-03 16:51:44 +02:00
|
|
|
name.value = result;
|
2025-09-23 14:18:04 +02:00
|
|
|
|
2025-10-11 10:55:49 +00:00
|
|
|
return result;
|
2025-09-23 14:18:04 +02:00
|
|
|
}
|
|
|
|
|
|
2025-10-03 16:51:44 +02:00
|
|
|
export async function approve(amount: bigint): Promise<Hash> {
|
|
|
|
|
const account = getAccount(config as Config);
|
|
|
|
|
if (!account.address) {
|
|
|
|
|
throw new Error('no address found');
|
|
|
|
|
}
|
|
|
|
|
const result = await writeContract(config as Config, {
|
|
|
|
|
abi: HarbContract.abi,
|
|
|
|
|
address: HarbContract.contractAddress,
|
|
|
|
|
functionName: 'approve',
|
|
|
|
|
args: [StakeContract.contractAddress, amount],
|
|
|
|
|
});
|
|
|
|
|
// console.log("result", result);
|
|
|
|
|
await waitForTransactionReceipt(config as Config, {
|
|
|
|
|
hash: result,
|
|
|
|
|
});
|
|
|
|
|
// console.log("transactionReceipt", transactionReceipt);
|
|
|
|
|
|
|
|
|
|
return result;
|
2025-09-23 14:18:04 +02:00
|
|
|
}
|
|
|
|
|
|
2025-10-03 16:51:44 +02:00
|
|
|
//claim
|
|
|
|
|
export async function claimUbi(address: Address): Promise<Hash> {
|
|
|
|
|
const result = await writeContract(config as Config, {
|
|
|
|
|
abi: HarbContract.abi,
|
|
|
|
|
address: HarbContract.contractAddress,
|
|
|
|
|
functionName: 'claimUbi',
|
|
|
|
|
args: [address],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2025-09-23 14:18:04 +02:00
|
|
|
|
|
|
|
|
export async function getTotalSupply() {
|
2025-10-03 16:51:44 +02:00
|
|
|
logger.contract('getTotalSupply');
|
|
|
|
|
|
|
|
|
|
const result = await readContract(config as Config, {
|
|
|
|
|
abi: HarbContract.abi,
|
|
|
|
|
address: HarbContract.contractAddress,
|
|
|
|
|
functionName: 'totalSupply',
|
|
|
|
|
args: [],
|
|
|
|
|
});
|
|
|
|
|
totalSupply.value = result as bigint;
|
|
|
|
|
return result;
|
2025-09-23 14:18:04 +02:00
|
|
|
}
|