bity.ts
1 import { WhitelistedCoins, bityConfig } from 'config'; 2 import { checkHttpStatus, parseJSON, filter } from './utils'; 3 import bitcoinIcon from 'assets/images/bitcoin.png'; 4 import repIcon from 'assets/images/augur.png'; 5 import etherIcon from 'assets/images/ether.png'; 6 7 const isCryptoPair = (from: string, to: string, arr: WhitelistedCoins[]) => { 8 return filter(from, arr) && filter(to, arr); 9 }; 10 11 const btcOptions = { 12 id: 'BTC', 13 status: 'available', 14 image: bitcoinIcon, 15 name: 'Bitcoin' 16 }; 17 18 const ethOptions = { 19 id: 'ETH', 20 status: 'available', 21 image: etherIcon, 22 name: 'Ether' 23 }; 24 25 const repOptions = { 26 id: 'REP', 27 status: 'available', 28 image: repIcon, 29 name: 'Augur' 30 }; 31 32 export interface MappedRates { 33 [key: string]: any; 34 } 35 36 export function getAllRates() { 37 const mappedRates: MappedRates = {}; 38 return _getAllRates().then(bityRates => { 39 bityRates.objects.forEach((each: any) => { 40 const pairName = each.pair; 41 const from = { id: pairName.substring(0, 3) }; 42 const to = { id: pairName.substring(3, 6) }; 43 // Check if rate exists= && check if the pair only crypto to crypto, not crypto to fiat, or any other combination 44 if (parseFloat(each.rate_we_sell) && isCryptoPair(from.id, to.id, ['BTC', 'ETH', 'REP'])) { 45 let fromOptions; 46 let toOptions; 47 switch (from.id) { 48 case 'BTC': 49 fromOptions = btcOptions; 50 break; 51 case 'ETH': 52 fromOptions = ethOptions; 53 break; 54 case 'REP': 55 fromOptions = repOptions; 56 } 57 switch (to.id) { 58 case 'BTC': 59 toOptions = btcOptions; 60 break; 61 case 'ETH': 62 toOptions = ethOptions; 63 break; 64 case 'REP': 65 toOptions = repOptions; 66 } 67 mappedRates[pairName] = { 68 id: pairName, 69 options: [fromOptions, toOptions], 70 rate: parseFloat(each.rate_we_sell) 71 }; 72 } 73 }); 74 return mappedRates; 75 }); 76 } 77 78 export function postOrder(amount: number, destAddress: string, mode: number, pair: string) { 79 return fetch(`${bityConfig.serverURL}/order`, { 80 method: 'post', 81 body: JSON.stringify({ 82 amount, 83 destAddress, 84 mode, 85 pair 86 }), 87 headers: new Headers(bityConfig.postConfig.headers) 88 }) 89 .then(checkHttpStatus) 90 .then(parseJSON); 91 } 92 93 export function getOrderStatus(orderId: string) { 94 return fetch(`${bityConfig.serverURL}/status`, { 95 method: 'POST', 96 body: JSON.stringify({ 97 orderid: orderId 98 }), 99 headers: new Headers(bityConfig.postConfig.headers) 100 }) 101 .then(checkHttpStatus) 102 .then(parseJSON); 103 } 104 105 function _getAllRates() { 106 return fetch(`${bityConfig.bityURL}/v1/rate2/`) 107 .then(checkHttpStatus) 108 .then(parseJSON); 109 }