Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor duplicate function and renamed gas option type #8

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 23 additions & 29 deletions src/kulap.ts
Original file line number Diff line number Diff line change
@@ -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<Number> {
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
10 changes: 5 additions & 5 deletions src/types/TradeOptions.ts
Original file line number Diff line number Diff line change
@@ -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
}
}

2 changes: 1 addition & 1 deletion src/types/Web3.ts
Original file line number Diff line number Diff line change
@@ -30,7 +30,7 @@
// from?: string,
// ) => Promise<Web3Signature>;

export interface Options {
export interface GasOption {
readonly gasLimit: string;
readonly gasPrice: any;
}
5 changes: 4 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
@@ -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,
6 changes: 3 additions & 3 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -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"]
}