Skip to main content

NitroliteService

The NitroliteService class is the core service that directly interacts with the Nitrolite Custody smart contract. It handles channel management, deposits, withdrawals, and all other channel-specific operations following the channel lifecycle.

Initialization

import { NitroliteService } from '@erc7824/nitrolite';

const nitroliteService = new NitroliteService(
publicClient, // viem PublicClient
addresses, // ContractAddresses
walletClient, // viem WalletClient
account // Account address
);

Channel Lifecycle Methods

1. Deposit Operations

MethodDescriptionParametersReturn Type
depositDeposits tokens/ETH into the custody contract.tokenAddress: Address, amount: bigintPromise<Hash>
withdrawWithdraws tokens from the custody contract.tokenAddress: Address, amount: bigintPromise<Hash>

Example:

// Deposit ETH or token
const txHash = await nitroliteService.deposit(tokenAddress, amount);

// Withdraw ETH or token
const txHash = await nitroliteService.withdraw(tokenAddress, amount);

2. Channel Creation

MethodDescriptionParametersReturn Type
createChannelCreates a new channel with the given parameters.channel: Channel, initialState: StatePromise<Hash>

Example:

// Create a channel
const txHash = await nitroliteService.createChannel(channel, initialState);

Where:

  • channel defines the participants, adjudicator, challenge period, and nonce
  • initialState contains the initial allocation of funds and state data

3. Channel Operations

MethodDescriptionParametersReturn Type
checkpointCheckpoints a state on-chain.channelId: ChannelId, candidateState: State, proofStates?: State[]Promise<Hash>
challengeChallenges a channel with a candidate state.channelId: ChannelId, candidateState: State, proofStates?: State[]Promise<Hash>
resizeResizes a channel with a candidate state.channelId: ChannelId, candidateState: State, proofStates?: State[]Promise<Hash>

Example:

// Checkpoint a channel state
const txHash = await nitroliteService.checkpoint(channelId, candidateState);

// Challenge a channel
const txHash = await nitroliteService.challenge(channelId, candidateState);

// Resize a channel
const txHash = await nitroliteService.resize(channelId, candidateState);

4. Channel Closing

MethodDescriptionParametersReturn Type
closeCloses a channel using a final state.channelId: ChannelId, finalState: StatePromise<Hash>

Example:

// Close a channel
const txHash = await nitroliteService.close(channelId, finalState);

5. Account Information

MethodDescriptionParametersReturn Type
getAccountChannelsGets channel IDs for an account.accountAddress: AddressPromise<ChannelId[]>
getAccountInfoGets account info for a token.accountAddress: Address, tokenAddress: AddressPromise<AccountInfo>

Example:

// Get all channels for an account
const channels = await nitroliteService.getAccountChannels(accountAddress);

// Get detailed account info
const info = await nitroliteService.getAccountInfo(accountAddress, tokenAddress);
console.log(`Available: ${info.available}, Locked: ${info.locked}`);

Transaction Preparation Methods

For Account Abstraction support, NitroliteService provides transaction preparation methods that return transaction data without executing it:

MethodDescriptionParametersReturn Type
prepareDepositPrepares deposit transaction.tokenAddress: Address, amount: bigintPromise<PreparedTransaction>
prepareCreateChannelPrepares channel creation transaction.channel: Channel, initialState: StatePromise<PreparedTransaction>
prepareCheckpointPrepares checkpoint transaction.channelId: ChannelId, candidateState: State, proofStates?: State[]Promise<PreparedTransaction>
prepareChallengePrepares challenge transaction.channelId: ChannelId, candidateState: State, proofStates?: State[]Promise<PreparedTransaction>
prepareResizePrepares resize transaction.channelId: ChannelId, candidateState: State, proofStates?: State[]Promise<PreparedTransaction>
prepareClosePrepares close transaction.channelId: ChannelId, finalState: StatePromise<PreparedTransaction>
prepareWithdrawPrepares withdraw transaction.tokenAddress: Address, amount: bigintPromise<PreparedTransaction>

Example:

// Prepare deposit transaction
const tx = await nitroliteService.prepareDeposit(tokenAddress, amount);

// Use with your Account Abstraction provider
const userOp = await aaProvider.buildUserOperation({
target: tx.to,
data: tx.data,
value: tx.value || 0n
});

Implementation Details

The NitroliteService connects to the Custody contract using:

  • A viem PublicClient for read operations
  • A viem WalletClient for write operations and signing
  • The contract address specified in the configuration

The service handles:

  • Contract interaction
  • Parameter validation
  • Error handling
  • Transaction preparation

Error Handling

The NitroliteService throws specific error types:

  • ContractCallError: When calls to the contract fail
  • InvalidParameterError: When parameters are invalid
  • MissingParameterError: When required parameters are missing
  • WalletClientRequiredError: When wallet client is needed but not provided
  • AccountRequiredError: When account is needed but not provided

Example:

try {
await nitroliteService.deposit(tokenAddress, amount);
} catch (error) {
if (error instanceof ContractCallError) {
console.error(`Contract call failed: ${error.message}`);
console.error(`Suggestion: ${error.suggestion}`);
}
}

Advanced Usage

Custom Contract Interaction

For advanced use cases, you might need to interact directly with the contract:

// Get the custody contract address
const custodyAddress = nitroliteService.custodyAddress;

// Use with your own custom contract interaction
const customContract = getContract({
address: custodyAddress,
abi: custodyAbi,
// Additional configuration...
});

Multiple Channel Management

For applications managing multiple channels:

// Get all channels for the account
const channels = await nitroliteService.getAccountChannels(accountAddress);

// Process each channel
for (const channelId of channels) {
// Get channel info from contract
// Process channel state
}