|
8 | 8 | [status-im.signing.eip1559 :as eip1559]
|
9 | 9 | [taoensso.timbre :as log]
|
10 | 10 | [status-im.popover.core :as popover]
|
| 11 | + [status-im.ethereum.core :as ethereum] |
11 | 12 | [clojure.string :as string]))
|
12 | 13 |
|
13 | 14 | (def min-gas-price-wei ^js (money/bignumber 1))
|
|
318 | 319 | ;; fee might be very small and using this value might slow transaction
|
319 | 320 | (def minimum-base-fee (money/->wei :gwei (money/bignumber 1)))
|
320 | 321 |
|
321 |
| -(defn recommended-base-fee [current perc20 perc80] |
| 322 | +(defn recommended-base-fee [current perc20] |
322 | 323 | (let [fee
|
323 |
| - (cond (and (money/greater-than-or-equals current perc20) |
324 |
| - (money/greater-than-or-equals perc80 current)) |
| 324 | + (cond (money/greater-than-or-equals current perc20) |
325 | 325 | current
|
326 | 326 |
|
327 | 327 | (money/greater-than perc20 current)
|
328 |
| - perc20 |
329 |
| - |
330 |
| - :else |
331 |
| - perc80)] |
| 328 | + perc20)] |
332 | 329 | (if (money/greater-than minimum-base-fee fee)
|
333 | 330 | minimum-base-fee
|
334 | 331 | fee)))
|
335 | 332 |
|
336 | 333 | (defn slow-base-fee [_ perc10] perc10)
|
337 | 334 |
|
338 |
| -(defn fast-base-fee [current _] |
| 335 | +(defn fast-base-fee [current] |
339 | 336 | (let [fee (money/mul current 2)]
|
340 | 337 | (if (money/greater-than minimum-base-fee fee)
|
341 | 338 | (money/mul minimum-base-fee 2)
|
342 | 339 | fee)))
|
343 | 340 |
|
344 |
| -(defn check-base-fee [{:keys [baseFeePerGas]}] |
| 341 | +(defn check-base-fee [{:keys [baseFeePerGas testnet?]}] |
345 | 342 | (let [all-base-fees (mapv money/bignumber baseFeePerGas)
|
346 | 343 | next-base-fee (peek all-base-fees)
|
347 | 344 | previous-fees (subvec all-base-fees 0 101)
|
348 | 345 | current-base-fee (peek previous-fees)
|
349 | 346 | percentiles (calc-percentiles previous-fees [10 20 80])]
|
350 | 347 | {:normal-base-fee (money/to-hex
|
351 |
| - (recommended-base-fee |
352 |
| - next-base-fee |
353 |
| - (get percentiles 20) |
354 |
| - (get percentiles 80))) |
| 348 | + (if testnet? |
| 349 | + (fast-base-fee next-base-fee) |
| 350 | + (recommended-base-fee |
| 351 | + next-base-fee |
| 352 | + (get percentiles 20)))) |
355 | 353 | :slow-base-fee (money/to-hex
|
356 | 354 | (slow-base-fee
|
357 | 355 | next-base-fee
|
358 | 356 | (get percentiles 10)))
|
359 |
| - :fast-base-fee (money/to-hex |
360 |
| - (fast-base-fee |
361 |
| - next-base-fee |
362 |
| - (get percentiles 80))) |
| 357 | + :fast-base-fee (money/to-hex (fast-base-fee next-base-fee)) |
363 | 358 | :current-base-fee (money/to-hex current-base-fee)}))
|
364 | 359 |
|
365 | 360 | (defn max-priority-fee-hex [gas-price base-fee]
|
|
369 | 364 |
|
370 | 365 | (fx/defn header-fetched
|
371 | 366 | {:events [::header-fetched]}
|
372 |
| - [_ {:keys [error-callback success-callback fee-history] :as params}] |
| 367 | + [{{:networks/keys [current-network networks]} :db} |
| 368 | + {:keys [error-callback success-callback fee-history] :as params}] |
373 | 369 | {::json-rpc/call
|
374 | 370 | [;; NOTE(rasom): eth_maxPriorityFeePerGas is not supported by some networks
|
375 | 371 | ;; so it is more reliable to calculate maxPriorityFeePerGas using the value
|
376 | 372 | ;; returned by eth_gasPrice and current base fee.
|
377 | 373 | {:method "eth_gasPrice"
|
378 | 374 | :on-success #(success-callback
|
379 | 375 | (let [{:keys [current-base-fee] :as base-fees}
|
380 |
| - (check-base-fee fee-history)] |
| 376 | + (check-base-fee |
| 377 | + (assoc fee-history |
| 378 | + :testnet? (ethereum/testnet? |
| 379 | + (get-in networks [current-network :config :NetworkId]))))] |
381 | 380 | (merge {:max-priority-fee
|
382 | 381 | (max-priority-fee-hex % current-base-fee)}
|
383 | 382 | base-fees)))
|
|
0 commit comments