Skip to content

Commit

Permalink
Merge f728dc3 into e752ce6
Browse files Browse the repository at this point in the history
  • Loading branch information
tomerweller committed Dec 15, 2017
2 parents e752ce6 + f728dc3 commit b1cceac
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
/coverage/
/jsdoc/
.DS_Store
.idea/
10 changes: 0 additions & 10 deletions src/orderbook_call_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,5 @@ export class OrderbookCallBuilder extends CallBuilder {
this.url.addQuery("buying_asset_type", 'native');
}
}

/**
* People on the Stellar network can make offers to buy or sell assets. These offers are summarized by the assets being bought and sold in orderbooks. When an offer is fully or partially fulfilled, a trade happens.
* @see [Trades for Orderbook](https://www.stellar.org/developers/horizon/reference/trades-for-orderbook.html)
* @returns {OrderbookCallBuilder}
*/
trades() {
this.filter.push(['order_book', 'trades']);
return this;
}
}

9 changes: 9 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {TransactionCallBuilder} from "./transaction_call_builder";
import {OperationCallBuilder} from "./operation_call_builder";
import {OfferCallBuilder} from "./offer_call_builder";
import {OrderbookCallBuilder} from "./orderbook_call_builder";
import {TradesCallBuilder} from "./trades_call_builder";
import {PathCallBuilder} from "./path_call_builder";
import {PaymentCallBuilder} from "./payment_call_builder";
import {EffectCallBuilder} from "./effect_call_builder";
Expand Down Expand Up @@ -121,6 +122,14 @@ export class Server {
return new OrderbookCallBuilder(URI(this.serverURL), selling, buying);
}

/**
* Returns new {@link TradesCallBuilder} object configured by a current Horizon server configuration.
* @returns {TradesCallBuilder}
*/
trades() {
return new TradesCallBuilder(URI(this.serverURL));
}

/**
* Returns new {@link OperationCallBuilder} object configured by a current Horizon server configuration.
* @returns {OperationCallBuilder}
Expand Down
40 changes: 40 additions & 0 deletions src/trades_call_builder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {CallBuilder} from "./call_builder";

/**
* Creates a new {@link TradesCallBuilder} pointed to server defined by serverUrl.
*
* Do not create this object directly, use {@link Server#orderbook}.
* @see [Orderbook Details](https://www.stellar.org/developers/horizon/reference/orderbook-details.html)
* @param {string} serverUrl serverUrl Horizon server URL.
*/
export class TradesCallBuilder extends CallBuilder {
constructor(serverUrl) {
super(serverUrl);
this.url.segment('trades');
}

/**
* Filter trades for a specific asset pair (orderbook)
* @param {Asset} base asset
* @param {Asset} counter asset
* @returns {TradesCallBuilder}
*/
withAssetPair(base, counter) {
if (!base.isNative()) {
this.url.addQuery("base_asset_type", base.getAssetType());
this.url.addQuery("base_asset_code", base.getCode());
this.url.addQuery("base_asset_issuer", base.getIssuer());
} else {
this.url.addQuery("base_asset_type", 'native');
}
if (!counter.isNative()) {
this.url.addQuery("counter_asset_type", counter.getAssetType());
this.url.addQuery("counter_asset_code", counter.getCode());
this.url.addQuery("counter_asset_issuer", counter.getIssuer());
} else {
this.url.addQuery("counter_asset_type", 'native');
}
return this;
}
}

46 changes: 33 additions & 13 deletions test/unit/server_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -670,21 +670,41 @@ describe("server.js tests", function () {
})
});

it("trades() requests the correct endpoint", function (done) {
});

describe("TradesCallBuilder", function() {
it("trades() requests the correct endpoint (no filters)", function (done) {
let response = "trades_no_filters";
this.axiosMock.expects('get')
.withArgs(sinon.match('https://horizon-live.stellar.org:1337/order_book/trades?selling_asset_type=native&buying_asset_type=credit_alphanum4&buying_asset_code=USD&buying_asset_issuer=GDVDKQFP665JAO7A2LSHNLQIUNYNAAIGJ6FYJVMG4DT3YJQQJSRBLQDG'))
.returns(Promise.resolve({data: orderBookResponse}));
.withArgs(sinon.match('https://horizon-live.stellar.org:1337/trades'))
.returns(Promise.resolve({data: response}));

this.server.orderbook(StellarSdk.Asset.native(), new StellarSdk.Asset('USD', "GDVDKQFP665JAO7A2LSHNLQIUNYNAAIGJ6FYJVMG4DT3YJQQJSRBLQDG"))
.trades()
.call()
.then(function (response) {
expect(response).to.be.deep.equal(orderBookResponse);
done();
})
.catch(function (err) {
done(err);
})
this.server.trades()
.call()
.then(function (response) {
expect(response).to.be.deep.equal(response);
done();
})
.catch(function (err) {
done(err);
})
});

it("trades() requests the correct endpoint (with assets filter)", function (done) {
let response = "trades_asset_filters";
this.axiosMock.expects('get')
.withArgs(sinon.match('https://horizon-live.stellar.org:1337/trades?base_asset_type=native&counter_asset_type=credit_alphanum4&counter_asset_code=USD&counter_asset_issuer=GDVDKQFP665JAO7A2LSHNLQIUNYNAAIGJ6FYJVMG4DT3YJQQJSRBLQDG'))
.returns(Promise.resolve({data: response}));

this.server.trades().withAssetPair(StellarSdk.Asset.native(), new StellarSdk.Asset('USD', "GDVDKQFP665JAO7A2LSHNLQIUNYNAAIGJ6FYJVMG4DT3YJQQJSRBLQDG"))
.call()
.then(function (response) {
expect(response).to.be.deep.equal(response);
done();
})
.catch(function (err) {
done(err);
})
});
});

Expand Down

0 comments on commit b1cceac

Please sign in to comment.