虽然函数purchaseProduct()声明为even,但我还不知道为什么会出现上述错误。我在Remix上尝试了这段代码,得到了这个错误。
VM错误:还原。还原事务已恢复到初始状态。注意:如果您发送值,则应支付被调用的函数,而您发送的值应小于当前余额。调试事务以获得更多信息。
除了purchaseProduct()函数之外,一切都很正常。我正在尝试从struct 'User‘、'Product’中获取数据,并在一个新的结构中(即“Order”)重新存储数据。
我已经使用require()进行了验证。在Remix上运行代码时:
pragma solidity >=0.5.0;
pragma experimental ABIEncoderV2;
//Address: '0x7772B9117586b5F745bDb563a935E91b00449157'
contract Buyproduct{
uint public orderCount = 0;
mapping(uint => Order) public orders;
uint public productCount = 0;
mapping(uint => Product) public products;
uint public userCount = 0;
mapping(address => User) public users;
struct Order {
uint oid;
address payable seller;
address payable buyer;
// string timestamp;
string status;
uint pid;
string location;
uint quantitiy;
}
struct Product {
uint pid;
string name;
uint price;
address payable seller;
string info;
uint quantity;
}
struct User {
uint uid;
string name;
uint role;
address payable user;
string location;
bool created;
}
event ProductCreated(
uint pid,
string name,
uint price,
address payable seller,
string info,
uint quantity
);
event UserCreated(
uint uid,
string name,
uint role,
address payable user,
string location,
bool created
);
event ProductPurchased(
uint oid,
address payable seller,
address payable buyer,
string status,
uint pid,
string location,
uint quantity
);
event ReviewAdded(
uint rid,
address reviewer,
uint pid,
string review
);
function createUser(string memory _name, uint _role, string memory _location) public payable{
//No repeated address
require(users[msg.sender].created == false, 'User already created');
//Increase userCount
userCount++;
//Add user
users[msg.sender] = User(userCount, _name, _role, msg.sender, _location, true);
//Trigger an event
emit UserCreated(userCount, _name, _role, msg.sender, _location, true);
}
function createProduct(string memory _name, uint _price, string memory _info, uint _quantity) public payable {
// Require a valid price
require(_price > 0, 'Invalid Price');
// Increment product count
productCount ++;
// Create the product
products[productCount] = Product(productCount, _name, _price, msg.sender, _info, _quantity);
// Trigger an event
emit ProductCreated(productCount, _name, _price, msg.sender, _info, _quantity);
}
function purchaseProduct(uint _id, /*string memory timestamp,*/ uint _quantity) public payable {
// Fetch the product
Product memory _product = products[_id];
// Fetch the owner
address payable _seller = _product.seller;
//Validate the buyer
require(users[msg.sender].created == true, 'Unregistered user');
// Make sure the product has a valid id
require(_product.pid > 0 && _product.pid <= productCount, 'Invalid Product ID');
// Require that there is enough Ether in the transaction
require(msg.value >= _product.price, 'Not enough ether in Wallet');
// Require that the buyer is not the seller
require(_seller != msg.sender,'Invalid Purchase');
//Incrmement orderCount
orderCount++;
// Transfer ownership to the buyer
orders[orderCount] = Order(orderCount, _seller, msg.sender,/* ts,*/ 'Ordered', _product.pid, users[msg.sender].location, _quantity);
//Reduce the quantity of the product
_product.quantity -= _quantity;
// Pay the contract by sending them Ether
address payable wallet = address(uint160(address(this)));
wallet.transfer(msg.value);
// Trigger an event
emit ProductPurchased(orderCount, _seller, msg.sender, /*timestamp,*/ 'Ordered', _product.pid, users[msg.sender].location, _quantity);
}
}'''发布于 2020-04-08 11:45:57
function createProduct(string memory _name, uint _price, string memory _info, uint _quantity) public payable {这个函数的签名在醚中有_price。因此,尝试更改默认单位从卫乙醚,并发送适当的价格的产品。

我的在工作
[vm]from:0x4b0...4d2dbto:Buyproduct.purchaseProduct(uint256,uint256) 0x1df...bda71value:2000000000000000000 weidata:0xa04...00001logs:1hash:0x4e0...0d2b0您也不需要显式地调用这些。
address payable wallet = address(uint160(address(this)));
wallet.transfer(msg.value);合同余额将自动更新。您可以通过在合同上实现此功能来进行检查。
function getBalance()
public
view
returns(uint) {
return address(this).balance;}https://ethereum.stackexchange.com/questions/82275
复制相似问题