Skip to content

Commit b5e55d4

Browse files
authored
Merge pull request #724 from Web3Auth/fix/add-web-viem
Add Viem library integration
2 parents cdaf6db + 31442f3 commit b5e55d4

File tree

11 files changed

+446
-45
lines changed

11 files changed

+446
-45
lines changed

docs/connect-blockchain/evm/ethereum/web.mdx

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ sidebar_label: Web
44
image: "guides/banners/ethereum.png"
55
displayed_sidebar: resources
66
keywords: [ethereum, web3auth, authentication, blockchain]
7-
description: "Integrate Web3Auth with the Ethereum Blockchain in JavaScript | Documentation - Web3Auth"
7+
description:
8+
"Integrate Web3Auth with the Ethereum Blockchain in JavaScript | Documentation - Web3Auth"
89
---
910

1011
import InstallationSnippet from "@site/src/common/docs/web-connect-blockchain/_evm-installation.mdx";
@@ -33,11 +34,14 @@ import SEO from "@site/src/components/SEO";
3334
slug="/connect-blockchain/evm/ethereum/web"
3435
/>
3536

36-
While using the Web3Auth Web SDK, you get a [`EIP1193`](https://eips.ethereum.org/EIPS/eip-1193) provider, similar to the
37-
[Metamask Provider](https://docs.metamask.io/guide/ethereum-provider.html). This provider can be used with libraries like
38-
[`web3.js`](https://web3js.readthedocs.io/en/v1.2.8/getting-started.html), [`ethers.js`](https://docs.ethers.io/v5/getting-started/) etc. to make
39-
[Ethereum](https://ethereum.org/) blockchain calls like getting the user's `account`, fetching `balance`, `sign transaction`, `send transaction`,
40-
`read` from and `write` to the smart contract, etc. We have highlighted a few here to get you started quickly on that.
37+
While using the Web3Auth Web SDK, you get a [`EIP1193`](https://eips.ethereum.org/EIPS/eip-1193)
38+
provider, similar to the [Metamask Provider](https://docs.metamask.io/guide/ethereum-provider.html).
39+
This provider can be used with libraries like
40+
[`web3.js`](https://web3js.readthedocs.io/en/v1.2.8/getting-started.html),
41+
[`ethers.js`](https://docs.ethers.io/v5/getting-started/), [`viem`](https://viem.sh/) etc. to make
42+
[Ethereum](https://ethereum.org/) blockchain calls like getting the user's `account`, fetching
43+
`balance`, `sign transaction`, `send transaction`, `read` from and `write` to the smart contract,
44+
etc. We have highlighted a few here to get you started quickly on that.
4145

4246
## Installation
4347

@@ -47,8 +51,9 @@ To interact with the Ethereum blockchain, you can use either library with Web3Au
4751

4852
## Initializing Provider
4953

50-
Using `eip155` as `chainNamespace` while initializing `web3auth` will provide an [`EIP1193`](https://eips.ethereum.org/EIPS/eip-1193) compatible
51-
provider as **`web3auth.provider`** after successful authentication.
54+
Using `eip155` as `chainNamespace` while initializing `web3auth` will provide an
55+
[`EIP1193`](https://eips.ethereum.org/EIPS/eip-1193) compatible provider as **`web3auth.provider`**
56+
after successful authentication.
5257

5358
### Getting the `chainConfig`
5459

docs/sdk/providers/evm-mpc.mdx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,15 @@ provider.setupProvider(coreKitInstance);
158158
## Using the provider
159159

160160
On connection, you can use this `provider` as an
161-
[`EIP1193`](https://eips.ethereum.org/EIPS/eip-1193) provider with `web3.js` or `ethers` library.
161+
[`EIP1193`](https://eips.ethereum.org/EIPS/eip-1193) provider with `web3.js`, `ethers` or `viem`
162+
library.
162163

163164
<Tabs
164165
defaultValue="web3"
165166
values={[
166167
{ label: "web3.js", value: "web3" },
167168
{ label: "ethers.js", value: "ethers" },
169+
{ label: "viem", value: "viem" },
168170
]}
169171
>
170172

@@ -188,6 +190,17 @@ const provider = new ethers.providers.Web3Provider(provider);
188190

189191
</TabItem>
190192

193+
<TabItem value="viem">
194+
195+
```tsx
196+
import { createWalletClient } from "viem";
197+
198+
const walletClient = createWalletClient({
199+
transport: custom(provider),
200+
});
201+
```
202+
203+
</TabItem>
191204
</Tabs>
192205

193206
Once you have set up the provider, you can use the standard functions in the web3 library to get

docs/sdk/providers/evm.mdx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,13 +262,15 @@ provider = ethereumPrivateKeyProvider.provider;
262262
## Using the provider
263263

264264
On connection, you can use this `provider` as an
265-
[`EIP1193`](https://eips.ethereum.org/EIPS/eip-1193) provider with `web3.js` or `ethers` library.
265+
[`EIP1193`](https://eips.ethereum.org/EIPS/eip-1193) provider with `web3.js`, `ethers` or `viem`
266+
library.
266267

267268
<Tabs
268269
defaultValue="web3"
269270
values={[
270271
{ label: "web3.js", value: "web3" },
271272
{ label: "ethers.js", value: "ethers" },
273+
{ label: "viem", value: "viem" },
272274
]}
273275
>
274276

@@ -292,6 +294,18 @@ const provider = new ethers.providers.Web3Provider(provider);
292294

293295
</TabItem>
294296

297+
<TabItem value="viem">
298+
299+
```tsx
300+
import { createWalletClient } from "viem";
301+
302+
const walletClient = createWalletClient({
303+
transport: custom(provider),
304+
});
305+
```
306+
307+
</TabItem>
308+
295309
</Tabs>
296310

297311
Once you have set up the provider, you can use the standard functions in the web3 library to get

src/common/docs/_smart-contract-solidity-deploy.mdx

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2021
const 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",
@@ -79,5 +90,52 @@ const contract = await contractFactory.deploy("Hello World!");
7990
const receipt = await contract.deployed();
8091
```
8192
93+
</TabItem>
94+
<TabItem value="viem">
95+
96+
```tsx
97+
/*
98+
Use code from the above Initializing Provider here
99+
*/
100+
101+
const publicClient = createPublicClient({
102+
chain: mainnet, // for mainnet
103+
transport: custom(this.provider),
104+
});
105+
106+
const contractABI = [
107+
{
108+
inputs: [{ internalType: "string", name: "initMessage", type: "string" }],
109+
stateMutability: "nonpayable",
110+
type: "constructor",
111+
},
112+
{
113+
inputs: [],
114+
name: "message",
115+
outputs: [{ internalType: "string", name: "", type: "string" }],
116+
stateMutability: "view",
117+
type: "function",
118+
},
119+
{
120+
inputs: [{ internalType: "string", name: "newMessage", type: "string" }],
121+
name: "update",
122+
outputs: [],
123+
stateMutability: "nonpayable",
124+
type: "function",
125+
},
126+
];
127+
const contractByteCode =
128+
"60806040523480156200001157600080fd5b5060405162000bee38038062000bee8339818101604052810190620000379190620001e3565b80600090816200004891906200047f565b505062000566565b6000604051905090565b600080fd5b600080fd5b600080fd5b600080fd5b6000601f19601f8301169050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b620000b9826200006e565b810181811067ffffffffffffffff82111715620000db57620000da6200007f565b5b80604052505050565b6000620000f062000050565b9050620000fe8282620000ae565b919050565b600067ffffffffffffffff8211156200012157620001206200007f565b5b6200012c826200006e565b9050602081019050919050565b60005b83811015620001595780820151818401526020810190506200013c565b60008484015250505050565b60006200017c620001768462000103565b620000e4565b9050828152602081018484840111156200019b576200019a62000069565b5b620001a884828562000139565b509392505050565b600082601f830112620001c857620001c762000064565b5b8151620001da84826020860162000165565b91505092915050565b600060208284031215620001fc57620001fb6200005a565b5b600082015167ffffffffffffffff8111156200021d576200021c6200005f565b5b6200022b84828501620001b0565b91505092915050565b600081519050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b600060028204905060018216806200028757607f821691505b6020821081036200029d576200029c6200023f565b5b50919050565b60008190508160005260206000209050919050565b60006020601f8301049050919050565b600082821b905092915050565b600060088302620003077fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff82620002c8565b620003138683620002c8565b95508019841693508086168417925050509392505050565b6000819050919050565b6000819050919050565b6000620003606200035a62000354846200032b565b62000335565b6200032b565b9050919050565b6000819050919050565b6200037c836200033f565b620003946200038b8262000367565b848454620002d5565b825550505050565b600090565b620003ab6200039c565b620003b881848462000371565b505050565b5b81811015620003e057620003d4600082620003a1565b600181019050620003be565b5050565b601f8211156200042f57620003f981620002a3565b6200040484620002b8565b8101602085101562000414578190505b6200042c6200042385620002b8565b830182620003bd565b50505b505050565b600082821c905092915050565b6000620004546000198460080262000434565b1980831691505092915050565b60006200046f838362000441565b9150826002028217905092915050565b6200048a8262000234565b67ffffffffffffffff811115620004a657620004a56200007f565b5b620004b282546200026e565b620004bf828285620003e4565b600060209050601f831160018114620004f75760008415620004e2578287015190505b620004ee858262000461565b8655506200055e565b601f1984166200050786620002a3565b60005b8281101562000531578489015182556001820191506020850194506020810190506200050a565b868310156200055157848901516200054d601f89168262000441565b8355505b6001600288020188555050505b505050505050565b61067880620005766000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80633d7403a31461003b578063e21f37ce14610057575b600080fd5b61005560048036038101906100509190610270565b610075565b005b61005f610088565b60405161006c9190610338565b60405180910390f35b80600090816100849190610570565b5050565b6000805461009590610389565b80601f01602080910402602001604051908101604052809291908181526020018280546100c190610389565b801561010e5780601f106100e35761010080835404028352916020019161010e565b820191906000526020600020905b8154815290600101906020018083116100f157829003601f168201915b505050505081565b6000604051905090565b600080fd5b600080fd5b600080fd5b600080fd5b6000601f19601f8301169050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b61017d82610134565b810181811067ffffffffffffffff8211171561019c5761019b610145565b5b80604052505050565b60006101af610116565b90506101bb8282610174565b919050565b600067ffffffffffffffff8211156101db576101da610145565b5b6101e482610134565b9050602081019050919050565b82818337600083830152505050565b600061021361020e846101c0565b6101a5565b90508281526020810184848401111561022f5761022e61012f565b5b61023a8482856101f1565b509392505050565b600082601f8301126102575761025661012a565b5b8135610267848260208601610200565b91505092915050565b60006020828403121561028657610285610120565b5b600082013567ffffffffffffffff8111156102a4576102a3610125565b5b6102b084828501610242565b91505092915050565b600081519050919050565b600082825260208201905092915050565b60005b838110156102f35780820151818401526020810190506102d8565b60008484015250505050565b600061030a826102b9565b61031481856102c4565b93506103248185602086016102d5565b61032d81610134565b840191505092915050565b6000602082019050818103600083015261035281846102ff565b905092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b600060028204905060018216806103a157607f821691505b6020821081036103b4576103b361035a565b5b50919050565b60008190508160005260206000209050919050565b60006020601f8301049050919050565b600082821b905092915050565b60006008830261041c7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff826103df565b61042686836103df565b95508019841693508086168417925050509392505050565b6000819050919050565b6000819050919050565b600061046d6104686104638461043e565b610448565b61043e565b9050919050565b6000819050919050565b61048783610452565b61049b61049382610474565b8484546103ec565b825550505050565b600090565b6104b06104a3565b6104bb81848461047e565b505050565b5b818110156104df576104d46000826104a8565b6001810190506104c1565b5050565b601f821115610524576104f5816103ba565b6104fe846103cf565b8101602085101561050d578190505b610521610519856103cf565b8301826104c0565b50505b505050565b600082821c905092915050565b600061054760001984600802610529565b1980831691505092915050565b60006105608383610536565b9150826002028217905092915050565b610579826102b9565b67ffffffffffffffff81111561059257610591610145565b5b61059c8254610389565b6105a78282856104e3565b600060209050601f8311600181146105da57600084156105c8578287015190505b6105d28582610554565b86555061063a565b601f1984166105e8866103ba565b60005b82811015610610578489015182556001820191506020850194506020810190506105eb565b8683101561062d5784890151610629601f891682610536565b8355505b6001600288020188555050505b50505050505056fea2646970667358221220eecabaeaef849ff90aa73071525a5fa1972cd301476f7718a27010569d13051264736f6c63430008120033";
129+
130+
const hash = await walletClient.deployContract({
131+
contractABI: contractABI,
132+
contractByteCode: contractByteCode,
133+
contractParams: ["Hello World!"],
134+
});
135+
136+
// Wait for deployment to finish
137+
const receipt = await walletClient.waitForTransaction(hash);
138+
```
139+
82140
</TabItem>
83141
</Tabs>

src/common/docs/_smart-contract-solidity-read.mdx

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2021
const 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();
4859
const signer = await provider.getSigner();
4960

5061
const 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
];
6182
const 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
6590
const 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

Comments
 (0)