// Wei to Etherweb3.utils.fromWei('1000000000000000000','ether')// "1"// Ether to Weiweb3.utils.toWei('0.001')// "1000000000000000"
Contract
// Initialize a contractconsttokenAbi= [...]; // JSON interfaceconsttokenAddress='0x1234...';constcontract=newweb3.eth.Contract(tokenAbi, tokenAddress);
Send Ether to Contract
// Send ether to the contract with interacting ABIcontract.example({value:web3.utils.toWei('0.001')})// Send ether to the contract from outsidecontract.sendTransaction({value:toWei('0.0001')})// Send ether to the contract from outside using `call` function to invoke fallback(bool success,) =payable(_victim_contract_address).call{value:'1.0'}("");// Send etehr to the contract from outside by invoking specific functioncontract.exampleFunc{value:msg.value}(address(this))
Get Storage of Contract
// the second arguement is the index of the storage.web3.eth.getStorageAt(contract.address,0)web3.eth.getStorageAt(contract.address,1)
Function Signature
We can retrieve a function signature with encodeFunctionSignature. We can also use it to invoke the contract function via sendTransaction.
constexample=web3.eth.abi.encodeFunctionSignature("example()")// We can invoke the function using this signatureawaitweb3.eth.sendTransaction({from: userAddress, to: contractAddress, data: example})