All files / Exchanges Luno.ts

11.11% Statements 6/54
0% Branches 0/8
0% Functions 0/18
11.11% Lines 6/54

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 1481x     1x 1x   1x   1x   1x                                                                                                                                                                                                                                                                                  
import axios, { AxiosInstance } from 'axios'
import ExchangeInterface from '../interfaces/ExchangeInterface'
import CryptoCoin from '../util/CrytoCoin'
import Order from '../util/Order'
import Ticker from '../util/Ticker'
import Account from '../util/Account'
import { OrderTypeEnum } from '../enums/index'
 
const BASE_URL = 'https://api.mybitx.com/api/1/'
 
export default class Luno implements ExchangeInterface {
    privateApi: AxiosInstance
    publicApi: AxiosInstance
    baseUrl: string
    apiKey: string
    secretKey: string
 
    constructor(obj?: Object) {
        if (obj) {
            const objKeys = Object.keys(obj)
            for (const key of objKeys) {
                this[key] = obj[key]
            }
        }
        this.baseUrl = BASE_URL
        this.privateApi = axios.create({
            baseURL: this.baseUrl
        })
        this.publicApi = axios.create({
            baseURL: this.baseUrl
        })
 
        this.privateApi.interceptors.request.use(
            config => this.axiosInterceptor(config),
            error => Promise.reject(error)
        )
    }
 
    axiosInterceptor(config: any) {
        return config
    }
 
    getBalance(cryptocoin?: CryptoCoin): Promise<Account> {
        throw new Error('Method not implemented.')
    }
    getMyOrders(): Promise<any> {
        throw new Error('Method not implemented.')
    }
    cancelMyAllOrders(cryptocoin?: CryptoCoin): Promise<any> {
        throw new Error('Method not implemented.')
    }
    cancelMyOrder(id: any, cryptocoin?: CryptoCoin): Promise<any> {
        throw new Error('Method not implemented.')
    }
    putOrder(order: Order): Promise<Order> {
        throw new Error('Method not implemented.')
    }
    async getBuyOrders(cryptocoin: CryptoCoin): Promise<Order[]> {
        const response = await this.publicApi.get(`/orderbook_top?pair=${Luno.getMarket(cryptocoin)}`)
        const ordersList: Order[] = []
        for (const orderObj of response.data.bids) {
            ordersList.push(new Order({
                type  : OrderTypeEnum.BUY,
                volume: Number(orderObj.volume),
                price : Number(orderObj.price),
                cryptocoin
            }))
        }
        return ordersList
    }
    async getSellOrders(cryptocoin: CryptoCoin): Promise<Order[]> {
        const response = await this.publicApi.get(`/orderbook_top?pair=${Luno.getMarket(cryptocoin)}`)
        const ordersList: Order[] = []
        for (const orderObj of response.data.asks) {
            ordersList.push(new Order({
                type  : OrderTypeEnum.SELL,
                volume: Number(orderObj.volume),
                price : Number(orderObj.price),
                cryptocoin
            }))
        }
        return ordersList
    }
    // eslint-disable-next-line
    async getOrders(
        cryptocoin: CryptoCoin): Promise<{ buyOrderList: Order[]; sellOrderList: Order[] }> {
        const url = `/orderbook_top?pair=${Luno.getMarket(cryptocoin)}`
        const response = await this.publicApi.get(url)
        const sellOrderList: Order[] = []
        const buyOrderList: Order[] = []
        for (const orderObj of response.data.asks) {
            sellOrderList.push(new Order({
                type  : OrderTypeEnum.SELL,
                volume: Number(orderObj.volume),
                price : Number(orderObj.price),
                cryptocoin
            }))
        }
 
        for (const orderObj of response.data.bids) {
            buyOrderList.push(new Order({
                type  : OrderTypeEnum.BUY,
                volume: Number(orderObj.volume),
                price : Number(orderObj.price),
                cryptocoin
            }))
        }
        return { buyOrderList, sellOrderList }
    }
    getTicker(cryptocoin?: CryptoCoin, withVolume: boolean = false): Promise<Ticker> {
        if (!withVolume) {
            return this.publicApi
                .get(`/pubticker/${Luno.getMarket(cryptocoin)}`)
                .then((response) => {
                    const result = response.data
                    return new Ticker({
                        ask : Number(result.ask),
                        bid : Number(result.bid),
                        last: Number(result.last),
                        cryptocoin
                    })
                })
                .catch((err) => {
                    console.log(err)
                    return new Ticker({})
                })
        }
        throw new Error('Method not implemented.')
    }
    toString(): string {
        throw new Error('Method not implemented.')
    }
    getExchangeName(): string {
        throw new Error('Method not implemented.')
    }
 
    static getMarket(cryptocoin: CryptoCoin): string {
        switch (cryptocoin.priCoin) {
            case 'btc':
                return 'btcusd'
            case 'ngn':
                return 'XBTNGN'
            default:
        }
        throw new Error('invalid cryptocoin')
    }
}