Skip to content

Commit

Permalink
Merge pull request #16 from ParadigmFoundation/feature/gas-price/esti…
Browse files Browse the repository at this point in the history
…mation

Feature/gas price/estimation
  • Loading branch information
Henry Harder authored Sep 23, 2019
2 parents 2c62c62 + 7ff34dd commit 17a6329
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zaidan-dealer-client",
"version": "0.3.7",
"version": "0.3.8",
"main": "dist/index.js",
"repository": "https://github.com/ParadigmFoundation/zaidan-dealer-client",
"license": "MIT",
Expand Down
37 changes: 31 additions & 6 deletions src/DealerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { TransactionReceiptWithDecodedLogs } from "ethereum-types";
import Web3 from "web3";

import { DealerResponse, ERC20Token } from ".";
import { GasPriority } from "./types";

/**
* A simple client for the Zaidan dealer server.
Expand Down Expand Up @@ -71,6 +72,12 @@ export class DealerClient {
/** Default gas price to use for allowance transactions. */
public GAS_PRICE: BigNumber;

/**
* Transaction priority (according to ethgasstation.info API), defaults to
* fast.
*/
public txPriority: GasPriority;

/**
* Instantiate a new DealerClient. Prior to use, `client.init()` should
* be called, which triggers a prompt for the user to allow MetaMask to
Expand All @@ -82,9 +89,9 @@ export class DealerClient {
*
* @param dealerUri the base RPC API path for the dealer server
* @param web3Uri optional Ethereum JSONRPC url for server-side usage
* @param gasPrice optionally set the gas price for allowance transactions
* @param txPriority optionally set the gas price (via ethgasstation.info) as "fast", "average", or "safeLow"
*/
constructor(dealerUri: string, web3Uri?: string, gasPrice: number = 5) {
constructor(dealerUri: string, web3Uri?: string, txPriority: GasPriority = "fast") {
this.initialized = false;

this.web3 = null;
Expand All @@ -94,7 +101,7 @@ export class DealerClient {
this.networkId = null;
this.coinbase = null;

this.GAS_PRICE = new BigNumber(gasPrice);
this.txPriority = txPriority;
this.contractWrappers = null;

this.dealerUrl = new URL(dealerUri);
Expand Down Expand Up @@ -141,6 +148,8 @@ export class DealerClient {

this.erc20Token = new ERC20Token(this.contractWrappers.getProvider());
this.coinbase = await this.web3.eth.getCoinbase();

this.GAS_PRICE = await this._getGasPrice(this.txPriority);
this.pairs = await this._loadMarkets();
this.tokens = await this._loadAssets();
this.initialized = true;
Expand Down Expand Up @@ -353,7 +362,13 @@ export class DealerClient {
*/
public async setAllowance(tokenTicker: string): Promise<TransactionReceiptWithDecodedLogs> {
const tokenAddress = this._getAddress(tokenTicker);
const txId = await this.erc20Token.setUnlimitedProxyAllowanceAsync(tokenAddress, { from: this.coinbase });
const txId = await this.erc20Token.setUnlimitedProxyAllowanceAsync(
tokenAddress,
{
from: this.coinbase,
gasPrice: this.GAS_PRICE,
},
);
return this.web3Wrapper.awaitTransactionSuccessAsync(txId);
}

Expand Down Expand Up @@ -503,9 +518,9 @@ export class DealerClient {
}
}

private async _call(endpoint: string, method: "GET" | "POST", data?: any): Promise<any> {
private async _callAny(url: string, method: "GET" | "POST", data?: any): Promise<any> {
const response = await axios(
`${this.apiBase}/${endpoint}`,
url,
{
method,
headers: {
Expand All @@ -518,6 +533,10 @@ export class DealerClient {
return response.data;
}

private async _call(endpoint: string, method: "GET" | "POST", data?: any): Promise<any> {
return this._callAny(`${this.apiBase}/${endpoint}`, method, data);
}

private _getAddress(ticker: string): string {
const tokenAddress = this.tokens[ticker];
assert(tokenAddress, "unsupported token ticker");
Expand All @@ -528,6 +547,12 @@ export class DealerClient {
return this._call("markets", "GET");
}

private async _getGasPrice(priority: GasPriority): Promise<BigNumber> {
const gasPriceApi = "https://www.etherchain.org/api/gasPriceOracle";
const prices = await this._callAny(gasPriceApi, "GET");
return new BigNumber(prices[priority]);
}

private async _loadAssets(): Promise<any> {
return this._call("assets", "GET");
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { ERC20Token } from "./ERC20Token";
export { DealerClient } from "./DealerClient";
export { DealerResponse } from "./types";
export * from "./types";
export { SignedOrder } from "0x.js";
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ export interface DealerResponse {
/** The signed maker order from the dealer server. */
order: SignedOrder;
}

/**
* Gas price priority (as used in ETH Gas Station API).
*/
export type GasPriority = "safeLow" | "standard" | "fast";

0 comments on commit 17a6329

Please sign in to comment.