@@ -6,6 +6,7 @@ import Tabs from "@theme/Tabs";
66 values = { [
77 { label: " web3.js" , value: " web3" },
88 { label: " ethers.js" , value: " ethers" },
9+ { label: " viem" , value: " viem" },
910 ]}
1011>
1112<TabItem value = " web3" >
@@ -18,8 +19,18 @@ import Tabs from "@theme/Tabs";
1819// web3 is const web3 = new Web3(web3authProvider); from above.
1920
2021const contractABI = [
21- { inputs: [{ internalType: " string" , name: " initMessage" , type: " string" }], stateMutability: " nonpayable" , type: " constructor" },
22- { inputs: [], name: " message" , outputs: [{ internalType: " string" , name: " " , type: " string" }], stateMutability: " view" , type: " function" },
22+ {
23+ inputs: [{ internalType: " string" , name: " initMessage" , type: " string" }],
24+ stateMutability: " nonpayable" ,
25+ type: " constructor" ,
26+ },
27+ {
28+ inputs: [],
29+ name: " message" ,
30+ outputs: [{ internalType: " string" , name: " " , type: " string" }],
31+ stateMutability: " view" ,
32+ type: " function" ,
33+ },
2334 {
2435 inputs: [{ internalType: " string" , name: " newMessage" , type: " string" }],
2536 name: " update" ,
@@ -48,8 +59,18 @@ const message = await contract.methods.message().call();
4859const signer = await provider .getSigner ();
4960
5061const contractABI = [
51- { inputs: [{ internalType: " string" , name: " initMessage" , type: " string" }], stateMutability: " nonpayable" , type: " constructor" },
52- { inputs: [], name: " message" , outputs: [{ internalType: " string" , name: " " , type: " string" }], stateMutability: " view" , type: " function" },
62+ {
63+ inputs: [{ internalType: " string" , name: " initMessage" , type: " string" }],
64+ stateMutability: " nonpayable" ,
65+ type: " constructor" ,
66+ },
67+ {
68+ inputs: [],
69+ name: " message" ,
70+ outputs: [{ internalType: " string" , name: " " , type: " string" }],
71+ stateMutability: " view" ,
72+ type: " function" ,
73+ },
5374 {
5475 inputs: [{ internalType: " string" , name: " newMessage" , type: " string" }],
5576 name: " update" ,
@@ -59,11 +80,60 @@ const contractABI = [
5980 },
6081];
6182const contractAddress = " 0x04cA407965D60C2B39d892a1DFB1d1d9C30d0334" ;
62- const contract = new ethers .Contract (contractAddress , JSON .parse (JSON .stringify (contractABI )), signer );
83+ const contract = new ethers .Contract (
84+ contractAddress ,
85+ JSON .parse (JSON .stringify (contractABI )),
86+ signer ,
87+ );
6388
6489// Read message from smart contract
6590const message = await contract .message ();
6691```
6792
93+ </TabItem >
94+ <TabItem value = " viem" >
95+
96+ ``` tsx
97+ /*
98+ Use code from the above Initializing Provider here
99+ */
100+
101+ const contractABI = [
102+ {
103+ inputs: [{ internalType: " string" , name: " initMessage" , type: " string" }],
104+ stateMutability: " nonpayable" ,
105+ type: " constructor" ,
106+ },
107+ {
108+ inputs: [],
109+ name: " message" ,
110+ outputs: [{ internalType: " string" , name: " " , type: " string" }],
111+ stateMutability: " view" ,
112+ type: " function" ,
113+ },
114+ {
115+ inputs: [{ internalType: " string" , name: " newMessage" , type: " string" }],
116+ name: " update" ,
117+ outputs: [],
118+ stateMutability: " nonpayable" ,
119+ type: " function" ,
120+ },
121+ ];
122+
123+ const publicClient = createPublicClient ({
124+ chain: mainnet , // for mainnet
125+ transport: custom (this .provider ),
126+ });
127+
128+ const contractAddress = " 0x04cA407965D60C2B39d892a1DFB1d1d9C30d0334" ;
129+
130+ // Read message from smart contract
131+ const message = await publicClient .readContract ({
132+ address: contractAddress ,
133+ abi: JSON .parse (JSON .stringify (contractABI )),
134+ functionName: " message" ,
135+ });
136+ ```
137+
68138</TabItem >
69139</Tabs >
0 commit comments