How to interact with ERC20 interface?












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.










share|improve this question

























  • Well what DID happen???

    – goodvibration
    2 hours ago











  • 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
    2 hours ago











  • BTW, are you sure that there's a contract which implements your ERC20 interface deployed at address 0xDb0040451F373949A4Be60dcd7b6B8D6E42658B6?

    – goodvibration
    2 hours ago











  • 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
    1 hour ago


















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.










share|improve this question

























  • Well what DID happen???

    – goodvibration
    2 hours ago











  • 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
    2 hours ago











  • BTW, are you sure that there's a contract which implements your ERC20 interface deployed at address 0xDb0040451F373949A4Be60dcd7b6B8D6E42658B6?

    – goodvibration
    2 hours ago











  • 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
    1 hour ago
















2












2








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.










share|improve this question
















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.







solidity erc-20 interfaces






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 hours ago







user3652172

















asked 2 hours ago









user3652172user3652172

154




154













  • Well what DID happen???

    – goodvibration
    2 hours ago











  • 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
    2 hours ago











  • BTW, are you sure that there's a contract which implements your ERC20 interface deployed at address 0xDb0040451F373949A4Be60dcd7b6B8D6E42658B6?

    – goodvibration
    2 hours ago











  • 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
    1 hour ago





















  • Well what DID happen???

    – goodvibration
    2 hours ago











  • 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
    2 hours ago











  • BTW, are you sure that there's a contract which implements your ERC20 interface deployed at address 0xDb0040451F373949A4Be60dcd7b6B8D6E42658B6?

    – goodvibration
    2 hours ago











  • 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
    1 hour ago



















Well what DID happen???

– goodvibration
2 hours ago





Well what DID happen???

– goodvibration
2 hours ago













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
2 hours ago





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
2 hours ago













BTW, are you sure that there's a contract which implements your ERC20 interface deployed at address 0xDb0040451F373949A4Be60dcd7b6B8D6E42658B6?

– goodvibration
2 hours ago





BTW, are you sure that there's a contract which implements your ERC20 interface deployed at address 0xDb0040451F373949A4Be60dcd7b6B8D6E42658B6?

– goodvibration
2 hours ago













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
1 hour ago







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
1 hour ago












1 Answer
1






active

oldest

votes


















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.






share|improve this answer


























  • 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
    1 hour ago











  • 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
    1 hour ago













  • that's worked. Can you edit your answer so I could marked as solved.

    – user3652172
    53 mins ago














Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "642"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fethereum.stackexchange.com%2fquestions%2f70155%2fhow-to-interact-with-erc20-interface%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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.






share|improve this answer


























  • 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
    1 hour ago











  • 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
    1 hour ago













  • that's worked. Can you edit your answer so I could marked as solved.

    – user3652172
    53 mins ago


















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.






share|improve this answer


























  • 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
    1 hour ago











  • 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
    1 hour ago













  • that's worked. Can you edit your answer so I could marked as solved.

    – user3652172
    53 mins ago
















2












2








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.






share|improve this answer















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.







share|improve this answer














share|improve this answer



share|improve this answer








edited 41 mins ago

























answered 2 hours ago









Eli DrionEli Drion

2,101214




2,101214













  • 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
    1 hour ago











  • 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
    1 hour ago













  • that's worked. Can you edit your answer so I could marked as solved.

    – user3652172
    53 mins ago





















  • 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
    1 hour ago











  • 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
    1 hour ago













  • that's worked. Can you edit your answer so I could marked as solved.

    – user3652172
    53 mins ago



















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
1 hour ago





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
1 hour ago













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
1 hour ago







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
1 hour ago















that's worked. Can you edit your answer so I could marked as solved.

– user3652172
53 mins ago







that's worked. Can you edit your answer so I could marked as solved.

– user3652172
53 mins ago




















draft saved

draft discarded




















































Thanks for contributing an answer to Ethereum Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fethereum.stackexchange.com%2fquestions%2f70155%2fhow-to-interact-with-erc20-interface%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Hivernacle

Fluorita

Hulsita