forked from Web3Auth/web3auth-pnp-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ethersRPC.js
86 lines (71 loc) · 2.02 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
import '@ethersproject/shims';
import {ethers} from 'ethers';
import {Buffer} from 'buffer';
global.Buffer = global.Buffer || Buffer;
const providerUrl = 'https://rpc.ankr.com/eth'; // Or your desired provider url
const getChainId = async () => {
try {
const ethersProvider = ethers.getDefaultProvider(providerUrl);
const networkDetails = await ethersProvider.getNetwork();
return networkDetails;
} catch (error) {
return error;
}
};
const getAccounts = async key => {
try {
const wallet = new ethers.Wallet(key);
const address = await wallet.address;
return address;
} catch (error) {
return error;
}
};
const getBalance = async key => {
try {
const ethersProvider = ethers.getDefaultProvider(providerUrl);
const wallet = new ethers.Wallet(key, ethersProvider);
const balance = await wallet.getBalance();
return balance;
} catch (error) {
return error;
}
};
const sendTransaction = async key => {
try {
const ethersProvider = ethers.getDefaultProvider(providerUrl);
const wallet = new ethers.Wallet(key, ethersProvider);
const destination = '0x40e1c367Eca34250cAF1bc8330E9EddfD403fC56';
// Convert 1 ether to wei
const amount = ethers.utils.parseEther('0.001');
// Submit transaction to the blockchain
const tx = await wallet.sendTransaction({
to: destination,
value: amount,
maxPriorityFeePerGas: '5000000000', // Max priority fee per gas
maxFeePerGas: '6000000000000', // Max fee per gas
});
return tx;
} catch (error) {
return error;
}
};
const signMessage = async key => {
try {
const ethersProvider = ethers.getDefaultProvider(providerUrl);
const wallet = new ethers.Wallet(key, ethersProvider);
const originalMessage = 'YOUR_MESSAGE';
// Sign the message
const signedMessage = await wallet.signMessage(originalMessage);
return signedMessage;
} catch (error) {
return error;
}
};
export default {
getChainId,
getAccounts,
getBalance,
sendTransaction,
signMessage,
};