2

My target is to send from the ERC20Wrapper.sol deployed contract BAT tokens to my hard coded address.

I've created ERC20 Interface:

pragma solidity 0.4.18;

interface ERC20 {
    function totalSupply() public view returns (uint supply);
    function balanceOf(address _owner) public view returns (uint balance);
    function transfer(address _to, uint _value) public returns (bool success);
    function transferFrom(address _from, address _to, uint _value) public returns (bool success);
    function approve(address _spender, uint _value) public returns (bool success);
    function allowance(address _owner, address _spender) public view returns (uint remaining);
    function decimals() public view returns(uint digits);
    event Approval(address indexed _owner, address indexed _spender, uint _value);
}

Then I Added ERC20Wrapper.sol:

pragma solidity ^0.4.18;

import "./ERC20Interface.sol";

contract ERC20Wrapper {
    ERC20 constant internal BAT_TOKEN_ADDRESS = ERC20(0xDb0040451F373949A4Be60dcd7b6B8D6E42658B6);
    address myAddress = address(0xAD53363200C71751FA325ED7bE483722256C3501);

    function BATSend(uint tokenAmount) public payable{
        ERC20(BAT_TOKEN_ADDRESS).transfer(myAddress,tokenAmount);
    }
}

Then I instantiate ERC20Wrapper.sol and got its address on Remix, sent to the newly created contract addrees 200 BAT tokens and tried to interact with BATSend by calling transfer on ERC20 interface and entered 20 in the tokenAmount parameter.

Expected output, my account will have +=tokenAmount and it didn't happened, tx was confirmed and account amount didn't changed.

  • Well what DID happen??? – goodvibration Apr 28 at 9:08
  • P.S.: You don't need the cast to ERC20 since the type of BAT_TOKEN_ADDRESS is already ERC20, and it doesn't look like you need the payable either. – goodvibration Apr 28 at 9:09
  • BTW, are you sure that there's a contract which implements your ERC20 interface deployed at address 0xDb0040451F373949A4Be60dcd7b6B8D6E42658B6? – goodvibration Apr 28 at 9:10
  • Yes, plus I instantiate BAT token address with ERC20 interface to check the balance of the contract address which I've send the BAT tokens to and for the balanceOf(createdContract) I got balance 200000000000000000000. and when I tried to send 13 on BATSend it tx was confirmed and still no change to myAddress. – user3652172 Apr 28 at 10:11
2

First, as pointed by @goodvibration, you don't need to recast BAT_TOKEN_ADDRESS to the ERC20 interface since you already did it when assigning its value.

Second, your BATSend function should check whether the tokens transfer was successful or not since transfer returns a boolean (true if the transfer was successful, false otherwise), like that :

    function BATSend(uint tokenAmount) public payable{
        require(ERC20(BAT_TOKEN_ADDRESS).transfer(myAddress,tokenAmount));
    }

Now, concerning your balance not being increased, it's probably because your contract's balance is empty, or inferior to the amount of tokens you are trying to transfer.

Whenever you transfer tokens, you should be aware of how many decimals the ERC20 uses. In your case, BAT uses 18 decimals, so you should multiply the amounts of tokens you want to send by 1e18.

  • edited the ERC20Wrapper.sol and added the require, made sure the balanceOf newly created contract address was changed when I've send BAT to it - balance 200000000000000000000. still no change when I tried to use BATSend with amount of 13, tx was confirmed and no change to myAddress balance. – user3652172 Apr 28 at 10:14
  • The amount should be in wei. By inputting just 13, you are actually sending 0.00...013 BAT, it's peanuts. Try 13000000000000000000. – Eli Drion Apr 28 at 10:22
  • that's worked. Can you edit your answer so I could marked as solved. – user3652172 Apr 28 at 10:53

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.