Ether

我們如何設置代幣的價格?在契約中,本身還是在部署之後?

  • August 10, 2021

這是我輸入買入價的功能嗎?如果是這樣,契約中如何、在哪里以及如何?

我希望代幣可兌換為 ETH,並試圖了解價格是如何設定的,因此它在交易所中上漲..?我們需要在部署之前在合約中設置價格嗎?

/// @notice Allow users to buy tokens for `newBuyPrice` eth 

/// @param newBuyPrice Price users can buy from the contract
function setPrices(uint256 newBuyPrice) onlyOwner public {
   buyPrice = newBuyPrice;

}

/// @notice Buy tokens from contract by sending ether
function buy() payable public {
   uint amount = msg.value / buyPrice;               // calculates the amount
   _transfer(this, msg.sender, amount);              // makes the transfers
}

整個契約:

pragma solidity ^0.4.16;


contract owned {
   address public owner;

   function owned() public {
       owner = msg.sender;
   }

   modifier onlyOwner {
       require(msg.sender == owner);
       _;
   }

   function transferOwnership(address newOwner) onlyOwner public {
       owner = newOwner;
   }
}

interface tokenRecipient { function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) public; }



/**
* @title SafeMath
* @dev Math operations with safety checks that throw on error
*/
library SafeMath {

 /**
 * @dev Multiplies two numbers, throws on overflow.
 */
 function mul(uint256 a, uint256 b) internal pure returns (uint256) {
   if (a == 0) {
     return 0;
   }
   uint256 c = a * b;
   assert(c / a == b);
   return c;
 }

 /**
 * @dev Integer division of two numbers, truncating the quotient.
 */
 function div(uint256 a, uint256 b) internal pure returns (uint256) {
   // assert(b > 0); // Solidity automatically throws when dividing by 0
   uint256 c = a / b;
   // assert(a == b * c + a % b); // There is no case in which this doesn't hold
   return c;
 }

 /**
 * @dev Substracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).
 */
 function sub(uint256 a, uint256 b) internal pure returns (uint256) {
   assert(b <= a);
   return a - b;
 }

 /**
 * @dev Adds two numbers, throws on overflow.
 */
 function add(uint256 a, uint256 b) internal pure returns (uint256) {
   uint256 c = a + b;
   assert(c >= a);
   return c;
 }
}

contract xxxxxx {

   // Public variables of the token
   string public name = 'xxxxxx';
   string public symbol = 'xx';
   uint8 public decimals = 18;
   // 18 decimals is the strongly suggested default, avoid changing it
   uint256 public totalSupply = 100000000000000000000000000;
   uint256 public unitsOneEthCanBuy = 9500;     // How many units of your coin can be bought by 1 ETH?
   address public fundsWallet = msg.sender;           // Where should the raised ETH go?

   // This creates an array with all balances
   mapping (address => uint256) public balanceOf;
   mapping (address => mapping (address => uint256)) public allowance;

   // This generates a public event on the blockchain that will notify clients
   event Transfer(address indexed from, address indexed to, uint256 value);

   // This notifies clients about the amount burnt
   event Burn(address indexed from, uint256 value);


   /**
    * Constrctor function
    *
    * Initializes contract with initial supply tokens to the creator of the contract
    */
   function xxxxxx (
       uint256 initialSupply,
       string tokenName,
       string tokenSymbol 

   ) public {
       totalSupply = 100000000000000000000000000; initialSupply = 75000000000000000000000000 * 10 ** uint256(decimals);  // Update total supply with the decimal amount
       balanceOf[msg.sender] = totalSupply;                // Give the creator all initial tokens
       name = tokenName;                                   // Set the name for display purposes
       symbol = tokenSymbol;                               // Set the symbol for display purposes


   }


   /**
    * Internal transfer, only can be called by this contract
    */
   function _transfer(address _from, address _to, uint _value) internal {
       // Prevent transfer to 0x0 address. Use burn() instead
       require(_to != 0x0);
       // Check if the sender has enough
       require(balanceOf[_from] >= _value);
       // Check for overflows
       require(balanceOf[_to] + _value > balanceOf[_to]);
       // Save this for an assertion in the future
       uint previousBalances = balanceOf[_from] + balanceOf[_to];
       // Subtract from the sender
       balanceOf[_from] -= _value;
       // Add the same to the recipient
       balanceOf[_to] += _value;
       Transfer(_from, _to, _value);
       // Asserts are used to use static analysis to find bugs in your code. They should never fail
       assert(balanceOf[_from] + balanceOf[_to] == previousBalances);
   }

   /**
    * Transfer tokens
    *
    * Send `_value` tokens to `_to` from your account
    *
    * @param _to The address of the recipient
    * @param _value the amount to send
    */
   function transfer(address _to, uint256 _value) public {
       _transfer(msg.sender, _to, _value);
   }

   /**
    * Transfer tokens from other address
    *
    * Send `_value` tokens to `_to` in behalf of `_from`
    *
    * @param _from The address of the sender
    * @param _to The address of the recipient
    * @param _value the amount to send
    */
   function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
       require(_value <= allowance[_from][msg.sender]);     // Check allowance
       allowance[_from][msg.sender] -= _value;
       _transfer(_from, _to, _value);
       return true;
   }

   /**
    * Set allowance for other address
    *
    * Allows `_spender` to spend no more than `_value` tokens in your behalf
    *
    * @param _spender The address authorized to spend
    * @param _value the max amount they can spend
    */

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

   /**
    * Set allowance for other address and notify
    *
    * Allows `_spender` to spend no more than `_value` tokens in your behalf, and then ping the contract about it
    *
    * @param _spender The address authorized to spend
    * @param _value the max amount they can spend
    * @param _extraData some extra information to send to the approved contract
    */
   function approveAndCall(address _spender, uint256 _value, bytes _extraData)
       public
       returns (bool success) {
       tokenRecipient spender = tokenRecipient(_spender);
       if (approve(_spender, _value)) {
           spender.receiveApproval(msg.sender, _value, this, _extraData);
           return true;
       }
       }

   /**
    * Destroy tokens
    *
    * Remove `_value` tokens from the system irreversibly
    *
    * @param _value the amount of money to burn
    */


   function burn(uint256 _value) public returns (bool success) {
       require(balanceOf[msg.sender] >= _value);   // Check if the sender has enough
       balanceOf[msg.sender] -= _value;            // Subtract from the sender
       totalSupply -= _value;                      // Updates totalSupply
       Burn(msg.sender, _value);
       return true;
   }

   /**
    * Destroy tokens from other account
    *
    * Remove `_value` tokens from the system irreversibly on behalf of `_from`.
    *
    * @param _from the address of the sender
    * @param _value the amount of money to burn
    */
   function burnFrom(address _from, uint256 _value) public returns (bool success) {
       require(balanceOf[_from] >= _value);                // Check if the targeted balance is enough
       require(_value <= allowance[_from][msg.sender]);    // Check allowance
       balanceOf[_from] -= _value;                         // Subtract from the targeted balance
       allowance[_from][msg.sender] -= _value;             // Subtract from the sender's allowance
       totalSupply -= _value;                              // Update totalSupply
       Burn(_from, _value);
       return true;
   }



}

/******************************************/
/*       ADVANCED TOKEN STARTS HERE       */
/******************************************/

contract MyAdvancedToken is owned, xxxxxx {

   uint256 public sellPrice;
   uint256 public buyPrice;

   mapping (address => bool) public frozenAccount;

   /* This generates a public event on the blockchain that will notify clients */
   event FrozenFunds(address target, bool frozen);

   /* Initializes contract with initial supply tokens to the creator of the contract */
   function MyAdvancedToken(
       uint256 initialSupply,
       string tokenName,
       string tokenSymbol
   ) xxxxxx (initialSupply, tokenName, tokenSymbol) public {}

   /* Internal transfer, only can be called by this contract */
   function _transfer(address _from, address _to, uint _value) internal {
       require (_to != 0x0);                               // Prevent transfer to 0x0 address. Use burn() instead
       require (balanceOf[_from] >= _value);               // Check if the sender has enough
       require (balanceOf[_to] + _value > balanceOf[_to]); // Check for overflows
       require(!frozenAccount[_from]);                     // Check if sender is frozen
       require(!frozenAccount[_to]);                       // Check if recipient is frozen
       balanceOf[_from] -= _value;                         // Subtract from the sender
       balanceOf[_to] += _value;                           // Add the same to the recipient
       Transfer(_from, _to, _value);
   }

   /// @notice Create `mintedAmount` tokens and send it to `target`
   /// @param target Address to receive the tokens
   /// @param mintedAmount the amount of tokens it will receive
   function mintToken(address target, uint256 mintedAmount) onlyOwner public {
       balanceOf[target] += mintedAmount;
       totalSupply += mintedAmount;
       Transfer(0, this, mintedAmount);
       Transfer(this, target, mintedAmount);
   }

   /// @notice `freeze? Prevent | Allow` `target` from sending & receiving tokens
   /// @param target Address to be frozen
   /// @param freeze either to freeze it or not
   function freezeAccount(address target, bool freeze) onlyOwner public {
       frozenAccount[target] = freeze;
       FrozenFunds(target, freeze);
   }

   /// @notice Allow users to buy tokens for `newBuyPrice` eth 

   /// @param newBuyPrice Price users can buy from the contract
   function setPrices(uint256 newBuyPrice) onlyOwner public {
       buyPrice = newBuyPrice;


   }

   /// @notice Buy tokens from contract by sending ether
   function buy() payable public {
       uint amount = msg.value / buyPrice;               // calculates the amount
       _transfer(this, msg.sender, amount);              // makes the transfers
   }


       function giveBlockReward() public {
       balanceOf[block.coinbase] += 1;
   }
}

區分pricerate.呼叫rate智能聯繫人中的值。速率不應該是可調的。通過使其不可調整,任何對您的 ICO 感興趣的人在購買之前就知道他們會得到什麼。他們不必相信任何人或任何事。他們“看到”程式碼中的確切匯率。因為程式碼是不可變的(並且軟體的供應商沒有能力改變費率),ICO 參與者能夠相信他們會得到他們所期望的。

我認為代price幣相對於鏈下其他貨幣的交易價格。price也不受代幣供應商的控制。人們買賣代幣price通過簡單的市場力量和供求關係來決定。

**注意:**我上面說過exchange rate應該是不可調節的。對於任何閱讀本文的人——永遠不要購買價格可調的智能合約。我不是指隨時間變化的費率——我是指契約所有者可以隨意更改的費率。他們可以啟動 ICO,等待幾秒鐘直到 FOMO 啟動,改變利率,然後拿比人們預期更多的錢來換取更少的代幣。

**注2:**我不確定exchange rate是正確的詞。如果有人建議一個更好的詞,我會改變它。

**注 3:**鑑於 OP 明顯不了解他/她在做什麼,我絕對不會購買這個 ICO。那是我個人說的,不以任何方式作為建議。

第一個價格由您在流動性池創建時的價格給出。

如果您創建一個代幣,然後首先以 100 ETH = 100 TKN 製作 LP,那麼您的代幣將以 1ETH 的價格開始。您需要在錢包中同時擁有 100ETH 和 100TKN 才能創建 LP。

之後,它將遵循市場的願望。如果人們開始購買 TKN,LP 中的利率將向 TKN 傾斜,價格將上漲。

如果人們開始出售 TKN,那麼 LP 中的利率將改變為有利於 ETH,並且價格將會降低。

所以……你的代幣價格是由流動性池中的匯率給出的,給定另一個代幣或硬幣作為參考。

將 LP 視為印鈔或燒錢的中央銀行。任何事物的價值都是由另一個事物作為參考來賦予的。沒有什麼(說沒有)有絕對值。

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