[#12819] Fix network fee calculation on xDai chain

This commit is contained in:
Roman Volosovskyi 2021-11-26 13:24:59 +02:00
parent a0430607d3
commit 469e082de1
No known key found for this signature in database
GPG Key ID: 0238A4B5ECEE70DE
1 changed files with 23 additions and 8 deletions

View File

@ -289,7 +289,9 @@
(fn [] (fn []
(json-rpc/call (json-rpc/call
{:method "eth_feeHistory" {:method "eth_feeHistory"
:params [101 "latest" nil] ;; NOTE(rasom): We don't need `reward` atm but if the last parameter is
;; `nil` request fails on some chains (particularly on xDai).
:params [101 "latest" [100]]
:on-success #(re-frame/dispatch [::header-fetched :on-success #(re-frame/dispatch [::header-fetched
(assoc params :fee-history %)])})) (assoc params :fee-history %)])}))
(fn [] (fn []
@ -341,31 +343,44 @@
(defn check-base-fee [{:keys [baseFeePerGas]}] (defn check-base-fee [{:keys [baseFeePerGas]}]
(let [all-base-fees (mapv money/bignumber baseFeePerGas) (let [all-base-fees (mapv money/bignumber baseFeePerGas)
current-base-fee (last all-base-fees) next-base-fee (peek all-base-fees)
previous-fees (subvec all-base-fees 0 101) previous-fees (subvec all-base-fees 0 101)
current-base-fee (peek previous-fees)
percentiles (calc-percentiles previous-fees [10 20 80])] percentiles (calc-percentiles previous-fees [10 20 80])]
{:normal-base-fee (money/to-hex {:normal-base-fee (money/to-hex
(recommended-base-fee (recommended-base-fee
current-base-fee next-base-fee
(get percentiles 20) (get percentiles 20)
(get percentiles 80))) (get percentiles 80)))
:slow-base-fee (money/to-hex :slow-base-fee (money/to-hex
(slow-base-fee (slow-base-fee
current-base-fee next-base-fee
(get percentiles 10))) (get percentiles 10)))
:fast-base-fee (money/to-hex :fast-base-fee (money/to-hex
(fast-base-fee (fast-base-fee
current-base-fee next-base-fee
(get percentiles 80))) (get percentiles 80)))
:current-base-fee (money/to-hex current-base-fee)})) :current-base-fee (money/to-hex current-base-fee)}))
(defn max-priority-fee-hex [gas-price base-fee]
(money/to-hex
(money/sub (money/bignumber gas-price)
(money/bignumber base-fee))))
(fx/defn header-fetched (fx/defn header-fetched
{:events [::header-fetched]} {:events [::header-fetched]}
[_ {:keys [error-callback success-callback fee-history] :as params}] [_ {:keys [error-callback success-callback fee-history] :as params}]
{::json-rpc/call {::json-rpc/call
[{:method "eth_maxPriorityFeePerGas" [;; NOTE(rasom): eth_maxPriorityFeePerGas is not supported by some networks
:on-success #(success-callback (merge {:max-priority-fee %} ;; so it is more reliable to calculate maxPriorityFeePerGas using the value
(check-base-fee fee-history))) ;; returned by eth_gasPrice and current base fee.
{:method "eth_gasPrice"
:on-success #(success-callback
(let [{:keys [current-base-fee] :as base-fees}
(check-base-fee fee-history)]
(merge {:max-priority-fee
(max-priority-fee-hex % current-base-fee)}
base-fees)))
:on-error (if error-callback :on-error (if error-callback
#(error-callback %) #(error-callback %)
#(log/error "Can't fetch header" %))}]}) #(log/error "Can't fetch header" %))}]})