From 7ce414f35bad8869a5c5b4e454f9bde0adff560b Mon Sep 17 00:00:00 2001 From: Henry Harder Date: Mon, 23 Sep 2019 14:23:33 -0700 Subject: [PATCH 1/2] add remote gas price fetching --- src/DealerClient.ts | 37 +++++++++++++++++++++++++++++++------ src/index.ts | 2 +- src/types.ts | 5 +++++ 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/DealerClient.ts b/src/DealerClient.ts index 3ab14e4..5432178 100644 --- a/src/DealerClient.ts +++ b/src/DealerClient.ts @@ -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. @@ -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 @@ -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; @@ -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); @@ -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; @@ -353,7 +362,13 @@ export class DealerClient { */ public async setAllowance(tokenTicker: string): Promise { 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); } @@ -503,9 +518,9 @@ export class DealerClient { } } - private async _call(endpoint: string, method: "GET" | "POST", data?: any): Promise { + private async _callAny(url: string, method: "GET" | "POST", data?: any): Promise { const response = await axios( - `${this.apiBase}/${endpoint}`, + url, { method, headers: { @@ -518,6 +533,10 @@ export class DealerClient { return response.data; } + private async _call(endpoint: string, method: "GET" | "POST", data?: any): Promise { + return this._callAny(`${this.apiBase}/${endpoint}`, method, data); + } + private _getAddress(ticker: string): string { const tokenAddress = this.tokens[ticker]; assert(tokenAddress, "unsupported token ticker"); @@ -528,6 +547,12 @@ export class DealerClient { return this._call("markets", "GET"); } + private async _getGasPrice(priority: GasPriority): Promise { + const gasPriceApi = "https://www.etherchain.org/api/gasPriceOracle"; + const prices = await this._callAny(gasPriceApi, "GET"); + return new BigNumber(prices[priority]); + } + private async _loadAssets(): Promise { return this._call("assets", "GET"); } diff --git a/src/index.ts b/src/index.ts index 8d299f2..3b84317 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ export { ERC20Token } from "./ERC20Token"; export { DealerClient } from "./DealerClient"; -export { DealerResponse } from "./types"; +export * from "./types"; export { SignedOrder } from "0x.js"; diff --git a/src/types.ts b/src/types.ts index 16112f7..dd36f98 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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"; From ad8e81498316f2e90bc560371c1ea9571dd2d42a Mon Sep 17 00:00:00 2001 From: Henry Harder Date: Mon, 23 Sep 2019 14:23:43 -0700 Subject: [PATCH 2/2] bump --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d97921d..dcf0697 100644 --- a/package.json +++ b/package.json @@ -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",