-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from coinsambacom/feat/paxos
Feat/paxos
- Loading branch information
Showing
5 changed files
with
135 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "@coinsamba/js-exchanges-connector", | ||
"description": "Collection of JavaScript implementations of cryptocurrency exchange APIs", | ||
"version": "2.1.11", | ||
"version": "2.1.12", | ||
"repository": "[email protected]:coinsambacom/js-exchanges-connector.git", | ||
"author": "Gustavo <[email protected]>", | ||
"license": "MIT", | ||
|
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,86 @@ | ||
import { | ||
Exchange, | ||
IExchangeImplementationConstructorArgs, | ||
} from "../interfaces/exchange"; | ||
import { IOrderbook, ITicker } from "../utils/DTOs"; | ||
|
||
interface PaxosTickerRes { | ||
market: string; | ||
best_bid: Bestbid; | ||
best_ask: Bestbid; | ||
last_execution: Bestbid; | ||
last_day: Lastday; | ||
today: Lastday; | ||
snapshot_at: string; | ||
} | ||
|
||
interface Lastday { | ||
high: string; | ||
low: string; | ||
open: string; | ||
volume: string; | ||
volume_weighted_average_price: string; | ||
range: Range; | ||
} | ||
|
||
interface Range { | ||
begin: string; | ||
end: string; | ||
} | ||
|
||
interface Bestbid { | ||
price: string; | ||
amount: string; | ||
} | ||
|
||
interface PaxosOrderbookRes { | ||
market: string; | ||
asks: IPaxosOrderbookOrder[]; | ||
bids: IPaxosOrderbookOrder[]; | ||
} | ||
|
||
interface IPaxosOrderbookOrder { | ||
price: string; | ||
amount: string; | ||
} | ||
|
||
export class paxos<T = any> extends Exchange<T> { | ||
constructor(args?: IExchangeImplementationConstructorArgs<T>) { | ||
super({ | ||
id: "paxos", | ||
baseUrl: "https://api.paxos.com", | ||
opts: args?.opts, | ||
}); | ||
} | ||
|
||
async getTicker(base: string, quote: string): Promise<ITicker> { | ||
const res = await this.fetch<PaxosTickerRes>( | ||
`${this.baseUrl}/v2/markets/${base}${quote}/ticker`, | ||
); | ||
|
||
return { | ||
exchangeId: this.id, | ||
base, | ||
quote, | ||
last: Number(res.last_execution.price), | ||
ask: Number(res.best_ask.price), | ||
bid: Number(res.best_bid.price), | ||
vol: Number(res.today.volume), | ||
}; | ||
} | ||
|
||
private parseOrder({ price, amount }: IPaxosOrderbookOrder) { | ||
return { price: Number(price), amount: Number(amount) }; | ||
} | ||
|
||
async getBook(base: string, quote: string): Promise<IOrderbook> { | ||
const res = await this.fetch<PaxosOrderbookRes>( | ||
`${this.baseUrl}/v2/markets/${base}${quote}/order-book`, | ||
); | ||
|
||
return { | ||
asks: res.asks.map(this.parseOrder), | ||
bids: res.bids.map(this.parseOrder), | ||
}; | ||
} | ||
} |
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,46 @@ | ||
import { expect } from "chai"; | ||
import { paxos } from "../src/connectors/paxos"; | ||
import { FetcherHandler } from "../src/utils/DTOs"; | ||
import { MyFetcher } from "./utils/MyFetcher"; | ||
import { expectPropertyTypes } from "./utils/helpers"; | ||
|
||
describe("paxos", () => { | ||
let exchange: paxos; | ||
|
||
beforeEach(() => { | ||
exchange = new paxos(); | ||
const fetcher = new MyFetcher(); | ||
|
||
FetcherHandler.setFetcher(fetcher); | ||
}); | ||
|
||
describe("getTicker", () => { | ||
it("should return an ITicker object", async () => { | ||
const ticker = await exchange.getTicker("BTC", "USD"); | ||
|
||
expectPropertyTypes(ticker, ["exchangeId", "base", "quote"], "string"); | ||
expectPropertyTypes(ticker, ["last", "ask", "bid", "vol"], "number"); | ||
}); | ||
}); | ||
|
||
describe("getBook", () => { | ||
it("should return an IOrderbook object", async () => { | ||
const book = await exchange.getBook("BTC", "USD"); | ||
|
||
expect(book).to.have.property("asks"); | ||
expect(book).to.have.property("bids"); | ||
expect(Array.isArray(book.asks)).to.be.true; | ||
expect(Array.isArray(book.bids)).to.be.true; | ||
|
||
if (book.asks.length > 0) { | ||
const ask = book.asks[0]; | ||
expectPropertyTypes(ask, ["price", "amount"], "number"); | ||
} | ||
|
||
if (book.bids.length > 0) { | ||
const bid = book.bids[0]; | ||
expectPropertyTypes(bid, ["price", "amount"], "number"); | ||
} | ||
}); | ||
}); | ||
}); |