diff --git a/src/kulap.ts b/src/kulap.ts index a6e8f70..8e9d860 100644 --- a/src/kulap.ts +++ b/src/kulap.ts @@ -1,7 +1,7 @@ import axios, { AxiosError } from 'axios'; import Web3 from "web3" import { ethers } from 'ethers' -import { Network, Configuration, APIError, TradeOptions, Rate, Options } from "./types" +import { GasOption, Network, Configuration, APIError, TradeOption, TradeOptions, Rate, Options } from "./types" import { SUPPORTED_TOKENS, API_BASE_URL, KULAP_DEX_CONTRACT } from "./constants" import { kulapAbi } from "./abi" import { resolveContractAddress, resolveTokenDecimals, constructGasOptions, defaultGasOptions } from "./utils" @@ -26,6 +26,13 @@ export class Kulap { } } + private getGasOption(option: TradeOption): GasOption { + return { + gasLimit: `${option.gasLimit}`, + gasPrice: `${this.web3.utils.toWei(option.gasPrice, "gwei")}` + } + } + async getNetworkId(): Promise { try { return await this.web3.eth.net.getId() @@ -63,13 +70,6 @@ export class Kulap { } }) const tradeOptions: TradeOptions = response.data - - const reduceOption = (option: any) => { - return { - gasLimit: option.gasLimit, - gasPrice: `${this.web3.utils.toWei(option.gasPrice, "gwei")}` - } - } return { rate: tradeOptions["STD"].trade.rate, @@ -79,9 +79,9 @@ export class Kulap { toAmount: tradeOptions["STD"].trade.toAmount, toSymbol: targetToken, gasOptions: { - "FAST": reduceOption(tradeOptions["FAST"]), - "STD": reduceOption(tradeOptions["STD"]), - "SLOW": reduceOption(tradeOptions["SLOW"]) + "FAST": this.getGasOption(tradeOptions["FAST"]), + "STD": this.getGasOption(tradeOptions["STD"]), + "SLOW": this.getGasOption(tradeOptions["SLOW"]) } } } catch (e) { @@ -108,13 +108,6 @@ export class Kulap { } }) const tradeOptions: TradeOptions = response.data - - const reduceOption = (option: any) => { - return { - gasLimit: option.gasLimit, - gasPrice: `${this.web3.utils.toWei(option.gasPrice, "gwei")}` - } - } return { rate: tradeOptions["STD"].trade.rate, @@ -124,9 +117,9 @@ export class Kulap { toAmount: tradeOptions["STD"].trade.toAmount, toSymbol: targetToken, gasOptions: { - "FAST": reduceOption(tradeOptions["FAST"]), - "STD": reduceOption(tradeOptions["STD"]), - "SLOW": reduceOption(tradeOptions["SLOW"]) + "FAST": this.getGasOption(tradeOptions["FAST"]), + "STD": this.getGasOption(tradeOptions["STD"]), + "SLOW": this.getGasOption(tradeOptions["SLOW"]) } } } catch (e) { @@ -198,14 +191,15 @@ export class Kulap { // @ts-ignore const dexContract = new this.web3.eth.Contract(kulapAbi, KULAP_DEX_CONTRACT) const currentAccount = await this.getAccount() - let gasOptions = (options && options.gasOptions) ? constructGasOptions(options.gasOptions, rate) : defaultGasOptions(rate) - // Supply value when the source is native ETH - if (rate.fromSymbol === "ETH") { - gasOptions = { - value: rate.fromAmount, - ...gasOptions, - } + const gasOption = (options && options.gasOptions) ? constructGasOptions(options.gasOptions, rate) : defaultGasOptions(rate) + const payload = { + gasLimit: gasOption.gasLimit, + gasPrice: gasOption.gasPrice, } + + // Supply value when the source is native ETH + if (rate.fromSymbol === "ETH") Object.assign(payload, { value: rate.fromAmount }) + const tradingProxyIndex = rate.routes[0] const fromAddress = resolveContractAddress(rate.fromSymbol) const toAddress = resolveContractAddress(rate.toSymbol) @@ -227,7 +221,7 @@ export class Kulap { options && options.partnerId ? options.partnerId : 0 ).send({ from: currentAccount, - ...gasOptions + ...payload }) return tx diff --git a/src/types/TradeOptions.ts b/src/types/TradeOptions.ts index 779b83e..fd9badf 100644 --- a/src/types/TradeOptions.ts +++ b/src/types/TradeOptions.ts @@ -1,4 +1,4 @@ -import { Options } from "./Web3" +import { GasOption } from "./Web3" type Trade = { routes: number[] @@ -7,7 +7,7 @@ type Trade = { rate: string } -type TradeOption = { +export type TradeOption = { gasPrice: string gasLimit: number trade: Trade @@ -27,9 +27,9 @@ export type Rate = { toAmount: string toSymbol : string gasOptions: { - "FAST": Options, - "STD": Options, - "SLOW": Options + "FAST": GasOption, + "STD": GasOption, + "SLOW": GasOption } } diff --git a/src/types/Web3.ts b/src/types/Web3.ts index a3499a9..0d60641 100644 --- a/src/types/Web3.ts +++ b/src/types/Web3.ts @@ -30,7 +30,7 @@ // from?: string, // ) => Promise; -export interface Options { +export interface GasOption { readonly gasLimit: string; readonly gasPrice: any; } diff --git a/src/types/index.ts b/src/types/index.ts index 8ba22ad..f3e04a5 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,5 +1,6 @@ -import { TradeOptions, Rate } from "./TradeOptions" +import { GasOption } from './Web3' +import { TradeOption, TradeOptions, Rate } from "./TradeOptions" import { Configuration, Network } from "./Configuration" @@ -15,6 +16,8 @@ export type Options = { } export { + GasOption, + TradeOption, TradeOptions, Configuration, Network, diff --git a/src/utils/index.ts b/src/utils/index.ts index 6c6f1be..f5eeeaf 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,5 +1,5 @@ import { TOKEN_ADDRESSES, TOKEN_DECIMALS } from "../constants" -import { Options } from "../types/Web3" +import { GasOption } from "../types/Web3" import { Rate } from "../types" export const resolveContractAddress = (symbol: string): String => { @@ -12,7 +12,7 @@ export const resolveTokenDecimals = (symbol: string): Number => { return TOKEN_DECIMALS[symbol] ? TOKEN_DECIMALS[symbol] : 18 } -export const constructGasOptions = (option: string, rate: Rate): any => { +export const constructGasOptions = (option: string, rate: Rate): GasOption => { switch (option) { case "FAST": return rate.gasOptions["FAST"] @@ -23,6 +23,6 @@ export const constructGasOptions = (option: string, rate: Rate): any => { } } -export const defaultGasOptions = (rate: Rate): any => { +export const defaultGasOptions = (rate: Rate): GasOption => { return rate.gasOptions["FAST"] } \ No newline at end of file