Channel

Example

await Channel.initialize({
url: 'ws://localhost:3001',
role: 'initiator'
initiatorId: 'ak_Y1NRjHuoc3CGMYMvCmdHSBpJsMDR6Ra2t5zjhRcbtMeXXLpLH',
responderId: 'ak_V6an1xhec1xVaAhLuak7QoEbi6t7w5hEtYWp9bMKaJ19i6A9E',
initiatorAmount: 1e18,
responderAmount: 1e18,
pushAmount: 0,
channelReserve: 0,
ttl: 1000,
host: 'localhost',
port: 3002,
lockPeriod: 10,
async sign (tag, tx) => await account.signTransaction(tx)
})

Hierarchy

Constructors

Properties

_actionQueue: ChannelAction[] = []
_channelId?: `ch_${string}`
_eventEmitter: EventEmitter = ...
_fsmId?: `ba_${string}`
_isActionQueueLocked: boolean = false
_isMessageQueueLocked: boolean = false
_messageQueue: ChannelMessage[] = []
_nextRpcMessageId: number = 0
_options: ChannelOptions
_pingTimeoutId: Timeout
_rpcCallbacks: Map<number, ((message) => void)> = ...

Type declaration

    • (message): void
    • Parameters

      • message: object

      Returns void

_state: "" | `tx_${string}` = ''
_status: ChannelStatus = 'disconnected'
_websocket: w3cwebsocket

Methods

  • Get 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.

    Parameters

    • accounts: `ak_${string}`[]

      List of addresses to fetch balances from

    Returns Promise<{
        [key: AccountAddress]: string;
    }>

    Example

    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:

    • Channel is already opened
    • No off-chain update/deposit/withdrawal is currently being performed
    • Channel is not being closed or in a solo closing state
    • The deposit amount must be equal to or greater than zero, and cannot exceed the available balance on the channel (minus the channel_reserve)

    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).

    Parameters

    • amount: number | BigNumber

      Amount of coins to deposit

    • sign: SignTx

      Function which verifies and signs deposit transaction

    • callbacks: Pick<ChannelState, "onOnChainTx" | "onOwnDepositLocked" | "onDepositLocked"> = {}

      Callbacks

    Returns Promise<{
        accepted: boolean;
        state: ChannelState;
    }>

    Example

    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')
    }
    })
  • 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).

    Returns Promise<{
        channelId: `ba_${string}`;
        signedTx: `tx_${string}`;
    }>

    Example

    channel.leave().then(({ channelId, signedTx }) => {
    console.log(channelId)
    console.log(signedTx)
    })
  • Register event listener function

    Possible events:

    • "error"
    • "stateChanged"
    • "statusChanged"
    • "message"
    • "peerDisconnected"
    • "onChainTx"
    • "ownWithdrawLocked"
    • "withdrawLocked"
    • "ownDepositLocked"
    • "depositLocked"
    • "channelReestablished"
    • "newContract"

    Type Parameters

    Parameters

    • eventName: E

      Event name

    • callback: ChannelEvents[E]

      Callback function

    Returns void

  • 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.

    Parameters

    • addresses: {
          accounts: `ak_${string}`[];
          contracts?: `ct_${string}`[];
      }

      Addresses

      • accounts: `ak_${string}`[]

        List of account addresses to include in poi

      • Optional contracts?: `ct_${string}`[]

        List of contract addresses to include in poi

    Returns Promise<Object>

    Example

    channel.poi({
    accounts: [
    'ak_Y1NRjHuoc3CGMYMvCmdHSBpJsMDR6Ra2t5zjhRcbtMeXXLpLH',
    'ak_V6an1xhec1xVaAhLuak7QoEbi6t7w5hEtYWp9bMKaJ19i6A9E'
    ],
    contracts: ['ct_2dCUAWYZdrWfACz3a2faJeKVTVrfDYxCQHCqAt5zM15f3u2UfA']
    }).then(poi => console.log(poi))
  • Get current round

    If round cannot be determined (for example when channel has not been opened) it will return null.

    Returns null | number

  • 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.

    Parameters

    • message: string | object

      Message

    • recipient: `ak_${string}`

      Address of the recipient

    Returns Promise<void>

    Example

    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.

    Parameters

    • sign: SignTx

      Function which verifies and signs mutual close transaction

    Returns Promise<`tx_${string}`>

    Example

    channel.shutdown(
    async (tx) => await account.signTransaction(tx)
    ).then(tx => console.log('on_chain_tx', tx))
  • Get current state

    Returns Promise<{
        calls: Object;
        halfSignedTx?: Object;
        signedTx?: Object;
        trees: Object;
    }>

  • 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.

    Parameters

    • from: `ak_${string}`

      Sender's public address

    • to: `ak_${string}`

      Receiver's public address

    • amount: number | BigNumber

      Transaction amount

    • sign: SignTx

      Function which verifies and signs offchain transaction

    • metadata: string[] = []

      Metadata

    Returns Promise<{
        accepted: boolean;
        errorCode?: number;
        errorMessage?: string;
        signedTx?: `tx_${string}`;
    }>

    Example

    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:

    • Channel is already opened
    • No off-chain update/deposit/withdrawal is currently being performed
    • Channel is not being closed or in a solo closing state
    • The withdrawal amount must be equal to or greater than zero, and cannot exceed the available balance on the channel (minus the channel_reserve)

    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).

    Parameters

    • amount: number | BigNumber

      Amount of coins to withdraw

    • sign: SignTx

      Function which verifies and signs withdraw transaction

    • callbacks: Pick<ChannelState, "onOnChainTx" | "onOwnWithdrawLocked" | "onWithdrawLocked"> = {}

      Callbacks

    Returns Promise<{
        accepted: boolean;
        signedTx: `tx_${string}`;
    }>

    Example

    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')
    }
    })

Generated using TypeDoc