Call to Trust.tokensend errored: VM error: invalid opcode

**I get an error when I call tokensend

transact to Trust.tokensend errored: VM error: invalid opcode. invalid opcode The execution might have thrown. Debug the transaction to get more information**

:1234: Code to reproduce

pragma solidity ^0.6.0;
 import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/IERC20.sol";
 import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/math/SafeMath.sol";
 import"https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol";
 contract Trust is Ownable{
    event _Free (uint);
   using SafeMath for uint256;
   
   uint public free;
   
   mapping (address => mapping (address => data1[])) _data1;
   mapping (address => address[])addre;
   

   struct data1{
       uint256 onceamount;
       uint256 startTime;
       uint256 intervals;
       uint256 alltoken;
       address ercaddr;
   }
   
   
   
   function _free (uint _value) public onlyOwner{
       free = _value;
   }
   
   function tokennum(address _msgfro, address frto, address _tokenaddress)private returns(uint8 e, uint8 ee){
       for(uint8 i; i <= _data1[_msgfro][frto].length;i++){
           if (_data1[_msgfro][frto][i].ercaddr == _tokenaddress){
               e++;
               ee = i;
           }
       }
   }
   
   function tokensend(address _tokenaddress,address to,uint256 startTime,uint256 intervals
   ,uint256 alltoken,uint256 onceamount)public payable{
       IERC20 token = IERC20 (_tokenaddress);
       require(msg.value == free ,"Fee error");
       require(token.transferFrom(msg.sender,address(this),alltoken));
       uint8   e;
       uint8  ee;
       (e,ee) = tokennum(msg.sender, to, _tokenaddress);
       require(_data1 [msg.sender][to][ee].alltoken == 0 ,"還有餘額");
       if (e == 0){
       _data1[msg.sender][_tokenaddress].push(data1 (onceamount, startTime + now, intervals, alltoken, _tokenaddress));
       addre[to].push(msg.sender);
       }else{
        _data1[msg.sender][_tokenaddress][ee] = data1 (onceamount, startTime + now, intervals, alltoken,_tokenaddress);
       addre[to][ee] = msg.sender;
       }
       token.transferFrom(msg.sender,address(this),alltoken);
       
   }
   
   function tokenhad(address _from, address _tokenaddress)public{
       IERC20 token = IERC20 (_tokenaddress);
       uint8 e;
       uint8 ee;
       (e,ee)=tokennum(_from, msg.sender, _tokenaddress);
       data1 memory data = _data1[_from][msg.sender][ee];
       require(data.alltoken >= data.onceamount ,"已無餘額");
       require(data.startTime >= now , "還沒到領取時間");
       data.startTime += data.intervals;
       data.alltoken -= data.onceamount;
       token.transfer(msg.sender, data.onceamount);
       if(data.alltoken < data.onceamount ){
           data.alltoken = 0;
       }
       _data1[_from][msg.sender][ee].startTime = data.startTime;
       _data1[_from][msg.sender][ee].alltoken = data.alltoken;
       
   }
   
   function CancelContract(address to, address _tokenaddress)public{
       IERC20 token = IERC20 (_tokenaddress);
       uint8 e;
       uint8 ee;
       (e,ee) = tokennum(msg.sender, to, _tokenaddress);
       data1 memory data = _data1[msg.sender][to][ee];
       require(data.alltoken < data.onceamount ,"已無餘額");
       token.transfer(msg.sender, data.alltoken);
       _data1[msg.sender][to][ee].alltoken = 0;
   }
   
 }
1 Like

You really should format your code, you can use solhint or prettier.
You can paste your code in the Remix(http://remix.ethereum.org), it will have some grammar checks. I get an error at line 27: _data1[_msgfro][frto].length

1 Like

Hi @1118,

I put a code block around your code to make it slightly easier to read.
```solidity
Code goes here
```

You may want to check with the person who wrote the code that you are trying to use.

I was able to compile in Remix but I didn’t try to call tokensend. I didn’t dig into what the parameters needed to be.

I assume that you provided a valid token and gave an appropriate allowance to the token (as the contract is doing transferFrom. (See: Example on how to use ERC20 token in another contract)

1 Like