Es gibt buyprice
on function buy
und sellprice
on function sell
. Ich möchte einrichten
1 ETH = 10000 (SYMBOL)
0.1 ETH = 1000 (SYMBOL)
0.01 ETH = 100 (SYMBOL)
0.001 ETH = 10 (SYMBOL)
Wie kann ich eine Zahl auf buyprice
und setzen sellprice
? Selbst wenn ich es berechnet habe, funktioniert es überhaupt nicht
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
}
Dies scheint Teil des Beispiels der Ethereum-Website zu sein, der gesamte Code ist dort gepostet. Der Preis kann im Konstruktor festgelegt werden
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()..
}
Wenn Sie möchten, dass der Preis im Laufe der Zeit geändert wird, fügen Sie diese Funktion hinzu:
function updateprice(uint _sellprice, uint buyprice) public {
sellprice = _sellprice;
buyprice = _buyprice;
}
Hoffe das hilft.
OK, zunächst müssen Sie alle Beträge in Basiseinheiten umrechnen. Für Ether ist die Grundeinheit Wei und 1 ETH = 1e18 Wei (1.000.000.000.000.000.000 Wei). So,
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)
Ihr Token scheint auch 18 Dezimalstellen zu haben, also ist ein ganzer Token 1e18 Basiseinheiten (BU), und
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)
Jetzt können Sie Ihre An- und Verkaufspreise in Basiseinheiten berechnen:
tokenAmount = ethAmount / buyPrice
10000 (SYMBOL) = 1 ETH / buyPrice
1e22 BU = 1e18 Wei / buyPrice
buyPrice = 1e-4
buyPrice = 0.0001
Das Problem ist, dass Solidity keine Bruchzahlen unterstützt, sodass Sie nicht direkt durch 0,0001 dividieren können. Glücklicherweise können Sie einfach so mit 10000 multiplizieren:
uint invertedBuyPrice = 10000;
amount = msg.value * invertedBuyPrice;
Jetzt Verkaufspreis:
ethAmount = tokenAmount * sellPrice
1 ETH = 10000 (SYMBOL) * sellPrice
1e18 Wei = 1e22 BU * sellPrice
sellPrice = 1e-4
sellPrice = 0.0001
Jetzt haben wir das gleiche Problem, der Seel-Preis ist gebrochen und Solidity kann nicht mit Brüchen umgehen, obwohl die gleiche Lösung auch hier funktioniert:
invertedSellPrice = 10000;
revenue = amount / invertedSellPrice;
Informationen zu HTML