Solidity

ERC20 代幣 - 小數和轉售

  • April 3, 2018

我剛剛創建了一個 ERC20 代幣;我將小數點設置為 4,獎金值設置為 1200,然後我將 0.3 乙太幣發送到其合約地址以購買我的新代幣。我收到了 36 萬億個代幣作為回報,我不明白為什麼!在 ETH 網路之前,我在 ROPSTEN 測試網創建了一個相同的合約,但有 18 位小數,我得到了 360 個代幣以換取 0.3 個乙太幣

我有這兩個問題:(a)這裡的數學是什麼,為什麼我在網路 ETH 中獲得了千萬億?(b) 作為發送者或合約所有者,我可以將代幣轉換回乙太幣嗎?如果是的話怎麼辦?

我也嘗試過從 myetherwallet 發送代幣,但它顯示了我的 0 個代幣,但我可以在 etherscan 和 ethplorer.io 的地址下看到代幣

合約程式碼:

   function constructorfunc() public {
       symbol = "TKN";
       name = "token name";
       decimals = 4;
       bonusEnds = now + 1 weeks;
       endDate = now + 7 weeks;

   }



 function () public payable {
       require(now >= startDate && now <= endDate);
       uint tokens;
       if (now <= bonusEnds) {
           tokens = msg.value * 1200;
       } else {
           tokens = msg.value * 1000;
       }
       balances[msg.sender] = safeAdd(balances[msg.sender], tokens);
       _totalSupply = safeAdd(_totalSupply, tokens);
       Transfer(address(0), msg.sender, tokens);
       owner.transfer(msg.value);
   }

我在契約中添加的其他功能

// ------------------------------------------------------------------------
   // Total supply
   // ------------------------------------------------------------------------
   function totalSupply() public constant returns (uint) {
       return _totalSupply  - balances[address(0)];
   }


   // ------------------------------------------------------------------------
   // Get the token balance for account `tokenOwner`
   // ------------------------------------------------------------------------
   function balanceOf(address tokenOwner) public constant returns (uint balance) {
       return balances[tokenOwner];
   }


   // ------------------------------------------------------------------------
   // Transfer the balance from token owner's account to `to` account
   // - Owner's account must have sufficient balance to transfer
   // - 0 value transfers are allowed
   // ------------------------------------------------------------------------
   function transfer(address to, uint tokens) public returns (bool success) {
       balances[msg.sender] = safeSub(balances[msg.sender], tokens);
       balances[to] = safeAdd(balances[to], tokens);
       Transfer(msg.sender, to, tokens);
       return true;
   }


   // ------------------------------------------------------------------------
   // Token owner can approve for `spender` to transferFrom(...) `tokens`
   // from the token owner's account
   //
   // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20-token-standard.md
   // recommends that there are no checks for the approval double-spend attack
   // as this should be implemented in user interfaces
   // ------------------------------------------------------------------------
   function approve(address spender, uint tokens) public returns (bool success) {
       allowed[msg.sender][spender] = tokens;
       Approval(msg.sender, spender, tokens);
       return true;
   }


   // ------------------------------------------------------------------------
   // Transfer `tokens` from the `from` account to the `to` account
   //
   // The calling account must already have sufficient tokens approve(...)-d
   // for spending from the `from` account and
   // - From account must have sufficient balance to transfer
   // - Spender must have sufficient allowance to transfer
   // - 0 value transfers are allowed
   // ------------------------------------------------------------------------
   function transferFrom(address from, address to, uint tokens) public returns (bool success) {
       balances[from] = safeSub(balances[from], tokens);
       allowed[from][msg.sender] = safeSub(allowed[from][msg.sender], tokens);
       balances[to] = safeAdd(balances[to], tokens);
       Transfer(from, to, tokens);
       return true;
   }


   // ------------------------------------------------------------------------
   // Returns the amount of tokens approved by the owner that can be
   // transferred to the spender's account
   // ------------------------------------------------------------------------
   function allowance(address tokenOwner, address spender) public constant returns (uint remaining) {
       return allowed[tokenOwner][spender];
   }


   // ------------------------------------------------------------------------
   // Token owner can approve for `spender` to transferFrom(...) `tokens`
   // from the token owner's account. The `spender` contract function
   // `receiveApproval(...)` is then executed
   // ------------------------------------------------------------------------
   function approveAndCall(address spender, uint tokens, bytes data) public returns (bool success) {
       allowed[msg.sender][spender] = tokens;
       Approval(msg.sender, spender, tokens);
       ApproveAndCallFallBack(spender).receiveApproval(msg.sender, tokens, this, data);
       return true;
   }

這裡的數學是什麼,為什麼我得到了千萬億?

if (now <= bonusEnds) {
 tokens = msg.value * 1200;
} else {
 tokens = msg.value * 1000;
}

msg.value在魏。通過發送0.3 ether,您將300000000000000000 * 1200代幣記入自己的賬戶。

作為發送者或合約所有者,我可以將代幣轉換回乙太幣嗎?

只要我想合約有足夠的 ETH,你就可以兩者都做。您可以包含一個簡單的函式,是否僅適用於所有者,它可以被呼叫並將轉換X tokensY ether您定義的轉換率。

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