Skip to content

Commit

Permalink
0.1.7
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunlight committed May 28, 2021
1 parent 8c9d33d commit a59650e
Showing 1 changed file with 64 additions and 32 deletions.
96 changes: 64 additions & 32 deletions src/atomex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,21 @@ export class Atomex {
private _baseUrl: string;
private _authToken?: string;

constructor(network: "mainnet" | "testnet", baseUrl: string, authToken?: string) {
constructor(
network: "mainnet" | "testnet",
baseUrl: string,
authToken?: string,
) {
this._network = network;
this._baseUrl = baseUrl;
this._authToken = authToken;
}

static create(network: "mainnet" | "testnet" | "localhost"): Atomex {
return new Atomex(network == "mainnet" ? "mainnet" : "testnet", config.api[network].baseUrl);
return new Atomex(
network == "mainnet" ? "mainnet" : "testnet",
config.api[network].baseUrl,
);
}

/**
Expand Down Expand Up @@ -147,14 +154,16 @@ export class Atomex {
* @param addOrderRequest details of the order being placed
* @returns order id
*/
async addOrder(addOrderRequest: AddOrderRequest): Promise<number> {
const [baseConfig, quoteConfig] = this.splitSymbol(addOrderRequest.symbol).map(x => this.getCurrencyConfig(x));
async addOrder(addOrderRequest: AddOrderRequest): Promise<number> {
const [baseConfig, quoteConfig] = this.splitSymbol(
addOrderRequest.symbol,
).map((x) => this.getCurrencyConfig(x));
const query: Query = addOrderRequest;
query.requisites = {
baseCurrencyContract: baseConfig.contractAddress,
quoteCurrencyContract: quoteConfig.contractAddress,
...query.requisites
}
...query.requisites,
};
return this.makeRequest<Record<string, number>>(
"post",
"/v1/Orders",
Expand Down Expand Up @@ -259,9 +268,9 @@ export class Atomex {
side: Side,
amount: number,
direction: "Send" | "Receive",
): OrderPreview {
): OrderPreview {
const availablePrices = orderBook.entries
.filter(entry => {
.filter((entry) => {
if (entry.side == side) {
return false;
}
Expand All @@ -273,16 +282,21 @@ export class Atomex {
default:
return amount;
}
}
};
return getOrderSize() <= Math.max(...entry.qtyProfile);
})
.map(entry => entry.price)
.map((entry) => entry.price);

if (availablePrices.length == 0) {
throw new Error(`No matching order found (${direction} ${amount} / ${side})`);
throw new Error(
`No matching order found (${direction} ${amount} / ${side})`,
);
}

const bestPrice = side == "Buy" ? Math.min(...availablePrices) : Math.max(...availablePrices);
const bestPrice =
side == "Buy"
? Math.min(...availablePrices)
: Math.max(...availablePrices);
const getExpectedAmount = () => {
switch (side + direction) {
case "BuySend":
Expand All @@ -291,39 +305,41 @@ export class Atomex {
default:
return amount * bestPrice;
}
}
};
return {
price: bestPrice,
amountSent: direction == "Send" ? amount : getExpectedAmount(),
amountReceived: direction == "Receive" ? amount : getExpectedAmount()
}
amountReceived: direction == "Receive" ? amount : getExpectedAmount(),
};
}

/**
* Split Atomex trading pair to base and quote currencies
*
*
* @param symbol Atomex trading pair {baseCurrency}/{quoteCurrency}
*/
splitSymbol(symbol: string): [string, string] {
const [baseCurrency, quoteCurrency] = symbol.split('/', 2)
const [baseCurrency, quoteCurrency] = symbol.split("/", 2);
return [baseCurrency, quoteCurrency];
}

/**
* Get currency & network specific configuration
*
*
* @param currency L1/L2 token symbol (uppercase)
*/
getCurrencyConfig(currency: string): CurrencyConfig {
const currencyEntry = Object.entries(config.currencies).find(([k, v]) => k == currency);
const currencyEntry = Object.entries(config.currencies).find(
([k, v]) => k == currency,
);
if (currencyEntry == undefined) {
throw new Error(`No matching config section for ${currency}`)
throw new Error(`No matching config section for ${currency}`);
}
return {
blockchain: currencyEntry[1].blockchain,
decimals: currencyEntry[1].decimals.original,
displayDecimals: currencyEntry[1].decimals.displayed,
contractAddress: currencyEntry[1].contracts[this._network].address
contractAddress: currencyEntry[1].contracts[this._network].address,
};
}

Expand All @@ -334,39 +350,55 @@ export class Atomex {
* @param currency L1/L2 token symbol (uppercase)
*/
formatAmount(amount: number | string, currency: string): number {
const cfg = this.getCurrencyConfig(currency)
const cfg = this.getCurrencyConfig(currency);
return typeof amount === "string"
? parseFloat(parseFloat(amount).toFixed(cfg.displayDecimals))
: parseFloat(amount.toFixed(cfg.displayDecimals));
}

/**
* Get order side for a particular trading pair given the bridge direction
*
*
* @param symbol Atomex trading pair {baseCurrency}/{quoteCurrency}
* @param srcBlockchain Left bridge leg
* @param dstBlockchain Right bridge leg
*/
getOrderSide(symbol: string, srcBlockchain: string, dstBlockchain: string): Side {
const [baseConfig, quoteConfig] = this.splitSymbol(symbol).map(x => this.getCurrencyConfig(x));
if (baseConfig.blockchain == srcBlockchain && quoteConfig.blockchain == dstBlockchain) {
getOrderSide(
symbol: string,
srcBlockchain: string,
dstBlockchain: string,
): Side {
const [baseConfig, quoteConfig] = this.splitSymbol(symbol).map((x) =>
this.getCurrencyConfig(x),
);
if (
baseConfig.blockchain == srcBlockchain &&
quoteConfig.blockchain == dstBlockchain
) {
return "Sell";
} else if (baseConfig.blockchain == dstBlockchain && quoteConfig.blockchain == srcBlockchain) {
} else if (
baseConfig.blockchain == dstBlockchain &&
quoteConfig.blockchain == srcBlockchain
) {
return "Buy";
} else {
throw new Error(`Mismatch ${srcBlockchain} => ${dstBlockchain} (${symbol})`);
throw new Error(
`Mismatch ${srcBlockchain} => ${dstBlockchain} (${symbol})`,
);
}
}

/**
* Get maximum available liquidity
*
*
* @param orderBook order-book received from [[getOrderBook]]
* @param side order side Buy/Sell
*/
getMaxOrderSize(orderBook: OrderBook, side: Side): number {
return Math.max(...orderBook.entries
.filter(entry => entry.side != side)
.map(entry => Math.max(...entry.qtyProfile)));
return Math.max(
...orderBook.entries
.filter((entry) => entry.side != side)
.map((entry) => Math.max(...entry.qtyProfile)),
);
}
}

0 comments on commit a59650e

Please sign in to comment.