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.
channel.balances([
'ak_Y1NRjHuoc3CGMYMvCmdHSBpJsMDR6Ra2t5zjhRcbtMeXXLpLH',
'ak_V6an1xhec1xVaAhLuak7QoEbi6t7w5hEtYWp9bMKaJ19i6A9E'
'ct_2dCUAWYZdrWfACz3a2faJeKVTVrfDYxCQHCqAt5zM15f3u2UfA'
]).then(balances =>
console.log(balances['ak_Y1NRjHuoc3CGMYMvCmdHSBpJsMDR6Ra2t5zjhRcbtMeXXLpLH'])
)
List of addresses to fetch balances from
Trigger call a contract update
The call contract update is calling a preexisting contract inside the channel's internal state tree. The update is a change to be applied on top of the latest state.
That would call a contract with the poster being the caller of it. Poster commits an amount of coins to the contract.
The call would also create a call object inside the channel state tree. It contains the result of the contract call.
It is worth mentioning that the gas is not consumed, because this is an off-chain contract call. It would be consumed if it were an on-chain one. This could happen if a call with a similar computation amount is to be forced on-chain.
channel.callContract({
contract: 'ct_9sRA9AVE4BYTAkh5RNfJYmwQe1NZ4MErasQLXZkFWG43TPBqa',
callData: 'cb_1111111111111111...',
amount: 0,
abiVersion: 1
}).then(({ accepted, signedTx }) => {
if (accepted) {
console.log('Contract called succesfully')
} else {
console.log('Contract call has been rejected')
}
})
Options
Function which verifies and signs contract call transaction
Call contract using dry-run
In order to get the result of a potential contract call, one might need to dry-run a contract call. It takes the exact same arguments as a call would and returns the call object.
The call is executed in the channel's state, but it does not impact the state whatsoever. It uses as an environment the latest channel's state and the current top of the blockchain as seen by the node.
channel.callContractStatic({
contract: 'ct_9sRA9AVE4BYTAkh5RNfJYmwQe1NZ4MErasQLXZkFWG43TPBqa',
callData: 'cb_1111111111111111...',
amount: 0,
abiVersion: 1
}).then(({ returnValue, gasUsed }) => {
console.log('Returned value:', returnValue)
console.log('Gas used:', gasUsed)
})
Options
Version of the ABI
Amount the caller of the contract commits to it
ABI encoded compiled AEVM call data for the code
Address of the contract to call
Clean up all locally stored contract calls
Contract calls are kept locally in order for the participant to be able to look them up. They consume memory and in order for the participant to free it - one can prune all messages. This cleans up all locally stored contract calls and those will no longer be available for fetching and inspection.
Trigger create contract update
The create contract update is creating a contract inside the channel's internal state tree. The update is a change to be applied on top of the latest state.
That would create a contract with the poster being the owner of it. Poster commits initially a deposit amount of coins to the new contract.
channel.createContract({
code: 'cb_HKtpipK4aCgYb17wZ...',
callData: 'cb_1111111111111111...',
deposit: 10,
vmVersion: 3,
abiVersion: 1
}).then(({ accepted, signedTx, address }) => {
if (accepted) {
console.log('New contract has been created')
console.log('Contract address:', address)
} else {
console.log('New contract has been rejected')
}
})
Options
Version of the Application Binary Interface
Api encoded compiled AEVM call data for the code
Api encoded compiled AEVM byte code
Initial amount the owner of the contract commits to it
Version of the Virtual Machine
Function which verifies and signs create contract transaction
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).
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')
}
})
Amount of coins to deposit
Function which verifies and signs deposit transaction
Callbacks
Trigger a force progress contract call This call is going on-chain
channel.forceProgress({
contract: 'ct_9sRA9AVE4BYTAkh5RNfJYmwQe1NZ4MErasQLXZkFWG43TPBqa',
callData: 'cb_1111111111111111...',
amount: 0,
abiVersion: 1,
gasPrice: 1000005554
}).then(({ accepted, signedTx }) => {
if (accepted) {
console.log('Contract force progress call successful')
} else {
console.log('Contract force progress call has been rejected')
}
})
Options
Version of the ABI
Amount the caller of the contract commits to it
ABI encoded compiled AEVM call data for the code
Address of the contract to call
Optional
gasOptional
gasFunction which verifies and signs contract force progress transaction
Callbacks
Get contract call result
The combination of a caller, contract and a round of execution determines the contract call. Providing an incorrect set of those results in an error response.
channel.getContractCall({
caller: 'ak_Y1NRjHuoc3CGMYMvCmdHSBpJsMDR6Ra2t5zjhRcbtMeXXLpLH',
contract: 'ct_9sRA9AVE4BYTAkh5RNfJYmwQe1NZ4MErasQLXZkFWG43TPBqa',
round: 3
}).then(({ returnType, returnValue }) => {
if (returnType === 'ok') console.log(returnValue)
})
Options
Address of contract caller
Address of the contract
Round when contract was called
Get the latest contract state
channel.getContractState(
'ct_9sRA9AVE4BYTAkh5RNfJYmwQe1NZ4MErasQLXZkFWG43TPBqa'
).then(({ contract }) => {
console.log('deposit:', contract.deposit)
})
Address of the contract
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:
TODO: the event list looks outdated
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.
channel.poi({
accounts: [
'ak_Y1NRjHuoc3CGMYMvCmdHSBpJsMDR6Ra2t5zjhRcbtMeXXLpLH',
'ak_V6an1xhec1xVaAhLuak7QoEbi6t7w5hEtYWp9bMKaJ19i6A9E'
],
contracts: ['ct_2dCUAWYZdrWfACz3a2faJeKVTVrfDYxCQHCqAt5zM15f3u2UfA']
}).then(poi => console.log(poi))
Addresses
List of account addresses to include in poi
Optional
contracts?: `ct_${string}`[]List of contract addresses to include in 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.
channel.sendMessage(
'hello world',
'ak_Y1NRjHuoc3CGMYMvCmdHSBpJsMDR6Ra2t5zjhRcbtMeXXLpLH'
)
Message
Address of the recipient
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.
channel.shutdown(
async (tx) => await account.signTransaction(tx)
).then(tx => console.log('on_chain_tx', tx))
Function which verifies and signs mutual close transaction
Get current state
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.
channel.update(
'ak_Y1NRjHuoc3CGMYMvCmdHSBpJsMDR6Ra2t5zjhRcbtMeXXLpLH',
'ak_V6an1xhec1xVaAhLuak7QoEbi6t7w5hEtYWp9bMKaJ19i6A9E',
10,
async (tx) => await account.signTransaction(tx)
).then(({ accepted, signedTx }) =>
if (accepted) {
console.log('Update has been accepted')
}
)
Sender's public address
Receiver's public address
Transaction amount
Function which verifies and signs offchain transaction
Metadata
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).
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')
}
})
Amount of coins to withdraw
Function which verifies and signs withdraw transaction
Callbacks
Static
initializeChannel params
Static
reconnectGenerated using TypeDoc
Channel
Example