Solidity

如何從總供應量中移除代幣?

  • January 29, 2018

嘿伙計們,我想知道在有人“購買”代幣後如何從總供應量中刪除代幣。我可以在這裡複製和粘貼什麼功能,因為它可以讓帳戶購買必要的代幣,但不會將它們從總供應量中刪除?

pragma solidity ^0.4.18;

import './XXERC20.sol';
import './SafeMath.sol';

  contract XXPreOrderToken is XXERC20 {

   using SafeMath for uint256;

   uint public _totalSupply = 72500000000000000000000;

   string public constant symbol ="XXPOT";
   string public constant name = "PreOrderToken";
   uint8 public constant decimals = 18;

   //1 ether = 1000 pre-order tokens
   uint256 public constant RATE = 1000;

   address public owner;

   mapping(address => uint256) balances;
   mapping(address => mapping(address => uint256)) allowed;

   function() payable{
       createTokens();
   }

   function XXPreOrderToken() {
       balances[msg.sender] = _totalSupply;
       owner = msg.sender;
   }

   function createTokens() payable {
       require(msg.value > 0);

       uint256 tokens = msg.value.mul(RATE);
       balances[msg.sender] = balances[msg.sender].add(tokens);
       _totalSupply = _totalSupply.add(tokens);

       owner.transfer(msg.value);
   }

   function totalSupply() constant returns (uint256 totalSupply) {
       return _totalSupply;
   }

   function balanceOf(address _owner) constant returns (uint256 balance) {
       return balances[_owner];
   }

   function transfer(address _to, uint256 _value) returns (bool success) {
       require(
           balances[msg.sender] >= _value
           && _value > 0
           );
       balances[msg.sender] = balances[msg.sender].sub(_value);
       balances[_to] = balances[_to].add(_value);
       Transfer(msg.sender, _to, _value);
       return true;
   }

   function transferFrom(address _from, address _to, uint256 _value) returns (bool success) {
       require(
           allowed[_from][msg.sender] >= _value
           && balances[_from] >= _value
           && _value > 0
           );
           balances[_from] = balances[_from].sub(_value);
           balances[_to] = balances[_to].add(_value);
           allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value);
           Transfer(_from, _to, _value);
           return true;
   }

   function approve(address _spender, uint256 _value) returns (bool success) {
       allowed[msg.sender][_spender] = _value;
       Approval(msg.sender, _spender, _value);
       return true;
   }

   function allowance(address _owner, address _spender) constant returns (uint256 remaining) {
       return allowed[_owner][_spender];
   }
}

在我看來,總供應量永遠不會改變。總供應量基本上是“最大”供應量。

通過將代幣從一個人轉移到另一個人,總供應量沒有改變,只是一個代幣的所有者發生了變化。

當你創建這個合約時,所有的代幣都會被創建並分配給創建合約的地址(在函式 XXPreOrderToken() 中)

當有人購買代幣時,它應該簡單地從合約所有者轉移到進行購買的人(地址)。

而不是這個功能:

function createTokens() payable {
   require(msg.value > 0);

   uint256 tokens = msg.value.mul(RATE);
   balances[msg.sender] = balances[msg.sender].add(tokens);
   _totalSupply = _totalSupply.add(tokens);

   owner.transfer(msg.value);
}

你可能想要這樣的東西:

function buyTokens() payable {
   require(msg.value > 0);

   uint256 _numTokens = msg.value.mul(RATE);

   require(balances[owner] >= _numTokens);

   balances[owner] = balances[owner].sub(_numTokens);
   balances[msg.sender] = balances[msg.sender].add(_numTokens);
   Transfer(owner, msg.sender, _numTokens);

   owner.transfer(msg.value);
}

引用自:https://ethereum.stackexchange.com/questions/37824