Increasing my Own Allowance in a Function of A Contract

This is basically one of my deposit functions in my voting contract. I am using a ERC20 token (I create a new one for each deployment of the contract) and am trying to deposit tokens into the voting contract by using a function defined in the contract itself.

function deposit(uint _amount) external {
    token.transferFrom(msg.sender, address(this), _amount);

I have enough tokens in my wallet, I check that from my token contract. for some reason whenever I call this I get an error saying i dont have enough allowance, from my understanding for this to function I have to increase my own allowance since I am both the spender and the source here. how do i increase my own allowance?, the user shouldnt be doing any extra work right?, or perhaps is there a simpler way to do this? I believe I can't use transfer since that would mean I am sending myself tokens from the contract, but I wish to do the opposite, I wish to send my tokens to the contract within a function in that aforementioned contract.

I'm sorry if this is a beginner question, but I ve been researching it for a long while and have tried countless things and couldn't find a way to send a token from myself to my contract with a function in my contract. I would really appreciate any help. Thank you for reading!

So have you called token.approve(contract, amount) at first?

Hi Skyge! Thank you for the feeedback. The line you suggest mean I am giving approval to contract to use my tokens right? Also in which part do you think I should use it?

Currenrly I have no problem with withdrawal of tokens from contract. The problem is when I call a function and that function tries to deposit my tokens to the contract via the line above.
I have currenlt found a solution, I overrided transferFrom and am approving the exact amount with _approve(source,_msgsender) before every transaction but i feel this is not a safe way to go at it.

Or if you think I am looking at this the wrong way( I feel like I am, I might be making a mistake in the long run with this fix) I would love to hear your feedback.

Could you please share the failed transaction hash?

You’re really doing something that doesn’t make sense/works unless you are explaining things wrong.

For a smart contract to be able to transfer tokens from the sender/user, the user has to first tell the token to approve the contract on behave of the sender. So the user will have to call the token’s approve function, it is not possible for your contract to transfer tokens from someone else unless your contract is approved by that user.

You mention you are overriding the transfer to approve, however that’s not something the contract is able to do, it has no access to the code of the token.

Did you mean that you have modified your token’s contract? That sounds extremely risky due to the limited knowledge you have.

You really have to provide more information/contract source :slight_smile: we can’t help you otherwise.