Early on in the lifecycle of a state channel -- i.e. after exchanging some setup states, but before executing any application logic -- participants will want to "fund it". They will stake assets on the channel so that the state updates are meaningful. The simplest way to do this is with an on chain deposit; a more advanced possibility is fund a new channel from an existing funded channel.
To start, let's explore the on chain deposit case. We will need to understand how channels are uniquely identified.
Compute a channel id
The id of a channel is the
keccak256 hash of the abi encoded
By choosing a new
channelNonce each time the same participants execute a state channel supported by the same chain, they can avoid replay attacks. The
getChannelId helper exported by
@statechannels/nitro-protocol accepts an object of type
Channel (which contains all the necessary properties) and computes the channel id for you.
deposit into the ETH asset holder
The deposit method allows ETH to be escrowed against a channel. We have the following call signature:
There are a few rules to obey when calling
destination must NOT be an external destination. Secondly, the on-chain holdings for
destination must be greater than or equal to
expectedHeld. Thirdly, the holdings for
destination must be less than the sum of the amount expected to be held and the amount declared in the deposit.
The first rule prevents funds being escrowed against something other than a channelId: funds may only be unlocked from channels, so you shouldn't deposit into anything else. The second rule prevents loss of funds: since holdings are paid out in preferential order, depositing before a counterparty has deposited implies that they can withdraw your funds. The check is performed in the same transaction as the deposit, making this safe even in the event of a chain re-org that reverts a previous participant's deposit. The third rule prevents the deposit of uneccessary funds: if my aim was to increase the holdings to a certain level, but they are already at or above that level, then I want my deposit to transaction revert.
Because we are depositing ETH, we must remember to send the right amount of ETH with the transaction. Depositing ERC20 tokens will be covered in a future tutorial.
Destination is a
bytes32 and either:
ChannelId(see the section on channelId), or
ExternalDestination, which is an ethereum address left-padded with zeros.
ExternalDestination corresponding to
address may be computed as