29 lines
No EOL
1.2 KiB
Text
29 lines
No EOL
1.2 KiB
Text
//deposit erc20 tokens
|
|
async function depositERC20(tokenAddress, depositAmount, receiverAddress) {
|
|
try {
|
|
const accounts = await web3.eth.getAccounts();
|
|
const senderAccount = accounts[0];
|
|
const contract = new web3.eth.Contract(erc20ABI, tokenAddress);
|
|
|
|
// Convert the deposit amount to the correct unit
|
|
const amountToDeposit = web3.utils.toWei(depositAmount, 'ether');
|
|
|
|
// Send the transaction and listen for confirmation
|
|
contract.methods.transfer(receiverAddress, amountToDeposit).send({ from: senderAccount })
|
|
.on('transactionHash', (hash) => {
|
|
// Update the UI with the transaction hash
|
|
console.log(`Transaction sent with hash: ${hash}`);
|
|
})
|
|
.on('receipt', (receipt) => {
|
|
// Update the UI with the transaction receipt
|
|
console.log(`Big sigh of relief, it worked! Receipt: ${receipt}`);
|
|
})
|
|
.on('error', (error) => {
|
|
// Handle errors appropriately
|
|
console.error(`Oh snap it failed: ${error.message}`);
|
|
});
|
|
} catch (error) {
|
|
// Handle any errors that occur during the process
|
|
console.error(`some shenanigans occurred try that again: ${error.message}`);
|
|
}
|
|
} |