简体   繁体   中英

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

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.

Consider a e-commerce business for example. So the way I did it was:

  1. The user would buy tokens buy sending Ether to your ERC-20 contract. The rate between Ether and your token is totally up to you as it is relative to the business and not Ether itself.
    In this business case let's say we give the 100 tokens for each Ether. The user passes 2 Ether to the contract and 200 of your business tokens are then put into your contract's
    mapping(address => uint) accounts .
    Given this the following code would give you 200:
    accounts[customer_address] //(returns 200)

  2. Then you would have another contract, let's say to purchase a given item. You just have to call this smart contract purchase function from that address. Then this function is responsible to call your ERC-20 contract to check if the user has enough funds considering his/her address. If so, then your contract would transfer the given number of tokens to your ERC-20 contract to the available tokens or another wallet you'd like it to, basically transferring the user (address) tokens to another address you decide to.

This way your user is using your tokens which have been previously bought and not even thinking about Ether. Please remember that the GAS fee has to be paid. So either you pay for it or your customers will (include it on the price of the item or so).

Hope this helps someone with the same question :)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM