Adjust taker_pays_funded

This commit is contained in:
wltsmrz
2014-08-15 20:05:57 -07:00
parent 60069d0a28
commit 81283eeb84
2 changed files with 79 additions and 28 deletions

View File

@@ -284,10 +284,8 @@ OrderBook.prototype.applyTransferRate = function(balance, transferRate) {
return balance; return balance;
} }
var adjustedBalance = Amount.from_json(balance var iouSuffix = '/USD/rrrrrrrrrrrrrrrrrrrrBZbvji';
+ '/' + this._currencyGets.to_json() var adjustedBalance = Amount.from_json(balance + iouSuffix)
+ '/' + this._issuerGets
)
.divide(transferRate) .divide(transferRate)
.multiply(Amount.from_json(OrderBook.DEFAULT_TRANSFER_RATE)) .multiply(Amount.from_json(OrderBook.DEFAULT_TRANSFER_RATE))
.to_json() .to_json()
@@ -352,22 +350,43 @@ OrderBook.prototype.setFundedAmount = function(offer, fundedAmount) {
assert.strictEqual(typeof offer, 'object', 'Offer is invalid'); assert.strictEqual(typeof offer, 'object', 'Offer is invalid');
assert(!isNaN(fundedAmount), 'Funds is invalid'); assert(!isNaN(fundedAmount), 'Funds is invalid');
var iouSuffix = '/USD/rrrrrrrrrrrrrrrrrrrrBZbvji'; if (fundedAmount === '0') {
offer.taker_gets_funded = '0';
offer.taker_pays_funded = '0';
offer.is_fully_funded = false;
return offer;
}
var takerGetsValue = (typeof offer.TakerGets === 'object') var takerGetsValue = (typeof offer.TakerGets === 'object')
? offer.TakerGets.value ? offer.TakerGets.value
: offer.TakerGets; : offer.TakerGets;
var takerGets = Amount.from_json(takerGetsValue + iouSuffix); var takerPaysValue = (typeof offer.TakerPays === 'object')
? offer.TakerPays.value
: offer.TakerPays;
offer.is_fully_funded = Amount.from_json( var iouSuffix = '/USD/rrrrrrrrrrrrrrrrrrrrBZbvji';
fundedAmount + iouSuffix var fundedTakerGets = Amount.from_json(fundedAmount + iouSuffix);
).compareTo(takerGets) >= 0; var takerGets = Amount.from_json(takerGetsValue + iouSuffix);
var takerPays = Amount.from_json(takerPaysValue + iouSuffix);
offer.is_fully_funded = fundedTakerGets.compareTo(takerGets) >= 0;
if (offer.is_fully_funded) { if (offer.is_fully_funded) {
offer.taker_gets_funded = takerGetsValue; offer.taker_gets_funded = takerGetsValue;
} else { offer.taker_pays_funded = takerPaysValue;
return offer;
}
offer.taker_gets_funded = fundedAmount; offer.taker_gets_funded = fundedAmount;
var rate = Amount.from_json(offer.TakerPays).divide(offer.TakerGets);
var takerPaysFunded = fundedTakerGets.multiply(rate);
if (takerPaysFunded.compareTo(takerPays) <= 0) {
offer.taker_pays_funded = takerPaysFunded.to_text();
} else {
offer.taker_pays_funded = takerPaysValue;
} }
return offer; return offer;
@@ -882,15 +901,25 @@ OrderBook.prototype.updateOfferFunds = function(account, fundedAmount) {
for (var i=0; i<this._offers.length; i++) { for (var i=0; i<this._offers.length; i++) {
var offer = this._offers[i]; var offer = this._offers[i];
if (offer.Account === account) { if (offer.Account !== account) {
// Update funds for account's offer continue;
}
var previousOffer = extend({}, offer); var previousOffer = extend({}, offer);
var previousAmount = offer.taker_gets_funded; var previousFundedGets = Amount.from_json(offer.taker_gets_funded);
this.setFundedAmount(offer, fundedAmount); this.setFundedAmount(offer, fundedAmount);
var hasChangedFunds = !previousFundedGets.equals(
Amount.from_json(offer.taker_gets_funded));
if (hasChangedFunds) {
this.emit('offer_changed', previousOffer, offer); this.emit('offer_changed', previousOffer, offer);
this.emit('offer_funds_changed', offer, previousAmount, fundedAmount); this.emit(
'offer_funds_changed', offer,
previousOffer.taker_gets_funded,
fundedAmount
);
} }
} }
}; };

View File

@@ -339,15 +339,18 @@ describe('OrderBook', function() {
value: '100', value: '100',
currency: 'BTC', currency: 'BTC',
issuer: 'rrrrrrrrrrrrrrrrrrrrBZbvji' issuer: 'rrrrrrrrrrrrrrrrrrrrBZbvji'
} },
TakerPays: '123456'
}; };
book.setFundedAmount(offer, '100.1234'); book.setFundedAmount(offer, '100.1234');
assert.deepEqual(offer, { assert.deepEqual(offer, {
TakerGets: offer.TakerGets, TakerGets: offer.TakerGets,
TakerPays: offer.TakerPays,
is_fully_funded: true,
taker_gets_funded: '100', taker_gets_funded: '100',
is_fully_funded: true taker_pays_funded: '123456'
}); });
}); });
@@ -364,15 +367,18 @@ describe('OrderBook', function() {
value: '100', value: '100',
currency: 'BTC', currency: 'BTC',
issuer: 'rrrrrrrrrrrrrrrrrrrrBZbvji' issuer: 'rrrrrrrrrrrrrrrrrrrrBZbvji'
} },
TakerPays: '123456'
}; };
book.setFundedAmount(offer, '99'); book.setFundedAmount(offer, '99');
assert.deepEqual(offer, { assert.deepEqual(offer, {
TakerGets: offer.TakerGets, TakerGets: offer.TakerGets,
TakerPays: offer.TakerPays,
is_fully_funded: false,
taker_gets_funded: '99', taker_gets_funded: '99',
is_fully_funded: false taker_pays_funded: '122166'
}); });
}); });
@@ -385,16 +391,25 @@ describe('OrderBook', function() {
}); });
var offer = { var offer = {
TakerGets: '100' TakerGets: '100',
TakerPays: {
value: '100.1234',
currency: 'USD',
issuer: 'rrrrrrrrrrrrrrrrrrrrBZbvji'
}
}; };
book.setFundedAmount(offer, '100'); book.setFundedAmount(offer, '100');
assert.deepEqual(offer, { var expected = {
TakerGets: '100', TakerGets: offer.TakerGets,
TakerPays: offer.TakerPays,
is_fully_funded: true,
taker_gets_funded: '100', taker_gets_funded: '100',
is_fully_funded: true taker_pays_funded: '100.1234'
}); };
assert.deepEqual(offer, expected);
}); });
it('Set funded amount - native currency - unfunded', function() { it('Set funded amount - native currency - unfunded', function() {
@@ -406,15 +421,22 @@ describe('OrderBook', function() {
}); });
var offer = { var offer = {
TakerGets: '100' TakerGets: '100',
TakerPays: {
value: '100.1234',
currency: 'USD',
issuer: 'rrrrrrrrrrrrrrrrrrrrBZbvji'
}
}; };
book.setFundedAmount(offer, '99'); book.setFundedAmount(offer, '99');
assert.deepEqual(offer, { assert.deepEqual(offer, {
TakerGets: '100', TakerGets: offer.TakerGets,
TakerPays: offer.TakerPays,
is_fully_funded: false,
taker_gets_funded: '99', taker_gets_funded: '99',
is_fully_funded: false taker_pays_funded: '99.122166'
}); });
}); });