Skip to content

Commit

Permalink
872 [1] EVM fee calculation (#890)
Browse files Browse the repository at this point in the history
* EVM online data provider interface

* getFeeRates roundRobin

* Thorchain estimation

* Decimal bugfix

* Update versions

* Deps updated
  • Loading branch information
0xp3gasus authored Nov 4, 2023
1 parent 450fd9a commit aba77c2
Show file tree
Hide file tree
Showing 21 changed files with 228 additions and 95 deletions.
6 changes: 6 additions & 0 deletions packages/xchain-avax/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# v0.3.4 (2023-11-02)

## Update

- Estimations can be done with data provider

# v0.3.3 (2023-10-26)

## Update
Expand Down
14 changes: 7 additions & 7 deletions packages/xchain-avax/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@xchainjs/xchain-avax",
"version": "0.3.3",
"version": "0.3.4",
"description": "Avax EVM client for XChainJS",
"keywords": [
"XChain",
Expand Down Expand Up @@ -35,20 +35,20 @@
"access": "public"
},
"devDependencies": {
"@xchainjs/xchain-client": "^0.15.1",
"@xchainjs/xchain-client": "^0.15.2",
"@xchainjs/xchain-crypto": "^0.3.0",
"@xchainjs/xchain-evm": "^0.3.4",
"@xchainjs/xchain-evm": "^0.3.5",
"@xchainjs/xchain-util": "^0.13.1",
"@xchainjs/xchain-evm-providers": "^0.1.1",
"@xchainjs/xchain-evm-providers": "^0.1.2",
"axios": "^1.3.6",
"ethers": "^5.7.2"
},
"peerDependencies": {
"@xchainjs/xchain-client": "^0.15.1",
"@xchainjs/xchain-client": "^0.15.2",
"@xchainjs/xchain-crypto": "^0.3.0",
"@xchainjs/xchain-evm": "^0.3.4",
"@xchainjs/xchain-evm": "^0.3.5",
"@xchainjs/xchain-util": "^0.13.1",
"@xchainjs/xchain-evm-providers": "^0.1.1",
"@xchainjs/xchain-evm-providers": "^0.1.2",
"axios": "^1.3.6",
"ethers": "^5.7.2"
}
Expand Down
6 changes: 6 additions & 0 deletions packages/xchain-bsc/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# v0.3.4 (2023-11-02)

## Update

- Estimations can be done with data provider

# v0.3.3 (2023-10-26)

## Update
Expand Down
14 changes: 7 additions & 7 deletions packages/xchain-bsc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@xchainjs/xchain-bsc",
"version": "0.3.3",
"version": "0.3.4",
"description": "Binance Smart Chain EVM client for XChainJS",
"keywords": [
"XChain",
Expand Down Expand Up @@ -35,20 +35,20 @@
"access": "public"
},
"devDependencies": {
"@xchainjs/xchain-client": "^0.15.1",
"@xchainjs/xchain-client": "^0.15.2",
"@xchainjs/xchain-crypto": "^0.3.0",
"@xchainjs/xchain-evm": "^0.3.4",
"@xchainjs/xchain-evm": "^0.3.5",
"@xchainjs/xchain-util": "^0.13.1",
"@xchainjs/xchain-evm-providers": "^0.1.1",
"@xchainjs/xchain-evm-providers": "^0.1.2",
"axios": "^1.3.6",
"ethers": "^5.7.2"
},
"peerDependencies": {
"@xchainjs/xchain-client": "^0.15.1",
"@xchainjs/xchain-client": "^0.15.2",
"@xchainjs/xchain-crypto": "^0.3.0",
"@xchainjs/xchain-evm": "^0.3.4",
"@xchainjs/xchain-evm": "^0.3.5",
"@xchainjs/xchain-util": "^0.13.1",
"@xchainjs/xchain-evm-providers": "^0.1.1",
"@xchainjs/xchain-evm-providers": "^0.1.2",
"axios": "^1.3.6",
"ethers": "^5.7.2"
}
Expand Down
6 changes: 6 additions & 0 deletions packages/xchain-client/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# v0.15.2 (2023-11-02)

## Update

- EVMOnlineDataProvider interface

# v0.15.1 (2023-10-26)

## Update
Expand Down
2 changes: 1 addition & 1 deletion packages/xchain-client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@xchainjs/xchain-client",
"version": "0.15.1",
"version": "0.15.2",
"license": "MIT",
"main": "lib/index.js",
"module": "lib/index.esm.js",
Expand Down
7 changes: 6 additions & 1 deletion packages/xchain-client/src/provider-types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Address, Asset } from '@xchainjs/xchain-util'

import { ExplorerProvider } from './explorer-provider'
import { Balance, Network, Tx, TxHash, TxHistoryParams, TxsPage } from './types'
import { Balance, FeeRates, Network, Tx, TxHash, TxHistoryParams, TxsPage } from './types'

export type Witness = {
value: number
Expand All @@ -27,6 +27,11 @@ export interface UtxoOnlineDataProvider extends OnlineDataProvider {
broadcastTx(txHex: string): Promise<TxHash>
}

export interface EvmOnlineDataProvider extends OnlineDataProvider {
getFeeRates(): Promise<FeeRates>
}

export type ExplorerProviders = Record<Network, ExplorerProvider>
export type OnlineDataProviders = Record<Network, OnlineDataProvider | undefined>
export type UtxoOnlineDataProviders = Record<Network, UtxoOnlineDataProvider | undefined>
export type EvmOnlineDataProviders = Record<Network, EvmOnlineDataProvider | undefined>
6 changes: 6 additions & 0 deletions packages/xchain-ethereum/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# v0.30.5 (2023-11-02)

## Update

- Estimations can be done with data provider

# v0.30.4 (2023-10-26)

## Update
Expand Down
55 changes: 53 additions & 2 deletions packages/xchain-ethereum/__e2e__/eth-client.e2e.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Balance, Network, TxType } from '@xchainjs/xchain-client'
import { ApproveParams, EstimateApproveParams, IsApprovedParams } from '@xchainjs/xchain-evm'
import { Balance, FeeOption, Network, TxType } from '@xchainjs/xchain-client'
import { ApproveParams, EstimateApproveParams, IsApprovedParams, Protocol } from '@xchainjs/xchain-evm'
import { Asset, assetAmount, assetToBase, assetToString } from '@xchainjs/xchain-util'

import Client from '../src/client'
Expand Down Expand Up @@ -207,4 +207,55 @@ describe('xchain-evm (Eth) Integration Tests', () => {
fail()
}
})
// Can not retrieve this info over testnet network, sepolia api in etherscan do not support gas Oracle endpoint and there is no testnet for thorchain
it('should estimate feeRates with default values', async () => {
try {
const feeRates = await clientTestnet.estimateGasPrices()
console.log({
[FeeOption.Average]: feeRates.average.amount().toString(),
[FeeOption.Fast]: feeRates.fast.amount().toString(),
[FeeOption.Fastest]: feeRates.fastest.amount().toString(),
})
expect(feeRates.fast.gte(feeRates.average)).toBe(true)
expect(feeRates.fastest.gte(feeRates.average)).toBe(true)
expect(feeRates.fastest.gte(feeRates.fast)).toBe(true)
} catch (err) {
console.error('ERR running test', err)
fail()
}
})
it('should estimate feeRates using data providers', async () => {
try {
const client = new Client({ ...defaultEthParams, network: Network.Mainnet })
const feeRates = await client.estimateGasPrices()
console.log({
[FeeOption.Average]: feeRates.average.amount().toString(),
[FeeOption.Fast]: feeRates.fast.amount().toString(),
[FeeOption.Fastest]: feeRates.fastest.amount().toString(),
})
expect(feeRates.fast.gte(feeRates.average)).toBe(true)
expect(feeRates.fastest.gte(feeRates.average)).toBe(true)
expect(feeRates.fastest.gte(feeRates.fast)).toBe(true)
} catch (err) {
console.error('ERR running test', err)
fail()
}
})
it('should estimate feeRates to interact with Thorchain', async () => {
try {
const client = new Client({ ...defaultEthParams, network: Network.Mainnet })
const feeRates = await client.estimateGasPrices(Protocol.THORCHAIN)
console.log({
[FeeOption.Average]: feeRates.average.amount().toString(),
[FeeOption.Fast]: feeRates.fast.amount().toString(),
[FeeOption.Fastest]: feeRates.fastest.amount().toString(),
})
expect(feeRates.fast.gte(feeRates.average)).toBe(true)
expect(feeRates.fastest.gte(feeRates.average)).toBe(true)
expect(feeRates.fastest.gte(feeRates.fast)).toBe(true)
} catch (err) {
console.error('ERR running test', err)
fail()
}
})
})
14 changes: 7 additions & 7 deletions packages/xchain-ethereum/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@xchainjs/xchain-ethereum",
"version": "0.30.4",
"version": "0.30.5",
"description": "Ethereum EVM client for XChainJS",
"keywords": [
"XChain",
Expand Down Expand Up @@ -35,20 +35,20 @@
"access": "public"
},
"devDependencies": {
"@xchainjs/xchain-client": "^0.15.1",
"@xchainjs/xchain-client": "^0.15.2",
"@xchainjs/xchain-crypto": "^0.3.0",
"@xchainjs/xchain-evm": "^0.3.4",
"@xchainjs/xchain-evm": "^0.3.5",
"@xchainjs/xchain-util": "^0.13.1",
"@xchainjs/xchain-evm-providers": "^0.1.1",
"@xchainjs/xchain-evm-providers": "^0.1.2",
"axios": "^1.3.6",
"ethers": "^5.7.2"
},
"peerDependencies": {
"@xchainjs/xchain-client": "^0.15.1",
"@xchainjs/xchain-client": "^0.15.2",
"@xchainjs/xchain-crypto": "^0.3.0",
"@xchainjs/xchain-evm": "^0.3.4",
"@xchainjs/xchain-evm": "^0.3.5",
"@xchainjs/xchain-util": "^0.13.1",
"@xchainjs/xchain-evm-providers": "^0.1.1",
"@xchainjs/xchain-evm-providers": "^0.1.2",
"axios": "^1.3.6",
"ethers": "^5.7.2"
}
Expand Down
6 changes: 6 additions & 0 deletions packages/xchain-evm-providers/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# v0.1.2 (2023-11-02)

## Update

- GetFeeRates

# v0.1.1 (2023-09-11)

## Update
Expand Down
8 changes: 4 additions & 4 deletions packages/xchain-evm-providers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@xchainjs/xchain-evm-providers",
"version": "0.1.1",
"version": "0.1.2",
"license": "MIT",
"main": "lib/index.js",
"module": "lib/index.esm.js",
Expand All @@ -27,18 +27,18 @@
"access": "public"
},
"devDependencies": {
"@xchainjs/xchain-client": "^0.15.1",
"@xchainjs/xchain-client": "^0.15.2",
"@xchainjs/xchain-crypto": "^0.3.0",
"@xchainjs/xchain-util": "^0.13.1",
"axios": "^1.3.6"
},
"peerDependencies": {
"@xchainjs/xchain-client": "^0.15.1",
"@xchainjs/xchain-client": "^0.15.2",
"@xchainjs/xchain-crypto": "^0.3.0",
"@xchainjs/xchain-util": "^0.13.1",
"axios": "^1.3.6"
},
"dependencies": {
"@supercharge/promise-pool": "^2.4.0"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import {
Balance,
FeeOption,
FeeType,
Fees,
OnlineDataProvider,
EvmOnlineDataProvider,
FeeRates,
Tx,
TxFrom,
TxHistoryParams,
Expand All @@ -25,7 +23,7 @@ import {
const AVAXChain: Chain = 'AVAX'
const AssetAVAX: Asset = { chain: AVAXChain, symbol: 'AVAX', ticker: 'AVAX', synth: false }

export class CovalentProvider implements OnlineDataProvider {
export class CovalentProvider implements EvmOnlineDataProvider {
private baseUrl = 'https://api.covalenthq.com'
private apiKey: string
private chainId: number
Expand Down Expand Up @@ -203,12 +201,8 @@ export class CovalentProvider implements OnlineDataProvider {
hash: '',
}
}
async getFees(): Promise<Fees> {
return {
[FeeOption.Average]: baseAmount(1),
[FeeOption.Fast]: baseAmount(1),
[FeeOption.Fastest]: baseAmount(1),
type: FeeType.PerByte,
}

getFeeRates(): Promise<FeeRates> {
throw new Error('Method not implemented.')
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,11 @@ export const getTxFromTokenTransaction = (tx: TokenTransactionInfo, chain: Chain
*/
export const getGasOracle = async (baseUrl: string, apiKey?: string): Promise<GasOracleResponse> => {
const url = baseUrl + '/api?module=gastracker&action=gasoracle'
const result = (await axios.get(url + getApiKeyQueryParameter(apiKey))).data.result

return (await axios.get(url + getApiKeyQueryParameter(apiKey))).data.result
if (typeof result === 'string') throw Error(`Can not retrieve gasOracle: ${result}`)

return result
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { Provider } from '@ethersproject/abstract-provider'
import { Balance, OnlineDataProvider, Tx, TxHistoryParams, TxsPage } from '@xchainjs/xchain-client'
import {
Balance,
EvmOnlineDataProvider,
FeeOption,
FeeRates,
Tx,
TxHistoryParams,
TxsPage,
} from '@xchainjs/xchain-client'
import { Address, Asset, Chain, assetToString, baseAmount } from '@xchainjs/xchain-util'
import axios from 'axios'
import { BigNumber, ethers } from 'ethers'
Expand All @@ -8,7 +16,7 @@ import erc20ABI from './erc20.json'
import * as etherscanAPI from './etherscan-api'
import { ERC20Tx, GetERC20TxsResponse } from './types'

export class EtherscanProvider implements OnlineDataProvider {
export class EtherscanProvider implements EvmOnlineDataProvider {
private provider: Provider
private baseUrl: string
private apiKey: string
Expand Down Expand Up @@ -184,4 +192,14 @@ export class EtherscanProvider implements OnlineDataProvider {

return tx
}

async getFeeRates(): Promise<FeeRates> {
const gasOracleResponse = await etherscanAPI.getGasOracle(this.baseUrl, this.apiKey)

return {
[FeeOption.Average]: Number(gasOracleResponse.SafeGasPrice) * 10 ** 9,
[FeeOption.Fast]: Number(gasOracleResponse.ProposeGasPrice) * 10 ** 9,
[FeeOption.Fastest]: Number(gasOracleResponse.FastGasPrice) * 10 ** 9,
}
}
}
6 changes: 6 additions & 0 deletions packages/xchain-evm/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# v0.3.5 (2023-11-02)

## Update

- estimateGasPrices can difference between protocols and non-protocol interactions

# v0.3.4 (2023-10-26)

## Update
Expand Down
Loading

0 comments on commit aba77c2

Please sign in to comment.