简体   繁体   English

我可以将我的自定义 ERC-20 与我的智能合约一起使用吗?

[英]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.所以我有一个合同,允许你用 ETH 交换我的自定义 ERC20 代币。 I am wanting to use that custom ERC20 token now with other smart contracts.我现在想将该自定义 ERC20 令牌与其他智能合约一起使用。 Is there a certain way I have to specify the custom token vs ETH?是否有某种方式我必须指定自定义令牌与 ETH?

example:例子:

pragma solidity ^0.4.24; pragma solidity ^0.4.24;

/* * ---How to use: * 1. Send HYPER Tokens to the smart contract address in any amount. /* * ---如何使用: * 1. 向智能合约地址发送任意数量的 HYPER 代币。 * 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 { * 2. 通过发送 0 次 HYPER 交易(每小时 1 次)来索取您的利润 * 3. 如果您不提款并且赚取超过 200%,则您只能提款一次超过 200% 的限额 */ 合约 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;函数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.我不想返回 ETH,我想使用我的自定义 ERC20 令牌给用户发送到合约并获得 ERC20 令牌的回报百分比。

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.用户将购买代币,购买将以太币发送到您的 ERC-20 合约。 The rate between Ether and your token is totally up to you as it is relative to the business and not Ether itself. Ether 和您的代币之间的比率完全取决于您,因为它与业务相关,而不是 Ether 本身。
    In this business case let's say we give the 100 tokens for each Ether.在这个商业案例中,假设我们为每个以太币提供 100 个代币。 The user passes 2 Ether to the contract and 200 of your business tokens are then put into your contract's用户将 2 个 Ether 传递给合约,然后将 200 个商业代币放入合约的
    mapping(address => uint) accounts . mapping(address => uint) accounts
    Given this the following code would give you 200:鉴于此,以下代码将为您提供 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.然后这个函数负责调用你的 ERC-20 合约来检查用户是否有足够的资金考虑他/她的地址。 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.如果是这样,那么您的合约会将给定数量的代币转移到您的 ERC-20 合约中,以转移到可用的代币或您想要的另一个钱包,基本上是将用户(地址)代币转移到您决定的另一个地址。

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.请记住,必须支付 GAS 费用。 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.您必须使用 ERC-20 代币的转移方法将代币转移到合约地址。 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.并且要将合约中的代币发送给其他人,您必须从合约内部调用 transfer ,除非您执行诸如为您的帐户提供大量津贴之类的操作,但我不建议这样做。 How exactly you can call methods from your ERC-20 from inside your other contract is explained in this post. 这篇文章解释了如何从其他合约内部调用 ERC-20 中的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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