Solidity

在 Remix 支付源中設置一個數字

  • April 20, 2022

buypriceon functionbuysellpriceon function sell。我想設置

1 ETH =  10000 (SYMBOL)
0.1 ETH = 1000 (SYMBOL)
0.01 ETH = 100 (SYMBOL)
0.001 ETH = 10 (SYMBOL)

如何在buypriceand上設置數字sellprice?即使我計算了它,它根本不起作用

function buy() payable returns (uint amount) {
 amount = msg.value / buyPrice;      // calculates the amount
 require(balanceOf[this] >= amount); // checks if it has enough to sell
 balanceOf[msg.sender] += amount;    // adds the amount to buyer's balance
 balanceOf[this] -= amount;          // subtracts amount from seller's balance
 Transfer(this, msg.sender, amount); // execute an event reflecting the change
 return amount;                      // ends function and returns
}

function sell(uint amount) returns (uint revenue) {
 require(balanceOf[msg.sender] >= amount); // checks if the sender has enough to sell
 balanceOf[this] += amount;                // adds the amount to owner's balance
 balanceOf[msg.sender] -= amount;          // subtracts the amount from seller's balance
 revenue = amount * sellPrice;
 msg.sender.transfer(revenue);             // sends ether to the seller: it's important to do this last to prevent recursion attacks
 Transfer(msg.sender, this, amount);       // executes an event reflecting on the change
 return revenue;                           // ends function and returns
}

這似乎是乙太坊網站範例的一部分,整個程式碼都發佈在那裡。價格可以在建構子中設置

contract Mycontract{
   uint public sellprice;
   uint public buyprice;
   address public admin;

   constructor(uint _sellprice, uint _buyprice) {
       sellprice = _sellprice;
       buyprice = _buyprice;
       admin = msg.sender;
   }

   function sell()...

   function buy()..


}

如果您需要隨時間更改價格,請添加此功能:

function updateprice(uint _sellprice, uint buyprice) public {
   sellprice = _sellprice;
   buyprice = _buyprice;
}

希望這可以幫助。

好的,首先您需要將所有金額轉換為基本單位。對於乙太幣,基本單位是 Wei,1 ETH = 1e18 Wei(1,000,000,000,000,000,000 Wei)。所以,

1 ETH = 1e18 Wei (1,000,000,000,000,000,000 Wei)
0.1 ETH = 1e17 Wei (100,000,000,000,000,000 Wei)
0.01 ETH = 1e16 Wei (10,000,000,000,000,000 Wei)
0.001 ETH = 1e15 Wei  (1,000,000,000,000,000 Wei)

你的代幣似乎也有 18 位小數,所以一個完整的代幣是 1e18 個基本單位 (BU),並且

10000 (SYMBOL) = 1e22 BU (10,000,000,000,000,000,000,000 BU)
1000 (SYMBOL) = 1e21 BU (1,000,000,000,000,000,000,000 BU)
100 (SYMBOL) = 1e20 BU (100,000,000,000,000,000,000 BU)
10 (SYMBOL) = 1e19 BU (10,000,000,000,000,000,000 BU)

現在您可以計算基本單位的買入和賣出價格:

tokenAmount = ethAmount / buyPrice
10000 (SYMBOL) = 1 ETH / buyPrice
1e22 BU = 1e18 Wei / buyPrice
buyPrice = 1e-4
buyPrice = 0.0001

問題是 Solidity 不支持小數,所以你不能直接除以 0.0001。幸運的是,您可以像這樣乘以 10000:

uint invertedBuyPrice = 10000;
amount = msg.value * invertedBuyPrice;

現在賣價:

ethAmount = tokenAmount * sellPrice
1 ETH = 10000 (SYMBOL) * sellPrice
1e18 Wei = 1e22 BU * sellPrice
sellPrice = 1e-4
sellPrice = 0.0001

現在我們遇到了同樣的問題,seel 價格是分數,Solidity 不能處理分數,儘管同樣的解決方案在這裡也適用:

invertedSellPrice = 10000;
revenue = amount / invertedSellPrice;

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