var async = require("async"); var buster = require("buster"); var Amount = require("ripple-lib").Amount; var Remote = require("ripple-lib").Remote; var Transaction = require("ripple-lib").Transaction; var Server = require("./server").Server; var testutils = require("./testutils"); var config = testutils.init_config(); buster.testRunner.timeout = 5000; buster.testCase("Offer tests", { 'setUp' : testutils.build_setup(), // 'setUp' : testutils.build_setup({ verbose: true }), // 'setUp' : testutils.build_setup({ verbose: true, standalone: true }), 'tearDown' : testutils.build_teardown(), "offer create then cancel in one ledger" : function (done) { var self = this; var final_create; async.waterfall([ function (callback) { self.remote.transaction() .offer_create("root", "500", "100/USD/root") .on('proposed', function (m) { // console.log("PROPOSED: offer_create: %s", JSON.stringify(m)); callback(m.result !== 'tesSUCCESS', m); }) .on('final', function (m) { // console.log("FINAL: offer_create: %s", JSON.stringify(m)); buster.assert.equals('tesSUCCESS', m.metadata.TransactionResult); buster.assert(final_create); }) .submit(); }, function (m, callback) { self.remote.transaction() .offer_cancel("root", m.tx_json.Sequence) .on('proposed', function (m) { // console.log("PROPOSED: offer_cancel: %s", JSON.stringify(m)); callback(m.result !== 'tesSUCCESS', m); }) .on('final', function (m) { // console.log("FINAL: offer_cancel: %s", JSON.stringify(m, undefined, 2)); buster.assert.equals('tesSUCCESS', m.metadata.TransactionResult); buster.assert(final_create); done(); }) .submit(); }, function (m, callback) { self.remote .once('ledger_closed', function (message) { // console.log("LEDGER_CLOSED: %d: %s", ledger_index, ledger_hash); final_create = message; }) .ledger_accept(); } ], function (error) { // console.log("result: error=%s", error); buster.refute(error, self.what || "Unspecifide Error"); done(); }); }, "offer create then offer create with cancel in one ledger" : function (done) { var self = this; var final_create; var sequence_first; var sequence_second; var dones = 0; async.waterfall([ function (callback) { self.remote.transaction() .offer_create("root", "500", "100/USD/root") .on('proposed', function (m) { // console.log("PROPOSED: offer_create: %s", JSON.stringify(m)); callback(m.result !== 'tesSUCCESS', m); }) .on('final', function (m) { // console.log("FINAL: offer_create: %s", JSON.stringify(m)); buster.assert.equals('tesSUCCESS', m.metadata.TransactionResult); buster.assert(final_create); if (3 === ++dones) done(); }) .submit(); }, function (m, callback) { sequence_first = m.tx_json.Sequence; // Test canceling existant offer. self.remote.transaction() .offer_create("root", "300", "100/USD/root", undefined, sequence_first) .on('proposed', function (m) { // console.log("PROPOSED: offer_create: %s", JSON.stringify(m)); callback(m.result !== 'tesSUCCESS', m); }) .on('final', function (m) { // console.log("FINAL: offer_create: %s", JSON.stringify(m)); buster.assert.equals('tesSUCCESS', m.metadata.TransactionResult); buster.assert(final_create); if (3 === ++dones) done(); }) .submit(); }, function (m, callback) { sequence_second = m.tx_json.Sequence; self.what = "Verify offer canceled."; testutils.verify_offer_not_found(self.remote, "root", sequence_first, callback); }, function (callback) { self.what = "Verify offer replaced."; testutils.verify_offer(self.remote, "root", sequence_second, "300", "100/USD/root", callback); }, function (callback) { // Test canceling non-existant offer. self.remote.transaction() .offer_create("root", "400", "200/USD/root", undefined, sequence_first) .on('proposed', function (m) { // console.log("PROPOSED: offer_create: %s", JSON.stringify(m)); callback(m.result !== 'tesSUCCESS', m); }) .on('final', function (m) { // console.log("FINAL: offer_create: %s", JSON.stringify(m)); buster.assert.equals('tesSUCCESS', m.metadata.TransactionResult); buster.assert(final_create); if (3 === ++dones) done(); }) .submit(); }, function (callback) { self.remote .once('ledger_closed', function (message) { // console.log("LEDGER_CLOSED: %d: %s", ledger_index, ledger_hash); final_create = message; }) .ledger_accept(); } ], function (error) { // console.log("result: error=%s", error); buster.refute(error, self.what); done(); }); }, "Offer create then self crossing offer, no trust lines with self" : function (done) { var self = this; async.waterfall([ function (callback) { self.what = "Create first offer."; self.remote.transaction() .offer_create("root", "500/BTC/root", "100/USD/root") .on('proposed', function (m) { // console.log("PROPOSED: offer_create: %s", JSON.stringify(m)); callback(m.result !== 'tesSUCCESS'); }) .submit(); }, function (callback) { self.what = "Create crossing offer."; self.remote.transaction() .offer_create("root", "100/USD/root", "500/BTC/root") .on('proposed', function (m) { // console.log("PROPOSED: offer_create: %s", JSON.stringify(m)); callback(m.result !== 'tesSUCCESS'); }) .submit(); } ], function (error) { // console.log("result: error=%s", error); buster.refute(error, self.what); done(); }); }, "Offer create then crossing offer with XRP. Negative balance." : function (done) { var self = this; var alices_initial_balance = 499946999680; var bobs_initial_balance = 10199999920; async.waterfall([ function (callback) { self.what = "Create mtgox account."; testutils.payment(self.remote, "root", "mtgox", 1149999730, callback); }, function (callback) { self.what = "Create alice account."; testutils.payment(self.remote, "root", "alice", alices_initial_balance, callback); }, function (callback) { self.what = "Create bob account."; testutils.payment(self.remote, "root", "bob", bobs_initial_balance, callback); }, function (callback) { self.what = "Set transfer rate."; self.remote.transaction() .account_set("mtgox") .transfer_rate(1005000000) .once('proposed', function (m) { // console.log("proposed: %s", JSON.stringify(m)); callback(m.result !== 'tesSUCCESS'); }) .submit(); }, function (callback) { self.what = "Set limits."; testutils.credit_limits(self.remote, { "alice" : "500/USD/mtgox", "bob" : "50/USD/mtgox", "mtgox" : "100/USD/alice", }, callback); }, function (callback) { self.what = "Distribute funds."; testutils.payments(self.remote, { "mtgox" : [ "50/USD/alice", "2710505431213761e-33/USD/bob" ] }, callback); }, function (callback) { self.what = "Create first offer."; self.remote.transaction() .offer_create("alice", "50/USD/mtgox", "150000.0") // get 50/USD pay 150000/XRP .on('proposed', function (m) { // console.log("PROPOSED: offer_create: %s", JSON.stringify(m)); callback(m.result !== 'tesSUCCESS'); }) .submit(); }, function (callback) { self.what = "Unfund offer."; testutils.payments(self.remote, { "alice" : "100/USD/mtgox" }, callback); }, function (callback) { self.what = "Set limits 2."; testutils.credit_limits(self.remote, { "mtgox" : "0/USD/alice", }, callback); }, function (callback) { self.what = "Verify balances. 1"; testutils.verify_balances(self.remote, { "alice" : [ "-50/USD/mtgox" ], "bob" : [ "2710505431213761e-33/USD/mtgox" ], }, callback); }, function (callback) { self.what = "Create crossing offer."; self.remote.transaction() .offer_create("bob", "2000.0", "1/USD/mtgox") // get 2,000/XRP pay 1/USD (has insufficient USD) .on('proposed', function (m) { // console.log("PROPOSED: offer_create: %s", JSON.stringify(m)); callback(m.result !== 'tesSUCCESS'); }) .submit(); }, function (callback) { self.what = "Verify balances. 2"; var alices_fees, alices_num_transactions, alices_tx_fee_units_total, alices_tx_fee_units_total, alices_final_balance, bobs_fees, bobs_num_transactions, bobs_tx_fee_units_total, bobs_tx_fee_units_total, bobs_final_balance; alices_num_transactions = 3; alices_tx_fee_units_total = alices_num_transactions * Transaction.fee_units["default"] alices_tx_fees_total = self.remote.fee_tx(alices_tx_fee_units_total); alices_final_balance = Amount.from_json(alices_initial_balance) .subtract(alices_tx_fees_total); bobs_num_transactions = 2; bobs_tx_fee_units_total = bobs_num_transactions * Transaction.fee_units["default"] bobs_tx_fees_total = self.remote.fee_tx(bobs_tx_fee_units_total); bobs_final_balance = Amount.from_json(bobs_initial_balance) .subtract(bobs_tx_fees_total); testutils.verify_balances(self.remote, { "alice" : [ "-50/USD/mtgox", alices_final_balance.to_json()], "bob" : [ "2710505431213761e-33/USD/mtgox", bobs_final_balance.to_json() // bobs_final_balance.to_json() // String(10199999920-(self.remote.fee_tx(2*(Transaction.fee_units['default'])))).to_number() ], }, callback); }, // function (callback) { // self.what = "Display ledger"; // // self.remote.request_ledger('current', true) // .on('success', function (m) { // console.log("Ledger: %s", JSON.stringify(m, undefined, 2)); // // callback(); // }) // .request(); // }, ], function (error) { // console.log("result: error=%s", error); buster.refute(error, self.what); done(); }); }, "Offer create then crossing offer with XRP. Reverse order." : function (done) { var self = this; async.waterfall([ function (callback) { self.what = "Create accounts."; testutils.create_accounts(self.remote, "root", "100000.0", ["alice", "bob", "mtgox"], callback); }, function (callback) { self.what = "Set limits."; testutils.credit_limits(self.remote, { "alice" : "1000/USD/mtgox", "bob" : "1000/USD/mtgox" }, callback); }, function (callback) { self.what = "Distribute funds."; testutils.payments(self.remote, { "mtgox" : "500/USD/alice" }, callback); }, function (callback) { self.what = "Create first offer."; self.remote.transaction() .offer_create("bob", "1/USD/mtgox", "4000.0") // get 1/USD pay 4000/XRP : offer pays 4000 XRP for 1 USD .on('proposed', function (m) { // console.log("PROPOSED: offer_create: %s", JSON.stringify(m)); callback(m.result !== 'tesSUCCESS'); }) .submit(); }, function (callback) { self.what = "Create crossing offer."; // Existing offer pays better than this wants. // Fully consume existing offer. // Pay 1 USD, get 4000 XRP. self.remote.transaction() .offer_create("alice", "150000.0", "50/USD/mtgox") // get 150,000/XRP pay 50/USD : offer pays 1 USD for 3000 XRP .on('proposed', function (m) { // console.log("PROPOSED: offer_create: %s", JSON.stringify(m)); callback(m.result !== 'tesSUCCESS'); }) .submit(); }, function (callback) { self.what = "Verify balances."; testutils.verify_balances(self.remote, { // "bob" : [ "1/USD/mtgox", String(100000000000-4000000000-(Number(self.remote.fee_tx(Transaction.fee_units['default'] * 2).to_json()))) ], "bob" : [ "1/USD/mtgox", String(100000000000-4000000000-(self.remote.fee_tx(Transaction.fee_units['default'] * 2).to_number())) ], "alice" : [ "499/USD/mtgox", String(100000000000+4000000000-(self.remote.fee_tx(Transaction.fee_units['default'] * 2).to_number())) ], }, callback); }, // function (callback) { // self.what = "Display ledger"; // // self.remote.request_ledger('current', true) // .on('success', function (m) { // console.log("Ledger: %s", JSON.stringify(m, undefined, 2)); // // callback(); // }) // .request(); // }, ], function (error) { // console.log("result: error=%s", error); buster.refute(error, self.what); done(); }); }, "Offer create then crossing offer with XRP." : function (done) { var self = this; async.waterfall([ function (callback) { self.what = "Create accounts."; testutils.create_accounts(self.remote, "root", "100000.0", ["alice", "bob", "mtgox"], callback); }, function (callback) { self.what = "Set limits."; testutils.credit_limits(self.remote, { "alice" : "1000/USD/mtgox", "bob" : "1000/USD/mtgox" }, callback); }, function (callback) { self.what = "Distribute funds."; testutils.payments(self.remote, { "mtgox" : "500/USD/alice" }, callback); }, function (callback) { self.what = "Create first offer."; self.remote.transaction() .offer_create("alice", "150000.0", "50/USD/mtgox") // pays 1 USD for 3000 XRP .on('proposed', function (m) { // console.log("PROPOSED: offer_create: %s", JSON.stringify(m)); callback(m.result !== 'tesSUCCESS'); }) .submit(); }, function (callback) { self.what = "Create crossing offer."; self.remote.transaction() .offer_create("bob", "1/USD/mtgox", "4000.0") // pays 4000 XRP for 1 USD .on('proposed', function (m) { // console.log("PROPOSED: offer_create: %s", JSON.stringify(m)); callback(m.result !== 'tesSUCCESS'); }) .submit(); }, function (callback) { self.what = "Verify balances."; // New offer pays better than old wants. // Fully consume new offer. // Pay 1 USD, get 3000 XRP. testutils.verify_balances(self.remote, { "alice" : [ "499/USD/mtgox", String(100000000000+3000000000-(self.remote.fee_tx(2*(Transaction.fee_units['default'])).to_number())) ], "bob" : [ "1/USD/mtgox", String(100000000000-3000000000-(self.remote.fee_tx(2*(Transaction.fee_units['default'])).to_number())) ], }, callback); }, // function (callback) { // self.what = "Display ledger"; // // self.remote.request_ledger('current', true) // .on('success', function (m) { // console.log("Ledger: %s", JSON.stringify(m, undefined, 2)); // // callback(); // }) // .request(); // }, ], function (error) { // console.log("result: error=%s", error); buster.refute(error, self.what); done(); }); }, "Offer create then crossing offer with XRP with limit override." : function (done) { var self = this; async.waterfall([ function (callback) { self.what = "Create accounts."; testutils.create_accounts(self.remote, "root", "100000.0", ["alice", "bob", "mtgox"], callback); }, function (callback) { self.what = "Set limits."; testutils.credit_limits(self.remote, { "alice" : "1000/USD/mtgox", // "bob" : "1000/USD/mtgox" }, callback); }, function (callback) { self.what = "Distribute funds."; testutils.payments(self.remote, { "mtgox" : "500/USD/alice" }, callback); }, function (callback) { self.what = "Create first offer."; self.remote.transaction() .offer_create("alice", "150000.0", "50/USD/mtgox") // 300 XRP = 1 USD .on('proposed', function (m) { // console.log("PROPOSED: offer_create: %s", JSON.stringify(m)); callback(m.result !== 'tesSUCCESS'); }) .submit(); }, // function (callback) { // self.what = "Display ledger"; // // self.remote.request_ledger('current', true) // .on('success', function (m) { // console.log("Ledger: %s", JSON.stringify(m, undefined, 2)); // // callback(); // }) // .request(); // }, function (callback) { self.what = "Create crossing offer."; self.remote.transaction() .offer_create("bob", "1/USD/mtgox", "3000.0") // .on('proposed', function (m) { // console.log("PROPOSED: offer_create: %s", JSON.stringify(m)); callback(m.result !== 'tesSUCCESS'); }) .submit(); }, function (callback) { self.what = "Verify balances."; testutils.verify_balances(self.remote, { "alice" : [ "499/USD/mtgox", String(100000000000+3000000000-(self.remote.fee_tx(2*(Transaction.fee_units['default'])).to_number())) ], "bob" : [ "1/USD/mtgox", String(100000000000-3000000000-(self.remote.fee_tx(1*(Transaction.fee_units['default'])).to_number())) ], }, callback); }, // function (callback) { // self.what = "Display ledger"; // // self.remote.request_ledger('current', true) // .on('success', function (m) { // console.log("Ledger: %s", JSON.stringify(m, undefined, 2)); // // callback(); // }) // .request(); // }, ], function (error) { // console.log("result: error=%s", error); buster.refute(error, self.what); done(); }); }, "offer_create then ledger_accept then offer_cancel then ledger_accept." : function (done) { var self = this; var final_create; var offer_seq; async.waterfall([ function (callback) { self.remote.transaction() .offer_create("root", "500", "100/USD/root") .on('proposed', function (m) { // console.log("PROPOSED: offer_create: %s", JSON.stringify(m)); offer_seq = m.tx_json.Sequence; callback(m.result !== 'tesSUCCESS'); }) .on('final', function (m) { // console.log("FINAL: offer_create: %s", JSON.stringify(m)); buster.assert.equals('tesSUCCESS', m.metadata.TransactionResult); final_create = m; callback(); }) .submit(); }, function (callback) { if (!final_create) { self.remote .once('ledger_closed', function (mesage) { // console.log("LEDGER_CLOSED: %d: %s", ledger_index, ledger_hash); }) .ledger_accept(); } else { callback(); } }, function (callback) { // console.log("CANCEL: offer_cancel: %d", offer_seq); self.remote.transaction() .offer_cancel("root", offer_seq) .on('proposed', function (m) { // console.log("PROPOSED: offer_cancel: %s", JSON.stringify(m)); callback(m.result !== 'tesSUCCESS'); }) .on('final', function (m) { // console.log("FINAL: offer_cancel: %s", JSON.stringify(m)); buster.assert.equals('tesSUCCESS', m.metadata.TransactionResult); buster.assert(final_create); done(); }) .submit(); }, // See if ledger_accept will crash. function (callback) { self.remote .once('ledger_closed', function (mesage) { // console.log("LEDGER_CLOSED: A: %d: %s", ledger_index, ledger_hash); callback(); }) .ledger_accept(); }, function (callback) { self.remote .once('ledger_closed', function (mesage) { // console.log("LEDGER_CLOSED: B: %d: %s", ledger_index, ledger_hash); callback(); }) .ledger_accept(); }, ], function (error) { // console.log("result: error=%s", error); buster.refute(error, self.what); if (error) done(); }); }, "//new user offer_create then ledger_accept then offer_cancel then ledger_accept." : function (done) { var self = this; var final_create; var offer_seq; async.waterfall([ function (callback) { self.remote.transaction() .payment('root', 'alice', "1000") .on('proposed', function (m) { // console.log("proposed: %s", JSON.stringify(m)); buster.assert.equals(m.result, 'tesSUCCESS'); callback(); }) .submit() }, function (callback) { self.remote.transaction() .offer_create("alice", "500", "100/USD/alice") .on('proposed', function (m) { // console.log("PROPOSED: offer_create: %s", JSON.stringify(m)); offer_seq = m.tx_json.Sequence; callback(m.result !== 'tesSUCCESS'); }) .on('final', function (m) { // console.log("FINAL: offer_create: %s", JSON.stringify(m)); buster.assert.equals('tesSUCCESS', m.metadata.TransactionResult); final_create = m; callback(); }) .submit(); }, function (callback) { if (!final_create) { self.remote .once('ledger_closed', function (mesage) { // console.log("LEDGER_CLOSED: %d: %s", ledger_index, ledger_hash); }) .ledger_accept(); } else { callback(); } }, function (callback) { // console.log("CANCEL: offer_cancel: %d", offer_seq); self.remote.transaction() .offer_cancel("alice", offer_seq) .on('proposed', function (m) { // console.log("PROPOSED: offer_cancel: %s", JSON.stringify(m)); callback(m.result !== 'tesSUCCESS'); }) .on('final', function (m) { // console.log("FINAL: offer_cancel: %s", JSON.stringify(m)); buster.assert.equals('tesSUCCESS', m.metadata.TransactionResult); buster.assert(final_create); done(); }) .submit(); }, // See if ledger_accept will crash. function (callback) { self.remote .once('ledger_closed', function (mesage) { // console.log("LEDGER_CLOSED: A: %d: %s", ledger_index, ledger_hash); callback(); }) .ledger_accept(); }, function (callback) { self.remote .once('ledger_closed', function (mesage) { // console.log("LEDGER_CLOSED: B: %d: %s", ledger_index, ledger_hash); callback(); }) .ledger_accept(); }, ], function (error) { // console.log("result: error=%s", error); buster.refute(error, self.what); if (error) done(); }); }, "offer cancel past and future sequence" : function (done) { var self = this; var final_create; async.waterfall([ function (callback) { self.remote.transaction() .payment('root', 'alice', Amount.from_json("10000.0")) .on('proposed', function (m) { // console.log("PROPOSED: CreateAccount: %s", JSON.stringify(m)); callback(m.result !== 'tesSUCCESS', m); }) .on('error', function(m) { // console.log("error: %s", m); buster.assert(false); callback(m); }) .submit(); }, // Past sequence but wrong function (m, callback) { self.remote.transaction() .offer_cancel("root", m.tx_json.Sequence) .on('proposed', function (m) { // console.log("PROPOSED: offer_cancel past: %s", JSON.stringify(m)); callback(m.result !== 'tesSUCCESS', m); }) .submit(); }, // Same sequence function (m, callback) { self.remote.transaction() .offer_cancel("root", m.tx_json.Sequence+1) .on('proposed', function (m) { // console.log("PROPOSED: offer_cancel same: %s", JSON.stringify(m)); callback(m.result !== 'temBAD_SEQUENCE', m); }) .submit(); }, // Future sequence function (m, callback) { self.remote.transaction() .offer_cancel("root", m.tx_json.Sequence+2) .on('proposed', function (m) { // console.log("ERROR: offer_cancel future: %s", JSON.stringify(m)); callback(m.result !== 'temBAD_SEQUENCE'); }) .submit(); }, // See if ledger_accept will crash. function (callback) { self.remote .once('ledger_closed', function (mesage) { // console.log("LEDGER_CLOSED: A: %d: %s", ledger_index, ledger_hash); callback(); }) .ledger_accept(); }, function (callback) { self.remote .once('ledger_closed', function (mesage) { // console.log("LEDGER_CLOSED: B: %d: %s", ledger_index, ledger_hash); callback(); }) .ledger_accept(); }, function (callback) { callback(); } ], function (error) { // console.log("result: error=%s", error); buster.refute(error, self.what); done(); }); }, "ripple currency conversion : entire offer" : // mtgox in, XRP out function (done) { var self = this; var seq; async.waterfall([ function (callback) { self.what = "Create accounts."; testutils.create_accounts(self.remote, "root", "10000.0", ["alice", "bob", "mtgox"], callback); }, function (callback) { self.what = "Owner count 0."; testutils.verify_owner_count(self.remote, "bob", 0, callback); }, function (callback) { self.what = "Set limits."; testutils.credit_limits(self.remote, { "alice" : "100/USD/mtgox", "bob" : "1000/USD/mtgox" }, callback); }, function (callback) { self.what = "Owner counts after trust."; testutils.verify_owner_counts(self.remote, { "alice" : 1, "bob" : 1, }, callback); }, function (callback) { self.what = "Distribute funds."; testutils.payments(self.remote, { "mtgox" : "100/USD/alice" }, callback); }, function (callback) { self.what = "Create offer."; self.remote.transaction() .offer_create("bob", "100/USD/mtgox", "500") .on('proposed', function (m) { // console.log("PROPOSED: offer_create: %s", JSON.stringify(m)); callback(m.result !== 'tesSUCCESS'); seq = m.tx_json.Sequence; }) .submit(); }, function (callback) { self.what = "Owner counts after offer create."; testutils.verify_owner_counts(self.remote, { "alice" : 1, "bob" : 2, }, callback); }, function (callback) { self.what = "Verify offer balance."; testutils.verify_offer(self.remote, "bob", seq, "100/USD/mtgox", "500", callback); }, function (callback) { self.what = "Alice converts USD to XRP."; self.remote.transaction() .payment("alice", "alice", "500") .send_max("100/USD/mtgox") .on('proposed', function (m) { // console.log("proposed: %s", JSON.stringify(m)); callback(m.result !== 'tesSUCCESS'); }) .submit(); }, function (callback) { self.what = "Verify balances."; testutils.verify_balances(self.remote, { "alice" : [ "0/USD/mtgox", String(10000000000+500-(self.remote.fee_tx(2*(Transaction.fee_units['default'])).to_number())) ], "bob" : "100/USD/mtgox", }, callback); }, function (callback) { self.what = "Verify offer consumed."; testutils.verify_offer_not_found(self.remote, "bob", seq, callback); }, function (callback) { self.what = "Owner counts after consumed."; testutils.verify_owner_counts(self.remote, { "alice" : 1, "bob" : 1, }, callback); }, ], function (error) { buster.refute(error, self.what); done(); }); }, "ripple currency conversion : offerer into debt" : // alice in, carol out function (done) { var self = this; var seq; async.waterfall([ function (callback) { self.what = "Create accounts."; testutils.create_accounts(self.remote, "root", "10000.0", ["alice", "bob", "carol"], callback); }, function (callback) { self.what = "Set limits."; testutils.credit_limits(self.remote, { "alice" : "2000/EUR/carol", "bob" : "100/USD/alice", "carol" : "1000/EUR/bob" }, callback); }, function (callback) { self.what = "Create offer to exchange."; self.remote.transaction() .offer_create("bob", "50/USD/alice", "200/EUR/carol") .on('proposed', function (m) { // console.log("PROPOSED: offer_create: %s", JSON.stringify(m)); callback(m.result !== 'tecUNFUNDED_OFFER'); seq = m.tx_json.Sequence; }) .submit(); }, // function (callback) { // self.what = "Alice converts USD to EUR via offer."; // // self.remote.transaction() // .offer_create("alice", "200/EUR/carol", "50/USD/alice") // .on('proposed', function (m) { // // console.log("PROPOSED: offer_create: %s", JSON.stringify(m)); // callback(m.result !== 'tesSUCCESS'); // // seq = m.tx_json.Sequence; // }) // .submit(); // }, // function (callback) { // self.what = "Verify balances."; // // testutils.verify_balances(self.remote, // { // "alice" : [ "-50/USD/bob", "200/EUR/carol" ], // "bob" : [ "50/USD/alice", "-200/EUR/carol" ], // "carol" : [ "-200/EUR/alice", "200/EUR/bob" ], // }, // callback); // }, // function (callback) { // self.what = "Verify offer consumed."; // // testutils.verify_offer_not_found(self.remote, "bob", seq, callback); // }, ], function (error) { buster.refute(error, self.what); done(); }); }, "ripple currency conversion : in parts" : function (done) { var self = this; var seq; async.waterfall([ function (callback) { self.what = "Create accounts."; testutils.create_accounts(self.remote, "root", "10000.0", ["alice", "bob", "mtgox"], callback); }, function (callback) { self.what = "Set limits."; testutils.credit_limits(self.remote, { "alice" : "200/USD/mtgox", "bob" : "1000/USD/mtgox" }, callback); }, function (callback) { self.what = "Distribute funds."; testutils.payments(self.remote, { "mtgox" : "200/USD/alice" }, callback); }, function (callback) { self.what = "Create offer."; self.remote.transaction() .offer_create("bob", "100/USD/mtgox", "500") .on('proposed', function (m) { // console.log("PROPOSED: offer_create: %s", JSON.stringify(m)); callback(m.result !== 'tesSUCCESS'); seq = m.tx_json.Sequence; }) .submit(); }, function (callback) { self.what = "Alice converts USD to XRP."; self.remote.transaction() .payment("alice", "alice", "200") .send_max("100/USD/mtgox") .on('proposed', function (m) { // console.log("proposed: %s", JSON.stringify(m)); callback(m.result !== 'tesSUCCESS'); }) .submit(); }, function (callback) { self.what = "Verify offer balance."; testutils.verify_offer(self.remote, "bob", seq, "60/USD/mtgox", "300", callback); }, function (callback) { self.what = "Verify balances."; testutils.verify_balances(self.remote, { "alice" : [ "160/USD/mtgox", String(10000000000+200-(self.remote.fee_tx(2*(Transaction.fee_units['default'])).to_number())) ], "bob" : "40/USD/mtgox", }, callback); }, function (callback) { self.what = "Alice converts USD to XRP should fail due to PartialPayment."; self.remote.transaction() .payment("alice", "alice", "600") .send_max("100/USD/mtgox") .on('proposed', function (m) { // console.log("proposed: %s", JSON.stringify(m)); callback(m.result !== 'tecPATH_PARTIAL'); }) .submit(); }, function (callback) { self.what = "Alice converts USD to XRP."; self.remote.transaction() .payment("alice", "alice", "600") .send_max("100/USD/mtgox") .set_flags('PartialPayment') .on('proposed', function (m) { // console.log("proposed: %s", JSON.stringify(m)); callback(m.result !== 'tesSUCCESS'); }) .submit(); }, function (callback) { self.what = "Verify offer consumed."; testutils.verify_offer_not_found(self.remote, "bob", seq, callback); }, function (callback) { self.what = "Verify balances."; testutils.verify_balances(self.remote, { "alice" : [ "100/USD/mtgox", String(10000000000+200+300-(self.remote.fee_tx(4*(Transaction.fee_units['default'])).to_number())) ], "bob" : "100/USD/mtgox", }, callback); }, ], function (error) { buster.refute(error, self.what); done(); }); }, }); buster.testCase("Offer cross currency", { 'setUp' : testutils.build_setup(), // 'setUp' : testutils.build_setup({ verbose: true }), 'tearDown' : testutils.build_teardown(), "ripple cross currency payment - start with XRP" : // alice --> [XRP --> carol --> USD/mtgox] --> bob function (done) { var self = this; var seq; // self.remote.set_trace(); async.waterfall([ function (callback) { self.what = "Create accounts."; testutils.create_accounts(self.remote, "root", "10000.0", ["alice", "bob", "carol", "mtgox"], callback); }, function (callback) { self.what = "Set limits."; testutils.credit_limits(self.remote, { "carol" : "1000/USD/mtgox", "bob" : "2000/USD/mtgox" }, callback); }, function (callback) { self.what = "Distribute funds."; testutils.payments(self.remote, { "mtgox" : "500/USD/carol" }, callback); }, function (callback) { self.what = "Create offer."; self.remote.transaction() .offer_create("carol", "500.0", "50/USD/mtgox") .on('proposed', function (m) { // console.log("PROPOSED: offer_create: %s", JSON.stringify(m)); callback(m.result !== 'tesSUCCESS'); seq = m.tx_json.Sequence; }) .submit(); }, function (callback) { self.what = "Alice send USD/mtgox converting from XRP."; self.remote.transaction() .payment("alice", "bob", "25/USD/mtgox") .send_max("333.0") .on('proposed', function (m) { // console.log("proposed: %s", JSON.stringify(m)); callback(m.result !== 'tesSUCCESS'); }) .submit(); }, function (callback) { self.what = "Verify balances."; testutils.verify_balances(self.remote, { // "alice" : [ "500" ], "bob" : "25/USD/mtgox", "carol" : "475/USD/mtgox", }, callback); }, function (callback) { self.what = "Verify offer consumed."; testutils.verify_offer_not_found(self.remote, "bob", seq, callback); }, ], function (error) { buster.refute(error, self.what); done(); }); }, "ripple cross currency payment - end with XRP" : // alice --> [USD/mtgox --> carol --> XRP] --> bob function (done) { var self = this; var seq; // self.remote.set_trace(); async.waterfall([ function (callback) { self.what = "Create accounts."; testutils.create_accounts(self.remote, "root", "10000.0", ["alice", "bob", "carol", "mtgox"], callback); }, function (callback) { self.what = "Set limits."; testutils.credit_limits(self.remote, { "alice" : "1000/USD/mtgox", "carol" : "2000/USD/mtgox" }, callback); }, function (callback) { self.what = "Distribute funds."; testutils.payments(self.remote, { "mtgox" : "500/USD/alice" }, callback); }, function (callback) { self.what = "Create offer."; self.remote.transaction() .offer_create("carol", "50/USD/mtgox", "500") .on('proposed', function (m) { // console.log("PROPOSED: offer_create: %s", JSON.stringify(m)); callback(m.result !== 'tesSUCCESS'); seq = m.tx_json.Sequence; }) .submit(); }, function (callback) { self.what = "Alice send XRP to bob converting from USD/mtgox."; self.remote.transaction() .payment("alice", "bob", "250") .send_max("333/USD/mtgox") .on('proposed', function (m) { // console.log("proposed: %s", JSON.stringify(m)); callback(m.result !== 'tesSUCCESS'); }) .submit(); }, function (callback) { self.what = "Verify balances."; testutils.verify_balances(self.remote, { "alice" : "475/USD/mtgox", "bob" : "10000000250", "carol" : "25/USD/mtgox", }, callback); }, function (callback) { self.what = "Verify offer partially consumed."; testutils.verify_offer(self.remote, "carol", seq, "25/USD/mtgox", "250", callback); }, ], function (error) { buster.refute(error, self.what); done(); }); }, "ripple cross currency bridged payment" : // alice --> [USD/mtgox --> carol --> XRP] --> [XRP --> dan --> EUR/bitstamp] --> bob function (done) { var self = this; var seq_carol; var seq_dan; // self.remote.set_trace(); async.waterfall([ function (callback) { self.what = "Create accounts."; testutils.create_accounts(self.remote, "root", "10000.0", ["alice", "bob", "carol", "dan", "bitstamp", "mtgox"], callback); }, function (callback) { self.what = "Set limits."; testutils.credit_limits(self.remote, { "alice" : "1000/USD/mtgox", "bob" : "1000/EUR/bitstamp", "carol" : "1000/USD/mtgox", "dan" : "1000/EUR/bitstamp" }, callback); }, function (callback) { self.what = "Distribute funds."; testutils.payments(self.remote, { "bitstamp" : "400/EUR/dan", "mtgox" : "500/USD/alice", }, callback); }, function (callback) { self.what = "Create offer carol."; self.remote.transaction() .offer_create("carol", "50/USD/mtgox", "500") .on('proposed', function (m) { // console.log("PROPOSED: offer_create: %s", JSON.stringify(m)); callback(m.result !== 'tesSUCCESS'); seq_carol = m.tx_json.Sequence; }) .submit(); }, function (callback) { self.what = "Create offer dan."; self.remote.transaction() .offer_create("dan", "500", "50/EUR/bitstamp") .on('proposed', function (m) { // console.log("PROPOSED: offer_create: %s", JSON.stringify(m)); callback(m.result !== 'tesSUCCESS'); seq_dan = m.tx_json.Sequence; }) .submit(); }, function (callback) { self.what = "Alice send EUR/bitstamp to bob converting from USD/mtgox."; self.remote.transaction() .payment("alice", "bob", "30/EUR/bitstamp") .send_max("333/USD/mtgox") .path_add( [ { currency: "XRP" } ]) .on('proposed', function (m) { // console.log("proposed: %s", JSON.stringify(m)); callback(m.result !== 'tesSUCCESS'); }) .submit(); }, function (callback) { self.what = "Verify balances."; testutils.verify_balances(self.remote, { "alice" : "470/USD/mtgox", "bob" : "30/EUR/bitstamp", "carol" : "30/USD/mtgox", "dan" : "370/EUR/bitstamp", }, callback); }, function (callback) { self.what = "Verify carol offer partially consumed."; testutils.verify_offer(self.remote, "carol", seq_carol, "20/USD/mtgox", "200", callback); }, function (callback) { self.what = "Verify dan offer partially consumed."; testutils.verify_offer(self.remote, "dan", seq_dan, "200", "20/EUR/mtgox", callback); }, ], function (error) { buster.refute(error, self.what); done(); }); }, }); buster.testCase("Offer tests 3", { 'setUp' : testutils.build_setup(), // 'setUp' : testutils.build_setup({ verbose: true }), // 'setUp' : testutils.build_setup({ verbose: true, standalone: true }), 'tearDown' : testutils.build_teardown(), "offer fee consumes funds" : function (done) { var self = this; var final_create; async.waterfall([ function (callback) { // Provide micro amounts to compensate for fees to make results round nice. self.what = "Create accounts."; // Alice has 3 entries in the ledger, via trust lines var max_owner_count = 3; // // We start off with a var reserve_amount = self.remote.reserve(max_owner_count); // console.log("\n"); // console.log("reserve_amount reserve(max_owner_count=%s): %s", max_owner_count, reserve_amount.to_human()); // this.tx_json.Fee = this.remote.fee_tx(this.fee_units()).to_json(); // 1 for each trust limit == 3 (alice < mtgox/amazon/bitstamp) // 1 for payment == 4 var max_txs_per_user = 4; // We don't have access to the tx object[s] created below so we // just dig into fee_units straight away var fee_units_for_all_txs = ( Transaction.fee_units["default"] * max_txs_per_user ); starting_xrp = reserve_amount.add(self.remote.fee_tx(fee_units_for_all_txs)) // console.log("starting_xrp after %s fee units: ", fee_units_for_all_txs, starting_xrp.to_human()); starting_xrp = starting_xrp.add(Amount.from_json('100.0')); // console.log("starting_xrp adding 100 xrp to sell", starting_xrp.to_human()); testutils.create_accounts(self.remote, "root", starting_xrp.to_json(), ["alice", "bob", "mtgox", "amazon", "bitstamp"], callback); }, function (callback) { self.what = "Set limits."; testutils.credit_limits(self.remote, { "alice" : ["1000/USD/mtgox", "1000/USD/amazon","1000/USD/bitstamp"], "bob" : ["1000/USD/mtgox", "1000/USD/amazon"], }, callback); }, function (callback) { self.what = "Distribute funds."; testutils.payments(self.remote, { "mtgox" : [ "500/USD/bob" ], }, callback); }, function (callback) { self.what = "Create offer bob."; self.remote.transaction() .offer_create("bob", "200.0", "200/USD/mtgox") .on('proposed', function (m) { // console.log("proposed: offer_create: %s", json.stringify(m)); callback(m.result !== 'tesSUCCESS'); seq_carol = m.tx_json.sequence; }) .submit(); }, function (callback) { // Alice has 350 fees - a reserve of 50 = 250 reserve = 100 available. // Ask for more than available to prove reserve works. self.what = "Create offer alice."; self.remote.transaction() .offer_create("alice", "200/USD/mtgox", "200.0") .on('proposed', function (m) { // console.log("proposed: offer_create: %s", json.stringify(m)); callback(m.result !== 'tesSUCCESS'); seq_carol = m.tx_json.sequence; }) .submit(); }, // function (callback) { // self.what = "Display ledger"; // // self.remote.request_ledger('current', true) // .on('success', function (m) { // console.log("Ledger: %s", JSON.stringify(m, undefined, 2)); // // callback(); // }) // .request(); // }, function (callback) { self.what = "Verify balances."; testutils.verify_balances(self.remote, { "alice" : [ "100/USD/mtgox", "350.0"], "bob" : ["400/USD/mtgox", ], }, callback); }, ], function (error) { // console.log("result: error=%s", error); buster.refute(error, self.what); done(); }); }, "offer create then cross offer" : function (done) { var self = this; var final_create; async.waterfall([ function (callback) { self.what = "Create accounts."; testutils.create_accounts(self.remote, "root", "10000.0", ["alice", "bob", "mtgox"], callback); }, function (callback) { self.what = "Set transfer rate."; self.remote.transaction() .account_set("mtgox") .transfer_rate(1005000000) .once('proposed', function (m) { // console.log("proposed: %s", JSON.stringify(m)); callback(m.result !== 'tesSUCCESS'); }) .submit(); }, function (callback) { self.what = "Set limits."; testutils.credit_limits(self.remote, { "alice" : "1000/USD/mtgox", "bob" : "1000/USD/mtgox", "mtgox" : "50/USD/alice", }, callback); }, function (callback) { self.what = "Distribute funds."; testutils.payments(self.remote, { "mtgox" : [ "1/USD/bob" ], "alice" : [ "50/USD/mtgox" ] }, callback); }, function (callback) { self.what = "Set limits 2."; testutils.credit_limits(self.remote, { "mtgox" : "0/USD/alice", }, callback); }, function (callback) { self.what = "Create offer alice."; self.remote.transaction() .offer_create("alice", "50/USD/mtgox", "150000.0") .on('proposed', function (m) { // console.log("proposed: offer_create: %s", json.stringify(m)); callback(m.result !== 'tesSUCCESS'); seq_carol = m.tx_json.sequence; }) .submit(); }, function (callback) { self.what = "Create offer bob."; self.remote.transaction() .offer_create("bob", "100.0", ".1/USD/mtgox") .on('proposed', function (m) { // console.log("proposed: offer_create: %s", json.stringify(m)); callback(m.result !== 'tesSUCCESS'); seq_carol = m.tx_json.sequence; }) .submit(); }, // function (callback) { // self.what = "Display ledger"; // // self.remote.request_ledger('current', true) // .on('success', function (m) { // console.log("Ledger: %s", JSON.stringify(m, undefined, 2)); // // callback(); // }) // .request(); // }, function (callback) { self.what = "Verify balances."; testutils.verify_balances(self.remote, { "alice" : "-49.96666666666667/USD/mtgox", "bob" : "0.9665/USD/mtgox", }, callback); }, ], function (error) { // console.log("result: error=%s", error); buster.refute(error, self.what); done(); }); }, }); buster.testCase("Offer tfSell", { 'setUp' : testutils.build_setup(), // 'setUp' : testutils.build_setup({ verbose: true }), // 'setUp' : testutils.build_setup({ verbose: true, standalone: true }), 'tearDown' : testutils.build_teardown(), "basic sell" : function (done) { var self = this; var final_create, seq_carol; async.waterfall([ function (callback) { // Provide micro amounts to compensate for fees to make results round nice. self.what = "Create accounts."; var req_amount = self.remote.reserve(1).add(self.remote.fee_tx(20)).add(100000000); testutils.create_accounts(self.remote, "root", req_amount.to_json(), ["alice", "bob", "mtgox"], callback); }, function (callback) { self.what = "Set limits."; testutils.credit_limits(self.remote, { "alice" : "1000/USD/mtgox", "bob" : "1000/USD/mtgox", }, callback); }, function (callback) { self.what = "Distribute funds."; testutils.payments(self.remote, { "mtgox" : [ "500/USD/bob" ], }, callback); }, function (callback) { self.what = "Create offer bob."; self.remote.transaction() .offer_create("bob", "200.0", "200/USD/mtgox") .set_flags('Sell') // Should not matter at all. .on('proposed', function (m) { // console.log("proposed: offer_create: %s", json.stringify(m)); if (m.result !== 'tesSUCCESS') { throw new Error("Bob's OfferCreate tx did not succeed: "+m.result); } else callback(null); seq_carol = m.tx_json.sequence; }) .submit(); }, function (callback) { // Alice has 350 + fees - a reserve of 50 = 250 reserve = 100 available. // Ask for more than available to prove reserve works. self.what = "Create offer alice."; self.remote.transaction() .offer_create("alice", "200/USD/mtgox", "200.0") .set_flags('Sell') // Should not matter at all. .on('proposed', function (m) { // console.log("proposed: offer_create: %s", json.stringify(m)); callback(m.result !== 'tesSUCCESS'); seq_carol = m.tx_json.sequence; }) .submit(); }, // function (callback) { // self.what = "Display ledger"; // // self.remote.request_ledger('current', true) // .on('success', function (m) { // console.log("Ledger: %s", JSON.stringify(m, undefined, 2)); // // callback(); // }) // .request(); // }, function (callback) { self.what = "Verify balances."; testutils.verify_balances(self.remote, { "alice" : [ "100/USD/mtgox", "250.0" ], "bob" : "400/USD/mtgox", }, callback); }, ], function (error) { // console.log("result: error=%s", error); buster.refute(error, self.what); done(); }); }, "2x sell exceed limit" : function (done) { var self = this; var final_create, seq_carol; async.waterfall([ function (callback) { // Provide micro amounts to compensate for fees to make results round nice. self.what = "Create accounts."; var starting_xrp = self.amount_for({ ledger_entries: 1, default_transactions: 2, extra: '100.0' }); testutils.create_accounts(self.remote, "root", starting_xrp, ["alice", "bob", "mtgox"], callback); }, function (callback) { self.what = "Set limits."; testutils.credit_limits(self.remote, { "alice" : "150/USD/mtgox", "bob" : "1000/USD/mtgox", }, callback); }, function (callback) { self.what = "Distribute funds."; testutils.payments(self.remote, { "mtgox" : [ "500/USD/bob" ], }, callback); }, function (callback) { self.what = "Create offer bob."; // Taker pays 200 XRP for 100 USD. // Selling USD. self.remote.transaction() .offer_create("bob", "100.0", "200/USD/mtgox") .on('proposed', function (m) { // console.log("proposed: offer_create: %s", json.stringify(m)); callback(m.result !== 'tesSUCCESS'); seq_carol = m.tx_json.sequence; }) .submit(); }, function (callback) { // Alice has 350 fees - a reserve of 50 = 250 reserve = 100 available. // Ask for more than available to prove reserve works. self.what = "Create offer alice."; // Taker pays 100 USD for 100 XRP. // Selling XRP. // Will sell all 100 XRP and get more USD than asked for. self.remote.transaction() .offer_create("alice", "100/USD/mtgox", "100.0") .set_flags('Sell') .on('proposed', function (m) { // console.log("proposed: offer_create: %s", json.stringify(m)); if (m.result !== 'tesSUCCESS') { callback(new Error("Alice's OfferCreate didn't succeed: "+m.result)); } else callback(null); seq_carol = m.tx_json.sequence; }) .submit(); }, // function (callback) { // self.what = "Display ledger"; // // self.remote.request_ledger('current', true) // .on('success', function (m) { // console.log("Ledger: %s", JSON.stringify(m, undefined, 2)); // // callback(); // }) // .request(); // }, function (callback) { self.what = "Verify balances."; testutils.verify_balances(self.remote, { "alice" : [ "200/USD/mtgox", "250.0" ], "bob" : "300/USD/mtgox", }, callback); }, ], function (error) { // console.log("result: error=%s", error); buster.refute(error, self.what); done(); }); }, }); buster.testCase("Client Issue #535", { 'setUp' : testutils.build_setup(), // 'setUp' : testutils.build_setup({ verbose: true }), // 'setUp' : testutils.build_setup({ verbose: true, standalone: true }), 'tearDown' : testutils.build_teardown(), "gateway cross currency" : function (done) { var self = this; var final_create; async.waterfall([ function (callback) { // Provide micro amounts to compensate for fees to make results round nice. self.what = "Create accounts."; var starting_xrp = self.amount_for({ ledger_entries: 1, default_transactions: 2, extra: '100.0' }); testutils.create_accounts(self.remote, "root", starting_xrp, ["alice", "bob", "mtgox"], callback); }, function (callback) { self.what = "Set limits."; testutils.credit_limits(self.remote, { "alice" : [ "1000/XTS/mtgox", "1000/XXX/mtgox" ], "bob" : [ "1000/XTS/mtgox", "1000/XXX/mtgox" ], }, callback); }, function (callback) { self.what = "Distribute funds."; testutils.payments(self.remote, { "mtgox" : [ "100/XTS/alice", "100/XXX/alice", "100/XTS/bob", "100/XXX/bob", ], }, callback); }, function (callback) { self.what = "Create offer alice."; self.remote.transaction() .offer_create("alice", "100/XTS/mtgox", "100/XXX/mtgox") .on('proposed', function (m) { // console.log("proposed: offer_create: %s", json.stringify(m)); callback(m.result !== 'tesSUCCESS'); seq_carol = m.tx_json.sequence; }) .submit(); }, function (callback) { self.what = "Bob converts XTS to XXX."; self.remote.transaction() .payment("bob", "bob", "1/XXX/bob") .send_max("1.5/XTS/bob") .build_path(true) .on('proposed', function (m) { if (m.result !== 'tesSUCCESS') console.log("proposed: %s", JSON.stringify(m, undefined, 2)); callback(m.result !== 'tesSUCCESS'); }) .submit(); }, // function (callback) { // self.what = "Display ledger"; // // self.remote.request_ledger('current', true) // .on('success', function (m) { // console.log("Ledger: %s", JSON.stringify(m, undefined, 2)); // // callback(); // }) // .request(); // }, function (callback) { self.what = "Verify balances."; testutils.verify_balances(self.remote, { "alice" : [ "101/XTS/mtgox", "99/XXX/mtgox", ], "bob" : [ "99/XTS/mtgox", "101/XXX/mtgox", ], }, callback); }, ], function (error) { if (error) console.log("result: %s: error=%s", self.what, error); buster.refute(error, self.what); done(); }); } }); // vim:sw=2:sts=2:ts=8:et