简体   繁体   English

ERC20合约内部余额/转账

[英]ERC20 contract inner balance / transfer

It is possible (even if I do not see the point yet) to send some tokens to its ERC20 contract itself even if Metamask warns you about sending to a contract.即使 Metamask 警告您发送到合约,也有可能(即使我还没有看到这一点)将一些代币发送到其 ERC20 合约本身。

I wondered a few things, let's say I have deployed an "ABC" ERC20 token contract:我想知道一些事情,假设我已经部署了一个“ABC”ERC20 代币合约:

  1. Is there any point at all for that ERC20 token contract to hold ABC tokens or even other tokens in it?该 ERC20 代币合约在其中持有 ABC 代币甚至其他代币是否有任何意义?
  2. If I send some ABC token to the ABC ERC20 token contract address, how should/could I call approve/transfer or transferFrom from an ABC ERC20 contract function in solidity to get them sent to a given address (I store the "owner" address used for deployment for instance)?如果我将一些 ABC 代币发送到 ABC ERC20 代币合约地址,我应该/可以如何从 ABC ERC20 合约 function 中调用批准/转移或 transferFrom 以将它们发送到给定地址(我存储使用的“所有者”地址例如部署)?

I hope this does make sense to someone;)我希望这对某人有意义;)

Thanks !谢谢 !

1. Is there any point at all for that ERC20 token contract to hold ABC tokens or even other tokens in it? 1. ERC20 代币合约中持有 ABC 代币甚至其他代币是否有任何意义?

Built-in buy and sell functions that allow you to partially control the token market price through arbitrage between your own contract and DeX pools.内置买卖功能,允许您通过自己的合约和 DeX 池之间的套利来部分控制代币市场价格。 For this, your contract needs to hold sufficient amounts of both ABC and USDT.为此,您的合约需要同时持有足够数量的 ABC 和 USDT。

Example: Your contract implements functions that allow users to buy 1 ABC token for 1 USDT, and sell 1 ABC token for 0.99 USDT.示例:您的合约实现了允许用户以 1 USDT 购买 1 个 ABC 代币并以 0.99 USDT 出售 1 个 ABC 代币的功能。 If the current price in a DeX pool is 1.05 USDT, arbitrage traders can buy ABC from your contract for the fixed price of 1 USDT and immediately sell it in the DeX pool - effectively balancing the DeX pool liquidity amounts up to the point where the price in the DeX pool reaches 1 USDT as well.如果 DeX 池中的当前价格为 1.05 USDT,套利交易者可以从您的合约中以 1 USDT 的固定价格买入 ABC,并立即在 DeX 池中卖出 - 有效平衡 DeX 池的流动性,直至达到价格点DeX 池中也达到 1 USDT。

2. If I send some ABC token to the ABC ERC20 token contract address, how should/could I call approve/transfer or transferFrom from an ABC ERC20 contract function in solidity to get them sent to a given address (I store the "owner" address used for deployment for instance)? 2. 如果我将一些 ABC 代币发送到 ABC ERC20 代币合约地址,我应该/可以如何从 ABC ERC20 合约 function 中调用批准/转移或 transferFrom 以将它们发送到给定地址(我存储“所有者”例如用于部署的地址)?

Your ABC contract needs to implement a custom function to withdraw the tokens from its address.您的 ABC 合约需要实现自定义 function 以从其地址中提取代币。 Example using OpenZeppelin libraries:使用 OpenZeppelin 库的示例:

contract ABC is ERC20, Ownable {
    // allows the owner to withdraw `amount` of ABC tokens from the ABC contract
    function withdraw(uint256 amount) onlyOwner {
        _transfer(address(this), owner(), amount);
    }
}

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

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