Skip to content

Commit a763cbe

Browse files
committed
exchange fixes and caching
1 parent 0019704 commit a763cbe

1 file changed

Lines changed: 29 additions & 12 deletions

File tree

src/services/ExchangeService.js

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import PriceService from "./PriceService";
88
import {BANCOR_EOS_PAIRS, BANCOR_RELAYS} from "../data/bancor_relays";
99
const bucket = couchbase('exchange');
1010

11+
const pairCaches = {};
12+
1113
const SERVICES = {
1214
COINSWITCH:'coinswitch',
1315
BANCOR_EOS:'bancor_eos'
@@ -129,21 +131,36 @@ export default class ExchangeService {
129131
const fromSymbol = token.symbol;
130132

131133
const pairs = {};
134+
const timestamp = new Date();
132135

133136
if(canUseCoinSwitch(token)){
134-
const coinswitchPairs = await this.post(`pairs`, {depositCoin:fromSymbol.toLowerCase(), destinationCoin:toSymbol.toLowerCase()}, coinSwitchApi)
135-
.then(res => res.filter(x => x.isActive))
136-
.then(res => res.map(x => ({
137-
service:SERVICES.COINSWITCH,
138-
type:TYPES.EXCHANGE,
139-
id:x.destinationCoin,
140-
symbol:x.destinationCoin.toUpperCase(),
141-
})))
142-
.catch(err => {
143-
console.error(err);
144-
return []
137+
let coinswitchPairs;
138+
139+
const key = `coinswitch::pairs::${fromSymbol.toLowerCase()}::${toSymbol.toLowerCase()}::${timestamp.getDate()}-${timestamp.getHours()}`;
140+
console.log('cache', key, pairCaches.hasOwnProperty(key), Object.keys(pairCaches));
141+
if(pairCaches.hasOwnProperty(key)) coinswitchPairs = pairCaches[key];
142+
else {
143+
coinswitchPairs = await this.post(`pairs`, {depositCoin:fromSymbol.toLowerCase(), destinationCoin:toSymbol.toLowerCase()}, coinSwitchApi)
144+
.then(res => res.filter(x => x.isActive))
145+
.then(res => res.map(x => ({
146+
service:SERVICES.COINSWITCH,
147+
type:TYPES.EXCHANGE,
148+
id:x.destinationCoin,
149+
symbol:x.destinationCoin.toUpperCase(),
150+
})))
151+
.catch(err => {
152+
console.error(err);
153+
return []
154+
});
155+
156+
Object.keys(pairCaches).filter(x => x.indexOf(`coinswitch::pairs::${fromSymbol.toLowerCase()}::${toSymbol.toLowerCase()}::`) > -1).map(_key => {
157+
delete pairCaches[_key];
145158
});
146159

160+
pairCaches[key] = coinswitchPairs;
161+
}
162+
163+
147164
pairs['base'] = coinswitchPairs.map(pair => {
148165
return Object.assign({token:BASETOKENS.find(x => x.symbol === pair.symbol)}, pair);
149166
}).filter(x => !!x.token);
@@ -270,7 +287,7 @@ export default class ExchangeService {
270287
const id = `${fromAccount}:${toSymbol}:${toAccount}:${amount}:${+new Date()}`;
271288

272289
const decimals = toSymbol.toUpperCase() === 'IQ' ? 3 : 4;
273-
const amountWithSlippage = amount*rate - ((amount*rate)*0.02);
290+
const amountWithSlippage = amount*rate - ((amount*rate)*0.04);
274291
const memo = `1,${converter1} BNT ${converter2} ${toSymbol.toUpperCase()},${parseFloat(amountWithSlippage).toFixed(decimals)},${toAccount}`;
275292

276293
const order = {

0 commit comments

Comments
 (0)