-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathethersRPC.js
More file actions
98 lines (84 loc) · 2.82 KB
/
ethersRPC.js
File metadata and controls
98 lines (84 loc) · 2.82 KB
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
import '@ethersproject/shims';
import { ethers } from 'ethers';
import { Buffer } from 'buffer';
import { DAPP_CONTRACT } from "@env"
// To do for contract compile
// 1. `yarn compile-contracts` WILL COMPILE ANY BASIC CONTRACTS
// 2. Change import to the new artifacts/abi.json
// or just use vscode solidity extension to compile (Need to do if import like @openzepplin in contracts):
// https://marketplace.visualstudio.com/items?itemName=JuanBlanco.solidity
// import * as ContractABI from './bin/contracts/FitMint.json'
// 3. Deploy the contract
// 4. Use ABI + Contract Deploy address to Interface
// const DAPP_CONTRACT = "Test"
// Note: ankr rpc was hacked so def want to change this
global.Buffer = global.Buffer || Buffer;
const EthproviderUrl = 'https://rpc.ankr.com/eth_goerli'; // Or your desired provider url
// Other Useful RPC Functions
const getChainId = async () => {
try {
const ethersProvider = ethers.getDefaultProvider(EthproviderUrl);
const networkDetails = await ethersProvider.getNetwork();
return networkDetails;
} catch (error) {
return error;
}
};
const getAccounts = async key => {
try {
const wallet = new ethers.Wallet(key);
const address = wallet.address;
return address;
} catch (error) {
return error;
}
};
const getBalance = async key => {
try {
let cleanKey = key.replace(/["]/g, "");
const ethersProvider = ethers.getDefaultProvider(EthproviderUrl);
const wallet = new ethers.Wallet(cleanKey, ethersProvider);
const balance = await wallet.getBalance();
const balanceInEth = ethers.utils.formatEther(balance);
return balanceInEth;
} catch (error) {
return error;
}
};
const sendTransaction = async (key, destination, maxPriorityFeePerGas, maxFeePerGas) => {
try {
const ethersProvider = ethers.getDefaultProvider(EthproviderUrl);
const wallet = new ethers.Wallet(key, ethersProvider);
// 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: maxPriorityFeePerGas ? maxPriorityFeePerGas : '5000000000', // Max priority fee per gas
maxFeePerGas: maxFeePerGas ? maxFeePerGas : '6000000000000', // Max fee per gas
});
return tx;
} catch (error) {
return error;
}
};
const signMessage = async (key, message) => {
try {
const ethersProvider = ethers.getDefaultProvider(EthproviderUrl);
const wallet = new ethers.Wallet(key, ethersProvider);
const originalMessage = message;
// Sign the message
const signedMessage = await wallet.signMessage(originalMessage);
return signedMessage;
} catch (error) {
return error;
}
};
export default {
getChainId,
getAccounts,
getBalance,
sendTransaction,
signMessage,
};