Skip to content

Commit c7bbce8

Browse files
boxbaggeertweening
authored andcommitted
[FEATURE] add offer autobridging
1 parent 5e2c26a commit c7bbce8

8 files changed

+2048
-606
lines changed

src/js/ripple/autobridgecalculator.js

+435
Large diffs are not rendered by default.

src/js/ripple/orderbook.js

+189-128
Large diffs are not rendered by default.

src/js/ripple/orderbookutils.js

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
'use strict';
2+
3+
var _ = require('lodash');
4+
var assert = require('assert');
5+
var SerializedObject = require('./serializedobject').SerializedObject;
6+
var Types = require('./serializedtypes');
7+
var Amount = require('./amount').Amount;
8+
9+
var IOU_SUFFIX = '/000/rrrrrrrrrrrrrrrrrrrrrhoLvTp';
10+
var OrderBookUtils = {};
11+
12+
function assertValidNumber(number, message) {
13+
assert(!_.isNull(number) && !isNaN(number), message);
14+
}
15+
16+
/**
17+
* Casts and returns offer's taker gets funded amount as a default IOU amount
18+
*
19+
* @param {Object} offer
20+
* @return {Amount}
21+
*/
22+
23+
OrderBookUtils.getOfferTakerGetsFunded = function(offer) {
24+
assertValidNumber(offer.taker_gets_funded, 'Taker gets funded is invalid');
25+
26+
return Amount.from_json(offer.taker_gets_funded + IOU_SUFFIX);
27+
};
28+
29+
/**
30+
* Casts and returns offer's taker pays funded amount as a default IOU amount
31+
*
32+
* @param {Object} offer
33+
* @return {Amount}
34+
*/
35+
36+
OrderBookUtils.getOfferTakerPaysFunded = function(offer) {
37+
assertValidNumber(offer.taker_pays_funded, 'Taker gets funded is invalid');
38+
39+
return Amount.from_json(offer.taker_pays_funded + IOU_SUFFIX);
40+
};
41+
42+
/**
43+
* Get offer taker gets amount
44+
*
45+
* @param {Object} offer
46+
*
47+
* @return {Amount}
48+
*/
49+
50+
OrderBookUtils.getOfferTakerGets = function(offer) {
51+
assert(typeof offer, 'object', 'Offer is invalid');
52+
53+
return Amount.from_json(offer.TakerGets + IOU_SUFFIX);
54+
};
55+
56+
/**
57+
* Retrieve offer quality
58+
*
59+
* @param {Object} offer
60+
* @param {Currency} currencyGets
61+
*/
62+
63+
OrderBookUtils.getOfferQuality = function(offer, currencyGets) {
64+
var amount;
65+
66+
if (currencyGets.has_interest()) {
67+
// XXX Should use Amount#from_quality
68+
amount = Amount.from_json(
69+
offer.TakerPays
70+
).ratio_human(offer.TakerGets, {
71+
reference_date: new Date()
72+
});
73+
} else {
74+
amount = Amount.from_json(offer.quality + IOU_SUFFIX);
75+
}
76+
77+
return amount;
78+
};
79+
80+
/**
81+
* Formats an offer quality amount to a hex that can be parsed by
82+
* Amount.parse_quality
83+
*
84+
* @param {Amount} quality
85+
*
86+
* @return {String}
87+
*/
88+
89+
OrderBookUtils.convertOfferQualityToHex = function(quality) {
90+
assert(quality instanceof Amount, 'Quality is not an amount');
91+
92+
var so = new SerializedObject();
93+
Types.Quality.serialize(so, quality.to_text() + IOU_SUFFIX);
94+
95+
return so.to_hex();
96+
};
97+
98+
/**
99+
*
100+
*/
101+
102+
OrderBookUtils.normalizeAmount = function(value) {
103+
return Amount.from_json(value + IOU_SUFFIX);
104+
};
105+
106+
module.exports = OrderBookUtils;

0 commit comments

Comments
 (0)