Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Commit

Permalink
refactor(Trade): move save() to delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
Sjors committed Sep 22, 2016
1 parent 4b3c521 commit e9a4c32
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 121 deletions.
39 changes: 15 additions & 24 deletions src/coinify/coinify.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

/* To use this class, three things are needed:
1 - a delegate object with functions that provide the following:
save() -> e.g. function () { return JSON.stringify(this._coinify); }
email() -> String : the users email address
isEmailVerified() -> Boolean : whether the users email is verified
getEmailToken() -> stringify : JSON web token {email: 'me@example.com'}
Expand All @@ -16,17 +17,11 @@
2 - a Coinify parner identifier
3 - a parent object with a save() method, e.g.:
var parent = {
save: function () { return JSON.stringify(this._coinify); }
}
var object = {user: 1, offline_token: 'token'};
var coinify = new Coinify(object, parent);
coinify.delegate = delegate;
coinify.partnerId = ...;
parent._coinify = coinify;
coinify.save()
// "{"user":1,"offline_token":"token"}"
var object = {user: 1, offline_token: 'token'};
var coinify = new Coinify(object, delegate);
coinify.partnerId = ...;
coinify.delegate.save.bind(coinify.delegate)()
// "{"user":1,"offline_token":"token"}"
*/

var CoinifyProfile = require('./profile');
Expand All @@ -49,11 +44,10 @@ var isString = function (str) {

module.exports = Coinify;

function Coinify (object, parent, delegate) {
function Coinify (object, delegate) {
assert(delegate, 'ExchangeDelegate required');
this._delegate = delegate;
var obj = object || {};
this._parent = parent; // parent this of external (for save)
this._partner_id = null;
this._user = obj.user;
this._auto_login = obj.auto_login;
Expand Down Expand Up @@ -111,7 +105,7 @@ Object.defineProperties(Coinify.prototype, {
'Boolean'
);
this._auto_login = value;
this.save();
this.delegate.save.bind(this.delegate)();
}
},
'profile': {
Expand Down Expand Up @@ -175,9 +169,6 @@ Coinify.prototype.toJSON = function () {

return coinify;
};
Coinify.prototype.save = function () {
return this._parent.save();
};
// Country and default currency must be set
// Email must be set and verified
Coinify.prototype.signup = function (countryCode, currencyCode) {
Expand Down Expand Up @@ -221,7 +212,7 @@ Coinify.prototype.signup = function (countryCode, currencyCode) {
this._user = res.trader.id;
this._offlineToken = res.offlineToken;
this._api._offlineToken = this._offlineToken;
return this.save().then(function () { return res; });
return this._delegate.save.bind(this._delegate)().then(function () { return res; });
};

return Promise.resolve().then(runChecks.bind(this))
Expand Down Expand Up @@ -260,7 +251,7 @@ Coinify.prototype.buy = function (amount, baseCurrency, medium) {
var addTrade = function (trade) {
trade.debug = this._debug;
this._trades.push(trade);
return this.save().then(function () { return trade; });
return this.delegate.save.bind(this.delegate)().then(function () { return trade; });
};

return CoinifyTrade.buy(
Expand Down Expand Up @@ -294,7 +285,7 @@ Coinify.prototype.updateList = function (list, items, ListClass) {
Coinify.prototype.getTrades = function () {
var self = this;
var save = function () {
return this.save().then(function () { return self._trades; });
return this.delegate.save.bind(this.delegate)().then(function () { return self._trades; });
};
var update = function (trades) {
this.updateList(this._trades, trades, CoinifyTrade);
Expand Down Expand Up @@ -323,7 +314,7 @@ Coinify.prototype.triggerKYC = function () {
Coinify.prototype.getKYCs = function () {
var self = this;
var save = function () {
return this.save().then(function () { return self._kycs; });
return this.delegate.save.bind(this.delegate)().then(function () { return self._kycs; });
};
var update = function (kycs) {
this.updateList(this._kycs, kycs, CoinifyKYC);
Expand Down Expand Up @@ -378,14 +369,14 @@ Coinify.prototype.getSellCurrencies = function () {
};

Coinify.prototype.monitorPayments = function () {
CoinifyTrade.monitorPayments(this._trades, this);
CoinifyTrade.monitorPayments(this._trades, this.delegate);
};

Coinify.new = function (parent, delegate) {
Coinify.new = function (delegate) {
assert(delegate, 'Coinify.new requires delegate');
var object = {
auto_login: true
};
var coinify = new Coinify(object, parent, delegate);
var coinify = new Coinify(object, delegate);
return coinify;
};
44 changes: 21 additions & 23 deletions src/coinify/trade.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ var assert = require('assert');

var BankAccount = require('./bank-account');
var Helpers = require('./helpers');
var Quote = require('./quote');

module.exports = CoinifyTrade;

function CoinifyTrade (obj, api, coinifyDelegate, coinify) {
// TODO: remove dependency on 'coinify'
function CoinifyTrade (obj, api, coinifyDelegate) {
assert(obj, 'JSON missing');
assert(api, 'Coinify API missing');
assert(coinifyDelegate, 'coinifyDelegate missing');
assert(coinify, 'Reference to Coinify missing');
this._coinifyDelegate = coinifyDelegate;
this._api = api;
this._coinify = coinify; // TODO: avoid this reference
this._id = obj.id;
this.set(obj);
}
Expand Down Expand Up @@ -235,7 +233,7 @@ CoinifyTrade.prototype.cancel = function () {

self._coinifyDelegate.releaseReceiveAddress(self);

return self._coinify.save();
return self._coinifyDelegate.save.bind(self._coinifyDelegate)();
};

return self._api.authPATCH('trades/' + self._id + '/cancel').then(processCancel);
Expand Down Expand Up @@ -281,7 +279,7 @@ CoinifyTrade.prototype.btcExpected = function () {
self._lastBtcExpectedGuessAt = new Date();
return self._lastBtcExpectedGuess;
};
return this._coinify.getBuyQuote(this.inAmount, this.inCurrency).then(processQuote);
return Quote.getQuote(this._api, -this.inAmount, this.inCurrency).then(processQuote);
}
}
} else {
Expand All @@ -304,7 +302,7 @@ CoinifyTrade.prototype.expireQuote = function () {
this._quoteExpireTime = new Date(new Date().getTime() + 3000);
};

CoinifyTrade.buy = function (quote, medium, api, coinifyDelegate, coinify, debug) {
CoinifyTrade.buy = function (quote, medium, api, coinifyDelegate, debug) {
assert(quote, 'Quote required');

if (debug) {
Expand All @@ -313,7 +311,7 @@ CoinifyTrade.buy = function (quote, medium, api, coinifyDelegate, coinify, debug
var reservation = coinifyDelegate.reserveReceiveAddress();

var processTrade = function (res) {
var trade = new CoinifyTrade(res, api, coinifyDelegate, coinify);
var trade = new CoinifyTrade(res, api, coinifyDelegate);
trade.debug = debug;
if (debug) {
console.info('Commit receive address for new trade');
Expand Down Expand Up @@ -368,7 +366,7 @@ CoinifyTrade.prototype.refresh = function () {
}
return this._api.authGET('trades/' + this._id)
.then(this.set.bind(this))
.then(this._coinify.save.bind(this._coinify))
.then(this._coinifyDelegate.save.bind(this._coinifyDelegate))
.then(this.self.bind(this));
};

Expand All @@ -389,7 +387,7 @@ CoinifyTrade.prototype._monitorAddress = function () {
var resolve = function () {
self._watchAddressResolve && self._watchAddressResolve(amount);
};
self._coinify.save.bind(self._coinify)().then(resolve);
self._coinifyDelegate.save.bind(self._coinifyDelegate)().then(resolve);
};

self._coinifyDelegate.monitorAddress(self.receiveAddress, function (hash, amount) {
Expand Down Expand Up @@ -438,7 +436,9 @@ CoinifyTrade.prototype._monitorAddress = function () {
});
};

CoinifyTrade._checkOnce = function (unfilteredTrades, tradeFilter, coinify) {
CoinifyTrade._checkOnce = function (unfilteredTrades, tradeFilter, coinifyDelegate) {
assert(coinifyDelegate, '_checkOnce needs delegate');

var getReceiveAddress = function (obj) { return obj.receiveAddress; };

var trades = unfilteredTrades.filter(tradeFilter);
Expand All @@ -452,18 +452,15 @@ CoinifyTrade._checkOnce = function (unfilteredTrades, tradeFilter, coinify) {
var promises = [];

for (var i = 0; i < trades.length; i++) {
promises.push(CoinifyTrade._getTransactionHash(trades[i]));
promises.push(CoinifyTrade._getTransactionHash(trades[i], coinifyDelegate));
}

var save = function () {
coinify.save.bind(coinify)();
};

return Promise.all(promises).then(save);
return Promise.all(promises).then(coinifyDelegate.save.bind(coinifyDelegate));
};

CoinifyTrade._getTransactionHash = function (trade) {
return trade._coinify.delegate.checkAddress(trade.receiveAddress)
CoinifyTrade._getTransactionHash = function (trade, coinifyDelegate) {
assert(coinifyDelegate, '_getTransactionHash needs delegate');
return coinifyDelegate.checkAddress(trade.receiveAddress)
.then(function (tx) {
if (tx) {
if (trade.state === 'completed_test' && !trade._txHash) {
Expand Down Expand Up @@ -497,8 +494,9 @@ CoinifyTrade._monitorWebSockets = function (unfilteredTrades, tradeFilter) {
};

// Monitor the receive addresses for pending and completed trades.
// TODO: avoid using coinify reference
CoinifyTrade.monitorPayments = function (trades, coinify) {
CoinifyTrade.monitorPayments = function (trades, coinifyDelegate) {
assert(coinifyDelegate, '_monitorPayments needs delegate');

var tradeFilter = function (trade) {
return [
'awaiting_transfer_in',
Expand All @@ -509,7 +507,7 @@ CoinifyTrade.monitorPayments = function (trades, coinify) {
].indexOf(trade.state) > -1 && !trade.confirmed;
};

CoinifyTrade._checkOnce(trades, tradeFilter, coinify).then(function () {
CoinifyTrade._checkOnce(trades, tradeFilter, coinifyDelegate).then(function () {
CoinifyTrade._monitorWebSockets(trades, tradeFilter);
});
};
Expand All @@ -523,7 +521,7 @@ CoinifyTrade.prototype.toJSON = function () {
is_buy: this.isBuy
};

this._coinify.delegate.serializeExtraFields(serialized, this);
this._coinifyDelegate.serializeExtraFields(serialized, this);

return serialized;
};
Expand Down
4 changes: 4 additions & 0 deletions src/exchange-delegate.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ Object.defineProperties(ExchangeDelegate.prototype, {
}
});

ExchangeDelegate.prototype.save = function () {
return this._wallet.external.save();
};

ExchangeDelegate.prototype.email = function () {
return this._wallet.accountInfo.email;
};
Expand Down
4 changes: 2 additions & 2 deletions src/external.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ External.prototype.fetch = function () {
if (object !== null) {
if (object.coinify) {
var delegate = new ExchangeDelegate(this._wallet);
this._coinify = new Coinify(object.coinify, this, delegate);
this._coinify = new Coinify(object.coinify, delegate);
delegate.trades = this._coinify.trades;
}
}
Expand Down Expand Up @@ -66,6 +66,6 @@ External.prototype.addCoinify = function () {
assert(!this._coinify, 'Already added');

var delegate = new ExchangeDelegate(this._wallet);
this._coinify = Coinify.new(this, delegate);
this._coinify = Coinify.new(delegate);
delegate.trades = this._coinify.trades;
};

0 comments on commit e9a4c32

Please sign in to comment.