# Auto renewable channels

This class is intended for automatic opening of new channels when the specified balance is reached and automatic payment from the current channel. Example: <https://github.com/BIoTws/endless-channels-example>

### Constructor

walletId - String

timeout - Number - in ms

maxOpenChannels - Number

timeForMutualClosingChannel - Number

## Example: init

{% tabs %}
{% tab title="JS module" %}

```javascript
const autoRenewableChannel = new AutoRenewableChannel(wallets[0], peerDeviceAddress, 20000, 2);
	
await autoRenewableChannel.openNewChannel({
	walletId: wallets[0],
	peerAddress: null,
	myAmount: 100,
	peerAmount: 100,
	age: 10
});
```

{% endtab %}
{% endtabs %}

## Example: channel approval

{% tabs %}
{% tab title="JS module" %}

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

async function start() {
	await core.init('test');
	let wallets = await core.getMyDeviceWallets();
	
	const autoRenewableChannel = new AutoRenewableChannel(wallets[0], null, 20000, 2);
	autoRenewableChannel.init();
	autoRenewableChannel.events.on('start', console.error);
	autoRenewableChannel.events.on('new_transfer', (amount, message, id) => {
		console.error('new transfer', amount, message, id);
	});
	autoRenewableChannel.events.on('request_approve_channel', async (objInfo) => {
		console.error('new Channel: ', objInfo);
		console.error(await autoRenewableChannel.approve(objInfo.index));
	});
}

start().catch(console.error);

```

{% endtab %}
{% endtabs %}

## Events

#### Error

```javascript
autoRenewableChannel.events.on('error', (error, id) => {
		console.error('channelError', id, error);
	});
```

#### start

This event is triggered once at the start of the first channel. The event fires after approve channel.

```javascript
autoRenewableChannel.events.on('start', (id) => {
			console.error('channel start:', id);
		});
```

#### init

init autoRenewableChannel

```javascript
autoRenewableChannel.events.on('init', stats => {
    console.error(peerId, device_address timeForMutualClosingChannel);
});
```

#### request\_approve\_channel

This event notifies us of the request to open the first channel. Contains technical information.

**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
autoRenewableChannel.events.on('request_approve_channel', async (objInfo) => {
    console.error('info: ', objInfo); //request to open
    if (objInfo.peerAmount === 100) {
		await autoRenewableChannel.approve(objInfo.id);
	} else {
	    await autoRenewableChannel.reject(objInfo.id);
	}
});
```

{% endtab %}
{% endtabs %}

#### changed\_step

```javascript
autoRenewableChannel.events.on('changed_step', (step, id) => {
			console.error('changed_step: ', step, id);
		});
```

#### new\_transfer

```javascript
autoRenewableChannel.events.on('new_transfer', async (amount, message, id) => {
			console.error('new_transfer: ', amount, message, id);
			
		});
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://biot-ws.gitbook.io/biot-core/channels/auto-re-new-channels.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
