## Getting started  

### Install package  

```bash
npm i --save @equilab/api # or yarn add @equilab/api
```

### Create adapter  

```typescript
const adapter = createAdapter({
      nodes: "wss://devnet.genshiro.io",
	  // you can also specify multiple fallback nodes, eg.
      // nodes: ["wss://node1.genshiro.io", "wss://node2.genshiro.io"],
      // keyring, 
	  // optional; use it if you want to broadcast transactions
    });
```

**NOTE** if you want to broadcast transactions to chain you need to initialize keyring.  
You need **NOT** install packages `@polkadot/keyring` and `@polkadot/util-crypto` independently.  

```typescript
import { createAdapter } from "../src/dex";
import Keyring from "@polkadot/keyring";
import { cryptoWaitReady } from "@polkadot/util-crypto";

const mnemonic = "lyrics topple split fold junk ecology woman talent jazz height wrist lonely";

(async () => {
    await cryptoWaitReady(); // need to initialize wasm first
    // create keyring to broadcast transactions to blockchain
    const kr = new Keyring();
    kr.addFromMnemonic(mnemonic, {}, "sr25519");
	
    const adapter = createAdapter({
      nodes: "wss://devnet.genshiro.io",
      keyring: kr,
    });
})();
```

### Fetching data  

You can fetch data in 2 ways:  
1. via promise:  
```typescript
adapter.getOrders("Wbtc").get().then((orders) => { 
	console.log(orders);
});
```  

2. via subscription:  

```typescript
const unsub = adapter.getOrders("Wbtc").subscribe((orders) => {
	console.log(orders);
});

// do not forget to unsubscribe after you do not need data
unsub();
```

### Broadcasting transactions  

Call `.exec()` method on transactions:  
```typescript
adapter.deposit(1, "Wbtc").exec().then(events => {
	console.log(events);
}).catch(e => {
	console.error(e.message);
});
```

## Api reference  

### Data types

```typescript
// Query handlers:
interface IDexQueryable<T> {
	observable: Observable<T>;
	get(): Promise<T>;
	subscribe(value: T): () => void;
}

// Transaction handlers
interface IDexTransaction {
	observable: Observable<IEvent<any>[]>;
	exec(): Promise<IEvent<any>[]>;
}

interface ExchangeEntry {
    id: number;
    currency: string;
    price: number;
    amount: number;
    blockNumber: number;
    exchangeDate: string;
}

## Getting started  

### Install package  

```bash
npm i --save @equilab/api # or yarn add @equilab/api
```

### Create adapter  

```typescript
const adapter = createAdapter({
      nodes: "wss://devnet.genshiro.io",
	  // you can also specify multiple fallback nodes, eg.
      // nodes: ["wss://node1.genshiro.io", "wss://node2.genshiro.io"],
      // keyring, 
	  // optional; use it if you want to broadcast transactions
    });
```

**NOTE** if you want to broadcast transactions to chain you need to initialize keyring.  
You need **NOT** install packages `@polkadot/keyring` and `@polkadot/util-crypto` independently.  

```typescript
import { createAdapter } from "../src/dex";
import Keyring from "@polkadot/keyring";
import { cryptoWaitReady } from "@polkadot/util-crypto";

const mnemonic = "lyrics topple split fold junk ecology woman talent jazz height wrist lonely";

(async () => {
    await cryptoWaitReady(); // need to initialize wasm first
    // create keyring to broadcast transactions to blockchain
    const kr = new Keyring();
    kr.addFromMnemonic(mnemonic, {}, "sr25519");
	
    const adapter = createAdapter({
      nodes: "wss://devnet.genshiro.io",
      keyring: kr,
    });
})();
```

### Fetching data  

You can fetch data in 2 ways:  
1. via promise:  
```typescript
adapter.getOrders("Wbtc").get().then((orders) => { 
	console.log(orders);
});
```  

2. via subscription:  

```typescript
const unsub = adapter.getOrders("Wbtc").subscribe((orders) => {
	console.log(orders);
});

// do not forget to unsubscribe after you do not need data
unsub();
```

### Broadcasting transactions  

Call `.exec()` method on transactions:  
```typescript
adapter.deposit(1, "Wbtc").exec().then(events => {
	console.log(events);
}).catch(e => {
	console.error(e.message);
});
```

## Api reference  

### Data types

```typescript
// Query handlers:
interface IDexQueryable<T> {
	observable: Observable<T>;
	get(): Promise<T>;
	subscribe(value: T): () => void;
}

// Transaction handlers
interface IDexTransaction {
	observable: Observable<IEvent<any>[]>;
	exec(): Promise<IEvent<any>[]>;
}

interface ExchangeEntry {
    id: number;
    currency: string;
    price: number;
    amount: number;
    blockNumber: number;
    exchangeDate: string;
}

interface BestPrice {
  ask: string;
  bid: string;
}

interface Order {
  id: number;
  account: string;
  side: "buy" | "sell";
  price: string;
  amount: string;
  createdAt: string;
  expirationTime: string;
}
```

### Queries

```typescript
// Fetch active orders
adapter.getOrders(
	token: string	// token of exchange pair {token} -> EQD
): IDexQueryable<Order[]>

// Fetch matched trades
adapter.getTrades(
	token: string	// token of exchange pair {token} -> EQD
): IDexQueryable<ExchangeEntry[]>

// Fetch best prices by token
adapter.getBestPrice(
	token: string	// token of exchange pair {token} -> EQD
): IDexQueryable<BestPrice>
```

### Transactions

```typescript
// Deposit tokens to trading account
adapter.deposit(
	amount: string | number  // amount to deposit
	token: string					// token to deposit
): IDexTransaction

// Withdraw tokens from trading account
adapter.withdraw(
	amount: string | number 	// amount to withdraw
	token: string					   // token to withdraw
): IDexTransaction

// Create new order
adapter.createOrder(
	token: string;			// token of exchange pair {token} -> EQD
	side: "Buy" | "Sell"; 	// order side
	amount: string | number;
	price: string | number;
): IDexTransaction

// Cancel order
adapter.cancelOrder(
	token: string;			// token of exchange pair {token} -> EQD
	id: number;				// order ID
	price: string | number;
): IDexTransaction

```
