## raiden-rpc
A module for interacting with a Raiden network node over RPC.
**Author**: Dylan Seago
**Installation**
```
npm install raiden-rpc
```
**Example**
Require the library
```javascript
var RaidenClient = require('raiden-rpc');
```
Create a new raiden instance for a specific node. See below for possible options.
```javascript
// Quick localhost development
var localNode = RaidenClient.localNode(); // Uses DEFAULT_RPC_HOST
// Custom hostname
var myNode = new RaidenClient('http://192.168.1.124:5004');
```
Join a token network and transfer tokens to another node
```javascript
const testnetToken = '0x0f114a1e9db192502e7856309cc899952b3db1ed';
const recipientAddress = '0x61c808d82a3ac53231750dadc13c777b59310bd9';
// Retrieve our own address as sanity check
myNode.getAddress()
// Deposit 100 testnet tokens, 20 amongst 3 channels, 40 reserved for future channels
.then(myAddress => myNode.joinNetwork(testnetToken, 100, 3, 0.4))
// Promise resolves after all channels opened
// Send 8 tokens to recipient with transfer ID 1337
.then(() => myNode.sendTokens(testnetToken, recipientAddress, 8, 1337))
// Promise resolves after transfer succeeds or fails
// Leave the token network
.then(() => myNode.leaveNetwork(testnetToken));
```
* [raiden-rpc](#module_raiden-rpc)
* [RaidenClient](#exp_module_raiden-rpc--RaidenClient) ⏏
* [new RaidenClient([rpcHost], [apiVersion])](#new_module_raiden-rpc--RaidenClient_new)
* _instance_
* [.customRequest(method, uri, [...options])](#module_raiden-rpc--RaidenClient+customRequest)
* [.getAddress([options])](#module_raiden-rpc--RaidenClient+getAddress) ⇒ Promise
* [.registerToken(tokenAddress, [options])](#module_raiden-rpc--RaidenClient+registerToken) ⇒ Promise
* [.getRegisteredTokens([options])](#module_raiden-rpc--RaidenClient+getRegisteredTokens) ⇒ Promise
* [.getTokenPartners(tokenAddress, [options])](#module_raiden-rpc--RaidenClient+getTokenPartners) ⇒ Promise
* [.getChannel(channelAddress, [options])](#module_raiden-rpc--RaidenClient+getChannel) ⇒ Promise
* [.getAllChannels([options])](#module_raiden-rpc--RaidenClient+getAllChannels) ⇒ Promise
* [.openChannel(partnerAddress, tokenAddress, initialBalance, [settleTimeout], [revealTimeout], [options])](#module_raiden-rpc--RaidenClient+openChannel) ⇒ Promise
* [.closeChannel(channelAddress, [options])](#module_raiden-rpc--RaidenClient+closeChannel) ⇒ Promise
* [.settleChannel(channelAddress, [options])](#module_raiden-rpc--RaidenClient+settleChannel) ⇒ Promise
* [.deposit(channelAddress, amount, [options])](#module_raiden-rpc--RaidenClient+deposit) ⇒ Promise
* [.joinNetwork(tokenAddress, depositAmount, [numberOfChannels], [reserveDepositRatio], [options])](#module_raiden-rpc--RaidenClient+joinNetwork) ⇒ Promise
* [.leaveNetwork(tokenAddress, [onlyReceivingChannels], [options])](#module_raiden-rpc--RaidenClient+leaveNetwork) ⇒ Promise
* [.sendTokens(tokenAddress, recipientAddress, amount, [transferId], [options])](#module_raiden-rpc--RaidenClient+sendTokens) ⇒ Promise
* [.makeTokenSwap(tokenSwap, [options])](#module_raiden-rpc--RaidenClient+makeTokenSwap) ⇒ Promise
* [.takeTokenSwap(tokenSwap, [options])](#module_raiden-rpc--RaidenClient+takeTokenSwap) ⇒ Promise
* [.getNetworkEvents([fromBlock], [options])](#module_raiden-rpc--RaidenClient+getNetworkEvents) ⇒ Promise
* [.getTokenEvents(tokenAddress, [fromBlock], [options])](#module_raiden-rpc--RaidenClient+getTokenEvents) ⇒ Promise
* [.getChannelEvents(channelAddress, [fromBlock], [options])](#module_raiden-rpc--RaidenClient+getChannelEvents) ⇒ Promise
* _static_
* [.localNode()](#module_raiden-rpc--RaidenClient.localNode) ⇒ Raiden
* _inner_
* [~DEFAULT_RPC_HOST](#module_raiden-rpc--RaidenClient..DEFAULT_RPC_HOST) : String
### RaidenClient ⏏
**Kind**: Exported class
**Properties**
| Name | Description |
| --- | --- |
| baseUrl | The baseUrl used to perform requests. |
#### new RaidenClient([rpcHost], [apiVersion])
A class that represents a raiden node. Holds information used to connect to it's rpc interface.
**Params**
- [rpcHost] String - the full raiden node hostname
- [apiVersion] String - the raiden api version
#### raidenClient.customRequest(method, uri, [...options])
Performs a manual API call on the Raiden node.
**Kind**: instance method of [RaidenClient](#exp_module_raiden-rpc--RaidenClient)
**Params**
- method String - request method to use
- uri String - the api endpoint to call (relative to [RaidenClient#baseUrl](RaidenClient#baseUrl))
- [...options] Object - custom request options
**Example**
```javascript
myNode.customRequest('PATCH', `/channels/${channelAddress}`, { body: { balance: amount } })
```
#### raidenClient.getAddress([options]) ⇒ Promise
Retrieves the Ethereum address associated with the node
**Kind**: instance method of [RaidenClient](#exp_module_raiden-rpc--RaidenClient)
**Fulfill**: String - Ethereum address
**Reject**: Error - request error
**See**: [Raiden docs](https://raiden-network.readthedocs.io/en/stable/rest_api.html#querying-your-address)
**Params**
- [options] Object - custom request options
#### raidenClient.registerToken(tokenAddress, [options]) ⇒ Promise
Registers a token by deploying a channel manager.
**Kind**: instance method of [RaidenClient](#exp_module_raiden-rpc--RaidenClient)
**Fulfill**: String - channel manager Ethereum address
**Reject**: Error - request error
**See**: [Raiden docs](https://raiden-network.readthedocs.io/en/stable/rest_api.html#registering-a-token)
**Params**
- tokenAddress String - Ethereum address
- [options] Object - custom request options
#### raidenClient.getRegisteredTokens([options]) ⇒ Promise
Get a list of addresses of all registered tokens.
**Kind**: instance method of [RaidenClient](#exp_module_raiden-rpc--RaidenClient)
**Fulfill**: String[] - array of Ethereum addresses
**Reject**: Error - request error
**See**: [Raiden docs](https://raiden-network.readthedocs.io/en/stable/rest_api.html#querying-all-traded-tokens)
**Params**
- [options] Object - custom request options
#### raidenClient.getTokenPartners(tokenAddress, [options]) ⇒ Promise
Get a list of all partners you have non-settled channels with.
**Kind**: instance method of [RaidenClient](#exp_module_raiden-rpc--RaidenClient)
**Fulfill**: Object[] - array of objects containing channel_address and partner_address
**Reject**: Error - request error
**See**: [Raiden docs](https://raiden-network.readthedocs.io/en/stable/rest_api.html#querying-all-partners-for-a-token)
**Params**
- tokenAddress String - Ethereum address
- [options] Object - custom request options
#### raidenClient.getChannel(channelAddress, [options]) ⇒ Promise
Query information about your channel.
**Kind**: instance method of [RaidenClient](#exp_module_raiden-rpc--RaidenClient)
**Fulfill**: Object - object containing information about your channel
**Reject**: Error - request error
**See**: [Raiden docs](https://raiden-network.readthedocs.io/en/stable/rest_api.html#querying-a-specific-channel)
**Params**
- channelAddress String - Ethereum address
- [options] Object - custom request options
#### raidenClient.getAllChannels([options]) ⇒ Promise
Get a list of all non-settled channels.
**Kind**: instance method of [RaidenClient](#exp_module_raiden-rpc--RaidenClient)
**Fulfill**: Object[] - array of objects containing information about your channels
**Reject**: Error - request error
**See**: [Raiden docs](https://raiden-network.readthedocs.io/en/stable/rest_api.html#querying-all-channels)
**Params**
- [options] Object - custom request options
#### raidenClient.openChannel(partnerAddress, tokenAddress, initialBalance, [settleTimeout], [revealTimeout], [options]) ⇒ Promise
Creates a channel with a partner.
**Kind**: instance method of [RaidenClient](#exp_module_raiden-rpc--RaidenClient)
**Fulfill**: Object - objects containing information about your newly created channel
**Reject**: Error - request error
**See**
- [Raiden docs](https://raiden-network.readthedocs.io/en/stable/rest_api.html#open-channel)
- [settleTimeout](https://raiden-network.readthedocs.io/en/stable/spec.html#channel-closing-and-settlement)
- [revealTimeout](https://raiden-network.readthedocs.io/en/stable/spec.html#safety-of-mediated-transfers)
**Params**
- partnerAddress String - Ethereum address of other Raiden node
- tokenAddress String - Ethereum address of token that will be transferred in
this channel
- initialBalance Number - Tokens to initially deposit
- [settleTimeout] Number - Number of blocks to wait for settlement after closing
this channel
- [revealTimeout] Number - Number of blocks to use for reveal timeout
- [options] Object - custom request options
#### raidenClient.closeChannel(channelAddress, [options]) ⇒ Promise
Closes an open channel.
**Kind**: instance method of [RaidenClient](#exp_module_raiden-rpc--RaidenClient)
**Fulfill**: Object - object containing information about your channel
**Reject**: Error - request error
**See**: [Raiden docs](https://raiden-network.readthedocs.io/en/stable/rest_api.html#close-channel)
**Params**
- channelAddress String - Ethereum address of channel
- [options] Object - custom request options
#### raidenClient.settleChannel(channelAddress, [options]) ⇒ Promise
Settle a closed channel.
**Kind**: instance method of [RaidenClient](#exp_module_raiden-rpc--RaidenClient)
**Fulfill**: Object - object containing information about your channel
**Reject**: Error - request error
**See**: [Raiden docs](https://raiden-network.readthedocs.io/en/stable/rest_api.html#settle-channel)
**Params**
- channelAddress String - Ethereum address of channel
- [options] Object - custom request options
#### raidenClient.deposit(channelAddress, amount, [options]) ⇒ Promise
Deposit more tokens into a channel. Token to deposit was specified on channel creation.
**Kind**: instance method of [RaidenClient](#exp_module_raiden-rpc--RaidenClient)
**Fulfill**: Object - object containing information about your channel
**Reject**: Error - request error
**See**: [Raiden docs](https://raiden-network.readthedocs.io/en/stable/rest_api.html#deposit-to-a-channel)
**Params**
- channelAddress String - Ethereum address of channel
- amount Number - number of tokens
- [options] Object - custom request options
#### raidenClient.joinNetwork(tokenAddress, depositAmount, [numberOfChannels], [reserveDepositRatio], [options]) ⇒ Promise
Join an existing token network.
**Kind**: instance method of [RaidenClient](#exp_module_raiden-rpc--RaidenClient)
**Fulfill**: - no content
**Reject**: Error - request error
**See**: [Raiden docs](https://raiden-network.readthedocs.io/en/stable/rest_api.html#connecting-to-a-token-network)
**Params**
- tokenAddress String - Ethereum address of token
- depositAmount Number - Total number of tokens to deposit
- [numberOfChannels] Number = 3 - Number of channels to open on the network
- [reserveDepositRatio] Number = 0.4 - Ratio of tokens deposited that should be reserved for
future channel creation. The rest are distributed amongst the [numberOfChannels](numberOfChannels) opened.
- [options] Object - custom request options
#### raidenClient.leaveNetwork(tokenAddress, [onlyReceivingChannels], [options]) ⇒ Promise
Close all open channels on a token network. The promise will only fulfill once all blockchain
calls for closing/settling a channel have completed.
Important note. If no arguments are given then raiden will only close and settle channels
where your node has received transfers.
**Kind**: instance method of [RaidenClient](#exp_module_raiden-rpc--RaidenClient)
**Fulfill**: String[] - Ethereum addresses of all closed channels.
**Reject**: Error - request error
**See**: [Raiden docs](https://raiden-network.readthedocs.io/en/stable/rest_api.html#leaving-a-token-network)
**Params**
- tokenAddress String - Ethereum address of token
- [onlyReceivingChannels] boolean = true - true if channels that have received transfers
should be closed, false if every channel should be closed.
- [options] Object - custom request options
#### raidenClient.sendTokens(tokenAddress, recipientAddress, amount, [transferId], [options]) ⇒ Promise
Transfer tokens to a recipient.
**Kind**: instance method of [RaidenClient](#exp_module_raiden-rpc--RaidenClient)
**Fulfill**: Object - object containing information about your transfer
**Reject**: Error - request error
**See**: [Raiden docs](https://raiden-network.readthedocs.io/en/stable/rest_api.html#initiating-a-transfer)
**Params**
- tokenAddress String - Ethereum address of token
- recipientAddress String - Ethereum address of recipient
- amount Number - number of tokens to send
- [transferId] Number - integer identifier to attach to the transfer
- [options] Object - custom request options
#### raidenClient.makeTokenSwap(tokenSwap, [options]) ⇒ Promise
Request a token swap to atomically exchange two tokens with a specified recipient.
**Kind**: instance method of [RaidenClient](#exp_module_raiden-rpc--RaidenClient)
**Fulfill**: - no content
**Reject**: Error - request error
**See**: [Raiden docs](https://raiden-network.readthedocs.io/en/stable/rest_api.html#token-swaps)
**Params**
- tokenSwap Object - object representing the token swap
- .identifier Number - Integer identifier to use for this swap
- [.makerAddress] String - Ethereum address of the swap maker
- .makerToken String - Ethereum address of token the maker wants to swap
- .makerAmount Number - number of tokens the maker wants to swap
- .takerAddress String - Ethereum address of the swap taker
- .takerToken String - Ethereum address of token the taker will swap
- .takerAmount Number - number of tokens the taker will swap
- [options] Object - custom request options
#### raidenClient.takeTokenSwap(tokenSwap, [options]) ⇒ Promise
Accept a token swap to atomically exchange two tokens with a specified recipient.
**Kind**: instance method of [RaidenClient](#exp_module_raiden-rpc--RaidenClient)
**Fulfill**: - no content
**Reject**: Error - request error
**See**: [Raiden docs](https://raiden-network.readthedocs.io/en/stable/rest_api.html#token-swaps)
**Params**
- tokenSwap Object - object representing the token swap
- .identifier Number - Integer identifier of the swap
- .makerAddress String - Ethereum address of the swap maker
- .makerToken String - Ethereum address of token the maker wants to swap
- .makerAmount Number - number of tokens the maker wants to swap
- [.takerAddress] String - Ethereum address of the swap taker
- .takerToken String - Ethereum address of token the taker will swap
- .takerAmount Number - number of tokens the taker will swap
- [options] Object - custom request options
#### raidenClient.getNetworkEvents([fromBlock], [options]) ⇒ Promise
Query for registry network events.
**Kind**: instance method of [RaidenClient](#exp_module_raiden-rpc--RaidenClient)
**Fulfill**: Object[] - array of objects containing information about each event
**Reject**: Error - request error
**See**: [Raiden docs](https://raiden-network.readthedocs.io/en/stable/rest_api.html#querying-general-network-events)
**Params**
- [fromBlock] Number - only get events that occurred after this block
- [options] Object - custom request options
#### raidenClient.getTokenEvents(tokenAddress, [fromBlock], [options]) ⇒ Promise
Query for all new channels opened for a token
**Kind**: instance method of [RaidenClient](#exp_module_raiden-rpc--RaidenClient)
**Fulfill**: Object[] - array of objects containing information about each event
**Reject**: Error - request error
**See**: [Raiden docs](https://raiden-network.readthedocs.io/en/stable/rest_api.html#querying-token-network-events)
**Params**
- tokenAddress String - Ethereum address of token
- [fromBlock] Number - only get events that occurred after this block
- [options] Object - custom request options
#### raidenClient.getChannelEvents(channelAddress, [fromBlock], [options]) ⇒ Promise
Query for events tied to a specific channel
**Kind**: instance method of [RaidenClient](#exp_module_raiden-rpc--RaidenClient)
**Fulfill**: Object[] - array of objects containing information about each event
**Reject**: Error - request error
**See**: [Raiden docs](https://raiden-network.readthedocs.io/en/stable/rest_api.html#querying-channel-events)
**Params**
- channelAddress String - Ethereum address of channel
- [fromBlock] Number - only get events that occurred after this block
- [options] Object - custom request options
#### RaidenClient.localNode() ⇒ Raiden
Returns a new instance of [RaideNode](RaideNode) connecting to the default localhost node.
**Kind**: static method of [RaidenClient](#exp_module_raiden-rpc--RaidenClient)
#### RaidenClient~DEFAULT_RPC_HOST : String
**Kind**: inner constant of [RaidenClient](#exp_module_raiden-rpc--RaidenClient)
**Default**: http://127.0.0.1:5001/