Revise remote.js and unit tests to use new amount.js.

This commit is contained in:
Arthur Britto
2012-10-11 21:41:07 -07:00
parent f419dcc77b
commit bb83bcf0d1
2 changed files with 79 additions and 32 deletions

View File

@@ -7,10 +7,15 @@
// YYY A better model might be to allow requesting a target state: keep connected or not.
//
var util = require('util');
// Node
var util = require('util');
// npm
var WebSocket = require('ws');
var amount = require('./amount.js');
var Amount = amount.Amount;
// --> trusted: truthy, if remote is trusted
var Remote = function (trusted, websocket_ip, websocket_port, config, trace) {
this.trusted = trusted;
@@ -57,10 +62,10 @@ var flags = {
// XXX This needs to be determined from the network.
var fees = {
'default' : 100,
'account_create' : 1000,
'nickname_create' : 1000,
'offer' : 100,
'default' : Amount.from_json("100"),
'account_create' : Amount.from_json("1000"),
'nickname_create' : Amount.from_json("1000"),
'offer' : Amount.from_json("100"),
};
Remote.prototype.connect_helper = function () {
@@ -383,40 +388,79 @@ Remote.prototype.dirty_account_root = function (account) {
// Transactions
//
Remote.prototype.ripple_line_set = function (secret, src, dst, amount, onDone) {
Remote.prototype.offer_create = function (secret, src, taker_pays, taker_gets, expiration, onDone) {
var secret = this.config.accounts[src] ? this.config.accounts[src].secret : secret;
var src_account = this.config.accounts[src] ? this.config.accounts[src].account : src;
var dst_account = this.config.accounts[dst] ? this.config.accounts[dst].account : dst;
var transaction = {
'TransactionType' : 'OfferCreate',
'Account' : src_account,
'Fee' : fees.offer.to_json(),
'TakerPays' : taker_pays.to_json(),
'TakerGets' : taker_gets.to_json(),
};
if (expiration)
transaction.Expiration = expiration;
this.submit_seq(
{
'transaction' : {
'TransactionType' : 'CreditSet',
'Account' : src_account,
'Destination' : dst_account,
'Fee' : create ? fees.account_create : fees['default'],
'Amount' : amount,
},
'transaction' : transaction,
'secret' : secret,
}, function () {
}, onDone);
};
Remote.prototype.send_xns = function (secret, src, dst, amount, create, onDone) {
Remote.prototype.ripple_line_set = function (secret, src, limit, quaility_in, quality_out, onDone) {
var secret = this.config.accounts[src] ? this.config.accounts[src].secret : secret;
var src_account = this.config.accounts[src] ? this.config.accounts[src].account : src;
var transaction = {
'TransactionType' : 'CreditSet',
'Account' : src_account,
'Fee' : fees['default'].to_json(),
};
if (limit)
transaction.LimitAmount = limit.to_json();
if (quaility_in)
transaction.QualityIn = quaility_in;
if (quaility_out)
transaction.QualityOut = quaility_out;
this.submit_seq(
{
'transaction' : transaction,
'secret' : secret,
}, function () {
}, onDone);
};
// --> create: is only valid if destination gets XNS.
Remote.prototype.send = function (secret, src, dst, deliver_amount, send_max, create, onDone) {
var secret = this.config.accounts[src] ? this.config.accounts[src].secret : secret;
var src_account = this.config.accounts[src] ? this.config.accounts[src].account : src;
var dst_account = this.config.accounts[dst] ? this.config.accounts[dst].account : dst;
var transaction = {
'TransactionType' : 'Payment',
'Account' : src_account,
'Fee' : (create ? fees.account_create : fees['default']).to_json(),
'Destination' : dst_account,
'Amount' : deliver_amount.to_json(),
};
if (create)
transaction.Flags = flags.tfCreateAccount;
if (send_max)
transaction.SendMax = send_max.to_json();
this.submit_seq(
{
'transaction' : {
'TransactionType' : 'Payment',
'Account' : src_account,
'Destination' : dst_account,
'Fee' : create ? fees.account_create : fees['default'],
'Flags' : create ? flags.tfCreateAccount : 0,
'Amount' : amount,
},
'transaction' : transaction,
'secret' : secret,
}, function () {
}, onDone);

View File

@@ -2,8 +2,11 @@ var buster = require("buster");
var config = require("./config.js");
var server = require("./server.js");
var amount = require("../js/amount.js");
var remote = require("../js/remote.js");
var Amount = amount.Amount;
// How long to wait for server to start.
var serverDelay = 1500;
@@ -113,8 +116,8 @@ buster.testCase("Websocket commands", {
alpha.request_ledger_entry({
'ledger_closed' : r.ledger_closed,
'type' : 'account_root',
'account_root' : 'iHb9CJAWyB4ij91VRWn96DkukG4bwdtyTh'
'type' : 'account_root',
'account_root' : 'iHb9CJAWyB4ij91VRWn96DkukG4bwdtyTh'
} , function (r) {
// console.log("account_root: %s", JSON.stringify(r));
@@ -133,8 +136,8 @@ buster.testCase("Websocket commands", {
alpha.request_ledger_entry({
'ledger_closed' : r.ledger_closed,
'type' : 'account_root',
'account_root' : 'foobar'
'type' : 'account_root',
'account_root' : 'foobar'
} , function (r) {
// console.log("account_root: %s", JSON.stringify(r));
@@ -153,8 +156,8 @@ buster.testCase("Websocket commands", {
alpha.request_ledger_entry({
'ledger_closed' : r.ledger_closed,
'type' : 'account_root',
'account_root' : 'iG1QQv2nh2gi7RCZ1P8YYcBUKCCN633jCn'
'type' : 'account_root',
'account_root' : 'iG1QQv2nh2gi7RCZ1P8YYcBUKCCN633jCn'
}, function (r) {
console.log("account_root: %s", JSON.stringify(r));
@@ -173,8 +176,8 @@ buster.testCase("Websocket commands", {
alpha.request_ledger_entry({
'ledger_closed' : r.ledger_closed,
'type' : 'account_root',
'index' : "2B6AC232AA4C4BE41BF49D2459FA4A0347E1B543A4C92FCEE0821C0201E2E9A8",
'type' : 'account_root',
'index' : "2B6AC232AA4C4BE41BF49D2459FA4A0347E1B543A4C92FCEE0821C0201E2E9A8",
} , function (r) {
console.log("node: %s", JSON.stringify(r));
@@ -186,7 +189,7 @@ buster.testCase("Websocket commands", {
'create account' :
function (done) {
alpha.send_xns(undefined, 'root', 'alice', 10000, true, function (r) {
alpha.send(undefined, 'root', 'alice', Amount.from_json("10000"), undefined, 'CREATE', function (r) {
console.log(r);
buster.refute(r.error);