2

I seem to be having problems interacting with a custom token contract. I am using truffle and ganache for my testing. I am trying to interact with the custom token contract so MyContract can transfer tokens to an address on behalf of the custom token contract. I do have the ABI as well.

custom token contract:

pragma solidity ^0.4.16;
import "./MintableToken.sol";
contract CustomToken is MintableToken {
  // Coin Properties
  string public name = "Custom Token";
  string public symbol = "Cust";
  uint256 public decimals = 18;
  function transfer(address _to, uint _value) public returns (bool) {
    return super.transfer(_to, _value);
  }
}

MyContract:

pragma solidity ^0.4.4;

import "./CustomToken.sol";

contract MyContract is CustomToken{

    function transferCustom(address _to, uint _value) public returns (bool) {
        require(_to != address(0));


        balances[msg.sender] = balances[msg.sender].sub(_value);
        balances[_to] = balances[_to].add(_value);
        Transfer(msg.sender,_to,_value);

        return true;
    }
}

truffle test:

const accountfirst  = web3.eth.accounts[1];
const amount = 100;

let token = await CustomToken.deployed();
let myContract = await MyContract.deployed();

var ctoken = CustomToken.at(token.address);
ctoken.approve(myContract.address, amount);
let tx = await myContract.transferCustom(accountfirst, amount);

When i run this test i get transaction reverted. Any ideas? thank you in advance!

  • i've narrowed down the error and it seems to come from this line: let tx = await myContract.transferCustom(accountfirst, amount); – DividedByZero Jun 17 '18 at 21:11
0

I'm pretty sure the issue is sending a null address to the contract and then require(_to != address(0));. The contract function requirement is good, but the sending null is a client-side problem.

I don't think this (below) works synchronously, so accountfirst is null when the function is invoked, so the transaction reverts.

const accountfirst  = web3.eth.accounts[1];

Try fetching the account asynchronously.

web3.eth.getAccounts()
.then(function(response) {
   account = response[0]);
   // carry on
}

or

web3.eth.getAccounts(function(err,response) { 
  account = response[0];
  // carry on
}

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.