Skip to content

Commit

Permalink
request wallet connection if trigger tx but not connected
Browse files Browse the repository at this point in the history
  • Loading branch information
ginesdt committed Jun 20, 2024
1 parent 4046c36 commit e20461d
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 105 deletions.
3 changes: 2 additions & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
district0x/district-ui-window-size {:mvn/version "1.0.1"}
is.d0x/cljs-web3-next {:mvn/version "23.3.1"}
is.d0x/district-server-config {:mvn/version "23.4.7"}
is.d0x/district-server-db {:mvn/version "24.6.19"}
;is.d0x/district-server-db {:mvn/version "24.6.19"}
is.d0x/district-server-db {:local/root "../d0x-libs/server/district-server-db"}
is.d0x/district-server-web3-events {:mvn/version "23.5.22"}
is.d0x/re-frame-web3-fx {:mvn/version "23.5.3"}
is.d0x/district-sendgrid {:mvn/version "23.9.25"}
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"expo-modules-core": "^1.12.15",
"expo-web-browser": "^13.0.3",
"express": "4.18.2",
"graphql": "16.6.0",
"graphql": "16.8.2",
"graphql-fields": "2.0.3",
"graphql-tools": "8.3.14",
"highlight.js": "11.5.1",
Expand Down Expand Up @@ -55,8 +55,8 @@
"xregexp": "5.1.1"
},
"resolutions": {
"graphql": "^16.6.0",
"**/graphql": "^16.6.0"
"graphql": "16.8.2",
"**/graphql": "16.8.2"
},
"devDependencies": {
"@openzeppelin/contracts": "4.8.2",
Expand Down
168 changes: 85 additions & 83 deletions src/streamtide/ui/components/connect_wallet.cljs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
(ns streamtide.ui.components.connect-wallet
(:require
["thirdweb/react" :refer [ConnectButton ThirdwebProvider useActiveWallet useActiveAccount useConnect useActiveWalletChain]]
["thirdweb/wallets" :refer [createWallet inAppWallet]]
["thirdweb" :refer [createThirdwebClient defineChain]]
["thirdweb/react" :refer [ConnectButton ThirdwebProvider useActiveWallet useActiveAccount useConnect useActiveWalletChain useConnectModal]]
["thirdweb/wallets" :refer [createWallet]]
["thirdweb" :refer [createThirdwebClient]]
["react" :as react]
[camel-snake-kebab.core :as csk]
[camel-snake-kebab.extras :refer [transform-keys]]
Expand All @@ -29,26 +29,28 @@
:chain-name :name
:native-currency :nativeCurrency}))))

(defn create-provider [^js wallet]
(defn create-provider [^js wallet ^js connectModal connect-config]
#js {:request (fn [^js args]
(case (.-method args)
"eth_chainId" (js/Promise. (fn [] (-> wallet .getChain .-id)))
"eth_sendTransaction" (let [chainId (-> wallet .getChain .-id)
params (js->clj (aget (.-params args) 0) :keywordize-keys true)
params (merge params {:chainId chainId})
params (dissoc params :gasPrice :gas)
params (clj->js params)]
(-> wallet .getAccount (.sendTransaction params )))
"personal_sign" (let [account (aget (.-params args) 1)
data-raw (aget (.-params args) 0)
data (web3-next/to-ascii data-raw)
params (clj->js {:account account
:message {:raw data}})
response (-> wallet .getAccount (.signMessage params))]
response)
;"eth_requestAccounts" ""
"wallet_switchEthereumChain" (.switchChain wallet (build-chain-info))
(js/Error. (str "Method not implemented: " (.-method args)))))})
(if (nil? wallet)
((.-connect connectModal) connect-config)
(case (.-method args)
"eth_chainId" (js/Promise. (fn [] (-> wallet .getChain .-id)))
"eth_sendTransaction" (let [chainId (-> wallet .getChain .-id)
params (js->clj (aget (.-params args) 0) :keywordize-keys true)
params (merge params {:chainId chainId})
params (dissoc params :gasPrice :gas)
params (clj->js params)]
(-> wallet .getAccount (.sendTransaction params )))
"personal_sign" (let [account (aget (.-params args) 1)
data-raw (aget (.-params args) 0)
data (web3-next/to-ascii data-raw)
params (clj->js {:account account
:message {:raw data}})
response (-> wallet .getAccount (.signMessage params))]
response)
;"eth_requestAccounts" ""
"wallet_switchEthereumChain" (.switchChain wallet (build-chain-info))
(js/Promise.reject (str "Method not implemented: " (.-method args))))))})

(re-frame/reg-event-fx
:logged-in?
Expand All @@ -58,26 +60,76 @@
:result (= address (:user/address active-session))}})))

(defn connect-wallet [{:keys [:class]}]
(let [
day-night (subscribe [::st-subs/day-night-switch])
(let [day-night (subscribe [::st-subs/day-night-switch])
active-account (subscribe [::accounts-subs/active-account])
active-chain (subscribe [::chain-subs/chain])
active-wallet (useActiveWallet)
loaded? (not (.-isConnecting (useConnect)))
account (useActiveAccount)
chain (useActiveWalletChain)
client (createThirdwebClient #js {:clientId "f478f4123340f16303e57df57b6e26ef"})]
client (createThirdwebClient #js {:clientId "f478f4123340f16303e57df57b6e26ef"})
connect-config (clj->js {:auth {:doLogin (fn [^js signedPayload]
(js/Promise. (fn [resolve reject]
(dispatch [:user/-authenticate
{:payload (transform-keys csk/->kebab-case (js->clj (.-payload signedPayload) :keywordize-keys true))
:signature (.-signature signedPayload)
:callback (fn [err res]
(if err
(reject err)
(resolve res)))}]))))
:getLoginPayload (fn [^js account]
(js/Promise.
(fn [resolve reject]
(dispatch [:user/request-login-payload
{:address (eip55/address->checksum (.-address account))
:chain-id (.-chainId account)
:callback
(fn [err res]
(if err
(reject err)
(resolve (clj->js
(into {}
(filter (fn [[_ v]]
(some? v))
(transform-keys csk/->snake_case res)))))))}]))))
:isLoggedIn (fn [address]
(js/Promise. (fn [resolve reject]
(dispatch [:logged-in? {:user/address (eip55/address->checksum address)
:callback (fn [err res]
(if err
(reject err)
(resolve res)))}]))))
:doLogout (fn []
(js/Promise. (fn [resolve]
(dispatch [:user/sign-out {:callback (fn []
(resolve))}]))))}
:connectButton {:className class}
:detailsButton {:className class}
:switchButton {:className class}
:signInButton {:className class}
:client client
:theme (if (= @day-night "day") "light" "dark")
:wallets [
(createWallet "io.metamask")
(createWallet "com.coinbase.wallet")
(createWallet "io.rabby")
(createWallet "walletConnect")]
:showAllWallets false
:chain (build-chain-info)
:switchToActiveChain true})
connectModal (useConnectModal)
provider (create-provider active-wallet connectModal connect-config)]
;; some libraries assume the provider is in window.ethereum, so we set our wrapper in there to intercept any call
(set! (.-ethereum js/window) provider)
(dispatch [::web3-events/create-web3-with-user-permitted-provider {} provider])
(react/useEffect (fn []
(when (and loaded? active-wallet)
(let [provider (create-provider active-wallet)]
(set! (.-ethereum js/window) provider)
(dispatch [::web3-events/create-web3-with-user-permitted-provider {} provider])
(when (and loaded? active-wallet)
(dispatch [::chain-events/set-chain (-> active-wallet .getChain .-id)])
(dispatch [::accounts-events/set-accounts [(-> active-wallet .getAccount .-address)]])))
js/undefined)
(dispatch [::accounts-events/set-accounts [(-> active-wallet .getAccount .-address)]]))
js/undefined)
(array active-wallet))
(react/useEffect (fn []
(when (and loaded?)
(when loaded?
(let [account-address (if account (.-address account) nil)]
(if account-address
(when (not= account-address @active-account)
Expand All @@ -94,57 +146,7 @@
js/undefined)
(array chain))
(r/create-element ConnectButton
(clj->js {
:auth {
:doLogin (fn [^js signedPayload]
(js/Promise. (fn [resolve reject]
(dispatch [:user/-authenticate
{:payload (transform-keys csk/->kebab-case (js->clj (.-payload signedPayload) :keywordize-keys true))
:signature (.-signature signedPayload)
:callback (fn [err res]
(if err
(reject err)
(resolve res)))}]))))
:getLoginPayload (fn [^js account]
(js/Promise.
(fn [resolve reject]
(dispatch [:user/request-login-payload
{:address (eip55/address->checksum (.-address account))
:chain-id (.-chainId account)
:callback
(fn [err res]
(if err
(reject err)
(resolve (clj->js
(into {}
(filter (fn [[_ v]]
(some? v))
(transform-keys csk/->snake_case res)))))))}]))))
:isLoggedIn (fn [address]
(js/Promise. (fn [resolve reject]
(dispatch [:logged-in? {:user/address (eip55/address->checksum address)
:callback (fn [err res]
(if err
(reject err)
(resolve res)))}]))))
:doLogout (fn []
(js/Promise. (fn [resolve]
(dispatch [:user/sign-out {:callback (fn []
(resolve))}]))))}
:connectButton {:className class}
:detailsButton {:className class}
:switchButton {:className class}
:signInButton {:className class}
:client client
:theme (if (= @day-night "day") "light" "dark")
:wallets [
(createWallet "io.metamask")
(createWallet "com.coinbase.wallet")
(createWallet "io.rabby")
(createWallet "walletConnect")]
:showAllWallets false
:chain (build-chain-info)
:switchToActiveChain true}))))
connect-config)))


(defn connect-wallet-btn [{:keys [:class] :as opts}]
Expand Down
7 changes: 4 additions & 3 deletions src/streamtide/ui/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@
{:register :on-navigate
:events #{::router-events/navigate}
:dispatch-to [::close-mobile-menu]}
{:register :web3-created
:events #{::web3-events/web3-created}
:dispatch-to [::st-events/web3-created]}
;{:register :web3-created
; :events #{::web3-events/web3-created}
; :dispatch-to [::st-events/web3-created]}
{:register :web3-creation-failed
:events #{::web3-events/web3-creation-failed}
:dispatch-to [::st-events/web3-creation-failed]}
Expand All @@ -142,6 +142,7 @@
:web3-accounts {:eip55? true
:disable-loading-at-start? true
:disable-polling? true}
:web3-chain {:disable-loading-at-start? true}
:smart-contracts {:format :truffle-json
:contracts-path "/contracts/build/"}
:web3-tx {:disable-loading-recommended-gas-prices? true}
Expand Down
1 change: 1 addition & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module.exports = {
"crypto": false,
"buffer": require.resolve("buffer"),
"process/browser": require.resolve('process/browser'),
"vm": false
}
},
module: {
Expand Down
Loading

0 comments on commit e20461d

Please sign in to comment.