diff --git a/src/status_im/constants.cljs b/src/status_im/constants.cljs index 65ee621db6..ff6ee0c212 100644 --- a/src/status_im/constants.cljs +++ b/src/status_im/constants.cljs @@ -201,6 +201,11 @@ (def ^:const web3-personal-sign "personal_sign") (def ^:const web3-get-logs "eth_getLogs") (def ^:const web3-transaction-receipt "eth_getTransactionReceipt") +(def ^:const web3-new-filter "eth_newFilter") +(def ^:const web3-new-pending-transaction-filter "eth_newPendingTransactionFilter") +(def ^:const web3-new-block-filter "eth_newBlockFilter") +(def ^:const web3-uninstall-filter "eth_uninstallFilter") +(def ^:const web3-get-filter-changes "eth_getFilterChanges") (def ^:const event-transfer-hash (ethereum/sha3 "Transfer(address,address,uint256)")) diff --git a/src/status_im/extensions/core.cljs b/src/status_im/extensions/core.cljs index dee2e0c0c3..59c21bc2f7 100644 --- a/src/status_im/extensions/core.cljs +++ b/src/status_im/extensions/core.cljs @@ -538,6 +538,24 @@ :value :extensions/ethereum-resolve-ens :arguments {:name :string :on-result :event}} + 'ethereum/create-filter + {:permissions [:read] + :value :extensions/ethereum-create-filter + :arguments {:filter-type :string + :from? :string + :to? :string + :address? :vector + :topics? :vector + :block-hash? :string + :on-result :event}} + 'ethereum/logs-changes + {:permissions [:read] + :value :extensions/ethereum-logs-changes + :arguments {:id :string}} + 'ethereum/cancel-filter + {:permissions [:read] + :value :extensions/ethereum-cancel-filter + :arguments {:id :string}} 'ethereum/call {:permissions [:read] :value :extensions/ethereum-call diff --git a/src/status_im/extensions/ethereum.cljs b/src/status_im/extensions/ethereum.cljs index 1b92246571..3643dd9ad7 100644 --- a/src/status_im/extensions/ethereum.cljs +++ b/src/status_im/extensions/ethereum.cljs @@ -222,3 +222,47 @@ :on-error [:extensions/wallet-ui-on-error on-result] :method constants/web3-personal-sign})} (navigation/navigate-to-cofx :wallet-sign-message-modal nil))))) + +;; poll logs implementation +(handlers/register-handler-fx + :extensions/ethereum-logs-changes + (fn [_ [_ _ {:keys [filterId on-result]}]] + (let [args {:jsonrpc "2.0" + :method constants/web3-get-filter-changes + :params [filterId]} + payload (types/clj->json args)] + (status/call-private-rpc payload #(let [{:keys [error result]} (types/json->clj %1) + response (merge {:result result} (when error {:error error}))] + (mapv (fn [one-result] + (re-frame/dispatch (on-result one-result))) result)))))) +(handlers/register-handler-fx + :extensions/ethereum-cancel-filter + (fn [_ [_ _ {:keys [filterId on-result]}]] + (let [args {:jsonrpc "2.0" + :method constants/web3-uninstall-filter + :params [filterId]} + payload (types/clj->json args)] + (status/call-private-rpc payload #(let [{:keys [error result]} (types/json->clj %1) + response (merge {:result result} (when error {:error error}))] + (re-frame/dispatch (on-result response))))))) + +(handlers/register-handler-fx + :extensions/ethereum-create-filter + (fn [_ [_ _ {:keys [filter-type from to address topics block-hash on-result]}]] + (let [parsed-topics (mapv parse-topic topics) + args (case filter-type + "filter" {:jsonrpc "2.0" + :method constants/web3-new-filter + :params [{:fromBlock (ensure-hex-bn from) + :toBlock (ensure-hex-bn to) + :address address + :topics parsed-topics + :blockhash block-hash}]} + "block" {:jsonrpc "2.0" + :method constants/web3-new-block-filter} + "pendingTransaction" {:jsonrpc "2.0" + :method constants/web3-new-pending-transaction-filter}) + payload (types/clj->json args)] + (status/call-private-rpc payload #(let [{:keys [error result]} (types/json->clj %1) + response (merge {:result result} (when error {:error error}))] + (re-frame/dispatch (on-result response)))))))