-
Notifications
You must be signed in to change notification settings - Fork 296
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a basic call builder for the
/liquidity_pools
endpoint. (#682)
* Add call builder for liquidity pools, w/ filters by reserve asset
- Loading branch information
Showing
7 changed files
with
215 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Asset } from "stellar-base"; | ||
|
||
import { CallBuilder } from "./call_builder"; | ||
import { ServerApi } from "./server_api"; | ||
|
||
/** | ||
* Creates a new {@link LiquidityPoolCallBuilder} pointed to server defined by serverUrl. | ||
* Do not create this object directly, use {@link Server#liquidityPools}. | ||
* | ||
* @class LiquidityPoolCallBuilder | ||
* @extends CallBuilder | ||
* @constructor | ||
* @param {string} serverUrl Horizon server URL. | ||
*/ | ||
export class LiquidityPoolCallBuilder extends CallBuilder< | ||
ServerApi.CollectionPage<ServerApi.LiquidityPoolRecord> | ||
> { | ||
constructor(serverUrl: URI) { | ||
super(serverUrl); | ||
this.url.segment("liquidity_pools"); | ||
} | ||
|
||
/** | ||
* Filters out liquidity pools whose reserves aren't in this list of assets. | ||
* | ||
* @see Asset | ||
* @param {Asset[]} assets | ||
* @returns {LiquidityPoolCallBuilder} current LiquidityPoolCallBuilder instance | ||
*/ | ||
public forAssets(...assets: Asset[]) { | ||
const commaSeparatedAssets: string = assets | ||
.map((asset: Asset) => { | ||
return asset.toString(); | ||
}) | ||
.join(","); | ||
this.url.setQuery("reserves", commaSeparatedAssets); | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
// Helper function to deep-copy JSON responses. | ||
function copyJson(js) { | ||
return JSON.parse(JSON.stringify(js)); | ||
} | ||
|
||
const BASE_URL = "https://horizon-live.stellar.org:1337"; | ||
const LP_URL = BASE_URL + "/liquidity_pools" | ||
|
||
|
||
describe('/liquidity_pools tests', function() { | ||
beforeEach(function() { | ||
this.server = new StellarSdk.Server(BASE_URL); | ||
this.axiosMock = sinon.mock(HorizonAxiosClient); | ||
StellarSdk.Config.setDefault(); | ||
}); | ||
|
||
afterEach(function() { | ||
this.axiosMock.verify(); | ||
this.axiosMock.restore(); | ||
}); | ||
|
||
it('can create a LiquidityPoolCallBuilder', function() { | ||
expect(this.server.liquidityPools()).not.to.be.undefined; | ||
}); | ||
|
||
const rootResponse = { | ||
"_links": { | ||
"self": { | ||
"href": "https://private-33c60-amm3.apiary-mock.com/liquidity_pools?cursor=113725249324879873&limit=10&order=asc" | ||
}, | ||
"next": { | ||
"href": "https://private-33c60-amm3.apiary-mock.com/liquidity_pools?cursor=113725249324879873&limit=10&order=asc" | ||
}, | ||
"prev": { | ||
"href": "https://private-33c60-amm3.apiary-mock.com/liquidity_pools?cursor=113725249324879873&limit=10&order=desc" | ||
} | ||
}, | ||
"_embedded": { | ||
"records": [ | ||
{ | ||
"id": "1", | ||
"paging_token": "113725249324879873", | ||
"fee_bp": 30, | ||
"type": "constant_product", | ||
"total_trustlines": "300", | ||
"total_shares": "5000", | ||
"reserves": [ | ||
{ | ||
"amount": "1000.0000005", | ||
"asset": "EURT:GAP5LETOV6YIE62YAM56STDANPRDO7ZFDBGSNHJQIYGGKSMOZAHOOS2S" | ||
}, | ||
{ | ||
"amount": "2000.0000000", | ||
"asset": "PHP:GAP5LETOV6YIE62YAM56STDANPRDO7ZFDBGSNHJQIYGGKSMOZAHOOS2S" | ||
} | ||
] | ||
}, | ||
{ | ||
"id": "2", | ||
"paging_token": "113725249324879874", | ||
"fee_bp": 30, | ||
"type": "constant_product", | ||
"total_trustlines": "200", | ||
"total_shares": "3500", | ||
"reserves": [ | ||
{ | ||
"amount": "1000.0000005", | ||
"asset": "EURT:GAP5LETOV6YIE62YAM56STDANPRDO7ZFDBGSNHJQIYGGKSMOZAHOOS2S" | ||
}, | ||
{ | ||
"amount": "1200.0000000", | ||
"asset": "USDC:GC5W3BH2MQRQK2H4A6LP3SXDSAAY2W2W64OWKKVNQIAOVWSAHFDEUSDC" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} | ||
|
||
let emptyResponse = copyJson(rootResponse); | ||
emptyResponse._embedded.records = []; | ||
|
||
let phpResponse = copyJson(rootResponse); | ||
phpResponse._embedded.records.pop(); // last elem doesn't have PHP asset | ||
|
||
const EURT = new StellarSdk.Asset("EURT", "GAP5LETOV6YIE62YAM56STDANPRDO7ZFDBGSNHJQIYGGKSMOZAHOOS2S") | ||
const PHP = new StellarSdk.Asset("PHP", "GAP5LETOV6YIE62YAM56STDANPRDO7ZFDBGSNHJQIYGGKSMOZAHOOS2S") | ||
|
||
it('returns the right root response', function(done) { | ||
this.axiosMock | ||
.expects('get') | ||
.withArgs(sinon.match(LP_URL)) | ||
.returns(Promise.resolve({ data: rootResponse })); | ||
|
||
this.server | ||
.liquidityPools() | ||
.call() | ||
.then((pools) => { | ||
expect(pools.records).to.deep.equal(rootResponse._embedded.records); | ||
done(); | ||
}) | ||
.catch(done); | ||
}); | ||
|
||
describe('filtering by asset', function() { | ||
const testCases = [ | ||
{ | ||
assets: [StellarSdk.Asset.native()], | ||
response: emptyResponse, | ||
}, { | ||
assets: [EURT], | ||
response: rootResponse, | ||
}, { | ||
assets: [PHP], | ||
response: phpResponse, | ||
}, { | ||
assets: [EURT, PHP], | ||
response: rootResponse, | ||
}, | ||
]; | ||
|
||
testCases.forEach((testCase) => { | ||
const queryStr = testCase.assets.map(asset => asset.toString()).join(','); | ||
const description = testCase.assets.map(asset => asset.getCode()).join(' + '); | ||
|
||
it('filters by asset(s) ' + description, function(done) { | ||
this.axiosMock | ||
.expects('get') | ||
.withArgs(sinon.match(`${LP_URL}?reserves=${encodeURIComponent(queryStr)}`)) | ||
.returns(Promise.resolve({ data: testCase.response })) | ||
|
||
this.server | ||
.liquidityPools() | ||
.forAssets(...testCase.assets) | ||
.call() | ||
.then((pools) => { | ||
expect(pools.records).to.deep.equal(testCase.response._embedded.records); | ||
done(); | ||
}) | ||
.catch(done); | ||
}); | ||
}); | ||
}); | ||
}); |