fix: Generic utilities (isRecord, coerceString, getErrorMessage, ensureAddress) have no shared home (#363)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
e53591de9d
commit
59ae30bb37
6 changed files with 63 additions and 35 deletions
|
|
@ -1,11 +1,12 @@
|
|||
import { computed, ref } from 'vue';
|
||||
import { useAccount } from '@wagmi/vue';
|
||||
import { readContract, writeContract } from '@wagmi/core';
|
||||
import { erc20Abi, getAddress, isAddress, maxUint256, parseEther, zeroAddress, type Abi, type Address } from 'viem';
|
||||
import { erc20Abi, maxUint256, parseEther, zeroAddress, type Abi } from 'viem';
|
||||
import { useToast } from 'vue-toastification';
|
||||
import { config as wagmiConfig } from '@/wagmi';
|
||||
import { getChain, DEFAULT_CHAIN_ID } from '@/config';
|
||||
import { useWallet } from '@/composables/useWallet';
|
||||
import { getErrorMessage, ensureAddress } from '@harb/utils';
|
||||
|
||||
export const WETH_ABI = [
|
||||
{
|
||||
|
|
@ -89,39 +90,6 @@ export const UNISWAP_POOL_ABI = [
|
|||
},
|
||||
] as const satisfies Abi;
|
||||
|
||||
export function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === 'object' && value !== null;
|
||||
}
|
||||
|
||||
export function coerceString(value: unknown): string | null {
|
||||
if (typeof value === 'string') {
|
||||
const trimmed = value.trim();
|
||||
if (trimmed.length > 0) return trimmed;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function getErrorMessage(error: unknown, fallback: string): string {
|
||||
if (error instanceof Error) {
|
||||
const msg = coerceString(error.message);
|
||||
if (msg) return msg;
|
||||
}
|
||||
if (isRecord(error)) {
|
||||
const short = coerceString(error.shortMessage);
|
||||
if (short) return short;
|
||||
const msg = coerceString(error.message);
|
||||
if (msg) return msg;
|
||||
}
|
||||
return fallback;
|
||||
}
|
||||
|
||||
export function ensureAddress(value: string, label: string): Address {
|
||||
if (!value || !isAddress(value)) {
|
||||
throw new Error(`${label} is not a valid address`);
|
||||
}
|
||||
return getAddress(value);
|
||||
}
|
||||
|
||||
export function useSwapKrk() {
|
||||
const toast = useToast();
|
||||
const { address, chainId } = useAccount();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue