EasyEthers is a lightweight and user-friendly JavaScript library designed to simplify interactions with Ethereum blockchain. Built on top of ethers.js, EasyEthers streamlines wallet management, transaction processing, and message signing to provide a seamless experience for developers working with Ethereum-based applications.
The getProviderUrl
function returns the appropriate provider URL based on the given provider, API key, and network.
provider
(String): The provider to be used. Supported values areinfura
,alchemy
, andquicknode
.apiKey
(String): The API key for the chosen provider.network
(Number): The network ID for the target blockchain network.
- (String): The provider URL.
Network ID | Network Name |
---|---|
1 | Ethereum Mainnet |
3 | Ropsten Testnet |
4 | Rinkeby Testnet |
5 | Görli Testnet |
42 | Kovan Testnet |
56 | Binance Smart Chain Mainnet |
97 | Binance Smart Chain Testnet |
100 | xDai Mainnet |
137 | Polygon (Matic) Mainnet |
80001 | Polygon (Matic) Mumbai Testnet |
const providerUrl = getProviderUrl("infura", "your_infura_api_key", 1);
console.log(providerUrl); // https://mainnet.infura.io/v3/your_infura_api_key
The createWalletConnectProvider
function creates and returns a WalletConnect provider instance for the specified blockchain network.
rpcUrl
(String): The RPC URL for the target blockchain network.chainId
(Number): The network ID for the target blockchain network.
- (Promise): Resolves to the WalletConnect provider instance.
(async () => {
const rpcUrl = getProviderUrl("infura", "your_infura_api_key", 1);
const walletConnectProvider = await createWalletConnectProvider(rpcUrl, 1);
console.log(walletConnectProvider);
})();
The getConnectedAddress
function retrieves the connected Ethereum address from the given provider instance.
provider
(Object): A provider instance (e.g., WalletConnect provider or Ethers.js provider).
- (Promise): Resolves to the connected Ethereum address as a string.
(async () => {
const rpcUrl = getProviderUrl("infura", "your_infura_api_key", 1);
const walletConnectProvider = await createWalletConnectProvider(rpcUrl, 1);
const connectedAddress = await getConnectedAddress(walletConnectProvider);
console.log(connectedAddress);
})();
The sendTransactionWithWalletConnect
function sends a transaction using a WalletConnect provider.
provider
(Object): The WalletConnect provider instance created usingcreateWalletConnectProvider
.toAddress
(String): The Ethereum address of the recipient.amount
(String): The amount to be sent in Ether.gasPrice
(String): The gas price for the transaction in Gwei.gasLimit
(Number): The gas limit for the transaction.
- (Promise): Resolves to the transaction hash.
(async () => {
const rpcUrl = getProviderUrl("infura", "your_infura_api_key", 1);
const walletConnectProvider = await createWalletConnectProvider(rpcUrl, 1);
const toAddress = "0x742d35Cc6634C0532925a3b844Bc454e4438f44e";
const amount = "0.01";
const gasPrice = "20";
const gasLimit = 21000;
const txHash = await sendTransactionWithWalletConnect(
walletConnectProvider,
toAddress,
amount,
gasPrice,
gasLimit
);
console.log(txHash);
})();
The disconnectWalletConnect
function disconnects from the WalletConnect provider.
provider
(Object): The WalletConnect provider instance to disconnect.
- (Promise): Resolves when the provider has been disconnected.
(async () => {
const rpcUrl = getProviderUrl("infura", "your_infura_api_key", 1);
const walletConnectProvider = await createWalletConnectProvider(rpcUrl, 1);
// Disconnect from the WalletConnect provider
await disconnectWalletConnect(walletConnectProvider);
})();
The generateRandomWallet
function generates a random Ethereum wallet using the ethers.js library.
None.
-
(Object): An object containing the generated wallet's address and private key.
address
(String): The wallet's Ethereum address.privateKey
(String): The wallet's private key.
const randomWallet = generateRandomWallet();
console.log(randomWallet);
// {
// address: '0x742d35Cc6634C0532925a3b844Bc454e4438f44e',
// privateKey: '0xf1c9449a5b3e2f8c3b8e3b91b9f9b87db5c1d5dfb88e0a25d869e8a068202d0b'
// }
The generateWalletFromSeed
function generates a wallet from a given secret seed (mnemonic phrase).
seed
(String): The secret seed (mnemonic phrase) to generate the wallet from.
-
(Object): An object containing the wallet's address and private key.
address
(String): The wallet's public address.privateKey
(String): The wallet's private key.
const seed = "your secret seed words here";
const wallet = generateWalletFromSeed(seed);
console.log(wallet.address); // 0x...
console.log(wallet.privateKey); // 0x...
The importWalletFromPrivateKey
function imports a wallet using a given private key.
privateKey
(String): The private key for the wallet to be imported.
- (Object): An object containing the
address
andprivateKey
of the imported wallet.
const wallet = importWalletFromPrivateKey("0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef");
console.log(wallet);
// {
// address: "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
// privateKey: "0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
// }
The getBalance
function retrieves the balance of a specified address.
address
(String): The Ethereum address to retrieve the balance for.providerUrl
(String): The provider URL obtained from thegetProviderUrl
function.
- (Promise): A promise that resolves to the balance of the specified address in Ether as a string.
(async () => {
const address = "0x742d35Cc6634C0532925a3b844Bc454e4438f44e";
const providerUrl = getProviderUrl("infura", "your_infura_api_key", 1);
const balance = await getBalance(address, providerUrl);
console.log(`Balance: ${balance} ETH`);
})();
The sendTransaction
function sends a transaction from one address to another, specifying the amount, gas price, and gas limit.
fromPrivateKey
(String): The private key of the sender's wallet.toAddress
(String): The recipient's wallet address.amount
(String): The amount of Ether to be sent (in ETH).gasPrice
(String): The gas price for the transaction (in Gwei).gasLimit
(Number): The gas limit for the transaction.providerUrl
(String): The provider URL to be used for sending the transaction.
- (Promise): A promise that resolves to the transaction hash.
(async () => {
const txHash = await sendTransaction(
"your_private_key",
"recipient_address",
"1", // Amount in ETH
"50", // Gas price in Gwei
21000, // Gas limit
"https://mainnet.infura.io/v3/your_infura_api_key"
);
console.log(txHash); // Transaction hash
})();