forked from Web3Auth/web3auth-pnp-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathethersRPC.js
91 lines (69 loc) · 2.34 KB
/
ethersRPC.js
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
const rpc = (() => {
/**
*
* @param {*} provider - provider received from Web3Auth login.
*/
const getChainId = async (provider) => {
const ethersProvider = new ethers.providers.Web3Provider(provider);
// Get the connected Chain's ID
const networkDetails = await ethersProvider.getNetwork();
return networkDetails.chainId;
};
const getAccounts = async (provider) => {
const ethersProvider = new ethers.providers.Web3Provider(provider);
const signer = ethersProvider.getSigner();
// Get user's Ethereum public address
const address = await signer.getAddress();
return address;
};
const getBalance = async (provider) => {
const ethersProvider = new ethers.providers.Web3Provider(provider);
const signer = ethersProvider.getSigner();
// Get user's Ethereum public address
const address = await signer.getAddress();
// Get user's balance in ether
const balance = ethers.utils.formatEther(
await ethersProvider.getBalance(address) // Balance is in wei
);
return balance;
};
const sendTransaction = async (provider) => {
const ethersProvider = new ethers.providers.Web3Provider(provider);
const signer = ethersProvider.getSigner();
const destination = "0x40e1c367Eca34250cAF1bc8330E9EddfD403fC56";
// Convert 1 ether to wei
const amount = ethers.utils.parseEther("0.001");
// Submit transaction to the blockchain
const tx = await signer.sendTransaction({
to: destination,
value: amount,
maxPriorityFeePerGas: "5000000000", // Max priority fee per gas
maxFeePerGas: "6000000000000", // Max fee per gas
});
// Wait for transaction to be mined
const receipt = await tx.wait();
return receipt;
};
const signMessage = async (provider) => {
const ethersProvider = new ethers.providers.Web3Provider(provider);
const signer = ethersProvider.getSigner();
const originalMessage = "YOUR_MESSAGE";
// Sign the message
const signedMessage = await signer.signMessage(originalMessage);
return signedMessage;
};
const getPrivateKey = async (provider) => {
const privateKey = await provider.request({
method: "eth_private_key",
});
return privateKey;
};
return {
getChainId,
getAccounts,
getBalance,
sendTransaction,
signMessage,
getPrivateKey,
};
})();