-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.vue
99 lines (78 loc) · 2.23 KB
/
app.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<template>
<div>
<h1>Nuxt3 Wagmi Starter</h1>
<button @click="connectMetaMask">Connect MetaMask</button>
<button @click="disconnectWallet">Disconnect</button>
<button @click="fetchUsdcBalancePolygon">Fetch USDC balance</button>
<p>Chain: {{chainId}}</p>
<p>Address: {{address}}</p>
</div>
</template>
<script>
import { ethers } from 'ethers';
import {
connect, disconnect, erc20ABI, fetchSigner, getAccount, getNetwork, getProvider, watchAccount, watchNetwork
} from '@wagmi/core';
export default {
methods: {
async connectMetaMask() {
console.log("connect");
const result = await connect({connector: this.$mmConnector});
console.log(result);
},
async disconnectWallet() {
console.log("disconnect");
await disconnect();
},
async fetchUsdcBalancePolygon() {
// read example
const newProvider = this.getProvider();
const contractRead = new ethers.Contract(
"0x2791bca1f2de4661ed88a30c99a7a9449aa84174", // USDC contract on Polygon
this.erc20ABI,
newProvider
);
const balance = await contractRead.balanceOf(this.address);
console.log("balance: ");
console.log(balance);
// write example
/*
const signer = await fetchSigner();
const contractWrite = new ethers.Contract(
"0x2791bca1f2de4661ed88a30c99a7a9449aa84174", // USDC contract on Polygon
this.erc20ABI,
signer
);
await contractWrite.transfer(this.address, "100000"); // send 0.1 USDC
*/
},
},
setup() {
const chainId = ref(0);
const address = ref("");
const account = getAccount();
const { chain } = getNetwork();
if (chain) {
chainId.value = chain.id;
}
if (account) {
address.value = account.address;
}
watchAccount(function(account) {
if (account.address) {
address.value = account.address;
} else {
address.value = "";
}
});
watchNetwork(function(network) {
if (network.chain) {
chainId.value = network.chain.id;
} else {
chainId.value = 0;
}
});
return { address, chainId, connect, disconnect, erc20ABI, fetchSigner, getProvider }
},
}
</script>