fix: fix fees calculation and add support for L1 fees for Optimism transactions (#19603)
Signed-off-by: Brian Sztamfater <brian@status.im>
This commit is contained in:
parent
3526355e23
commit
17afff272e
|
@ -4,23 +4,20 @@
|
|||
|
||||
(defn calculate-gas-fee
|
||||
[data]
|
||||
(let [gas-amount (money/bignumber (get data :gas-amount))
|
||||
gas-fees (get data :gas-fees)
|
||||
eip1559-enabled? (get gas-fees :eip1559-enabled)
|
||||
billion (money/bignumber "1000000000")]
|
||||
(if eip1559-enabled?
|
||||
(let [base-fee (money/bignumber (get gas-fees :base-fee))
|
||||
priority-fee (money/bignumber (get gas-fees :max-priority-fee-per-gas))
|
||||
fee-with-tip (money/bignumber (money/add base-fee priority-fee))
|
||||
total-gas-fee (money/mul gas-amount fee-with-tip)]
|
||||
(money/with-precision (money/div total-gas-fee billion) 10))
|
||||
(let [gas-price (money/bignumber (get gas-fees :gas-price))
|
||||
total-gas-fee (money/mul gas-amount gas-price)]
|
||||
(money/with-precision (money/div total-gas-fee billion) 10)))))
|
||||
(let [gas-amount (money/bignumber (get data :gas-amount))
|
||||
gas-fees (get data :gas-fees)
|
||||
eip1559-enabled? (get gas-fees :eip-1559-enabled)
|
||||
optimal-price-gwei (money/bignumber (if eip1559-enabled?
|
||||
(get gas-fees :max-fee-per-gas-medium)
|
||||
(get gas-fees :gas-price)))
|
||||
total-gas-fee-wei (money/mul (money/->wei :gwei optimal-price-gwei) gas-amount)
|
||||
l1-fee-wei (money/->wei :gwei (get gas-fees :l-1-gas-fee))]
|
||||
(money/add total-gas-fee-wei l1-fee-wei)))
|
||||
|
||||
(defn calculate-full-route-gas-fee
|
||||
"Sums all the routes fees in wei and then convert the total value to ether"
|
||||
[route]
|
||||
(reduce money/add (map calculate-gas-fee route)))
|
||||
(money/wei->ether (reduce money/add (map calculate-gas-fee route))))
|
||||
|
||||
(defn find-affordable-networks
|
||||
[{:keys [balances-per-chain input-value selected-networks disabled-chain-ids]}]
|
||||
|
|
|
@ -4,22 +4,75 @@
|
|||
[utils.money :as money]))
|
||||
|
||||
(deftest test-calculate-gas-fee
|
||||
(testing "Test calculate-gas-fee function with EIP-1559 enabled"
|
||||
(let [data-eip1559-enabled {:gas-amount "23487"
|
||||
:gas-fees {:base-fee "32.325296406"
|
||||
:max-priority-fee-per-gas "0.011000001"
|
||||
:eip1559-enabled true}}
|
||||
expected-eip1559-enabled-result (money/bignumber 0.0007594826)]
|
||||
(is (money/equal-to (utils/calculate-gas-fee data-eip1559-enabled)
|
||||
expected-eip1559-enabled-result)))
|
||||
(testing "EIP-1559 transaction without L1 fee"
|
||||
(let [data {:gas-amount "23487"
|
||||
:gas-fees {:max-fee-per-gas-medium "2.259274911"
|
||||
:eip-1559-enabled true
|
||||
:l-1-gas-fee "0"}}
|
||||
expected-result (money/bignumber "53063589834657")] ; This is in Wei
|
||||
(is (money/equal-to (utils/calculate-gas-fee data)
|
||||
expected-result))))
|
||||
|
||||
(testing "Test calculate-gas-fee function with EIP-1559 disabled"
|
||||
(let [data-eip1559-disabled {:gas-amount "23487"
|
||||
:gas-fees {:gas-price "32.375609968"
|
||||
:eip1559-enabled false}}
|
||||
expected-eip1559-disabled-result (money/bignumber 0.000760406)]
|
||||
(is (money/equal-to (utils/calculate-gas-fee data-eip1559-disabled)
|
||||
expected-eip1559-disabled-result))))))
|
||||
(testing "EIP-1559 transaction with L1 fee of 60,000 Gwei"
|
||||
(let [data {:gas-amount "23487"
|
||||
:gas-fees {:max-fee-per-gas-medium "2.259274911"
|
||||
:eip-1559-enabled true
|
||||
:l-1-gas-fee "60000"}}
|
||||
expected-result (money/bignumber "113063589834657")] ; Added 60,000 Gwei in Wei to the
|
||||
; previous result
|
||||
(is (money/equal-to (utils/calculate-gas-fee data)
|
||||
expected-result))))
|
||||
|
||||
(testing "Non-EIP-1559 transaction with specified gas price"
|
||||
(let [data {:gas-amount "23487"
|
||||
:gas-fees {:gas-price "2.872721089"
|
||||
:eip-1559-enabled false
|
||||
:l-1-gas-fee "0"}}
|
||||
expected-result (money/bignumber "67471600217343")] ; This is in Wei, for the specified
|
||||
; gas amount and price
|
||||
(is (money/equal-to (utils/calculate-gas-fee data)
|
||||
expected-result)))))
|
||||
|
||||
(deftest test-calculate-full-route-gas-fee
|
||||
(testing "Route with a single EIP-1559 transaction, no L1 fees"
|
||||
(let [route [{:gas-amount "23487"
|
||||
:gas-fees {:max-fee-per-gas-medium "2.259274911"
|
||||
:eip-1559-enabled true
|
||||
:l-1-gas-fee "0"}}]
|
||||
expected-result (money/bignumber "0.000053063589834657")] ; The Wei amount for the
|
||||
; transaction, converted to
|
||||
; Ether
|
||||
(is (money/equal-to (utils/calculate-full-route-gas-fee route)
|
||||
expected-result))))
|
||||
|
||||
(testing "Route with two EIP-1559 transactions, no L1 fees"
|
||||
(let [route [{:gas-amount "23487"
|
||||
:gas-fees {:max-fee-per-gas-medium "2.259274911"
|
||||
:eip-1559-enabled true
|
||||
:l-1-gas-fee "0"}}
|
||||
{:gas-amount "23487"
|
||||
:gas-fees {:max-fee-per-gas-medium "2.259274911"
|
||||
:eip-1559-enabled true
|
||||
:l-1-gas-fee "0"}}]
|
||||
expected-result (money/bignumber "0.000106127179669314")] ; Sum of both transactions' Wei
|
||||
; amounts, converted to Ether
|
||||
(is (money/equal-to (utils/calculate-full-route-gas-fee route)
|
||||
expected-result))))
|
||||
|
||||
(testing "Route with two EIP-1559 transactions, one with L1 fee of 60,000 Gwei"
|
||||
(let [route [{:gas-amount "23487"
|
||||
:gas-fees {:max-fee-per-gas-medium "2.259274911"
|
||||
:eip-1559-enabled true
|
||||
:l-1-gas-fee "0"}}
|
||||
{:gas-amount "23487"
|
||||
:gas-fees {:max-fee-per-gas-medium "2.259274911"
|
||||
:eip-1559-enabled true
|
||||
:l-1-gas-fee "60000"}}]
|
||||
expected-result (money/bignumber "0.000166127179669314")] ; Added 60,000 Gwei in Wei to
|
||||
; the previous total and
|
||||
; converted to Ether
|
||||
(is (money/equal-to (utils/calculate-full-route-gas-fee route)
|
||||
expected-result)))))
|
||||
|
||||
(deftest test-find-affordable-networks
|
||||
(testing "All networks affordable and selected, none disabled"
|
||||
|
|
|
@ -256,8 +256,9 @@
|
|||
(<= input-num-value 0)
|
||||
(> input-num-value (current-limit)))
|
||||
amount-text (str @input-value " " token-symbol)
|
||||
first-route (first route)
|
||||
native-currency-symbol (when-not confirm-disabled?
|
||||
(get-in route [:from :native-currency-symbol]))
|
||||
(get-in first-route [:from :native-currency-symbol]))
|
||||
native-token (when native-currency-symbol
|
||||
(rf/sub [:wallet/token-by-symbol
|
||||
native-currency-symbol]))
|
||||
|
|
Loading…
Reference in New Issue