This repository has been archived by the owner on Nov 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 362
/
Copy pathethTransactions.ts
77 lines (62 loc) · 1.97 KB
/
ethTransactions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import axios from 'axios'
import { BigNumber } from 'bignumber.js'
import { getWeb3, web3ReadOnly } from 'src/logic/wallets/getWeb3'
import { getGasPrice, getGasPriceOracle } from 'src/config'
export const EMPTY_DATA = '0x'
export const checkReceiptStatus = async (hash) => {
if (!hash) {
return Promise.reject(new Error('No valid Tx hash to get receipt from'))
}
const txReceipt = await web3ReadOnly.eth.getTransactionReceipt(hash)
const { status }: any = txReceipt
if (!status) {
return Promise.reject(new Error('No status found on this transaction receipt'))
}
const hasError = status === '0x0'
if (hasError) {
return Promise.reject(new Error('Obtained a transaction failure in the receipt'))
}
return Promise.resolve()
}
export const calculateGasPrice = async (): Promise<string> => {
if (process.env.NODE_ENV === 'test') {
return '20000000000'
}
const gasPrice = getGasPrice()
const gasPriceOracle = getGasPriceOracle()
if (gasPrice) {
// Fixed gas price in configuration. xDai uses this approach
return new BigNumber(gasPrice).toString()
} else if (gasPriceOracle) {
const { url, gasParameter } = gasPriceOracle
// Fetch from gas price provider
const { data } = await axios.get(url)
return new BigNumber(data[gasParameter]).multipliedBy(1e8).toString()
} else {
const errorMsg = 'gasPrice or gasPriceOracle not set in config'
return Promise.reject(errorMsg)
}
}
export const calculateGasOf = async (txConfig: {
to: string
from: string
data: string
gasPrice?: number
gas?: number
}): Promise<number> => {
const web3 = getWeb3()
try {
const gas = await web3.eth.estimateGas(txConfig)
return gas * 2
} catch (err) {
return Promise.reject(err)
}
}
export const getUserNonce = async (userAddress: string): Promise<number> => {
const web3 = getWeb3()
try {
return await web3.eth.getTransactionCount(userAddress, 'pending')
} catch (error) {
return Promise.reject(error)
}
}