woo-web3
Web3Manager Class
The Web3Manager class provides a convenient interface for interacting with an Ethereum blockchain. Here's a guide on how to use its methods:
Class Instantiation
To use the `Web3Manager`` class, you need to instantiate it by providing the Ethereum node provider URL and the private key of the Ethereum account you want to manage.
const Web3Manager = require('woo-web3')
const web3Manager = new Web3Manager('your-ethereum-node-url', 'your-private-key');
Methods
getTransaction(txHash: string): Promise<TransactionResponse | null>
This method retrieves details of a specific Ethereum transaction using its hash.
Usage:
const transactionDetails = await web3Manager.getTransaction('transaction-hash');
console.log(transactionDetails);
getBlock(blockHashOrBlockTag: ethers.BlockTag): Promise<ethers.Block | null>
Retrieve details of a specific Ethereum block using its hash or tag.
Usage:
const blockDetails = await web3Manager.getBlock('block-hash-or-tag');
console.log(blockDetails);
getBalance(address: string): Promise<string>
Get the balance of an Ethereum account in ether.
Usage:
const balance = await web3Manager.getBalance('ethereum-address');
console.log(balance);
transferBalance(amount: string, destinationAddress: string): Promise<TransactionResponse>
Transfer a specified amount of Ether to a destination address.
Usage:
const transactionResponse = await web3Manager.transferBalance('0.1', 'recipient-address');
console.log(transactionResponse);
isLegacyChain(): Promise<boolean>
Check if the Ethereum chain is using the legacy gas fee system.
Usage:
const isLegacy = await web3Manager.isLegacyChain();
console.log(isLegacy);
execute(options: ExecuteFunctionOptions): Promise<unknown>
Execute a function on a smart contract.
Params:
ExecuteFunctionOptions {
contractAddress: string
contractAbi: Array<any>
method: string
value?: string
params?: Array<any>
overrides?: {
gasLimit?: BigNumberish
gasPrice?: BigNumberish
maxPriorityFeePerGas?: BigNumberish
maxFeePerGas?: BigNumberish
}
}
Usage:
const executeResult = await web3Manager.execute({
contractAddress: 'contract-address',
contractAbi: [...],
method: 'contract-function-name',
value: '0',
params: [param1, param2],
overrides: { gasLimit: 50000 }
});
console.log(executeResult);
Complete example:
async function executeContractFunction() {
try {
const web3Manager = new Web3Manager('your-ethereum-node-url', 'your-private-key');
const executeOptions: ExecuteFunctionOptions = {
contractAddress: '0x',
contractAbi: [...],
method: 'balanceOf',
params: [...],
};
const result = await web3Manager.execute(executeOptions);
console.log('Contract executed successfully. Result:', result);
} catch (error) {
console.error('Error executing the contract:', error);
}
}
executeContractFunction();
Replace placeholder values like 'your-ethereum-node-url', 'your-private-key', 'transaction-hash', 'block-hash-or-tag', 'ethereum-address', 'recipient-address', 'contract-address', and other relevant values with actual data.