Skip to content

Commit

Permalink
✨ Add wagmi page
Browse files Browse the repository at this point in the history
  • Loading branch information
williamchong committed Dec 21, 2024
1 parent 7d34412 commit c956eee
Show file tree
Hide file tree
Showing 6 changed files with 2,397 additions and 44 deletions.
33 changes: 33 additions & 0 deletions example/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from 'path'
import { defineNuxtConfig } from '@nuxt/bridge'

export default defineNuxtConfig({
Expand Down Expand Up @@ -27,6 +28,7 @@ export default defineNuxtConfig({
// Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
plugins: [
{ src: '~/plugins/textEncoder' },
{ src: '~/plugins/wagmi' },
],

// Auto import components: https://go.nuxtjs.dev/config-components
Expand All @@ -39,11 +41,42 @@ export default defineNuxtConfig({

// Build Configuration: https://go.nuxtjs.dev/config-build
build: {
extend(config) {
config.module.rules.push({
test: /\.mjs$/,
include: /node_modules/,
type: 'javascript/auto',
})
config.resolve.alias['node:crypto'] = path.join(__dirname, './node_modules/crypto-browserify');
},
babel: {
presets: [[
'@nuxt/babel-preset-app',
{
corejs: { version: 3 },
exclude: ['transform-exponentiation-operator']
}
]],
plugins: [
'@babel/plugin-transform-nullish-coalescing-operator',
'@babel/plugin-proposal-numeric-separator',
],
},
transpile: [
'@cosmjs',
'@metamask',
'@tanstack/query-core',
'@tanstack/vue-query',
'@wagmi',
'@walletconnect',
'@web3modal',
'abitype',
'ethers',
'eth-block-tracker',
'use-wagmi',
'unstorage',
'superstruct',
'viem',
'@noble/curves',
],
}
Expand Down
7 changes: 5 additions & 2 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
},
"dependencies": {
"@cosmjs/stargate": "^0.28.10",
"core-js": "^3.19.3",
"@tanstack/vue-query": "^5.62.7",
"core-js": "^3.39.0",
"ethers": "^6.13.4",
"nuxt": "^2.17.2",
"react": "^18.2.0",
"react-dom": "^18.2.0"
"react-dom": "^18.2.0",
"use-wagmi": "^1.5.0",
"viem": "2.x"
},
"devDependencies": {
"@nuxt/bridge": "^3.3.1",
Expand Down
285 changes: 285 additions & 0 deletions example/pages/eth/wagmi.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,285 @@
<template>
<v-app>
<v-app-bar app>
<template v-if="walletAddress">
<v-chip label>{{ method }}</v-chip>
<v-toolbar-title class="ml-4">{{ formattedWalletAddress }}</v-toolbar-title>
<v-spacer />
<v-btn
class="text-truncate"
outlined
style="max-width: 150px"
@click="logout"
>Logout</v-btn>
</template>
<template v-if="walletAddress" v-slot:extension>
<v-tabs
v-model="tab"
grow
>
<v-tab key="send">Send</v-tab>
<v-tab key="sign-arbitrary">Sign Arbitrary</v-tab>
</v-tabs>
</template>
</v-app-bar>

<v-main>
<v-container v-if="!walletAddress" fill-height>
<v-row>
<v-col class="d-flex justify-center">
<v-btn
:loading="isLoading"
elevation="2"
@click="connect({ connector, chainId })"
>Connect {{ connector }}</v-btn>
</v-col>
</v-row>
</v-container>
<v-container v-else fill-height>
<v-row>
<v-col>
<v-card
:loading="isSending || isSigningArbitrary"
class="mx-auto my-12"
max-width="480"
outlined
>
<v-tabs-items v-model="tab">

<v-tab-item key="send">
<v-form
class="pa-4"
@submit.prevent="send"
>
<v-text-field
:value="walletAddress"
label="From address"
readonly
/>
<v-text-field
v-model="toAddress"
label="To address"
:disabled="isSending"
required
/>
<v-text-field
v-model="amount"
label="Amount"
type="number"
:disabled="isSending"
suffix="ETH"
required
/>
<div class="d-flex justify-end">
<v-btn
type="submit"
:elevation="isSending ? 0 : 2"
:disabled="isSending"
:loading="isSending"
>Send</v-btn>
</div>
</v-form>
</v-tab-item>

<v-tab-item key="sign-arbitrary">
<v-form
class="pa-4"
@submit.prevent="signArbitrary"
>
<v-text-field
:value="signArbitraryMessage"
label="Message to sign"
required
/>
<div class="d-flex justify-end">
<v-btn
type="submit"
:elevation="isSigningArbitrary ? 0 : 2"
:disabled="isSigningArbitrary"
:loading="isSigningArbitrary"
>Sign</v-btn>
</div>
</v-form>
<v-divider />
<v-textarea
class="mt-4 mx-4"
:value="signArbitraryResult"
label="Signature"
background-color="grey lighten-4"
placeholder="Result"
persistent-placeholder
outlined
readonly
auto-grow
/>
</v-tab-item>

</v-tabs-items>
</v-card>
<v-alert
v-model="isShowAlert"
class="mx-auto"
:type="error ? 'error' : 'success'"
elevation="2"
max-width="480"
prominent
dismissible
>
<div v-if="error">{{ error }}</div>
<v-row
v-else
align="center"
>
<v-col class="grow">The transaction is broadcasted.</v-col>
<v-col class="shrink">
<v-btn
:href="txURL"
color="white"
target="_blank"
rel="noreferrer noopener"
small
outlined
>Details</v-btn>
</v-col>
</v-row>
</v-alert>
</v-col>
</v-row>
</v-container>
</v-main>
<v-footer app>
<v-container>Demo of ethers + @likecoin/wallet-connector</v-container>
</v-footer>
</v-app>
</template>

<script>
import {
useAccount,
useConnect,
useSendTransaction,
useDisconnect,
useSignMessage,
useWaitForTransactionReceipt,
} from 'use-wagmi'
import { parseEther, verifyMessage } from 'viem';
export default {
setup() {
const { address, connector } = useAccount()
const { connect } = useConnect()
const { data: hash, sendTransaction } = useSendTransaction()
const { disconnect } = useDisconnect()
const { signMessage } = useSignMessage()
return {
walletAddress: address,
connector,
connect,
txHash: hash,
sendTransaction,
disconnect,
signMessage,
useWaitForTransactionReceipt,
};
},
data() {
return {
tab: 'send',
isLoading: false,
toAddress: '0xCb3152aCb16a60325Abd5Ab0e0CD2174a5292414',
amount: 1,
signArbitraryMessage: 'Hello, @likecoin/wallet-connector!',
signArbitraryResult: '',
error: '',
isSending: false,
isSigningArbitrary: false,
isShowAlert: false,
};
},
computed: {
formattedWalletAddress() {
const address = this.walletAddress;
const length = address.length;
return `${address.substring(0, 8)}...${address.substring(length - 3, length)}`;
},
txURL() {
return `https://sepolia-optimism.etherscan.io/tx/${this.txHash}`
},
},
watch: {
txHash(value) {
if (value) {
this.isShowAlert = true;
}
},
error(value) {
if (value) {
this.isShowAlert = true;
}
},
},
methods: {
reset() {
this.signArbitraryResult = '';
this.txHash = '';
this.error = '';
this.isSending = false;
this.isShowAlert = false;
},
logout() {
this.disconnect()
this.reset()
},
async handleAccountChange(method) {
},
async send() {
try {
this.error = '';
this.isShowAlert = false;
this.isSending = true;
this.sendTransaction({
to: this.toAddress,
value: parseEther(this.amount.toString()),
})
const { isLoading:isConfirming, isSuccess: isConfirmed } = this.useWaitForTransactionReceipt({
hash: this.txHash,
})
} catch (error) {
this.error = `${error.message || error.name || error}`;
console.error(error);
} finally {
this.isSending = false;
}
},
async signArbitrary() {
try {
this.error = '';
this.isShowAlert = false;
this.signArbitraryResult = '';
this.isSigningArbitrary = true;
this.signArbitraryResult = await this.signMessage({ message: this.signArbitraryMessage });
const valid = await verifyMessage(
{ message: this.signArbitraryMessage,
signature: this.signArbitraryResult,
address: this.walletAddress
})
if (!valid) {
throw new Error('Invalid signature');
}
} catch (error) {
this.error = `${error.message || error.name || error}`;
console.error(error);
} finally {
this.isSigningArbitrary = false;
}
},
},
};
</script>
12 changes: 12 additions & 0 deletions example/plugins/wagmi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { QueryClient, VueQueryPlugin } from '@tanstack/vue-query'
import { UseWagmiPlugin } from 'use-wagmi'
import { defineNuxtPlugin } from "@nuxt/bridge/dist"
import { config } from '../utils/wagmi/config'

const queryClient = new QueryClient()

export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.vueApp
.use(UseWagmiPlugin, { config })
.use(VueQueryPlugin, { queryClient })
})
14 changes: 14 additions & 0 deletions example/utils/wagmi/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { http, createConfig } from 'use-wagmi'
import { optimism, optimismSepolia } from 'use-wagmi/chains'
import { injected } from 'use-wagmi/connectors'

export const config = createConfig({
chains: [optimism, optimismSepolia],
connectors: [
injected(),
],
transports: {
[optimism.id]: http(),
[optimismSepolia.id]: http(),
},
})
Loading

0 comments on commit c956eee

Please sign in to comment.