@exodus/ethereum-api 6.3.28 → 6.3.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -3
- package/src/get-balances.js +27 -13
- package/src/staking/ethereum/service.js +15 -20
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@exodus/ethereum-api",
|
|
3
|
-
"version": "6.3.
|
|
3
|
+
"version": "6.3.30",
|
|
4
4
|
"description": "Ethereum Api",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"files": [
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"dependencies": {
|
|
17
17
|
"@exodus/asset-lib": "^3.7.1",
|
|
18
18
|
"@exodus/crypto": "^1.0.0-rc.0",
|
|
19
|
-
"@exodus/ethereum-lib": "^
|
|
19
|
+
"@exodus/ethereum-lib": "^4.0.0",
|
|
20
20
|
"@exodus/ethereumjs-util": "^7.1.0-exodus.6",
|
|
21
21
|
"@exodus/fetch": "^1.3.0-beta.4",
|
|
22
22
|
"@exodus/simple-retry": "^0.0.6",
|
|
@@ -34,5 +34,5 @@
|
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@exodus/models": "^8.10.4"
|
|
36
36
|
},
|
|
37
|
-
"gitHead": "
|
|
37
|
+
"gitHead": "44bf590d6ecef38d828b466687ad579c7528ea84"
|
|
38
38
|
}
|
package/src/get-balances.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { isRpcBalanceAsset } from '@exodus/ethereum-lib'
|
|
2
|
+
import { get } from 'lodash'
|
|
2
3
|
|
|
3
4
|
const fixBalance = ({ txLog, balance }) => {
|
|
4
5
|
for (const tx of txLog) {
|
|
@@ -14,25 +15,38 @@ const fixBalance = ({ txLog, balance }) => {
|
|
|
14
15
|
return balance
|
|
15
16
|
}
|
|
16
17
|
|
|
18
|
+
const getBalanceFromAccountState = ({ asset, accountState }) => {
|
|
19
|
+
const isBase = asset.name === asset.baseAsset.name
|
|
20
|
+
return get(
|
|
21
|
+
accountState,
|
|
22
|
+
isBase ? ['balance'] : ['tokenBalances', asset.name],
|
|
23
|
+
asset.currency.ZERO
|
|
24
|
+
)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const getBalanceFromTxLog = ({ txLog, asset }) => {
|
|
28
|
+
return txLog.size > 0 ? txLog.getMutations().slice(-1)[0].balance : asset.currency.ZERO
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const getBalance = ({ asset, accountState, txLog }) => {
|
|
32
|
+
const balance = isRpcBalanceAsset(asset)
|
|
33
|
+
? getBalanceFromAccountState({ asset, accountState })
|
|
34
|
+
: getBalanceFromTxLog({ txLog, asset })
|
|
35
|
+
|
|
36
|
+
const shouldFixBalance = isRpcBalanceAsset(asset)
|
|
37
|
+
|
|
38
|
+
return shouldFixBalance ? fixBalance({ txLog, balance }) : balance
|
|
39
|
+
}
|
|
40
|
+
|
|
17
41
|
/**
|
|
18
42
|
* Api method to return the balance based on either account state balances or tx history.
|
|
19
43
|
*
|
|
20
44
|
* @param asset the asset to get the balances
|
|
21
45
|
* @param txLog the txLog when the balance is transaction based
|
|
22
46
|
* @param accountState the account state when the balance is loaded from RPC
|
|
23
|
-
* @returns {{balance}|null} an object with the balance or null if the balance is unknown
|
|
47
|
+
* @returns {{balance}|null} an object with the balance or null if the balance is unknown
|
|
24
48
|
*/
|
|
25
49
|
export const getBalances = ({ asset, txLog, accountState }) => {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
asset.baseAsset.name === asset.name
|
|
29
|
-
? accountState?.balance
|
|
30
|
-
: accountState?.tokenBalances?.[asset.name]
|
|
31
|
-
return balance && !balance.isZero ? { balance: fixBalance({ txLog, balance }) } : null
|
|
32
|
-
}
|
|
33
|
-
return txLog.size
|
|
34
|
-
? {
|
|
35
|
-
balance: txLog.getMutations().slice(-1)[0].balance,
|
|
36
|
-
}
|
|
37
|
-
: null
|
|
50
|
+
const balance = getBalance({ asset, accountState, txLog })
|
|
51
|
+
return { balance }
|
|
38
52
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { estimateGasLimit, EthereumStaking, getServer } from '@exodus/ethereum-api'
|
|
2
2
|
import BN from 'bn.js'
|
|
3
3
|
|
|
4
|
-
const
|
|
4
|
+
const extraGasLimit = 150000 // extra gas Limit to prevent tx failing if something change on pool state (till tx is in mempool)
|
|
5
5
|
|
|
6
6
|
export function createEthereumStakingService({
|
|
7
7
|
asset,
|
|
@@ -143,30 +143,25 @@ export function createEthereumStakingService({
|
|
|
143
143
|
return
|
|
144
144
|
}
|
|
145
145
|
|
|
146
|
-
|
|
147
|
-
|
|
146
|
+
let amount, data, fee
|
|
147
|
+
try {
|
|
148
|
+
;({ amount, data } = await delegateOperation(args))
|
|
149
|
+
;({ fee } = await estimateTxFee(delegatorAddress, staking.poolAddress, amount, data))
|
|
150
|
+
} catch (e) {
|
|
151
|
+
// If the operation is to unstake and failed retry with unstakePending
|
|
152
|
+
if (operation === 'undelegate') {
|
|
153
|
+
;({ amount, data } = await staking.unstakePending(args))
|
|
154
|
+
;({ fee } = await estimateTxFee(delegatorAddress, staking.poolAddress, amount, data))
|
|
155
|
+
} else {
|
|
156
|
+
throw e
|
|
157
|
+
}
|
|
158
|
+
}
|
|
148
159
|
|
|
149
160
|
return fee
|
|
150
161
|
}
|
|
151
162
|
|
|
152
|
-
async function estimateUndelegateTxFee() {
|
|
153
|
-
const gasPrice = parseInt(await getServer(asset).gasPrice(), 16)
|
|
154
|
-
|
|
155
|
-
const gasLimit = delegateGas
|
|
156
|
-
const fee = new BN(gasLimit).mul(new BN(gasPrice))
|
|
157
|
-
return {
|
|
158
|
-
gasLimit,
|
|
159
|
-
gasPrice: asset.currency.baseUnit(gasPrice),
|
|
160
|
-
fee: asset.currency.baseUnit(fee.toString()),
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
163
|
async function estimateTxFee(from, to, amount, txInput, gasPrice = '0x0') {
|
|
165
164
|
amount = amount || asset.currency.ZERO
|
|
166
|
-
if (amount.isZero) {
|
|
167
|
-
// unstaking txs
|
|
168
|
-
return estimateUndelegateTxFee() // otherwise server estimateGasLimit will throw "execution reverted"
|
|
169
|
-
}
|
|
170
165
|
|
|
171
166
|
const gasLimit = await estimateGasLimit(
|
|
172
167
|
asset,
|
|
@@ -182,7 +177,7 @@ export function createEthereumStakingService({
|
|
|
182
177
|
}
|
|
183
178
|
|
|
184
179
|
gasPrice = parseInt(gasPrice, 16)
|
|
185
|
-
const fee = new BN(gasPrice).mul(new BN(gasLimit))
|
|
180
|
+
const fee = new BN(gasPrice).mul(new BN(gasLimit + extraGasLimit))
|
|
186
181
|
|
|
187
182
|
return {
|
|
188
183
|
gasLimit,
|