@hydro-protocol/hydro-client-js
Version:
Javascript SDK for the Hydro API
289 lines (288 loc) • 12.1 kB
TypeScript
import { Candle } from '../models/Candle';
import { Fee } from '../models/Fee';
import { LockedBalance } from '../models/LockedBalance';
import { Market } from '../models/Market';
import { Order, OrderType, Side, Status } from '../models/Order';
import { Orderbook, OrderbookLevel } from '../models/Orderbook';
import { OrderList } from '../models/OrderList';
import { Ticker } from '../models/Ticker';
import { TradeList } from '../models/TradeList';
export interface HydroClientOptions {
apiUrl?: string;
web3Url?: string;
}
export declare class HydroClient {
private account;
private apiHandler;
private web3Handler;
private tokenAddresses;
private constructor();
/**
* If you only want to make public API calls, no authentication is needed
*/
static withoutAuth(options?: HydroClientOptions): HydroClient;
/**
* Provide a private key for authentication purposes
* @param privateKey A private key in hex format with the form "0x..."
*/
static withPrivateKey(privateKey: string, options?: HydroClientOptions): HydroClient;
/**
* If you don't want to supply your private key, or want to integrate with a wallet, provide
* your own function to sign messages and the account you will be using.
*
* @param address The address of the account that will be doing the signing
* @param sign A function that takes the input message and signs it with the private key of the account
* @param signTransaction An async function that takes a transaction object and signs it with the private key of the account
*/
static withCustomAuth(address: string, sign: (message: string) => Promise<string>, signTransaction: (tx: Transaction) => Promise<string>, options?: HydroClientOptions): HydroClient;
/**
* Public API Calls
*
* These calls do not require any authentication to complete, and will generally give you
* public state about the Hydro API
*
* See https://docs.ddex.io/#public-rest-api
*/
/**
* Returns all active markets
*
* See https://docs.ddex.io/#list-markets
*/
listMarkets(): Promise<Market[]>;
/**
* Returns a specific market
*
* See https://docs.ddex.io/#get-a-market
*
* @param marketId The id of the market, specified as a trading pair, e.g. "HOT-WETH"
*/
getMarket(marketId: string): Promise<Market>;
/**
* Returns tickers for all active markets
*
* See https://docs.ddex.io/#list-tickers
*/
listTickers(): Promise<Ticker[]>;
/**
* Returns ticker for a specific market
*
* See https://docs.ddex.io/#get-a-ticker
*
* @param marketId The id of the market, specified as a trading pair, e.g. "HOT-WETH"
*/
getTicker(marketId: string): Promise<Ticker>;
/**
* Returns the orderbook for a specific market
*
* See https://docs.ddex.io/#get-orderbook
*
* @param marketId The id of the market, specified as a trading pair, e.g. "HOT-WETH"
* @param level (Optional) The amount of detail returned in the orderbook. Default is level ONE.
*/
getOrderbook(marketId: string, level?: OrderbookLevel): Promise<Orderbook>;
/**
* Returns paginated trades for a specific market
*
* See https://docs.ddex.io/#get-trades
*
* @param marketId The id of the market, specified as a trading pair, e.g. "HOT-WETH"
* @param page (Optional) Which page to return. Default is page 1.
* @param perPage (Optional) How many results per page. Default is 20.
*/
listTrades(marketId: string, page?: number, perPage?: number): Promise<TradeList>;
/**
* Returns "candles" for building a trading chart for a specific market
*
* See https://docs.ddex.io/#get-candles
*
* @param marketId The id of the market, specified as a trading pair, e.g. "HOT-WETH"
* @param from The beginning of the time range as a UNIX timestamp
* @param to The end of the time range as a UNIX timestamp
* @param granularity The width of each candle in seconds
*/
listCandles(marketId: string, from: number, to: number, granularity: number): Promise<Candle[]>;
/**
* Calculate an estimated fee taken by the exchange given a price and amount for an order
*
* See https://docs.ddex.io/#calculate-fees
*
* @param marketId The id of the market, specified as a trading pair, e.g. "HOT-WETH"
* @param price The price of the order
* @param amount The amount of token in the order
*/
calculateFees(marketId: string, price: string, amount: string): Promise<Fee>;
/**
* Private API Calls
*
* These calls require authentication, meaning you must have a valid trading address
* and the ability to sign requests with that address' private key.
*
* See https://docs.ddex.io/#private-rest-api
*/
/**
* Build a new order to submit to the exchange
*
* See https://docs.ddex.io/#build-unsigned-order
*
* @param marketId The id of the market, specified as a trading pair, e.g. "HOT-WETH"
* @param side Whether this is a "buy" or "sell" order
* @param orderType Whether this is a "limit" or "market" order
* @param price The price of the order
* @param amount The amount of token in the order
* @param expires (Optional) A number of seconds after which this order will expire. Defaults to 0 (no expiration).
*/
buildOrder(marketId: string, side: Side, orderType: OrderType, price: string, amount: string, expires?: number): Promise<Order>;
/**
* Submit a signed order to the exchange
*
* See https://docs.ddex.io/#place-order
*
* @param orderId The id of a built order
* @param signature String created by signing the orderId
*/
placeOrder(orderId: string, signature: string): Promise<Order>;
/**
* A convenience function that will build an order, sign the order, and then
* immediately place the order on the system using the signing method passed
* in.
*
* @param marketId The id of the market, specified as a trading pair, e.g. "HOT-WETH"
* @param side Whether this is a "buy" or "sell" order
* @param orderType Whether this is a "limit" or "market" order
* @param price The price of the order
* @param amount The amount of token in the order
* @param expires (Optional) A number of seconds after which this order will expire. Defaults to 0 (no expiration).
*/
createOrder(marketId: string, side: Side, orderType: OrderType, price: string, amount: string, expires?: number): Promise<Order>;
/**
* Cancel an order you have submitted to the exchange
*
* See https://docs.ddex.io/#cancel-order
*
* @param orderId The id of the order you wish to cancel
*/
cancelOrder(orderId: string): Promise<void>;
/**
* Return paginated orders you have submitted to the exchange
*
* See https://docs.ddex.io/#list-orders
*
* @param marketId (Optional) The id of the market, specified as a trading pair, e.g. "HOT-WETH"
* @param status (Optional) Choose between "pending" or "all" orders
* @param page (Optional) Which page to return. Default is page 1.
* @param perPage (Optional) How many results per page. Default is 20.
*/
listOrders(marketId?: string, status?: Status, page?: number, perPage?: number): Promise<OrderList>;
/**
* Return a specific order you have submitted to the exchange
*
* See https://docs.ddex.io/#get-order
*
* @param orderId The id of the order
*/
getOrder(orderId: string): Promise<Order>;
/**
* Return paginated list of all trades you have made
*
* See https://docs.ddex.io/#list-account-trades
*
* @param marketId The id of the market, specified as a trading pair, e.g. "HOT-WETH"
* @param page (Optional) Which page to return. Default is page 1.
* @param perPage (Optional) How many results per page. Default is 20.
*/
listAccountTrades(marketId: string, page?: number, perPage?: number): Promise<TradeList>;
/**
* Return locked balances for each active token
*
* See https://docs.ddex.io/#list-locked-balances
*/
listLockedBalances(): Promise<LockedBalance[]>;
/**
* Return a specific locked balance
*
* See https://docs.ddex.io/#get-locked-balance
*
* @param symbol The symbol for the token you want to see your locked balance
*/
getLockedBalance(symbol: string): Promise<LockedBalance>;
/**
* Helper Methods (requires auth)
*
* These helper methods don't generally don't call the Hydro API, instead querying the blockchain
* directly. They are useful in helping to wrap/unwrap ETH on your account, and allowing you to
* approve tokens to be traded on the DDEX-1.0 relayer.
*
* To use these methods, you must provide a mainnet endpoint url, like infura, which will be used
* to interact with the blockchain. It is taken in as one of the HydroClient options, as web3_url.
*
* See
* * https://docs.ddex.io/#wrapping-ether
* * https://docs.ddex.io/#enabling-token-trading
*/
/**
* Query your balance of a token.
*
* @param symbol Symbol of a token you wish to query the balance of. No token returns ETH balance.
* @return Balance in your account for this token.
*/
getBalance(symbol?: string): Promise<string>;
/**
* Wrap a specified amount of ETH from your account into WETH. This is required because the
* DDEX-1.0 relayer can only perform atomic trading between two ERC20 tokens, and unfortunately
* ETH itself does not conform to the ERC20 standard. ETH and WETH are always exchanged at a 1:1
* ratio, so you can wrap and unwrap ETH anytime you like with only the cost of gas.
*
* @param amount The amount of ETH to wrap
* @param wait If true, the promise will only resolve when the transaction is confirmed
* @return Transaction hash
*/
wrapEth(amount: string, wait?: boolean): Promise<string>;
/**
* Unwrap a specified amount of WETH from your account back into ETH.
*
* @param amount The amount of WETH to unwrap
* @param wait If true, the promise will only resolve when the transaction is confirmed
* @return Transaction hash
*/
unwrapEth(amount: string, wait?: boolean): Promise<string>;
/**
* Determine if this token has a proxy allowance set on the Hydro proxy contract.
*
* @param symbol Symbol of a token you wish to check if it is enabled or diabled for sale.
*/
isTokenEnabled(symbol: string): Promise<boolean>;
/**
* Enable token to be sold via Hydro API. This will allow the Hydro proxy contract to send tokens
* of this type on your behalf, allowing atomic trading of tokens between two parties.
*
* @param symbol Symbol of a token you wish to enable for sale via Hydro API
* @param wait If true, the promise will only resolve when the transaction is confirmed
* @return Transaction hash
*/
enableToken(symbol: string, wait?: boolean): Promise<string>;
/**
* Disable token to be sold via Hydro API. The Hydro proxy contract will no longer be able to send
* tokens of this type on your behalf.
*
* @param symbol Symbol of a token you wish to disable for sale via Hydro API
* @param wait If true, the promise will only resolve when the transaction is confirmed
* @return Transaction hash
*/
disableToken(symbol: string, wait?: boolean): Promise<string>;
private getTokenAddress;
}
export interface Transaction {
nonce?: number;
chainId?: number;
from?: string;
to?: string;
data?: string;
value?: string | number;
gas?: string | number;
gasPrice?: string | number;
}
export interface Account {
address: string;
sign(message: string): Promise<string>;
signTransaction(tx: Transaction): Promise<string>;
}