-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #129 from consensusnetworks/feature/wallet-connect…
…-init Add WalletConnect functionality (enable, disable, sendTransaction)
- Loading branch information
Showing
8 changed files
with
1,814 additions
and
1,080 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import WalletConnect from '@walletconnect/client' | ||
import QRCodeModal from '@walletconnect/qrcode-modal' | ||
import { ref, Ref } from 'vue' | ||
import { ethers } from 'ethers' | ||
|
||
export default function useWalletConnect() { | ||
const connector: Ref<WalletConnect | undefined> = ref() | ||
const walletConnectAddress: Ref<string> = ref('') | ||
function enableWalletConnect() { | ||
connector.value = new WalletConnect({ | ||
bridge: 'https://bridge.walletconnect.org', // Required | ||
qrcodeModal: QRCodeModal, | ||
}) | ||
if (!connector.value.connected) { | ||
connector.value.createSession() | ||
} | ||
connector.value.on('connect', (error: any, payload: any) => { | ||
if (error) { | ||
throw error | ||
} | ||
// Get provided accounts and chainId | ||
const { accounts, chainId } = payload.params[0] | ||
walletConnectAddress.value = accounts[0] | ||
}) | ||
connector.value.on('session_update', (error: any, payload: any) => { | ||
if (error) { | ||
throw error | ||
} | ||
// Get updated accounts and chainId | ||
const { accounts, chainId } = payload.params[0] | ||
}) | ||
connector.value.on('disconnect', (error: any) => { | ||
if (error) { | ||
console.log(`disconnect error :>> ${error}`) | ||
// throw error | ||
} | ||
// Delete connector | ||
try { | ||
connector.value?.killSession() | ||
} catch (error) { | ||
console.log(`disconnect error in listener :>> ${error}`) | ||
} | ||
}) | ||
return | ||
} | ||
|
||
async function sendWalletConnectTx(amount: string, toAddress: string) { | ||
const amountInWei = ethers.utils.parseEther(amount).toString() | ||
|
||
// TODO: Better understand and handle gasPrice and gasLimit | ||
const gasLimit = ethers.utils.hexlify(21000).toString() | ||
const gasPrice = ethers.utils.hexlify(1000000000).toString() | ||
const tx = { | ||
from: walletConnectAddress.value, | ||
to: toAddress, | ||
gas: gasLimit, | ||
gasPrice: gasPrice, | ||
value: amountInWei, | ||
// data: 'data', // TODO: Determine when this is needed. | ||
// nonce: 'nonce', // TODO: Determine when this is needed. | ||
} | ||
try { | ||
const result = await connector.value?.sendTransaction(tx) | ||
console.log('result :>> ', result) | ||
} catch (err) { | ||
console.log('error in sendWalletConnectTx :>> ', err) | ||
} | ||
} | ||
|
||
async function disableWalletConnect() { | ||
try { | ||
await connector.value?.killSession() | ||
} catch (err) { | ||
console.log('error in disableWalletConnect :>> ', err) | ||
} | ||
} | ||
|
||
return { | ||
enableWalletConnect, | ||
sendWalletConnectTx, | ||
disableWalletConnect, | ||
walletConnectAddress, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,7 @@ | ||
import { BrowserProviders } from '@/interfaces/BrowserProviders' | ||
export type ProviderString = keyof BrowserProviders | 'IoPay' | 'Ledger' |'' | ||
export type ProviderString = | ||
| keyof BrowserProviders | ||
| 'IoPay' | ||
| 'Ledger' | ||
| 'WalletConnect' | ||
| '' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.