Add fee calculation based on load.

This commit is contained in:
Stefan Thomas
2013-07-23 20:59:14 -07:00
parent 0d00a30d19
commit 6bc2493d8d
2 changed files with 54 additions and 12 deletions

View File

@@ -288,6 +288,7 @@ Request.prototype.books = function (books, snapshot) {
websocket_ssl websocket_ssl
trace trace
maxListeners maxListeners
fee_cushion : Extra fee multiplier to account for async fee changes.
Events: Events:
'connect' 'connect'
@@ -319,6 +320,8 @@ function Remote(opts, trace) {
this.local_fee = opts.local_fee; // Locally set fees this.local_fee = opts.local_fee; // Locally set fees
this.local_signing = (typeof opts.local_signing === 'undefined') this.local_signing = (typeof opts.local_signing === 'undefined')
? true : opts.local_signing; ? true : opts.local_signing;
this.fee_cushion = (typeof opts.fee_cushion === 'undefined')
? 1.05 : opts.fee_cushion;
this.id = 0; this.id = 0;
this.trace = opts.trace || trace; this.trace = opts.trace || trace;
@@ -1434,6 +1437,37 @@ Remote.prototype.transaction = function () {
return new Transaction(this); return new Transaction(this);
}; };
/**
* Get the current recommended transaction fee unit.
*
* Multiply this value with the number of fee units in order to calculate the
* recommended fee for the transaction you are trying to submit.
*
* @return {Number} Recommended amount for one fee unit.
*/
Remote.prototype.fee_tx = function ()
{
var fee_unit = this._fee_base / this._fee_ref;
// Apply load fees
fee_unit *= this._load_factor / this._load_base;
// Apply fee cushion (a safety margin in case fees rise since we were last updated
fee_unit *= this.fee_cushion;
return fee_unit;
};
/**
* Get the current recommended reserve base.
*
* Returns the base reserve with load fees and safety margin applied.
*/
Remote.prototype.fee_reserve_base = function ()
{
// XXX
};
exports.Remote = Remote; exports.Remote = Remote;
// vim:sw=2:sts=2:ts=8:et // vim:sw=2:sts=2:ts=8:et

View File

@@ -109,9 +109,7 @@ util.inherits(Transaction, EventEmitter);
// XXX This needs to be determined from the network. // XXX This needs to be determined from the network.
Transaction.fees = { Transaction.fees = {
'default' : Amount.from_json('10'), 'default' : 10,
'nickname_create' : Amount.from_json('1000'),
'offer' : Amount.from_json('10'),
}; };
Transaction.flags = { Transaction.flags = {
@@ -197,11 +195,11 @@ Transaction.prototype.set_state = function (state) {
Transaction.prototype.complete = function () { Transaction.prototype.complete = function () {
var tx_json = this.tx_json; var tx_json = this.tx_json;
if (tx_json.Fee === undefined && this.remote.local_fee) { if ("undefined" === typeof tx_json.Fee && this.remote.local_fee) {
tx_json.Fee = Transaction.fees['default'].to_json(); this.tx_json.Fee = "" + Math.ceil(this.remote.fee_tx() * this.fee_units());
} }
if (tx_json.SigningPubKey === undefined && (!this.remote || this.remote.local_signing)) { if ("undefined" === typeof tx_json.SigningPubKey && (!this.remote || this.remote.local_signing)) {
var seed = Seed.from_json(this._secret); var seed = Seed.from_json(this._secret);
var key = seed.get_key(this.tx_json.Account); var key = seed.get_key(this.tx_json.Account);
tx_json.SigningPubKey = key.to_hex_pub(); tx_json.SigningPubKey = key.to_hex_pub();
@@ -257,8 +255,8 @@ Transaction.prototype.submit = function (callback) {
var self = this; var self = this;
var tx_json = this.tx_json; var tx_json = this.tx_json;
this.callback = typeof callback === 'function' this.callback = typeof callback === 'function'
? callback ? callback
: function(){}; : function(){};
function finish(err) { function finish(err) {
@@ -616,10 +614,6 @@ Transaction.prototype.offer_create = function (src, taker_pays, taker_gets, expi
this.tx_json.TakerPays = Amount.json_rewrite(taker_pays); this.tx_json.TakerPays = Amount.json_rewrite(taker_pays);
this.tx_json.TakerGets = Amount.json_rewrite(taker_gets); this.tx_json.TakerGets = Amount.json_rewrite(taker_gets);
if (this.remote.local_fee) {
this.tx_json.Fee = Transaction.fees.offer.to_json();
}
if (expiration) { if (expiration) {
this.tx_json.Expiration = expiration instanceof Date this.tx_json.Expiration = expiration instanceof Date
? expiration.getTime() ? expiration.getTime()
@@ -710,6 +704,20 @@ Transaction.prototype.wallet_add = function (src, amount, authorized_key, public
return this; return this;
}; };
/**
* Returns the number of fee units this transaction will cost.
*
* Each Ripple transaction based on its type and makeup costs a certain number
* of fee units. The fee units are calculated on a per-server basis based on the
* current load on both the network and the server.
*
* @see https://ripple.com/wiki/Transaction_Fee
*/
Transaction.prototype.fee_units = function ()
{
return Transaction.fees["default"];
};
exports.Transaction = Transaction; exports.Transaction = Transaction;
// vim:sw=2:sts=2:ts=8:et // vim:sw=2:sts=2:ts=8:et