mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-02 08:25:55 +00:00
Revise remote.js and unit tests to use new amount.js.
This commit is contained in:
90
js/remote.js
90
js/remote.js
@@ -7,10 +7,15 @@
|
|||||||
// YYY A better model might be to allow requesting a target state: keep connected or not.
|
// 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 WebSocket = require('ws');
|
||||||
|
|
||||||
|
var amount = require('./amount.js');
|
||||||
|
var Amount = amount.Amount;
|
||||||
|
|
||||||
// --> trusted: truthy, if remote is trusted
|
// --> trusted: truthy, if remote is trusted
|
||||||
var Remote = function (trusted, websocket_ip, websocket_port, config, trace) {
|
var Remote = function (trusted, websocket_ip, websocket_port, config, trace) {
|
||||||
this.trusted = trusted;
|
this.trusted = trusted;
|
||||||
@@ -57,10 +62,10 @@ var flags = {
|
|||||||
|
|
||||||
// XXX This needs to be determined from the network.
|
// XXX This needs to be determined from the network.
|
||||||
var fees = {
|
var fees = {
|
||||||
'default' : 100,
|
'default' : Amount.from_json("100"),
|
||||||
'account_create' : 1000,
|
'account_create' : Amount.from_json("1000"),
|
||||||
'nickname_create' : 1000,
|
'nickname_create' : Amount.from_json("1000"),
|
||||||
'offer' : 100,
|
'offer' : Amount.from_json("100"),
|
||||||
};
|
};
|
||||||
|
|
||||||
Remote.prototype.connect_helper = function () {
|
Remote.prototype.connect_helper = function () {
|
||||||
@@ -383,40 +388,79 @@ Remote.prototype.dirty_account_root = function (account) {
|
|||||||
// Transactions
|
// 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 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 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(
|
this.submit_seq(
|
||||||
{
|
{
|
||||||
'transaction' : {
|
'transaction' : transaction,
|
||||||
'TransactionType' : 'CreditSet',
|
|
||||||
'Account' : src_account,
|
|
||||||
'Destination' : dst_account,
|
|
||||||
'Fee' : create ? fees.account_create : fees['default'],
|
|
||||||
'Amount' : amount,
|
|
||||||
},
|
|
||||||
'secret' : secret,
|
'secret' : secret,
|
||||||
}, function () {
|
}, function () {
|
||||||
}, onDone);
|
}, 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 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 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 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(
|
this.submit_seq(
|
||||||
{
|
{
|
||||||
'transaction' : {
|
'transaction' : transaction,
|
||||||
'TransactionType' : 'Payment',
|
|
||||||
'Account' : src_account,
|
|
||||||
'Destination' : dst_account,
|
|
||||||
'Fee' : create ? fees.account_create : fees['default'],
|
|
||||||
'Flags' : create ? flags.tfCreateAccount : 0,
|
|
||||||
'Amount' : amount,
|
|
||||||
},
|
|
||||||
'secret' : secret,
|
'secret' : secret,
|
||||||
}, function () {
|
}, function () {
|
||||||
}, onDone);
|
}, onDone);
|
||||||
|
|||||||
@@ -2,8 +2,11 @@ var buster = require("buster");
|
|||||||
|
|
||||||
var config = require("./config.js");
|
var config = require("./config.js");
|
||||||
var server = require("./server.js");
|
var server = require("./server.js");
|
||||||
|
var amount = require("../js/amount.js");
|
||||||
var remote = require("../js/remote.js");
|
var remote = require("../js/remote.js");
|
||||||
|
|
||||||
|
var Amount = amount.Amount;
|
||||||
|
|
||||||
// How long to wait for server to start.
|
// How long to wait for server to start.
|
||||||
var serverDelay = 1500;
|
var serverDelay = 1500;
|
||||||
|
|
||||||
@@ -113,8 +116,8 @@ buster.testCase("Websocket commands", {
|
|||||||
|
|
||||||
alpha.request_ledger_entry({
|
alpha.request_ledger_entry({
|
||||||
'ledger_closed' : r.ledger_closed,
|
'ledger_closed' : r.ledger_closed,
|
||||||
'type' : 'account_root',
|
'type' : 'account_root',
|
||||||
'account_root' : 'iHb9CJAWyB4ij91VRWn96DkukG4bwdtyTh'
|
'account_root' : 'iHb9CJAWyB4ij91VRWn96DkukG4bwdtyTh'
|
||||||
} , function (r) {
|
} , function (r) {
|
||||||
// console.log("account_root: %s", JSON.stringify(r));
|
// console.log("account_root: %s", JSON.stringify(r));
|
||||||
|
|
||||||
@@ -133,8 +136,8 @@ buster.testCase("Websocket commands", {
|
|||||||
|
|
||||||
alpha.request_ledger_entry({
|
alpha.request_ledger_entry({
|
||||||
'ledger_closed' : r.ledger_closed,
|
'ledger_closed' : r.ledger_closed,
|
||||||
'type' : 'account_root',
|
'type' : 'account_root',
|
||||||
'account_root' : 'foobar'
|
'account_root' : 'foobar'
|
||||||
} , function (r) {
|
} , function (r) {
|
||||||
// console.log("account_root: %s", JSON.stringify(r));
|
// console.log("account_root: %s", JSON.stringify(r));
|
||||||
|
|
||||||
@@ -153,8 +156,8 @@ buster.testCase("Websocket commands", {
|
|||||||
|
|
||||||
alpha.request_ledger_entry({
|
alpha.request_ledger_entry({
|
||||||
'ledger_closed' : r.ledger_closed,
|
'ledger_closed' : r.ledger_closed,
|
||||||
'type' : 'account_root',
|
'type' : 'account_root',
|
||||||
'account_root' : 'iG1QQv2nh2gi7RCZ1P8YYcBUKCCN633jCn'
|
'account_root' : 'iG1QQv2nh2gi7RCZ1P8YYcBUKCCN633jCn'
|
||||||
}, function (r) {
|
}, function (r) {
|
||||||
console.log("account_root: %s", JSON.stringify(r));
|
console.log("account_root: %s", JSON.stringify(r));
|
||||||
|
|
||||||
@@ -173,8 +176,8 @@ buster.testCase("Websocket commands", {
|
|||||||
|
|
||||||
alpha.request_ledger_entry({
|
alpha.request_ledger_entry({
|
||||||
'ledger_closed' : r.ledger_closed,
|
'ledger_closed' : r.ledger_closed,
|
||||||
'type' : 'account_root',
|
'type' : 'account_root',
|
||||||
'index' : "2B6AC232AA4C4BE41BF49D2459FA4A0347E1B543A4C92FCEE0821C0201E2E9A8",
|
'index' : "2B6AC232AA4C4BE41BF49D2459FA4A0347E1B543A4C92FCEE0821C0201E2E9A8",
|
||||||
} , function (r) {
|
} , function (r) {
|
||||||
console.log("node: %s", JSON.stringify(r));
|
console.log("node: %s", JSON.stringify(r));
|
||||||
|
|
||||||
@@ -186,7 +189,7 @@ buster.testCase("Websocket commands", {
|
|||||||
|
|
||||||
'create account' :
|
'create account' :
|
||||||
function (done) {
|
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);
|
console.log(r);
|
||||||
|
|
||||||
buster.refute(r.error);
|
buster.refute(r.error);
|
||||||
|
|||||||
Reference in New Issue
Block a user