# Channels manager

## list

Returns list of channels from database

```javascript
const ChannelsManager = require('biot-core/lib/ChannelsManager');

const channelsManager = new ChannelsManager(wallets[0], timeout);
let list = await channelsManager.list();
```

## newChannel

Create new channel

{% tabs %}
{% tab title="JS module" %}
```javascript
const ChannelsManager = require('biot-core/lib/ChannelsManager');
let channel = channelsManager.newChannel({
			peerDeviceAddress,
			myAmount: 100,
			peerAmount: 100,
			age: 10
		});
channel.init();
```
{% endtab %}

{% tab title="WS" %}
```javascript
socket.send(JSON.stringify({
	id: i++,
	name: 'subscribeToChannelUpdates',
	args: []
}));

let message_id = i++;
socket.send(JSON.stringify({
	id: message_id,
	name: 'newChannel',
	args: [{
		peerDeviceAddress,
		myAmount: 100,
		peerAmount: 100,
		age: 10
	}]
}));

socket.onmessage = function (event) {
	let data = JSON.parse(event.data);
	if (data.id === message_id && data.name === 'newChannel') {
		console.log('channelID:', data.result);
	}
};
```
{% endtab %}
{% endtabs %}

## Event: New channel

**Structure objInfo:**

**peerDeviceAddress -** String - peer device address for opening channel

**myAddress** - String - peer byteball address

**peerAmount** - Number - your amount in bytes

**myAmount**: - Number - peer amount in bytes

**age** - Number - Timeout in MCI for withdrawal bytes from channel when using one-side closure

**id** - String - channel id

**myUnilateralAddress** - Array - The address of the peer for signing transactions inside the channel

**myDestinationAddress** - Array - The address of the peer for withdrawal from the channel

{% tabs %}
{% tab title="JS module" %}
```javascript
const ChannelsManager = require('biot-core/lib/ChannelsManager');

channelsManager.events.on('newChannel', async (objInfo) => {
	let channel = channelsManager.getNewChannel(objInfo);
	channel.events.on('error', error => { 
	    console.error('channelError', channel.id, error); 
	});
	channel.events.on('start', () => { 
	    console.error('channel start', channel.id); 
	});
	channel.events.on('changed_step', (step) => { 
	    console.error('changed_step: ', step); 
	});
	channel.events.on('new_transfer', (amount) => { 
	    console.error('new_transfer: ', amount); 
	});
	
	await channel.init();
	if (channel.myAmount === 100) {
		await channel.approve();
	} else {
		await channel.reject();
	}
	console.error(channel.info());
});
```
{% endtab %}

{% tab title="WS" %}
```javascript
socket.send(JSON.stringify({
	id: i++,
	name: 'subscribeToChannelUpdates',
	args: []
}));

socket.onmessage = function (event) {
	let data = JSON.parse(event.data);
	if (data.id === -2 && data.event === 'newChannel') {
		console.log('channelId', data.channelId);
		console.log('channel info: ', data.info);
		if (data.info.myAmount === 100) {
		    socket.send(JSON.stringify({
            	id: i++,
        	    name: 'channel',
            	args: [data.channelId, 'approve']
            }));
	    } else {
		    socket.send(JSON.stringify({
            	id: i++,
        	    name: 'channel',
            	args: [data.channelId, 'reject']
            }));
	    }
	}
};
```
{% endtab %}
{% endtabs %}

