@thetanuts-test/core
v0.1.0
Published
Core utilities, types, and provider management for Thetanuts SDK
Maintainers
Readme
@thetanuts-test/core
Core utilities, types, and client management for the Thetanuts SDK.
Installation
# pnpm
pnpm add @thetanuts-test/core
# npm
npm install @thetanuts-test/core
# yarn
yarn add @thetanuts-test/coreQuick Start
import { createThetanutsClient, formatAmount, parseAmount } from '@thetanuts-test/core'
// Create a read-only client
const client = createThetanutsClient({
chainId: 8453, // Base
})
// With custom RPC URL
const clientCustomRpc = createThetanutsClient({
chainId: 8453,
rpcUrl: 'https://your-rpc-endpoint.com',
})Adding Wallet Support
For write operations (transactions), provide a wallet client:
import { createThetanutsClient } from '@thetanuts-test/core'
import { createWalletClient, http } from 'viem'
import { base } from 'viem/chains'
import { privateKeyToAccount } from 'viem/accounts'
const account = privateKeyToAccount('0x...')
const walletClient = createWalletClient({
account,
chain: base,
transport: http(),
})
const client = createThetanutsClient({
chainId: 8453,
walletClient,
})
// Now you can use write operations
console.log('Connected address:', client.address)API Reference
createThetanutsClient(config)
Creates a new Thetanuts client instance.
const client = createThetanutsClient({
chainId: 8453, // Required: Chain ID
rpcUrl: 'https://...', // Optional: Custom RPC URL
walletClient: wallet, // Optional: Viem wallet client
})Returns: ThetanutsClient
| Property | Type | Description |
|----------|------|-------------|
| chainId | number | Connected chain ID |
| chain | Chain | Viem chain object |
| publicClient | PublicClient | For read operations |
| walletClient | WalletClient | For write operations (if provided) |
| address | Address | Connected wallet address (if wallet provided) |
Utility Functions
formatAmount(value, decimals)
Format a bigint value to human-readable string.
import { formatAmount } from '@thetanuts-test/core'
formatAmount(1000000n, 6) // "1"
formatAmount(1500000000000000000n, 18) // "1.5"parseAmount(value, decimals)
Parse a human-readable string to bigint.
import { parseAmount } from '@thetanuts-test/core'
parseAmount("1", 6) // 1000000n
parseAmount("1.5", 18) // 1500000000000000000ntruncateAddress(address, chars?)
Truncate address for display.
import { truncateAddress } from '@thetanuts-test/core'
truncateAddress("0x1234567890abcdef1234567890abcdef12345678")
// "0x1234...5678"
truncateAddress("0x1234567890abcdef1234567890abcdef12345678", 6)
// "0x123456...345678"isValidAddress(address)
Check if a string is a valid Ethereum address.
import { isValidAddress } from '@thetanuts-test/core'
isValidAddress("0x1234567890abcdef1234567890abcdef12345678") // true
isValidAddress("invalid") // falseChain Configuration
SUPPORTED_CHAINS
Map of supported chain configurations.
import { SUPPORTED_CHAINS } from '@thetanuts-test/core'
const baseConfig = SUPPORTED_CHAINS[8453]
// { chainId: 8453, name: 'Base', symbol: 'ETH', ... }isSupportedChain(chainId)
Check if a chain ID is supported.
import { isSupportedChain } from '@thetanuts-test/core'
isSupportedChain(8453) // true
isSupportedChain(99999) // falsegetChainConfig(chainId)
Get configuration for a chain.
import { getChainConfig } from '@thetanuts-test/core'
const config = getChainConfig(8453)
// { chainId: 8453, name: 'Base', rpcUrl: '...', ... }Constants
MAX_UINT256
Maximum uint256 value for unlimited approvals.
import { MAX_UINT256 } from '@thetanuts-test/core'
// 2^256 - 1SUPPORTED_CHAIN_IDS
Array of all supported chain IDs.
import { SUPPORTED_CHAIN_IDS } from '@thetanuts-test/core'
// [1, 8453, 42161, 56, 137, 43114, 5000]Error Classes
import {
ThetanutsError,
UnsupportedChainError,
WalletNotConnectedError,
TransactionError,
} from '@thetanuts-test/core'
try {
// SDK operation
} catch (error) {
if (error instanceof UnsupportedChainError) {
console.error('Chain not supported')
} else if (error instanceof WalletNotConnectedError) {
console.error('Please connect wallet')
} else if (error instanceof TransactionError) {
console.error('Transaction failed:', error.cause)
}
}Supported Chains
| Chain | Chain ID | Symbol | |-------|----------|--------| | Ethereum | 1 | ETH | | Base | 8453 | ETH | | Arbitrum | 42161 | ETH | | BNB Smart Chain | 56 | BNB | | Polygon | 137 | MATIC | | Avalanche | 43114 | AVAX | | Mantle | 5000 | MNT |
TypeScript
This package is fully typed. Import types as needed:
import type {
ThetanutsClient,
ThetanutsClientConfig,
SupportedChainId,
ChainConfig,
TransactionResult,
TokenInfo,
} from '@thetanuts-test/core'License
MIT
