Optional
_channelOptional
_fsmGet balances
The accounts param contains a list of addresses to fetch balances of. Those can be either account balances or a contract ones, encoded as an account addresses.
If a certain account address had not being found in the state tree - it is simply skipped in the response.
List of addresses to fetch balances from
channel.balances([
'ak_Y1NRjHuoc3CGMYMvCmdHSBpJsMDR6Ra2t5zjhRcbtMeXXLpLH',
'ak_V6an1xhec1xVaAhLuak7QoEbi6t7w5hEtYWp9bMKaJ19i6A9E'
'ct_2dCUAWYZdrWfACz3a2faJeKVTVrfDYxCQHCqAt5zM15f3u2UfA'
]).then(balances =>
console.log(balances['ak_Y1NRjHuoc3CGMYMvCmdHSBpJsMDR6Ra2t5zjhRcbtMeXXLpLH'])
)
Deposit coins into the channel
After the channel had been opened any of the participants can initiate a deposit. The process closely resembles the update. The most notable difference is that the transaction has been co-signed: it is channel_deposit_tx and after the procedure is finished - it is being posted on-chain.
Any of the participants can initiate a deposit. The only requirements are:
After the other party had signed the deposit transaction, the transaction is posted on-chain and onOnChainTx callback is called with on-chain transaction as first argument. After computing transaction hash it can be tracked on the chain: entering the mempool, block inclusion and a number of confirmations.
After the minimum_depth block confirmations onOwnDepositLocked callback is called (without any arguments).
When the other party had confirmed that the block height needed is reached onDepositLocked callback is called (without any arguments).
Amount of coins to deposit
Function which verifies and signs deposit transaction
Callbacks
channel.deposit(
100,
async (tx) => await account.signTransaction(tx),
{ onOnChainTx: (tx) => console.log('on_chain_tx', tx) }
).then(({ accepted, state }) => {
if (accepted) {
console.log('Deposit has been accepted')
console.log('The new state is:', state)
} else {
console.log('Deposit has been rejected')
}
})
Protected
enqueueOptional
state?: Partial<ChannelState>Leave channel
It is possible to leave a channel and then later reestablish the channel off-chain state and continue operation. When a leave method is called, the channel fsm passes it on to the peer fsm, reports the current mutually signed state and then terminates.
The channel can be reestablished by instantiating another Channel instance with two extra params: existingChannelId and offchainTx (returned from leave method as channelId and signedTx respectively).
channel.leave().then(({ channelId, signedTx }) => {
console.log(channelId)
console.log(signedTx)
})
Remove event listener function
Event name
Callback function
Register event listener function
Possible events:
Event name
Callback function
Get proof of inclusion
If a certain address of an account or a contract is not found in the state tree - the response is an error.
Addresses
List of account addresses to include in poi
Optional
contracts?: `ct_${string}`[]List of contract addresses to include in poi
channel.poi({
accounts: [
'ak_Y1NRjHuoc3CGMYMvCmdHSBpJsMDR6Ra2t5zjhRcbtMeXXLpLH',
'ak_V6an1xhec1xVaAhLuak7QoEbi6t7w5hEtYWp9bMKaJ19i6A9E'
],
contracts: ['ct_2dCUAWYZdrWfACz3a2faJeKVTVrfDYxCQHCqAt5zM15f3u2UfA']
}).then(poi => console.log(poi))
Send generic message
If message is an object it will be serialized into JSON string before sending.
If there is ongoing update that has not yet been finished the message will be sent after that update is finalized.
Message
Address of the recipient
channel.sendMessage(
'hello world',
'ak_Y1NRjHuoc3CGMYMvCmdHSBpJsMDR6Ra2t5zjhRcbtMeXXLpLH'
)
Trigger mutual close
At any moment after the channel is opened, a closing procedure can be triggered. This can be done by either of the parties. The process is similar to the off-chain updates.
Function which verifies and signs mutual close transaction
channel.shutdown(
async (tx) => await account.signTransaction(tx)
).then(tx => console.log('on_chain_tx', tx))
Get current status
Trigger a transfer update
The transfer update is moving coins from one channel account to another. The update is a change to be applied on top of the latest state.
Sender and receiver are the channel parties. Both the initiator and responder can take those roles. Any public key outside the channel is considered invalid.
Sender's public address
Receiver's public address
Transaction amount
Function which verifies and signs offchain transaction
Metadata
channel.update(
'ak_Y1NRjHuoc3CGMYMvCmdHSBpJsMDR6Ra2t5zjhRcbtMeXXLpLH',
'ak_V6an1xhec1xVaAhLuak7QoEbi6t7w5hEtYWp9bMKaJ19i6A9E',
10,
async (tx) => await account.signTransaction(tx)
).then(({ accepted, signedTx }) =>
if (accepted) {
console.log('Update has been accepted')
}
)
Withdraw coins from the channel
After the channel had been opened any of the participants can initiate a withdrawal. The process closely resembles the update. The most notable difference is that the transaction has been co-signed: it is channel_withdraw_tx and after the procedure is finished - it is being posted on-chain.
Any of the participants can initiate a withdrawal. The only requirements are:
After the other party had signed the withdraw transaction, the transaction is posted on-chain and onOnChainTx callback is called with on-chain transaction as first argument. After computing transaction hash it can be tracked on the chain: entering the mempool, block inclusion and a number of confirmations.
After the minimum_depth block confirmations onOwnWithdrawLocked callback is called (without any arguments).
When the other party had confirmed that the block height needed is reached onWithdrawLocked callback is called (without any arguments).
Amount of coins to withdraw
Function which verifies and signs withdraw transaction
Callbacks
channel.withdraw(
100,
async (tx) => await account.signTransaction(tx),
{ onOnChainTx: (tx) => console.log('on_chain_tx', tx) }
).then(({ accepted, signedTx }) => {
if (accepted) {
console.log('Withdrawal has been accepted')
} else {
console.log('Withdrawal has been rejected')
}
})
Static
_initializeStatic
initializeChannel params
Static
reconnectGenerated using TypeDoc
Channel
Example