Skip to content
This repository has been archived by the owner on Sep 2, 2023. It is now read-only.

Commit

Permalink
[TASK] update place orders API to accept XRP amounts
Browse files Browse the repository at this point in the history
  • Loading branch information
boxbag committed Dec 3, 2014
1 parent d36d345 commit 274f523
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 4 deletions.
15 changes: 11 additions & 4 deletions api/orders.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,20 @@ function placeOrder(request, response, next) {
});

function validateParams(async_callback) {
var takerGetsJSON, takerPaysJSON;

if (_.isObject(params.order)) {
takerGetsJSON = ripple.Amount.from_json(params.order.taker_gets);
takerPaysJSON = ripple.Amount.from_json(params.order.taker_pays);
}

if (!params.order) {
async_callback(new InvalidRequestError('Missing parameter: order. Submission must have order object in JSON form'));
return async_callback(new InvalidRequestError('Missing parameter: order. Submission must have order object in JSON form'));
} else if (!/^buy|sell$/.test(params.order.type)) {
async_callback(new InvalidRequestError('Parameter must be "buy" or "sell": type'));
} else if (!ripple.Amount.is_valid_full(params.order.taker_gets)) {
return async_callback(new InvalidRequestError('Parameter must be "buy" or "sell": type'));
} else if (!takerGetsJSON._currency || !takerGetsJSON.is_valid() || (!takerGetsJSON._is_native && !takerGetsJSON.is_valid_full())) {
async_callback(new InvalidRequestError('Parameter must be in the format "amount[/currency/issuer]": taker_gets'));
} else if (!ripple.Amount.is_valid_full(params.order.taker_pays)) {
} else if (!takerPaysJSON._currency || !takerPaysJSON.is_valid() || (!takerPaysJSON._is_native && !takerPaysJSON.is_valid_full())) {
async_callback(new InvalidRequestError('Parameter must be in the format "amount[/currency/issuer]": taker_pays'));
} else {
async_callback();
Expand Down
76 changes: 76 additions & 0 deletions test/orders-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,82 @@ suite('post orders', function() {
.end(done);
});

test('/orders -- with xrp taker gets', function(done) {
var lastLedger = self.app.remote._ledger_current_index;
var hash = testutils.generateHash();

self.wss.once('request_account_info', function(message, conn) {
assert.strictEqual(message.command, 'account_info');
assert.strictEqual(message.account, addresses.VALID);
conn.send(fixtures.accountInfoResponse(message));
});

self.wss.once('request_submit', function(message, conn) {
var so = new ripple.SerializedObject(message.tx_blob).to_json();
assert.strictEqual(message.command, 'submit');
assert.strictEqual(so.Account, addresses.VALID);
assert.strictEqual(so.TransactionType, 'OfferCreate');
assert.deepEqual(so.TakerGets, '100000');

conn.send(fixtures.requestSubmitResponse(message, {
hash: hash,
taker_gets: '100000'
}));
});

self.app
.post('/v1/accounts/' + addresses.VALID + '/orders')
.send(fixtures.order({
taker_gets: '100000'
}))
.expect(testutils.checkStatus(200))
.expect(testutils.checkHeaders)
.expect(testutils.checkBody(fixtures.RESTSubmitTransactionResponse({
hash: hash,
last_ledger: lastLedger,
taker_gets: '100000'
})))
.end(done);
});

test('/orders -- with xrp taker pays', function(done) {
var lastLedger = self.app.remote._ledger_current_index;
var hash = testutils.generateHash();

self.wss.once('request_account_info', function(message, conn) {
assert.strictEqual(message.command, 'account_info');
assert.strictEqual(message.account, addresses.VALID);
conn.send(fixtures.accountInfoResponse(message));
});

self.wss.once('request_submit', function(message, conn) {
var so = new ripple.SerializedObject(message.tx_blob).to_json();
assert.strictEqual(message.command, 'submit');
assert.strictEqual(so.Account, addresses.VALID);
assert.strictEqual(so.TransactionType, 'OfferCreate');
assert.deepEqual(so.TakerPays, '100000');

conn.send(fixtures.requestSubmitResponse(message, {
hash: hash,
taker_pays: '100000'
}));
});

self.app
.post('/v1/accounts/' + addresses.VALID + '/orders')
.send(fixtures.order({
taker_pays: '100000'
}))
.expect(testutils.checkStatus(200))
.expect(testutils.checkHeaders)
.expect(testutils.checkBody(fixtures.RESTSubmitTransactionResponse({
hash: hash,
last_ledger: lastLedger,
taker_pays: '100000'
})))
.end(done);
});

test('/orders -- with unfunded offer error', function(done) {
var hash = testutils.generateHash();

Expand Down

0 comments on commit 274f523

Please sign in to comment.