|
| 1 | +<template> |
| 2 | + <div> |
| 3 | + <h1>{{ msg }}</h1> |
| 4 | + |
| 5 | + <div class="container"> |
| 6 | + <div class="wallet" v-if="connectedWallet"> |
| 7 | + <div class="avatar" /> |
| 8 | + <div class="details"> |
| 9 | + <div v-if="ens">{{ ens.name }}</div> |
| 10 | + <div v-if="address">{{ address }}</div> |
| 11 | + |
| 12 | + <span>Connected Wallet: {{ connectedWallet.label }}</span> |
| 13 | + </div> |
| 14 | + <button type="button" @click="disconnect">Disconnect</button> |
| 15 | + </div> |
| 16 | + <div v-else> |
| 17 | + <button type="button" @click="connect">Connect Wallet</button> |
| 18 | + </div> |
| 19 | + </div> |
| 20 | + </div> |
| 21 | +</template> |
| 22 | + |
| 23 | +<script> |
| 24 | +import { init, useOnboard } from '@web3-onboard/vue'; |
| 25 | +import injectedModule from '@web3-onboard/injected-wallets'; |
| 26 | +
|
| 27 | +const injected = injectedModule(); |
| 28 | +// With vite |
| 29 | +const infuraKey = import.meta.env.VITE_INFURA_KEY; |
| 30 | +
|
| 31 | +// Without vite |
| 32 | +//const infuraKey = process.env.INFURA_KEY; |
| 33 | +
|
| 34 | +const rpcUrl = `https://mainnet.infura.io/v3/${infuraKey}`; |
| 35 | +
|
| 36 | +const web3Onboard = init({ |
| 37 | + wallets: [injected], |
| 38 | + chains: [ |
| 39 | + { |
| 40 | + id: '0x1', |
| 41 | + token: 'ETH', |
| 42 | + label: 'Ethereum Mainnet', |
| 43 | + rpcUrl, |
| 44 | + }, |
| 45 | + ], |
| 46 | +}); |
| 47 | +
|
| 48 | +const { wallets, connectWallet, disconnectConnectedWallet, connectedWallet } = |
| 49 | + useOnboard(); |
| 50 | +
|
| 51 | +const trunc = (address) => |
| 52 | + !!address ? address.slice(0, 6) + '...' + address.slice(-6) : null; |
| 53 | +
|
| 54 | +export default { |
| 55 | + props: { |
| 56 | + msg: String, |
| 57 | + }, |
| 58 | + data() { |
| 59 | + return { |
| 60 | + connectedWallet, |
| 61 | + count: 0, |
| 62 | + }; |
| 63 | + }, |
| 64 | + methods: { |
| 65 | + connect: () => connectWallet(), |
| 66 | + disconnect: () => disconnectConnectedWallet(), |
| 67 | + }, |
| 68 | + computed: { |
| 69 | + address: function () { |
| 70 | + if ( |
| 71 | + this.connectedWallet.accounts && |
| 72 | + this.connectedWallet.accounts[0].address |
| 73 | + ) { |
| 74 | + console.log(this.connectedWallet.accounts[0].address); |
| 75 | + return trunc(this.connectedWallet.accounts[0].address); |
| 76 | + } |
| 77 | + }, |
| 78 | + ens: function () { |
| 79 | + if ( |
| 80 | + this.connectedWallet.accounts && |
| 81 | + this.connectedWallet.accounts[0].ens?.name |
| 82 | + ) { |
| 83 | + return trunc(this.connectedWallet.accounts[0].ens); |
| 84 | + } |
| 85 | + }, |
| 86 | + }, |
| 87 | +}; |
| 88 | +</script> |
| 89 | +
|
| 90 | +<style scoped> |
| 91 | +a { |
| 92 | + color: var(--vt-c-brand); |
| 93 | + text-decoration: none; |
| 94 | +} |
| 95 | +
|
| 96 | +button { |
| 97 | + border: none; |
| 98 | + border-radius: 4px; |
| 99 | + padding: 0 12px; |
| 100 | + letter-spacing: 0.8px; |
| 101 | + line-height: 36px; |
| 102 | + font-size: 13px; |
| 103 | + font-weight: 500; |
| 104 | + color: rgba(255, 255, 255, 0.87); |
| 105 | + background-color: var(--vt-c-brand); |
| 106 | + transition: background-color 0.25s; |
| 107 | + cursor: pointer; |
| 108 | +} |
| 109 | +
|
| 110 | +button:hover { |
| 111 | + background-color: var(--vt-c-brand-dark); |
| 112 | +} |
| 113 | +
|
| 114 | +.container { |
| 115 | + max-width: 64vw; |
| 116 | + margin: auto; |
| 117 | +} |
| 118 | +
|
| 119 | +.wallet { |
| 120 | + display: inline-flex; |
| 121 | + align-items: center; |
| 122 | + justify-content: space-between; |
| 123 | + border-radius: 8px; |
| 124 | + padding: 0.5rem 0.75rem; |
| 125 | + font-size: 16px; |
| 126 | + width: 100%; |
| 127 | + transition: border-color 0.25s, background-color 0.25s; |
| 128 | + margin: 28px 0; |
| 129 | + border-radius: 8px; |
| 130 | + overflow-x: auto; |
| 131 | + transition: color 0.5s, background-color 0.5s; |
| 132 | + position: relative; |
| 133 | + font-size: 14px; |
| 134 | + line-height: 1.6; |
| 135 | + font-weight: 500; |
| 136 | + background-color: var(--vt-c-bg-soft); |
| 137 | +} |
| 138 | +
|
| 139 | +.avatar { |
| 140 | + height: 36px; |
| 141 | + width: 36px; |
| 142 | + border-radius: 100%; |
| 143 | + background-image: linear-gradient( |
| 144 | + to right, |
| 145 | + rgb(6, 182, 212), |
| 146 | + rgb(59, 130, 246) |
| 147 | + ); |
| 148 | +} |
| 149 | +
|
| 150 | +.details { |
| 151 | + display: flex; |
| 152 | + flex-flow: column; |
| 153 | + align-items: flex-start; |
| 154 | + margin-right: auto; |
| 155 | + margin-left: 12px; |
| 156 | + text-align: left; |
| 157 | +} |
| 158 | +</style> |
0 commit comments