Deposit Tokens in Smart Contract - SafeTransferFrom Issues

So I’m having some issues with the safe transfer function while I run tests for a deposit tokens feature in a simple vesting contract I’m working on.

I’m only pasting the section that is having issues, but just for clarity:

  • “token” in “token.safeApprove…” has already had it’s token address set in state variables of contract

  • for the “vestingContract” method - that too is set/saved to state variable after deployment

  • I am using safeERC20 from OZ

I’m simply trying to test this function which would allow any holder of the “token” to deposit it to the contract.

But I keep getting a “transfer amount exceeds allowance”, “data”:" error that won’t let me transfer the tokens from my wallet to the contract calling this feature.

I’m sure I’m missing something simple but not sure what it is here. Would appreciate any help!

:1234: Code to reproduce

//First I tried this function

function depositAndLockTokens(uint256 _amount) external {       
        token.safeApprove(address(this), _amount);  
        token.safeTransferFrom(msg.sender, address(this), _amount);    
        emit TokensDeposited(msg.sender, _amount);
    }

//then I created a state variable to store the vesting contract, but same problem

 function depositAndLockTokens(uint256 _amount) external {       
        token.safeApprove(vestingContract, _amount);  
        token.safeTransferFrom(msg.sender, vestingContract, _amount);    
        emit TokensDeposited(msg.sender, _amount);
    }

:computer: Environment

Right now just trying to get this working on remix.

Hi, I think you should call token.approve(YOUR_DEPOSIT_CONTRACT, amount) by your wallet account, and then use this account to call your function depositAndLockTokens(), and this function should be

function depositAndLockTokens(uint256 _amount) external {       
        token.safeTransferFrom(msg.sender, address(this), _amount);    
        emit TokensDeposited(msg.sender, _amount);
    }