# EQUILIBRIUM API

API bindings to access Equilibruim substrate queries and transactions  
**NOTE** Typescript bindings included  

## Installation

```bash
$ npm i --save @equilab/api # if you are using npm or  
$ yarn add @equilab/api     # for yarn package manager  
```

## Usage

Use *createApi(node: string)* factory from __@equilab/api__ package  
```typescript
import { createApi } from "@equilab/api";  

(async () => { // async/await usage
  // Connect to TGE node with websocket
  const api = await createApi("wss://alice.tge-testnet-release.equilibrium.io");
  // do the interaction below
  const balance = await api.getBalance("YOUR_ADDRESS", "EQ"); // get EQ tokens
})();

createApi("wss://alice.tge-testnet-release.equilibrium.io")
.then(api => api.getBalance("YOUR_ADDRESS", "EQ"))
.then(balance => console.log(balance.toJSON())); // Promise usage
```  

## Methods  
### *api.setSigner(signer: Signer)*  
sets transaction signer, can be used with [injected wallet](https://github.com/polkadot-js/extension/tree/master/packages/extension-dapp)  

### *api.multi(...)* - useful for subscribing to [multiple queries](https://polkadot.js.org/api/start/api.query.multi.html)  

### *api.claim(address: string, signature: string)*
create claim transaction  
```typescript
import { createApi } from "@equilab/api";
import { u8aToHex } from "@polkadot/util";
import { decodeAddress } from "@polkadot/util-crypto";
import Web3 from "web3";
const polkadotAddress = "5DnoYz3koaRcMZ9Hj4FmQ2nNRKQfS73yBmEzzM9SsPn9cLtb";
const ethAddress = "0xE372d786E3E6529BBe9CeA4040466531E2Ac4796";
const web3 = new Web3(Web3.givenProvider);
const message = `Pay EQ to the account:${u8aToHex(decodeAddress(account.address), -1, false)}`;

Promise.all([
  createApi("wss://alice.tge-testnet-release.equilibrium.io"),
  web3.current.eth.personal.sign(message, ethAddress)
]).then(([api, message]) => api.claim(polkadotAddress, sig).send() /* transaction must be unsigned */) 
```  

### *api.vest()*  
claim unlocked vested amount for current address  
```typescript
createApi(...).then(api => api.vest().signAndSend(YOUR_ADDRESS)) // transaction must be signed with destination address
```  

### *api.getBalance(address: string, currency)*  
query balance, *NOTE* you can call multi to subscribe to multiple address or currencies balance change  
```typescript
const subscribeToEQBalances = async (...addresses: string[]) => {
  const api = await createAPI(NODE);
  const unsub = await api.getBalance.multi(
    addresses.map(address => [address, "EQ"]),
    (balances) => console.log(balances)
  );

  return unsub; // call this function to unsubscribe
}
```  

### *api.transfer(currency: "Usd" | "EQ" | "Eth" | "Btc" | "Eos" | "Dot", to: string | Uint8Array | AccountId, amount: string | number )  
transfer *amount* of *currency* *to* address 
```typescript
import { createApi } from "@equilab/api";

(async () => {
  const api = await createApi("wss://api.mvp.equilibrium.io");
  await api.transfer(
    "EQ",
    "5DnoYz3koaRcMZ9Hj4FmQ2nNRKQfS73yBmEzzM9SsPn9cLtb",
    "1000000000"
  ); // 1 EQ token has 9 decimals
})();
```  

### Other
 - *api.getClaim*  
 - *api.getClaimSigning*  
 - *api.getClaimVesting*  
 - *api.getTotalClaim*  
 - *api.getVested*  
 - *api.getVesting*  
