1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

WalletConnect Providerで、接続をキャンセルした事をキャッチする

Posted at

問題

WalletConnectでMetamaskを立ち上げ、Metamaskから接続をキャンセルした場合に、それ以降の処理が実行されなかった。

.ts
const provider = new WalletConnectProvider({.....省略});

// Modalを立ち上げて、Metamaskを開く
await provider.enable();
// Metamaskでキャンセルした場合、これ以降の処理が実行されない

エラーをキャッチできれば問題ないが、try catchenable().catch()などを試しても、このエラーをキャッチ出来なかった。

解決方法

provider.wc.on('disconnect',()=>{})を使うことで接続のキャンセルを検知することが出来た。

.ts
provider.wc.on('disconnect', (error, payload) => {
  if (error) {
    throw error
  }
  console.log("Cancel")
})

具体的使い方

.ts
const provider = new WalletConnectProvider({.....省略});

provider.wc.on('disconnect', (error, payload) => {
  if (error) {
    throw error
  }
  console.log("Cancel")
  setLoading(false)
})

setLoading(true)
await provider.enable();
setLoading(false)

ボタンをクリックした時にボタンがローディング状態になるような場合に、provider.wc.on('disconnect',()=>{})を使わないと、永遠にローディング状態になってしまうが、使うことでローディングを終了することが出来る。

参考

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?