Can I use my custom ERC-20 with my smart contract?










0















So I have a contract that allows you to exchange ETH for my custom ERC20 token. I am wanting to use that custom ERC20 token now with other smart contracts. Is there a certain way I have to specify the custom token vs ETH?



example:



pragma solidity ^0.4.24;



/*
* ---How to use:
* 1. Send HYPER Tokens to the smart contract address in any amount.
* 2. Claim your profit by sending 0 HYPER transaction (1 time per hour)
* 3. If you do not withdraw and earn more than 200%, you can withdraw only one time over the 200% allowance
*/
contract HyperLENDtest {



using SafeMath for uint;
mapping(address => uint) public balance;
mapping(address => uint) public time;
mapping(address => uint) public percentWithdraw;
mapping(address => uint) public allPercentWithdraw;


function percentRate() public view returns(uint) {
uint contractBalance = address(this).balance;



 if (contractBalance < 100 ether) 
return (20);

if (contractBalance >= 500 ether && contractBalance < 1000 ether)
return (40);

if (contractBalance >= 1000 ether && contractBalance < 2000 ether)
return (60);

if (contractBalance >= 2000 ether)
return (80);



Instead of returning ETH I want to use my custom ERC20 token to users to send to the contract and get in return % of the ERC20 token back.










share|improve this question




























    0















    So I have a contract that allows you to exchange ETH for my custom ERC20 token. I am wanting to use that custom ERC20 token now with other smart contracts. Is there a certain way I have to specify the custom token vs ETH?



    example:



    pragma solidity ^0.4.24;



    /*
    * ---How to use:
    * 1. Send HYPER Tokens to the smart contract address in any amount.
    * 2. Claim your profit by sending 0 HYPER transaction (1 time per hour)
    * 3. If you do not withdraw and earn more than 200%, you can withdraw only one time over the 200% allowance
    */
    contract HyperLENDtest {



    using SafeMath for uint;
    mapping(address => uint) public balance;
    mapping(address => uint) public time;
    mapping(address => uint) public percentWithdraw;
    mapping(address => uint) public allPercentWithdraw;


    function percentRate() public view returns(uint) {
    uint contractBalance = address(this).balance;



     if (contractBalance < 100 ether) 
    return (20);

    if (contractBalance >= 500 ether && contractBalance < 1000 ether)
    return (40);

    if (contractBalance >= 1000 ether && contractBalance < 2000 ether)
    return (60);

    if (contractBalance >= 2000 ether)
    return (80);



    Instead of returning ETH I want to use my custom ERC20 token to users to send to the contract and get in return % of the ERC20 token back.










    share|improve this question


























      0












      0








      0








      So I have a contract that allows you to exchange ETH for my custom ERC20 token. I am wanting to use that custom ERC20 token now with other smart contracts. Is there a certain way I have to specify the custom token vs ETH?



      example:



      pragma solidity ^0.4.24;



      /*
      * ---How to use:
      * 1. Send HYPER Tokens to the smart contract address in any amount.
      * 2. Claim your profit by sending 0 HYPER transaction (1 time per hour)
      * 3. If you do not withdraw and earn more than 200%, you can withdraw only one time over the 200% allowance
      */
      contract HyperLENDtest {



      using SafeMath for uint;
      mapping(address => uint) public balance;
      mapping(address => uint) public time;
      mapping(address => uint) public percentWithdraw;
      mapping(address => uint) public allPercentWithdraw;


      function percentRate() public view returns(uint) {
      uint contractBalance = address(this).balance;



       if (contractBalance < 100 ether) 
      return (20);

      if (contractBalance >= 500 ether && contractBalance < 1000 ether)
      return (40);

      if (contractBalance >= 1000 ether && contractBalance < 2000 ether)
      return (60);

      if (contractBalance >= 2000 ether)
      return (80);



      Instead of returning ETH I want to use my custom ERC20 token to users to send to the contract and get in return % of the ERC20 token back.










      share|improve this question
















      So I have a contract that allows you to exchange ETH for my custom ERC20 token. I am wanting to use that custom ERC20 token now with other smart contracts. Is there a certain way I have to specify the custom token vs ETH?



      example:



      pragma solidity ^0.4.24;



      /*
      * ---How to use:
      * 1. Send HYPER Tokens to the smart contract address in any amount.
      * 2. Claim your profit by sending 0 HYPER transaction (1 time per hour)
      * 3. If you do not withdraw and earn more than 200%, you can withdraw only one time over the 200% allowance
      */
      contract HyperLENDtest {



      using SafeMath for uint;
      mapping(address => uint) public balance;
      mapping(address => uint) public time;
      mapping(address => uint) public percentWithdraw;
      mapping(address => uint) public allPercentWithdraw;


      function percentRate() public view returns(uint) {
      uint contractBalance = address(this).balance;



       if (contractBalance < 100 ether) 
      return (20);

      if (contractBalance >= 500 ether && contractBalance < 1000 ether)
      return (40);

      if (contractBalance >= 1000 ether && contractBalance < 2000 ether)
      return (60);

      if (contractBalance >= 2000 ether)
      return (80);



      Instead of returning ETH I want to use my custom ERC20 token to users to send to the contract and get in return % of the ERC20 token back.







      solidity smartcontracts erc20






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 21:46







      JimmyJong

















      asked Nov 14 '18 at 21:17









      JimmyJongJimmyJong

      11




      11






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Your contract is just another address so yes, you can send tokens to your contract. But you cannot send them the same way you send ether, that is using a payable function. You have to transfer the tokens to the contract's address using the transfer method of you ERC-20 token. And to send tokens from the contract to someone else you have to call transfer from inside your contract unless you do something like provide a lot of allowance for your account, but I wouldn't suggest this. How exactly you can call methods from your ERC-20 from inside your other contract is explained in this post.






          share|improve this answer























          • Thanks Nikos, so I am aware of the transfer function but what I am trying to achieve is using my contract as it exists and using my custom ERC20 token vs ETH. So for example in this game, a user sends X eth to the contract and in return gets a % back each day. But instead of ETH in return I want to provide them my custom ERC20 token and not ETH.

            – JimmyJong
            Nov 15 '18 at 13:30











          • It certainly is possible, but you will need to provide them of a way to buy and send your ERC-20 token. I think the simplest way would using metamask unless you want to create an interface yourself. So if you use metamask you can have them buy tokens buy sending ether to your ERC-20 contract, which is the standard way to do it. After they have some tokens they need to add this token to their metamask account so that they can transfer tokens using metamask. This guide describes how to add custom ERC-20 tokens to metamask: luvcrypto.com/add-send-tokens-metamask

            – nikos fotiadis
            Nov 15 '18 at 14:06












          • And you also need to notify your other contract about the token transfer. I know it is a little more complicated than using ether.

            – nikos fotiadis
            Nov 15 '18 at 14:12











          • thats where I am running into the issue, I am not sure how to differentiate ETH vs the custom token in the contract. I have a UI currently built. But when I use the smart contract and try to replace ETHER value with my token it throws errors. I do understand up to adding the token to metamask and all that. Its really how to get the contract to recognize its an ERC20 token and not ETH ( code noted in original post above)

            – JimmyJong
            Nov 15 '18 at 15:07











          • Ether and tokens are sent in different ways. A transaction may contain ether, but a transaction cannot contain tokens. The transaction will tell the contract to transfer tokens. So the contract can know if it is ERC-20 or Ether based on the function that is executed. When you "send" ERC-20 tokens it is actually just tha balances that change in the ERC-20 contract, you don't really send something with the transaction.

            – nikos fotiadis
            Nov 15 '18 at 15:56










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          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: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          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%2fstackoverflow.com%2fquestions%2f53308858%2fcan-i-use-my-custom-erc-20-with-my-smart-contract%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









          0














          Your contract is just another address so yes, you can send tokens to your contract. But you cannot send them the same way you send ether, that is using a payable function. You have to transfer the tokens to the contract's address using the transfer method of you ERC-20 token. And to send tokens from the contract to someone else you have to call transfer from inside your contract unless you do something like provide a lot of allowance for your account, but I wouldn't suggest this. How exactly you can call methods from your ERC-20 from inside your other contract is explained in this post.






          share|improve this answer























          • Thanks Nikos, so I am aware of the transfer function but what I am trying to achieve is using my contract as it exists and using my custom ERC20 token vs ETH. So for example in this game, a user sends X eth to the contract and in return gets a % back each day. But instead of ETH in return I want to provide them my custom ERC20 token and not ETH.

            – JimmyJong
            Nov 15 '18 at 13:30











          • It certainly is possible, but you will need to provide them of a way to buy and send your ERC-20 token. I think the simplest way would using metamask unless you want to create an interface yourself. So if you use metamask you can have them buy tokens buy sending ether to your ERC-20 contract, which is the standard way to do it. After they have some tokens they need to add this token to their metamask account so that they can transfer tokens using metamask. This guide describes how to add custom ERC-20 tokens to metamask: luvcrypto.com/add-send-tokens-metamask

            – nikos fotiadis
            Nov 15 '18 at 14:06












          • And you also need to notify your other contract about the token transfer. I know it is a little more complicated than using ether.

            – nikos fotiadis
            Nov 15 '18 at 14:12











          • thats where I am running into the issue, I am not sure how to differentiate ETH vs the custom token in the contract. I have a UI currently built. But when I use the smart contract and try to replace ETHER value with my token it throws errors. I do understand up to adding the token to metamask and all that. Its really how to get the contract to recognize its an ERC20 token and not ETH ( code noted in original post above)

            – JimmyJong
            Nov 15 '18 at 15:07











          • Ether and tokens are sent in different ways. A transaction may contain ether, but a transaction cannot contain tokens. The transaction will tell the contract to transfer tokens. So the contract can know if it is ERC-20 or Ether based on the function that is executed. When you "send" ERC-20 tokens it is actually just tha balances that change in the ERC-20 contract, you don't really send something with the transaction.

            – nikos fotiadis
            Nov 15 '18 at 15:56















          0














          Your contract is just another address so yes, you can send tokens to your contract. But you cannot send them the same way you send ether, that is using a payable function. You have to transfer the tokens to the contract's address using the transfer method of you ERC-20 token. And to send tokens from the contract to someone else you have to call transfer from inside your contract unless you do something like provide a lot of allowance for your account, but I wouldn't suggest this. How exactly you can call methods from your ERC-20 from inside your other contract is explained in this post.






          share|improve this answer























          • Thanks Nikos, so I am aware of the transfer function but what I am trying to achieve is using my contract as it exists and using my custom ERC20 token vs ETH. So for example in this game, a user sends X eth to the contract and in return gets a % back each day. But instead of ETH in return I want to provide them my custom ERC20 token and not ETH.

            – JimmyJong
            Nov 15 '18 at 13:30











          • It certainly is possible, but you will need to provide them of a way to buy and send your ERC-20 token. I think the simplest way would using metamask unless you want to create an interface yourself. So if you use metamask you can have them buy tokens buy sending ether to your ERC-20 contract, which is the standard way to do it. After they have some tokens they need to add this token to their metamask account so that they can transfer tokens using metamask. This guide describes how to add custom ERC-20 tokens to metamask: luvcrypto.com/add-send-tokens-metamask

            – nikos fotiadis
            Nov 15 '18 at 14:06












          • And you also need to notify your other contract about the token transfer. I know it is a little more complicated than using ether.

            – nikos fotiadis
            Nov 15 '18 at 14:12











          • thats where I am running into the issue, I am not sure how to differentiate ETH vs the custom token in the contract. I have a UI currently built. But when I use the smart contract and try to replace ETHER value with my token it throws errors. I do understand up to adding the token to metamask and all that. Its really how to get the contract to recognize its an ERC20 token and not ETH ( code noted in original post above)

            – JimmyJong
            Nov 15 '18 at 15:07











          • Ether and tokens are sent in different ways. A transaction may contain ether, but a transaction cannot contain tokens. The transaction will tell the contract to transfer tokens. So the contract can know if it is ERC-20 or Ether based on the function that is executed. When you "send" ERC-20 tokens it is actually just tha balances that change in the ERC-20 contract, you don't really send something with the transaction.

            – nikos fotiadis
            Nov 15 '18 at 15:56













          0












          0








          0







          Your contract is just another address so yes, you can send tokens to your contract. But you cannot send them the same way you send ether, that is using a payable function. You have to transfer the tokens to the contract's address using the transfer method of you ERC-20 token. And to send tokens from the contract to someone else you have to call transfer from inside your contract unless you do something like provide a lot of allowance for your account, but I wouldn't suggest this. How exactly you can call methods from your ERC-20 from inside your other contract is explained in this post.






          share|improve this answer













          Your contract is just another address so yes, you can send tokens to your contract. But you cannot send them the same way you send ether, that is using a payable function. You have to transfer the tokens to the contract's address using the transfer method of you ERC-20 token. And to send tokens from the contract to someone else you have to call transfer from inside your contract unless you do something like provide a lot of allowance for your account, but I wouldn't suggest this. How exactly you can call methods from your ERC-20 from inside your other contract is explained in this post.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 4:14









          nikos fotiadisnikos fotiadis

          6922514




          6922514












          • Thanks Nikos, so I am aware of the transfer function but what I am trying to achieve is using my contract as it exists and using my custom ERC20 token vs ETH. So for example in this game, a user sends X eth to the contract and in return gets a % back each day. But instead of ETH in return I want to provide them my custom ERC20 token and not ETH.

            – JimmyJong
            Nov 15 '18 at 13:30











          • It certainly is possible, but you will need to provide them of a way to buy and send your ERC-20 token. I think the simplest way would using metamask unless you want to create an interface yourself. So if you use metamask you can have them buy tokens buy sending ether to your ERC-20 contract, which is the standard way to do it. After they have some tokens they need to add this token to their metamask account so that they can transfer tokens using metamask. This guide describes how to add custom ERC-20 tokens to metamask: luvcrypto.com/add-send-tokens-metamask

            – nikos fotiadis
            Nov 15 '18 at 14:06












          • And you also need to notify your other contract about the token transfer. I know it is a little more complicated than using ether.

            – nikos fotiadis
            Nov 15 '18 at 14:12











          • thats where I am running into the issue, I am not sure how to differentiate ETH vs the custom token in the contract. I have a UI currently built. But when I use the smart contract and try to replace ETHER value with my token it throws errors. I do understand up to adding the token to metamask and all that. Its really how to get the contract to recognize its an ERC20 token and not ETH ( code noted in original post above)

            – JimmyJong
            Nov 15 '18 at 15:07











          • Ether and tokens are sent in different ways. A transaction may contain ether, but a transaction cannot contain tokens. The transaction will tell the contract to transfer tokens. So the contract can know if it is ERC-20 or Ether based on the function that is executed. When you "send" ERC-20 tokens it is actually just tha balances that change in the ERC-20 contract, you don't really send something with the transaction.

            – nikos fotiadis
            Nov 15 '18 at 15:56

















          • Thanks Nikos, so I am aware of the transfer function but what I am trying to achieve is using my contract as it exists and using my custom ERC20 token vs ETH. So for example in this game, a user sends X eth to the contract and in return gets a % back each day. But instead of ETH in return I want to provide them my custom ERC20 token and not ETH.

            – JimmyJong
            Nov 15 '18 at 13:30











          • It certainly is possible, but you will need to provide them of a way to buy and send your ERC-20 token. I think the simplest way would using metamask unless you want to create an interface yourself. So if you use metamask you can have them buy tokens buy sending ether to your ERC-20 contract, which is the standard way to do it. After they have some tokens they need to add this token to their metamask account so that they can transfer tokens using metamask. This guide describes how to add custom ERC-20 tokens to metamask: luvcrypto.com/add-send-tokens-metamask

            – nikos fotiadis
            Nov 15 '18 at 14:06












          • And you also need to notify your other contract about the token transfer. I know it is a little more complicated than using ether.

            – nikos fotiadis
            Nov 15 '18 at 14:12











          • thats where I am running into the issue, I am not sure how to differentiate ETH vs the custom token in the contract. I have a UI currently built. But when I use the smart contract and try to replace ETHER value with my token it throws errors. I do understand up to adding the token to metamask and all that. Its really how to get the contract to recognize its an ERC20 token and not ETH ( code noted in original post above)

            – JimmyJong
            Nov 15 '18 at 15:07











          • Ether and tokens are sent in different ways. A transaction may contain ether, but a transaction cannot contain tokens. The transaction will tell the contract to transfer tokens. So the contract can know if it is ERC-20 or Ether based on the function that is executed. When you "send" ERC-20 tokens it is actually just tha balances that change in the ERC-20 contract, you don't really send something with the transaction.

            – nikos fotiadis
            Nov 15 '18 at 15:56
















          Thanks Nikos, so I am aware of the transfer function but what I am trying to achieve is using my contract as it exists and using my custom ERC20 token vs ETH. So for example in this game, a user sends X eth to the contract and in return gets a % back each day. But instead of ETH in return I want to provide them my custom ERC20 token and not ETH.

          – JimmyJong
          Nov 15 '18 at 13:30





          Thanks Nikos, so I am aware of the transfer function but what I am trying to achieve is using my contract as it exists and using my custom ERC20 token vs ETH. So for example in this game, a user sends X eth to the contract and in return gets a % back each day. But instead of ETH in return I want to provide them my custom ERC20 token and not ETH.

          – JimmyJong
          Nov 15 '18 at 13:30













          It certainly is possible, but you will need to provide them of a way to buy and send your ERC-20 token. I think the simplest way would using metamask unless you want to create an interface yourself. So if you use metamask you can have them buy tokens buy sending ether to your ERC-20 contract, which is the standard way to do it. After they have some tokens they need to add this token to their metamask account so that they can transfer tokens using metamask. This guide describes how to add custom ERC-20 tokens to metamask: luvcrypto.com/add-send-tokens-metamask

          – nikos fotiadis
          Nov 15 '18 at 14:06






          It certainly is possible, but you will need to provide them of a way to buy and send your ERC-20 token. I think the simplest way would using metamask unless you want to create an interface yourself. So if you use metamask you can have them buy tokens buy sending ether to your ERC-20 contract, which is the standard way to do it. After they have some tokens they need to add this token to their metamask account so that they can transfer tokens using metamask. This guide describes how to add custom ERC-20 tokens to metamask: luvcrypto.com/add-send-tokens-metamask

          – nikos fotiadis
          Nov 15 '18 at 14:06














          And you also need to notify your other contract about the token transfer. I know it is a little more complicated than using ether.

          – nikos fotiadis
          Nov 15 '18 at 14:12





          And you also need to notify your other contract about the token transfer. I know it is a little more complicated than using ether.

          – nikos fotiadis
          Nov 15 '18 at 14:12













          thats where I am running into the issue, I am not sure how to differentiate ETH vs the custom token in the contract. I have a UI currently built. But when I use the smart contract and try to replace ETHER value with my token it throws errors. I do understand up to adding the token to metamask and all that. Its really how to get the contract to recognize its an ERC20 token and not ETH ( code noted in original post above)

          – JimmyJong
          Nov 15 '18 at 15:07





          thats where I am running into the issue, I am not sure how to differentiate ETH vs the custom token in the contract. I have a UI currently built. But when I use the smart contract and try to replace ETHER value with my token it throws errors. I do understand up to adding the token to metamask and all that. Its really how to get the contract to recognize its an ERC20 token and not ETH ( code noted in original post above)

          – JimmyJong
          Nov 15 '18 at 15:07













          Ether and tokens are sent in different ways. A transaction may contain ether, but a transaction cannot contain tokens. The transaction will tell the contract to transfer tokens. So the contract can know if it is ERC-20 or Ether based on the function that is executed. When you "send" ERC-20 tokens it is actually just tha balances that change in the ERC-20 contract, you don't really send something with the transaction.

          – nikos fotiadis
          Nov 15 '18 at 15:56





          Ether and tokens are sent in different ways. A transaction may contain ether, but a transaction cannot contain tokens. The transaction will tell the contract to transfer tokens. So the contract can know if it is ERC-20 or Ether based on the function that is executed. When you "send" ERC-20 tokens it is actually just tha balances that change in the ERC-20 contract, you don't really send something with the transaction.

          – nikos fotiadis
          Nov 15 '18 at 15:56



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • 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%2fstackoverflow.com%2fquestions%2f53308858%2fcan-i-use-my-custom-erc-20-with-my-smart-contract%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







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

          Museum of Modern and Contemporary Art of Trento and Rovereto