fix: routes ui for bridging (#19959)

Signed-off-by: Brian Sztamfater <brian@status.im>
This commit is contained in:
Brian Sztamfater 2024-05-20 08:07:35 -03:00 committed by GitHub
parent 29b02a544c
commit dc551031fe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 274 additions and 152 deletions

View File

@ -35,6 +35,7 @@
receiver-networks (get-in db [:wallet :ui :send :receiver-networks]) receiver-networks (get-in db [:wallet :ui :send :receiver-networks])
receiver-network-values (get-in db [:wallet :ui :send :receiver-network-values]) receiver-network-values (get-in db [:wallet :ui :send :receiver-network-values])
sender-network-values (get-in db [:wallet :ui :send :sender-network-values]) sender-network-values (get-in db [:wallet :ui :send :sender-network-values])
tx-type (get-in db [:wallet :ui :send :tx-type])
disabled-from-chain-ids (or (get-in db [:wallet :ui :send :disabled-from-chain-ids]) []) disabled-from-chain-ids (or (get-in db [:wallet :ui :send :disabled-from-chain-ids]) [])
token-decimals (if collectible 0 (:decimals token)) token-decimals (if collectible 0 (:decimals token))
native-token? (and token (= token-display-name "ETH")) native-token? (and token (= token-display-name "ETH"))
@ -61,6 +62,7 @@
:disabled-chain-ids disabled-from-chain-ids :disabled-chain-ids disabled-from-chain-ids
:receiver-networks receiver-networks :receiver-networks receiver-networks
:token-networks-ids token-networks-ids :token-networks-ids token-networks-ids
:tx-type tx-type
:receiver? false}) :receiver? false})
(send-utils/reset-network-amounts-to-zero (send-utils/reset-network-amounts-to-zero
sender-network-values)) sender-network-values))
@ -70,6 +72,7 @@
:disabled-chain-ids disabled-from-chain-ids :disabled-chain-ids disabled-from-chain-ids
:receiver-networks receiver-networks :receiver-networks receiver-networks
:token-networks-ids token-networks-ids :token-networks-ids token-networks-ids
:tx-type tx-type
:receiver? true}) :receiver? true})
(send-utils/reset-network-amounts-to-zero (send-utils/reset-network-amounts-to-zero
receiver-network-values)) receiver-network-values))
@ -343,17 +346,27 @@
token-networks-ids (when token (mapv #(:chain-id %) (:networks token))) token-networks-ids (when token (mapv #(:chain-id %) (:networks token)))
sender-network-values (when token-available-networks-for-suggested-routes sender-network-values (when token-available-networks-for-suggested-routes
(send-utils/loading-network-amounts (send-utils/loading-network-amounts
{:valid-networks token-available-networks-for-suggested-routes {:valid-networks (if (= transaction-type :tx/bridge)
(filter
#(not= bridge-to-chain-id %)
token-available-networks-for-suggested-routes)
token-available-networks-for-suggested-routes)
:disabled-chain-ids disabled-from-chain-ids :disabled-chain-ids disabled-from-chain-ids
:receiver-networks receiver-networks :receiver-networks receiver-networks
:token-networks-ids token-networks-ids :token-networks-ids token-networks-ids
:tx-type transaction-type
:receiver? false})) :receiver? false}))
receiver-network-values (when token-available-networks-for-suggested-routes receiver-network-values (when token-available-networks-for-suggested-routes
(send-utils/loading-network-amounts (send-utils/loading-network-amounts
{:valid-networks token-available-networks-for-suggested-routes {:valid-networks (if (= transaction-type :tx/bridge)
(filter
#(= bridge-to-chain-id %)
token-available-networks-for-suggested-routes)
token-available-networks-for-suggested-routes)
:disabled-chain-ids disabled-from-chain-ids :disabled-chain-ids disabled-from-chain-ids
:receiver-networks receiver-networks :receiver-networks receiver-networks
:token-networks-ids token-networks-ids :token-networks-ids token-networks-ids
:tx-type transaction-type
:receiver? true})) :receiver? true}))
request-params [transaction-type-param request-params [transaction-type-param
from-address from-address

View File

@ -310,7 +310,7 @@
:button-one-label (if should-try-again? :button-one-label (if should-try-again?
(i18n/label :t/try-again) (i18n/label :t/try-again)
button-one-label) button-one-label)
:button-one-props (merge button-one-props :button-one-props (merge (when-not should-try-again? button-one-props)
{:disabled? (and (not should-try-again?) confirm-disabled?) {:disabled? (and (not should-try-again?) confirm-disabled?)
:on-press (cond :on-press (cond
should-try-again? should-try-again?

View File

@ -94,7 +94,7 @@
network-amounts)) network-amounts))
(defn network-amounts (defn network-amounts
[{:keys [network-values disabled-chain-ids receiver-networks token-networks-ids receiver?]}] [{:keys [network-values disabled-chain-ids receiver-networks token-networks-ids tx-type receiver?]}]
(let [disabled-set (set disabled-chain-ids) (let [disabled-set (set disabled-chain-ids)
receiver-networks-set (set receiver-networks) receiver-networks-set (set receiver-networks)
network-values-keys (set (keys network-values)) network-values-keys (set (keys network-values))
@ -150,11 +150,12 @@
(vec)) (vec))
(and receiver? (and receiver?
routes-found? routes-found?
(< (count network-values-with-not-available-chains) available-networks-count)) (< (count network-values-with-not-available-chains) available-networks-count)
(not= tx-type :tx/bridge))
(conj {:type :add})))) (conj {:type :add}))))
(defn loading-network-amounts (defn loading-network-amounts
[{:keys [valid-networks disabled-chain-ids receiver-networks token-networks-ids receiver?]}] [{:keys [valid-networks disabled-chain-ids receiver-networks token-networks-ids tx-type receiver?]}]
(let [disabled-set (set disabled-chain-ids) (let [disabled-set (set disabled-chain-ids)
receiver-networks-set (set receiver-networks) receiver-networks-set (set receiver-networks)
receiver-networks-count (count receiver-networks) receiver-networks-count (count receiver-networks)
@ -164,11 +165,13 @@
(filter #(not (token-networks-ids-set %)) receiver-networks) (filter #(not (token-networks-ids-set %)) receiver-networks)
[]) [])
not-available-networks-set (set not-available-networks) not-available-networks-set (set not-available-networks)
valid-networks (concat valid-networks valid-networks (-> (concat valid-networks
disabled-chain-ids (when (not (and receiver? (= tx-type :tx/bridge)))
disabled-chain-ids)
(when receiver? (when receiver?
(filter #(not (valid-networks-set %)) (filter #(not (valid-networks-set %))
not-available-networks)))] not-available-networks)))
(distinct))]
(cond-> (->> valid-networks (cond-> (->> valid-networks
(map (map
(fn [chain-id] (fn [chain-id]
@ -181,16 +184,18 @@
(and (not receiver?) (contains? disabled-set chain-id)) :disabled)} (and (not receiver?) (contains? disabled-set chain-id)) :disabled)}
(and (not receiver?) (contains? disabled-set chain-id)) (and (not receiver?) (contains? disabled-set chain-id))
(assoc :total-amount (money/bignumber "0"))))) (assoc :total-amount (money/bignumber "0")))))
(filter
(fn [network-amount]
(or (and receiver?
(or (= tx-type :tx/bridge)
(contains? receiver-networks-set (:chain-id network-amount))))
(not receiver?))))
(sort-by (fn [network-amount] (sort-by (fn [network-amount]
(get network-priority-score (get network-priority-score
(network-utils/id->network (:chain-id network-amount))))) (network-utils/id->network (:chain-id network-amount)))))
(filter
(fn [network-amount]
(or (and receiver? (contains? receiver-networks-set (:chain-id network-amount)))
(and (not receiver?)
(not (contains? disabled-chain-ids (:chain-id network-amount)))))))
(vec)) (vec))
(and receiver? (< receiver-networks-count available-networks-count)) (conj {:type :add})))) (and receiver? (< receiver-networks-count available-networks-count) (not= tx-type :tx/bridge))
(conj {:type :add}))))
(defn network-links (defn network-links
[route from-values-by-chain to-values-by-chain] [route from-values-by-chain to-values-by-chain]

View File

@ -33,9 +33,9 @@
(deftest test-network-amounts-by-chain (deftest test-network-amounts-by-chain
(testing "Correctly calculates network amounts for transaction with native token" (testing "Correctly calculates network amounts for transaction with native token"
(let [route [{:amount-in "0xde0b6b3a7640000" (let [route [{:amount-in "0xde0b6b3a7640000"
:to {:chain-id "1"}} :to {:chain-id 1}}
{:amount-in "0xde0b6b3a7640000" {:amount-in "0xde0b6b3a7640000"
:to {:chain-id "2"}}] :to {:chain-id 10}}]
token-decimals 18 token-decimals 18
native-token? true native-token? true
receiver? true receiver? true
@ -43,17 +43,17 @@
:token-decimals token-decimals :token-decimals token-decimals
:native-token? native-token? :native-token? native-token?
:receiver? receiver?}) :receiver? receiver?})
expected {"1" (money/bignumber "1") expected {1 (money/bignumber "1")
"2" (money/bignumber "1")}] 10 (money/bignumber "1")}]
(doseq [[chain-id exp-value] expected] (doseq [[chain-id exp-value] expected]
(is (money/equal-to (get result chain-id) exp-value))))) (is (money/equal-to (get result chain-id) exp-value)))))
(testing (testing
"Correctly calculates network amounts for transaction with native token and multiple routes to same chain-id" "Correctly calculates network amounts for transaction with native token and multiple routes to same chain-id"
(let [route [{:amount-in "0xde0b6b3a7640000" (let [route [{:amount-in "0xde0b6b3a7640000"
:to {:chain-id "1"}} :to {:chain-id 1}}
{:amount-in "0xde0b6b3a7640000" {:amount-in "0xde0b6b3a7640000"
:to {:chain-id "1"}}] :to {:chain-id 1}}]
token-decimals 18 token-decimals 18
native-token? true native-token? true
receiver? true receiver? true
@ -61,15 +61,15 @@
:token-decimals token-decimals :token-decimals token-decimals
:native-token? native-token? :native-token? native-token?
:receiver? receiver?}) :receiver? receiver?})
expected {"1" (money/bignumber "2")}] expected {1 (money/bignumber "2")}]
(doseq [[chain-id exp-value] expected] (doseq [[chain-id exp-value] expected]
(is (money/equal-to (get result chain-id) exp-value))))) (is (money/equal-to (get result chain-id) exp-value)))))
(testing "Correctly calculates network amounts for transaction with non-native token" (testing "Correctly calculates network amounts for transaction with non-native token"
(let [route [{:amount-out "0x1e8480" (let [route [{:amount-out "0x1e8480"
:from {:chain-id "1"}} :from {:chain-id 1}}
{:amount-out "0x1e8480" {:amount-out "0x1e8480"
:from {:chain-id "2"}}] :from {:chain-id 10}}]
token-decimals 6 token-decimals 6
native-token? false native-token? false
receiver? false receiver? false
@ -77,20 +77,20 @@
:token-decimals token-decimals :token-decimals token-decimals
:native-token? native-token? :native-token? native-token?
:receiver? receiver?}) :receiver? receiver?})
expected {"1" (money/bignumber "2") expected {1 (money/bignumber "2")
"2" (money/bignumber "2")}] 10 (money/bignumber "2")}]
(doseq [[chain-id exp-value] expected] (doseq [[chain-id exp-value] expected]
(is (money/equal-to (get result chain-id) exp-value)))))) (is (money/equal-to (get result chain-id) exp-value))))))
(deftest test-network-values-for-ui (deftest test-network-values-for-ui
(testing "Sanitizes values correctly for display" (testing "Sanitizes values correctly for display"
(let [amounts {"1" (money/bignumber "0") (let [amounts {1 (money/bignumber "0")
"2" (money/bignumber "2.5") 10 (money/bignumber "2.5")
"3" (money/bignumber "0.005")} 42161 (money/bignumber "0.005")}
result (utils/network-values-for-ui amounts) result (utils/network-values-for-ui amounts)
expected {"1" "<0.01" expected {1 "<0.01"
"2" (money/bignumber "2.5") 10 (money/bignumber "2.5")
"3" (money/bignumber "0.005")}] 42161 (money/bignumber "0.005")}]
(doseq [[chain-id exp-value] expected] (doseq [[chain-id exp-value] expected]
(is #(or (= (get result chain-id) exp-value) (is #(or (= (get result chain-id) exp-value)
(money/equal-to (get result chain-id) exp-value))))))) (money/equal-to (get result chain-id) exp-value)))))))
@ -168,32 +168,32 @@
(deftest test-token-available-networks-for-suggested-routes (deftest test-token-available-networks-for-suggested-routes
(testing "Excludes disabled chain-ids correctly" (testing "Excludes disabled chain-ids correctly"
(let [balances-per-chain {"1" {:chain-id "1" :balance 100} (let [balances-per-chain {1 {:chain-id 1 :balance 100}
"10" {:chain-id "10" :balance 200} 10 {:chain-id 10 :balance 200}
"42161" {:chain-id "42161" :balance 300}} 42161 {:chain-id 42161 :balance 300}}
disabled-chain-ids ["10"] disabled-chain-ids [10]
expected ["1" "42161"]] expected [1 42161]]
(is (= expected (is (= expected
(utils/token-available-networks-for-suggested-routes {:balances-per-chain balances-per-chain (utils/token-available-networks-for-suggested-routes {:balances-per-chain balances-per-chain
:disabled-chain-ids :disabled-chain-ids
disabled-chain-ids}))))) disabled-chain-ids})))))
(testing "Returns all chains when no disabled chains are specified" (testing "Returns all chains when no disabled chains are specified"
(let [balances-per-chain {"1" {:chain-id "1" :balance 100} (let [balances-per-chain {1 {:chain-id 1 :balance 100}
"10" {:chain-id "10" :balance 200} 10 {:chain-id 10 :balance 200}
"42161" {:chain-id "42161" :balance 300}} 42161 {:chain-id 42161 :balance 300}}
disabled-chain-ids [] disabled-chain-ids []
expected ["1" "10" "42161"]] expected [1 10 42161]]
(is (= expected (is (= expected
(utils/token-available-networks-for-suggested-routes {:balances-per-chain balances-per-chain (utils/token-available-networks-for-suggested-routes {:balances-per-chain balances-per-chain
:disabled-chain-ids :disabled-chain-ids
disabled-chain-ids}))))) disabled-chain-ids})))))
(testing "Returns empty list when all chains are disabled" (testing "Returns empty list when all chains are disabled"
(let [balances-per-chain {"1" {:chain-id "1" :balance 100} (let [balances-per-chain {1 {:chain-id 1 :balance 100}
"10" {:chain-id "10" :balance 200} 10 {:chain-id 10 :balance 200}
"42161" {:chain-id "42161" :balance 300}} 42161 {:chain-id 42161 :balance 300}}
disabled-chain-ids ["1" "10" "42161"] disabled-chain-ids [1 10 42161]
expected []] expected []]
(is (= expected (is (= expected
(utils/token-available-networks-for-suggested-routes {:balances-per-chain balances-per-chain (utils/token-available-networks-for-suggested-routes {:balances-per-chain balances-per-chain
@ -201,9 +201,9 @@
disabled-chain-ids}))))) disabled-chain-ids})))))
(testing "Handles non-existent chain-ids gracefully" (testing "Handles non-existent chain-ids gracefully"
(let [balances-per-chain {"59144" {:chain-id "59144" :balance 400}} (let [balances-per-chain {59144 {:chain-id 59144 :balance 400}}
disabled-chain-ids ["1" "10" "42161"] disabled-chain-ids [1 10 42161]
expected ["59144"]] expected [59144]]
(is (= expected (is (= expected
(utils/token-available-networks-for-suggested-routes {:balances-per-chain balances-per-chain (utils/token-available-networks-for-suggested-routes {:balances-per-chain balances-per-chain
:disabled-chain-ids :disabled-chain-ids
@ -211,10 +211,10 @@
(deftest test-reset-network-amounts-to-zero (deftest test-reset-network-amounts-to-zero
(testing "Correctly resets loading network amounts to zero and changes type to default" (testing "Correctly resets loading network amounts to zero and changes type to default"
(let [network-amounts [{:chain-id "1" :total-amount (money/bignumber "100") :type :loading} (let [network-amounts [{:chain-id 1 :total-amount (money/bignumber "100") :type :loading}
{:chain-id "10" :total-amount (money/bignumber "200") :type :default}] {:chain-id 10 :total-amount (money/bignumber "200") :type :default}]
expected [{:chain-id "1" :total-amount (money/bignumber "0") :type :default} expected [{:chain-id 1 :total-amount (money/bignumber "0") :type :default}
{:chain-id "10" :total-amount (money/bignumber "200") :type :default}] {:chain-id 10 :total-amount (money/bignumber "200") :type :default}]
result (utils/reset-network-amounts-to-zero network-amounts) result (utils/reset-network-amounts-to-zero network-amounts)
comparisons (map #(map/deep-compare %1 %2) comparisons (map #(map/deep-compare %1 %2)
expected expected
@ -222,10 +222,10 @@
(is (every? identity comparisons)))) (is (every? identity comparisons))))
(testing "Leaves non-loading types unchanged" (testing "Leaves non-loading types unchanged"
(let [network-amounts [{:chain-id "1" :total-amount (money/bignumber "100") :type :default} (let [network-amounts [{:chain-id 1 :total-amount (money/bignumber "100") :type :default}
{:chain-id "10" :total-amount (money/bignumber "0") :type :disabled}] {:chain-id 10 :total-amount (money/bignumber "0") :type :disabled}]
expected [{:chain-id "1" :total-amount (money/bignumber "100") :type :default} expected [{:chain-id 1 :total-amount (money/bignumber "100") :type :default}
{:chain-id "10" :total-amount (money/bignumber "0") :type :disabled}] {:chain-id 10 :total-amount (money/bignumber "0") :type :disabled}]
result (utils/reset-network-amounts-to-zero network-amounts) result (utils/reset-network-amounts-to-zero network-amounts)
comparisons (map #(map/deep-compare %1 %2) comparisons (map #(map/deep-compare %1 %2)
expected expected
@ -242,10 +242,10 @@
(is (every? identity comparisons)))) (is (every? identity comparisons))))
(testing "Applies transformations to multiple loading entries" (testing "Applies transformations to multiple loading entries"
(let [network-amounts [{:chain-id "1" :total-amount (money/bignumber "100") :type :loading} (let [network-amounts [{:chain-id 1 :total-amount (money/bignumber "100") :type :loading}
{:chain-id "10" :total-amount (money/bignumber "200") :type :loading}] {:chain-id 10 :total-amount (money/bignumber "200") :type :loading}]
expected [{:chain-id "1" :total-amount (money/bignumber "0") :type :default} expected [{:chain-id 1 :total-amount (money/bignumber "0") :type :default}
{:chain-id "10" :total-amount (money/bignumber "0") :type :default}] {:chain-id 10 :total-amount (money/bignumber "0") :type :default}]
result (utils/reset-network-amounts-to-zero network-amounts) result (utils/reset-network-amounts-to-zero network-amounts)
comparisons (map #(map/deep-compare %1 %2) comparisons (map #(map/deep-compare %1 %2)
expected expected
@ -253,14 +253,14 @@
(is (every? identity comparisons)))) (is (every? identity comparisons))))
(testing "Mix of loading and non-loading types" (testing "Mix of loading and non-loading types"
(let [network-amounts [{:chain-id "1" :total-amount (money/bignumber "100") :type :loading} (let [network-amounts [{:chain-id 1 :total-amount (money/bignumber "100") :type :loading}
{:chain-id "10" :total-amount (money/bignumber "200") :type :default} {:chain-id 10 :total-amount (money/bignumber "200") :type :default}
{:chain-id "42161" :total-amount (money/bignumber "300") :type :loading} {:chain-id 42161 :total-amount (money/bignumber "300") :type :loading}
{:chain-id "59144" :total-amount (money/bignumber "0") :type :disabled}] {:chain-id 59144 :total-amount (money/bignumber "0") :type :disabled}]
expected [{:chain-id "1" :total-amount (money/bignumber "0") :type :default} expected [{:chain-id 1 :total-amount (money/bignumber "0") :type :default}
{:chain-id "10" :total-amount (money/bignumber "200") :type :default} {:chain-id 10 :total-amount (money/bignumber "200") :type :default}
{:chain-id "42161" :total-amount (money/bignumber "0") :type :default} {:chain-id 42161 :total-amount (money/bignumber "0") :type :default}
{:chain-id "59144" :total-amount (money/bignumber "0") :type :disabled}] {:chain-id 59144 :total-amount (money/bignumber "0") :type :disabled}]
result (utils/reset-network-amounts-to-zero network-amounts) result (utils/reset-network-amounts-to-zero network-amounts)
comparisons (map #(map/deep-compare %1 %2) comparisons (map #(map/deep-compare %1 %2)
expected expected
@ -269,42 +269,46 @@
(deftest test-network-amounts (deftest test-network-amounts
(testing "Handles disabled and receiver networks correctly when receiver? is true" (testing "Handles disabled and receiver networks correctly when receiver? is true"
(let [network-values {"1" (money/bignumber "100") (let [network-values {1 (money/bignumber "100")
"10" (money/bignumber "200")} 10 (money/bignumber "200")}
disabled-chain-ids ["1"] disabled-chain-ids [1]
receiver-networks ["10"] receiver-networks [10]
token-networks-ids ["1" "10" "42161"] token-networks-ids [1 10 42161]
receiver? true receiver? true
expected [{:chain-id "1" expected [{:chain-id 1
:total-amount (money/bignumber "100") :total-amount (money/bignumber "100")
:type :default} :type :default}
{:chain-id "10" {:chain-id 10
:total-amount (money/bignumber "200") :total-amount (money/bignumber "200")
:type :default} :type :default}
{:type :add}] {:type :add}]
tx-type :send
result (utils/network-amounts {:network-values network-values result (utils/network-amounts {:network-values network-values
:disabled-chain-ids disabled-chain-ids :disabled-chain-ids disabled-chain-ids
:receiver-networks receiver-networks :receiver-networks receiver-networks
:token-networks-ids token-networks-ids :token-networks-ids token-networks-ids
:tx-type tx-type
:receiver? receiver?})] :receiver? receiver?})]
(is (every? identity (map #(map/deep-compare %1 %2) expected result))))) (is (every? identity (map #(map/deep-compare %1 %2) expected result)))))
(testing "Adds default amount for non-disabled non-receiver networks when receiver? is false" (testing "Adds default amount for non-disabled non-receiver networks when receiver? is false"
(let [network-values {"1" (money/bignumber "100")} (let [network-values {1 (money/bignumber "100")}
disabled-chain-ids ["10"] disabled-chain-ids [10]
receiver-networks [] receiver-networks []
token-networks-ids ["1" "10" "42161"] token-networks-ids [1 10 42161]
receiver? false receiver? false
expected [{:chain-id "1" expected [{:chain-id 1
:total-amount (money/bignumber "100") :total-amount (money/bignumber "100")
:type :default} :type :default}
{:chain-id "10" {:chain-id 10
:total-amount (money/bignumber "0") :total-amount (money/bignumber "0")
:type :disabled}] :type :disabled}]
tx-type :send
result (utils/network-amounts {:network-values network-values result (utils/network-amounts {:network-values network-values
:disabled-chain-ids disabled-chain-ids :disabled-chain-ids disabled-chain-ids
:receiver-networks receiver-networks :receiver-networks receiver-networks
:token-networks-ids token-networks-ids :token-networks-ids token-networks-ids
:tx-type tx-type
:receiver? receiver?})] :receiver? receiver?})]
(is (every? identity (map #(map/deep-compare %1 %2) expected result))))) (is (every? identity (map #(map/deep-compare %1 %2) expected result)))))
@ -315,90 +319,139 @@
token-networks-ids [] token-networks-ids []
receiver? true receiver? true
expected [] expected []
tx-type :send
result (utils/network-amounts {:network-values network-values result (utils/network-amounts {:network-values network-values
:disabled-chain-ids disabled-chain-ids :disabled-chain-ids disabled-chain-ids
:receiver-networks receiver-networks :receiver-networks receiver-networks
:token-networks-ids token-networks-ids :token-networks-ids token-networks-ids
:tx-type tx-type
:receiver? receiver?})] :receiver? receiver?})]
(is (= expected result)))) (is (= expected result))))
(testing "Processes case with multiple network interactions" (testing "Processes case with multiple network interactions"
(let [network-values {"1" (money/bignumber "300") (let [network-values {1 (money/bignumber "300")
"10" (money/bignumber "400") 10 (money/bignumber "400")
"42161" (money/bignumber "500")} 42161 (money/bignumber "500")}
disabled-chain-ids ["1" "42161"] disabled-chain-ids [1 42161]
receiver-networks ["10"] receiver-networks [10]
token-networks-ids ["1" "10" "42161"] token-networks-ids [1 10 42161]
receiver? true receiver? true
expected [{:chain-id "1" expected [{:chain-id 1
:total-amount (money/bignumber "300") :total-amount (money/bignumber "300")
:type :default} :type :default}
{:chain-id "10" {:chain-id 10
:total-amount (money/bignumber "400") :total-amount (money/bignumber "400")
:type :default} :type :default}
{:chain-id "42161" {:chain-id 42161
:total-amount (money/bignumber "500") :total-amount (money/bignumber "500")
:type :default}] :type :default}]
tx-type :send
result (utils/network-amounts {:network-values network-values result (utils/network-amounts {:network-values network-values
:disabled-chain-ids disabled-chain-ids :disabled-chain-ids disabled-chain-ids
:receiver-networks receiver-networks :receiver-networks receiver-networks
:token-networks-ids token-networks-ids :token-networks-ids token-networks-ids
:tx-type tx-type
:receiver? receiver?})] :receiver? receiver?})]
(is (every? identity (map #(map/deep-compare %1 %2) expected result))))) (is (every? identity (map #(map/deep-compare %1 %2) expected result)))))
(testing "Does not assign :not-available type when receiver? is false" (testing "Does not assign :not-available type when receiver? is false"
(let [network-values {"1" (money/bignumber "100")} (let [network-values {1 (money/bignumber "100")}
disabled-chain-ids ["10"] disabled-chain-ids [10]
receiver-networks ["1"] receiver-networks [1]
token-networks-ids ["1" "10"] token-networks-ids [1 10]
receiver? false receiver? false
expected [{:chain-id "1" expected [{:chain-id 1
:total-amount (money/bignumber "100") :total-amount (money/bignumber "100")
:type :default} :type :default}
{:chain-id "10" {:chain-id 10
:total-amount (money/bignumber "0") :total-amount (money/bignumber "0")
:type :disabled}] :type :disabled}]
tx-type :send
result (utils/network-amounts {:network-values network-values result (utils/network-amounts {:network-values network-values
:disabled-chain-ids disabled-chain-ids :disabled-chain-ids disabled-chain-ids
:receiver-networks receiver-networks :receiver-networks receiver-networks
:token-networks-ids token-networks-ids :token-networks-ids token-networks-ids
:tx-type tx-type
:receiver? receiver?})] :receiver? receiver?})]
(is (every? identity (map #(map/deep-compare %1 %2) expected result))))) (is (every? identity (map #(map/deep-compare %1 %2) expected result)))))
(testing (testing
"Assigns :not-available type to networks not available in token-networks-ids when receiver? is true" "Assigns :not-available type to networks not available in token-networks-ids when receiver? is true"
(let [network-values {"1" (money/bignumber "100")} (let [network-values {1 (money/bignumber "100")}
disabled-chain-ids [] disabled-chain-ids []
receiver-networks ["1" "10"] receiver-networks [1 10]
token-networks-ids ["1"] token-networks-ids [1]
receiver? false receiver? false
expected [{:chain-id "1" expected [{:chain-id 1
:total-amount (money/bignumber "100") :total-amount (money/bignumber "100")
:type :default} :type :default}
{:chain-id "10" {:chain-id 10
:total-amount nil :total-amount nil
:type :not-available}] :type :not-available}]
tx-type :send
result (utils/network-amounts {:network-values network-values result (utils/network-amounts {:network-values network-values
:disabled-chain-ids disabled-chain-ids :disabled-chain-ids disabled-chain-ids
:receiver-networks receiver-networks :receiver-networks receiver-networks
:token-networks-ids token-networks-ids :token-networks-ids token-networks-ids
:tx-type tx-type
:receiver? receiver?})]
(is (every? identity (map #(map/deep-compare %1 %2) expected result)))))
(testing "Handles disabled and receiver networks correctly when to? is false and tx-type is :tx/bridge"
(let [network-values {10 (money/bignumber "200")}
disabled-chain-ids [1]
receiver-networks [10]
token-networks-ids [1 10]
tx-type :tx/bridge
receiver? false
expected [{:chain-id 1
:total-amount (money/bignumber "0")
:type :disabled}
{:chain-id 10
:total-amount (money/bignumber "200")
:type :default}]
result (utils/network-amounts {:network-values network-values
:disabled-chain-ids disabled-chain-ids
:receiver-networks receiver-networks
:token-networks-ids token-networks-ids
:tx-type tx-type
:receiver? receiver?})]
(is (every? identity (map #(map/deep-compare %1 %2) expected result)))))
(testing "Handles disabled and receiver networks correctly when to? is true and tx-type is :tx/bridge"
(let [network-values {10 (money/bignumber "200")}
disabled-chain-ids [1]
receiver-networks [10]
token-networks-ids [1 10]
tx-type :tx/bridge
receiver? true
expected [{:chain-id 10
:total-amount (money/bignumber "200")
:type :default}]
result (utils/network-amounts {:network-values network-values
:disabled-chain-ids disabled-chain-ids
:receiver-networks receiver-networks
:token-networks-ids token-networks-ids
:tx-type tx-type
:receiver? receiver?})] :receiver? receiver?})]
(is (every? identity (map #(map/deep-compare %1 %2) expected result)))))) (is (every? identity (map #(map/deep-compare %1 %2) expected result))))))
(deftest test-loading-network-amounts (deftest test-loading-network-amounts
(testing "Assigns :loading type to valid networks except for disabled ones" (testing "Assigns :loading type to valid networks except for disabled ones"
(let [valid-networks ["1" "10" "42161"] (let [valid-networks [1 10 42161]
disabled-chain-ids ["42161"] disabled-chain-ids [42161]
receiver-networks ["1" "10"] receiver-networks [1 10]
token-networks-ids ["1" "10" "42161"] token-networks-ids [1 10 42161]
receiver? true receiver? true
expected [{:chain-id "1" :type :loading} expected [{:chain-id 1 :type :loading}
{:chain-id "10" :type :loading} {:chain-id 10 :type :loading}
{:type :add}] {:type :add}]
tx-type :send
result (utils/loading-network-amounts {:valid-networks valid-networks result (utils/loading-network-amounts {:valid-networks valid-networks
:disabled-chain-ids disabled-chain-ids :disabled-chain-ids disabled-chain-ids
:receiver-networks receiver-networks :receiver-networks receiver-networks
:token-networks-ids token-networks-ids :token-networks-ids token-networks-ids
:tx-type tx-type
:receiver? receiver?}) :receiver? receiver?})
comparisons (map #(map/deep-compare %1 %2) comparisons (map #(map/deep-compare %1 %2)
expected expected
@ -406,18 +459,20 @@
(is (every? identity comparisons)))) (is (every? identity comparisons))))
(testing "Assigns :disabled type with zero total-amount to disabled networks when receiver? is false" (testing "Assigns :disabled type with zero total-amount to disabled networks when receiver? is false"
(let [valid-networks ["1" "10" "42161"] (let [valid-networks [1 10 42161]
disabled-chain-ids ["10" "42161"] disabled-chain-ids [10 42161]
receiver-networks ["1"] receiver-networks [1]
token-networks-ids ["1" "10" "42161"] token-networks-ids [1 10 42161]
receiver? false receiver? false
expected [{:chain-id "1" :type :loading} expected [{:chain-id 1 :type :loading}
{:chain-id "10" :type :disabled :total-amount (money/bignumber "0")} {:chain-id 10 :type :disabled :total-amount (money/bignumber "0")}
{:chain-id "42161" :type :disabled :total-amount (money/bignumber "0")}] {:chain-id 42161 :type :disabled :total-amount (money/bignumber "0")}]
tx-type :send
result (utils/loading-network-amounts {:valid-networks valid-networks result (utils/loading-network-amounts {:valid-networks valid-networks
:disabled-chain-ids disabled-chain-ids :disabled-chain-ids disabled-chain-ids
:receiver-networks receiver-networks :receiver-networks receiver-networks
:token-networks-ids token-networks-ids :token-networks-ids token-networks-ids
:tx-type tx-type
:receiver? receiver?}) :receiver? receiver?})
comparisons (map #(map/deep-compare %1 %2) comparisons (map #(map/deep-compare %1 %2)
expected expected
@ -425,17 +480,19 @@
(is (every? identity comparisons)))) (is (every? identity comparisons))))
(testing "Filters out networks not in receiver networks when receiver? is true" (testing "Filters out networks not in receiver networks when receiver? is true"
(let [valid-networks ["1" "10" "42161" "59144"] (let [valid-networks [1 10 42161 59144]
disabled-chain-ids ["10"] disabled-chain-ids [10]
receiver-networks ["1" "42161"] receiver-networks [1 42161]
token-networks-ids ["1" "10" "42161"] token-networks-ids [1 10 42161]
receiver? true receiver? true
expected [{:chain-id "1" :type :loading} expected [{:chain-id 1 :type :loading}
{:chain-id "42161" :type :loading}] {:chain-id 42161 :type :loading}]
tx-type :send
result (utils/loading-network-amounts {:valid-networks valid-networks result (utils/loading-network-amounts {:valid-networks valid-networks
:disabled-chain-ids disabled-chain-ids :disabled-chain-ids disabled-chain-ids
:receiver-networks receiver-networks :receiver-networks receiver-networks
:token-networks-ids token-networks-ids :token-networks-ids token-networks-ids
:tx-type tx-type
:receiver? receiver?}) :receiver? receiver?})
comparisons (map #(map/deep-compare %1 %2) comparisons (map #(map/deep-compare %1 %2)
expected expected
@ -444,17 +501,19 @@
(testing (testing
"Appends :add type if receiver network count is less than available networks and receiver? is true" "Appends :add type if receiver network count is less than available networks and receiver? is true"
(let [valid-networks ["1" "10" "42161"] (let [valid-networks [1 10 42161]
disabled-chain-ids ["10"] disabled-chain-ids [10]
receiver-networks ["1"] receiver-networks [1]
token-networks-ids ["1" "10" "42161"] token-networks-ids [1 10 42161]
receiver? true receiver? true
expected [{:chain-id "1" :type :loading} expected [{:chain-id 1 :type :loading}
{:type :add}] {:type :add}]
tx-type :send
result (utils/loading-network-amounts {:valid-networks valid-networks result (utils/loading-network-amounts {:valid-networks valid-networks
:disabled-chain-ids disabled-chain-ids :disabled-chain-ids disabled-chain-ids
:receiver-networks receiver-networks :receiver-networks receiver-networks
:token-networks-ids token-networks-ids :token-networks-ids token-networks-ids
:tx-type tx-type
:receiver? receiver?}) :receiver? receiver?})
comparisons (map #(map/deep-compare %1 %2) comparisons (map #(map/deep-compare %1 %2)
expected expected
@ -463,16 +522,18 @@
(testing (testing
"Assigns :not-available type to networks not available in token-networks-ids when receiver? is false" "Assigns :not-available type to networks not available in token-networks-ids when receiver? is false"
(let [valid-networks ["42161"] (let [valid-networks [42161]
disabled-chain-ids [] disabled-chain-ids []
receiver-networks ["1"] receiver-networks [1]
token-networks-ids ["42161"] token-networks-ids [42161]
receiver? false receiver? false
expected [{:chain-id "42161" :type :loading}] expected [{:chain-id 42161 :type :loading}]
tx-type :send
result (utils/loading-network-amounts {:valid-networks valid-networks result (utils/loading-network-amounts {:valid-networks valid-networks
:disabled-chain-ids disabled-chain-ids :disabled-chain-ids disabled-chain-ids
:receiver-networks receiver-networks :receiver-networks receiver-networks
:token-networks-ids token-networks-ids :token-networks-ids token-networks-ids
:tx-type tx-type
:receiver? receiver?}) :receiver? receiver?})
comparisons (map #(map/deep-compare %1 %2) comparisons (map #(map/deep-compare %1 %2)
expected expected
@ -481,17 +542,60 @@
(testing (testing
"Assigns :not-available type to networks not available in token-networks-ids when receiver? is true" "Assigns :not-available type to networks not available in token-networks-ids when receiver? is true"
(let [valid-networks ["42161"] (let [valid-networks [42161]
disabled-chain-ids [] disabled-chain-ids []
receiver-networks ["1"] receiver-networks [1]
token-networks-ids ["42161"] token-networks-ids [42161]
receiver? true receiver? true
expected [{:chain-id "1" :type :not-available} expected [{:chain-id 1 :type :not-available}
{:type :add}] {:type :add}]
tx-type :send
result (utils/loading-network-amounts {:valid-networks valid-networks result (utils/loading-network-amounts {:valid-networks valid-networks
:disabled-chain-ids disabled-chain-ids :disabled-chain-ids disabled-chain-ids
:receiver-networks receiver-networks :receiver-networks receiver-networks
:token-networks-ids token-networks-ids :token-networks-ids token-networks-ids
:tx-type tx-type
:receiver? receiver?})
comparisons (map #(map/deep-compare %1 %2)
expected
result)]
(is (every? identity comparisons))))
(testing
"Assigns :loading type to valid networks and :disabled for disabled ones when tx-type is :tx/bridge and to? false"
(let [valid-networks [1 10]
disabled-chain-ids [10]
receiver-networks []
token-networks-ids [1 10]
tx-type :tx/bridge
receiver? false
expected [{:chain-id 1 :type :loading}
{:chain-id 10 :type :disabled :total-amount (money/bignumber "0")}]
result (utils/loading-network-amounts {:valid-networks valid-networks
:disabled-chain-ids disabled-chain-ids
:receiver-networks receiver-networks
:token-networks-ids token-networks-ids
:tx-type tx-type
:receiver? receiver?})
comparisons (map #(map/deep-compare %1 %2)
expected
result)]
(is (every? identity comparisons))))
(testing
"Assigns :loading type to valid networks, ignore disabled ones and do not add {:type :add} when tx-type is :tx/bridge and to? true"
(let [valid-networks [1]
disabled-chain-ids [10]
receiver-networks []
token-networks-ids [1 10]
tx-type :tx/bridge
receiver? true
expected [{:chain-id 1 :type :loading}]
result (utils/loading-network-amounts {:valid-networks valid-networks
:disabled-chain-ids disabled-chain-ids
:receiver-networks receiver-networks
:token-networks-ids token-networks-ids
:tx-type tx-type
:receiver? receiver?}) :receiver? receiver?})
comparisons (map #(map/deep-compare %1 %2) comparisons (map #(map/deep-compare %1 %2)
expected expected
@ -500,22 +604,22 @@
(deftest test-network-links (deftest test-network-links
(testing "Calculates position differences correctly" (testing "Calculates position differences correctly"
(let [route [{:from {:chain-id "1"} :to {:chain-id "42161"}} (let [route [{:from {:chain-id 1} :to {:chain-id 42161}}
{:from {:chain-id "10"} :to {:chain-id "1"}} {:from {:chain-id 10} :to {:chain-id 1}}
{:from {:chain-id "42161"} :to {:chain-id "10"}}] {:from {:chain-id 42161} :to {:chain-id 10}}]
from-values-by-chain [{:chain-id "1"} {:chain-id "10"} {:chain-id "42161"}] from-values-by-chain [{:chain-id 1} {:chain-id 10} {:chain-id 42161}]
to-values-by-chain [{:chain-id "42161"} {:chain-id "1"} {:chain-id "10"}] to-values-by-chain [{:chain-id 42161} {:chain-id 1} {:chain-id 10}]
expected [{:from-chain-id "1" :to-chain-id "42161" :position-diff 0} expected [{:from-chain-id 1 :to-chain-id 42161 :position-diff 0}
{:from-chain-id "10" :to-chain-id "1" :position-diff 0} {:from-chain-id 10 :to-chain-id 1 :position-diff 0}
{:from-chain-id "42161" :to-chain-id "10" :position-diff 0}] {:from-chain-id 42161 :to-chain-id 10 :position-diff 0}]
result (utils/network-links route from-values-by-chain to-values-by-chain)] result (utils/network-links route from-values-by-chain to-values-by-chain)]
(is (= expected result)))) (is (= expected result))))
(testing "Handles cases with no position difference" (testing "Handles cases with no position difference"
(let [route [{:from {:chain-id "1"} :to {:chain-id "1"}}] (let [route [{:from {:chain-id 1} :to {:chain-id 1}}]
from-values-by-chain [{:chain-id "1"} {:chain-id "10"} {:chain-id "42161"}] from-values-by-chain [{:chain-id 1} {:chain-id 10} {:chain-id 42161}]
to-values-by-chain [{:chain-id "1"} {:chain-id "10"} {:chain-id "42161"}] to-values-by-chain [{:chain-id 1} {:chain-id 10} {:chain-id 42161}]
expected [{:from-chain-id "1" :to-chain-id "1" :position-diff 0}] expected [{:from-chain-id 1 :to-chain-id 1 :position-diff 0}]
result (utils/network-links route from-values-by-chain to-values-by-chain)] result (utils/network-links route from-values-by-chain to-values-by-chain)]
(is (= expected result)))) (is (= expected result))))
@ -528,9 +632,9 @@
(is (= expected result)))) (is (= expected result))))
(testing "Verifies negative position differences" (testing "Verifies negative position differences"
(let [route [{:from {:chain-id "1"} :to {:chain-id "42161"}}] (let [route [{:from {:chain-id 1} :to {:chain-id 42161}}]
from-values-by-chain [{:chain-id "1"} {:chain-id "10"} {:chain-id "42161"}] from-values-by-chain [{:chain-id 1} {:chain-id 10} {:chain-id 42161}]
to-values-by-chain [{:chain-id "1"} {:chain-id "10"} {:chain-id "42161"}] to-values-by-chain [{:chain-id 1} {:chain-id 10} {:chain-id 42161}]
expected [{:from-chain-id "1" :to-chain-id "42161" :position-diff -2}] expected [{:from-chain-id 1 :to-chain-id 42161 :position-diff -2}]
result (utils/network-links route from-values-by-chain to-values-by-chain)] result (utils/network-links route from-values-by-chain to-values-by-chain)]
(is (= expected result))))) (is (= expected result)))))