I have this function in my smart contract that should be able to transfer an ERC-20 token that I have created from the user's wallet back to the contract in a particular pool (The contract has already sent the tokens to the user previously - there is another function for that)
function depositToCompanyReserve(uint256 _amount) public {
// Check that the requested amount of tokens to deposit is more than 0
require(_amount > 0, "Cannot deposit nothing");
// Transfer tokens from the sender to the contract's address
bool sent = token.transferFrom(msg.sender, address(this), _amount);
require(sent, "Failed to transfer tokens");
// Add the deposited amount to the company reserve pool
poolOfCompanyReserve += _amount;
// Emit an event or perform any other necessary actions
emit CompanyReserveDeposited(msg.sender, _amount);
}
I also have a react code that interacts with this function. Its snippet is as follows -
async function handleDepositToCompanyReserve() {
if (depositAmount !== "0") {
try {
// Importing WEB3
const web3 = window.web3;
// Get Account
const accounts = await window.ethereum.request({
method: "eth_requestAccounts",
});
console.log("First accounts", accounts[0]);
let userAccount = accounts[0];
console.log(userAccount);
// Get Network ID
let networkId = await web3.eth.net.getId();
console.log("networkId", networkId);
// Load ALCORContract
let ethSwap = {};
if (alcorContractAddress) {
ethSwap = new web3.eth.Contract(ALCORContract.abi, alcorContractAddress);
} else {
window.alert("Invalid Contract Address");
}
await ethSwap.methods.depositToCompanyReserve(depositAmount).send({ from: userAccount });
} catch (error) {
console.error(error);
alert("An error occurred while depositing to the Company Reserve.");
}
} else {
alert("Deposit Amount cannot be 0.");
}
}
The transaction is failing for some reason and I cannot figure out why since the only error that is being thrown is 'Transaction Reverted by VM'
I have tried to debug the code and figure out what the issue is but I cannot find the problem. Any help would be appreciated! Thanks.