|
| 1 | +import get from 'lodash/get' |
| 2 | +import axios from 'axios' |
| 3 | + |
| 4 | +import { getAPIHostname } from './apiTarget' |
| 5 | + |
| 6 | +export interface CreateCpsQuotePayload { |
| 7 | + from: { |
| 8 | + amount?: number |
| 9 | + currency: string |
| 10 | + } |
| 11 | + to: { |
| 12 | + amount?: number |
| 13 | + currency: string |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +export interface CreateCpsTradePayload { |
| 18 | + idempotencyKey: string |
| 19 | + quoteId: string |
| 20 | +} |
| 21 | + |
| 22 | +export interface Consideration { |
| 23 | + quoteId: string |
| 24 | + base: string |
| 25 | + quote: string |
| 26 | + quoteAmount: number |
| 27 | + baseAmount: number |
| 28 | + maturity: number |
| 29 | +} |
| 30 | + |
| 31 | +export interface PiFXTraderDetails { |
| 32 | + recipient: string |
| 33 | + deadline: number |
| 34 | + nonce: number |
| 35 | + consideration: Consideration |
| 36 | +} |
| 37 | + |
| 38 | +export interface CreatePiFXSignaturePayload { |
| 39 | + tradeId: string |
| 40 | + type: string |
| 41 | + address: string |
| 42 | + details: PiFXTraderDetails |
| 43 | + signature: string |
| 44 | +} |
| 45 | + |
| 46 | +const instance = axios.create({ |
| 47 | + baseURL: getAPIHostname(), |
| 48 | +}) |
| 49 | + |
| 50 | +const CPS_TRADES_PATH = '/v1/exchange/cps/trades' |
| 51 | +const CPS_QUOTES_PATH = '/v1/exchange/cps/quotes' |
| 52 | +const CPS_SIGNATURES_PATH = '/v1/exchange/cps/signatures' |
| 53 | + |
| 54 | +/** |
| 55 | + * Global error handler: |
| 56 | + * Intercepts all axios reponses and maps |
| 57 | + * to errorHandler object |
| 58 | + */ |
| 59 | +instance.interceptors.response.use( |
| 60 | + function (response) { |
| 61 | + if (get(response, 'data.data')) { |
| 62 | + return response.data.data |
| 63 | + } |
| 64 | + return response |
| 65 | + }, |
| 66 | + function (error) { |
| 67 | + let response = get(error, 'response') |
| 68 | + if (!response) { |
| 69 | + response = error.toJSON() |
| 70 | + } |
| 71 | + return Promise.reject(response) |
| 72 | + } |
| 73 | +) |
| 74 | + |
| 75 | +/** Returns the axios instance */ |
| 76 | +function getInstance() { |
| 77 | + return instance |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Create CPS Quote |
| 82 | + */ |
| 83 | +function createQuote(payload: CreateCpsQuotePayload) { |
| 84 | + if (!payload.from.amount) { |
| 85 | + delete payload.from.amount |
| 86 | + } |
| 87 | + if (!payload.to.amount) { |
| 88 | + delete payload.to.amount |
| 89 | + } |
| 90 | + |
| 91 | + return instance.post(CPS_QUOTES_PATH, payload) |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * Create CPS Trade |
| 96 | + */ |
| 97 | +function createTrade(payload: CreateCpsTradePayload) { |
| 98 | + return instance.post(CPS_TRADES_PATH, payload) |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + * Get CPS Trades |
| 103 | + */ |
| 104 | +function getTrades() { |
| 105 | + return instance.get(CPS_TRADES_PATH) |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * Get CPS Trade |
| 110 | + */ |
| 111 | +function getTrade(tradeId: string) { |
| 112 | + const url = `${CPS_TRADES_PATH}/${tradeId}` |
| 113 | + |
| 114 | + return instance.get(url) |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * Register CPS Signature |
| 119 | + */ |
| 120 | +function registerSignature(payload: CreatePiFXSignaturePayload) { |
| 121 | + return instance.post(CPS_SIGNATURES_PATH, payload) |
| 122 | +} |
| 123 | + |
| 124 | +export default { |
| 125 | + getInstance, |
| 126 | + createQuote, |
| 127 | + createTrade, |
| 128 | + getTrades, |
| 129 | + getTrade, |
| 130 | + registerSignature, |
| 131 | +} |
0 commit comments