> ## Documentation Index
> Fetch the complete documentation index at: https://docs.oaknetwork.org/llms.txt
> Use this file to discover all available pages before exploring further.

# Utilities

The SDK exports pure utility functions and constants that have no client dependency. Import them from `@oaknetwork/contracts-sdk` or `@oaknetwork/contracts-sdk/utils`.

```typescript theme={null}
import {
  keccak256,
  id,
  toHex,
  isHex,
  getCurrentTimestamp,
  addDays,
  getChainFromId,
  parseEther,
  formatEther,
  parseUnits,
  isAddress,
  getAddress,
  stringToHex,
  CHAIN_IDS,
  BPS_DENOMINATOR,
  BYTES32_ZERO,
  DATA_REGISTRY_KEYS,
  multicall,
  scopedToPlatform,
} from '@oaknetwork/contracts-sdk';
```

## Hashing

### keccak256(data)

Hashes a string, hex string, or `Uint8Array` using keccak256.

```typescript theme={null}
const platformHash = keccak256(toHex('my-platform'));
const fromHex      = keccak256('0x1234');
```

### id(input)

Produces a bytes32 hash from a UTF-8 string. Equivalent to Solidity's `keccak256(abi.encodePacked(str))`.

```typescript theme={null}
const topic = id('Transfer(address,address,uint256)');
```

## Encoding

### isHex(data)

Type guard that checks if a string is a valid `0x`-prefixed hex string.

```typescript theme={null}
isHex('0x1234'); // true
isHex('hello');  // false
```

### toHex(value, options?)

Converts a value to a hex string. Pass `{ size: 32 }` for fixed-length bytes32 encoding.

```typescript theme={null}
const currency = toHex('USD', { size: 32 }); // bytes32
const raw      = toHex('hello');
```

### stringToHex(value)

Converts a UTF-8 string to a hex string.

```typescript theme={null}
const hex = stringToHex('hello');
```

### isAddress(value)

Checks if a string is a valid Ethereum address.

```typescript theme={null}
isAddress('0x1234...'); // true or false
```

### getAddress(value)

Returns the checksummed version of an address. Throws if invalid.

```typescript theme={null}
const checksummed = getAddress('0x1234...');
```

## Number formatting

### parseEther(value)

Converts a human-readable ether string to `bigint` (wei).

```typescript theme={null}
const wei = parseEther('1.5'); // 1500000000000000000n
```

### formatEther(value)

Converts a `bigint` (wei) to a human-readable ether string.

```typescript theme={null}
const eth = formatEther(1500000000000000000n); // "1.5"
```

### parseUnits(value, decimals)

Converts a human-readable string to `bigint` with the specified decimal places.

```typescript theme={null}
const usdc = parseUnits('100', 6); // 100000000n
```

## Time helpers

### getCurrentTimestamp()

Returns the current Unix time as a `bigint` in seconds.

```typescript theme={null}
const now = getCurrentTimestamp();
```

### addDays(timestamp, days)

Adds days to a Unix timestamp.

```typescript theme={null}
const deadline = addDays(getCurrentTimestamp(), 30); // 30 days from now
```

## Chain helpers

### getChainFromId(chainId)

Resolves a numeric chain ID to a viem `Chain` object. Falls back to a minimal definition for unknown IDs.

```typescript theme={null}
const chain = getChainFromId(CHAIN_IDS.CELO_TESTNET_SEPOLIA);
```

### Browser wallet helpers

For frontend integrations:

```typescript theme={null}
import {
  createBrowserProvider,
  getSigner,
  createJsonRpcProvider,
  createWallet,
} from '@oaknetwork/contracts-sdk';

// Browser (MetaMask, etc.)
const provider = createBrowserProvider(window.ethereum, chain);
const signer   = await getSigner(window.ethereum, chain);

// Server-side
const rpcProvider = createJsonRpcProvider('https://rpc-url.com', chain);
const wallet      = createWallet('0x...privateKey', 'https://rpc-url.com', chain);
```

## Constants

### CHAIN\_IDS

```typescript theme={null}
CHAIN_IDS.ETHEREUM_MAINNET          // 1
CHAIN_IDS.CELO_MAINNET              // 42220
CHAIN_IDS.ETHEREUM_TESTNET_SEPOLIA  // 11155111
CHAIN_IDS.ETHEREUM_TESTNET_GOERLI   // 5
CHAIN_IDS.CELO_TESTNET_SEPOLIA      // 11142220
```

### BPS\_DENOMINATOR

Basis-points denominator for fee calculations. `10_000n = 100%`.

```typescript theme={null}
const feeAmount = (raisedAmount * platformFee) / BPS_DENOMINATOR;
```

### BYTES32\_ZERO

Zero bytes32 value (`0x0000...0000`). Used as a default or unset marker.

### DATA\_REGISTRY\_KEYS

Pre-computed keccak256 hashes for common registry keys:

```typescript theme={null}
DATA_REGISTRY_KEYS.BUFFER_TIME              // keccak256("bufferTime")
DATA_REGISTRY_KEYS.MAX_PAYMENT_EXPIRATION   // keccak256("maxPaymentExpiration")
DATA_REGISTRY_KEYS.CAMPAIGN_LAUNCH_BUFFER   // keccak256("campaignLaunchBuffer")
DATA_REGISTRY_KEYS.MINIMUM_CAMPAIGN_DURATION // keccak256("minimumCampaignDuration")
```

### scopedToPlatform(baseKey, platformHash)

Computes a platform-scoped registry key. Matches the on-chain `DataRegistryKeys.scopedToPlatform`.

```typescript theme={null}
const scopedKey = scopedToPlatform(
  DATA_REGISTRY_KEYS.BUFFER_TIME,
  keccak256(toHex('my-platform')),
);
const value = await gp.getFromRegistry(scopedKey);
```

## Multicall

Batch multiple read calls into a single RPC round-trip. Pass an array of lazy closures — the same entity read methods you'd normally `await` individually.

```typescript theme={null}
import { multicall } from '@oaknetwork/contracts-sdk';

const gp = oak.globalParams('0x...');

const [platformCount, feePercent, admin] = await multicall([
  () => gp.getNumberOfListedPlatforms(),
  () => gp.getProtocolFeePercent(),
  () => gp.getProtocolAdminAddress(),
]);
```

> For full usage details including cross-contract batching, see the dedicated [Multicall](/contracts-sdk/multicall) page.

## Viem re-exports

The SDK re-exports commonly used viem primitives so you don't need to install viem separately:

```typescript theme={null}
import {
  createPublicClient,
  createWalletClient,
  http,
  custom,
  mainnet,
  sepolia,
  goerli,
} from '@oaknetwork/contracts-sdk';
```

| Export               | Description                           |
| -------------------- | ------------------------------------- |
| `createPublicClient` | Create a viem PublicClient for reads  |
| `createWalletClient` | Create a viem WalletClient for writes |
| `http`               | HTTP transport factory                |
| `custom`             | Custom transport factory              |
| `mainnet`            | Ethereum mainnet chain config         |
| `sepolia`            | Ethereum Sepolia chain config         |
| `goerli`             | Ethereum Goerli chain config          |
