我的问题是无法通过transfer()
功能从买家地址到所有者地址付款。我在completeOrder()
中尝试过这样做,但我一次又一次地遇到同样的错误。然后我试着在Remix ide上的买家地址通过sendtoOwner
功能进行调试。错误保持不变。
- 所有者地址=0xca35b7d915458ef540ade6068dfe2f44e8fa733c
- 买方地址=0x14723a09acff6d2a60dcdf7aa4aff308fddc160c
我收到的错误消息:
交易到UPChain.sendtoOwner错误:VM错误:revert
revert事务已恢复到初始状态
注意:如果您发送值,则构造函数应该是应付的
调试事务以获取更多信息。
我的代码:
pragma solidity ^0.5.0;
contract UPChain {
address payable public owner;
address payable public buyerAd;
uint private price;
Order private order;
mapping(uint => Product) offeredProducts;
uint private productseq;
uint private orderedProductseq;
struct Product{
string productName;
uint productPrice;
}
struct Order{
uint orderNo;
mapping(uint => Product) products;
}
modifier ownerMod {
require(owner == msg.sender);
_;
}
modifier buyerMod {
require(buyerAd == msg.sender);
_;
}
function () external payable {
buyerAd.transfer(msg.value);
}
constructor (address payable _buyerAd) public payable {
buyerAd = _buyerAd;
order = Order(1);
owner = msg.sender;
}
function addProduct(string memory _productName,uint _productPrice) public ownerMod{
offeredProducts[productseq] = Product(_productName,_productPrice);
productseq++;
}
function queryProduct(uint _productseq) public view returns(string memory, uint){
return (offeredProducts[_productseq].productName,offeredProducts[_productseq].productPrice);
}
// functions used by buyer
function addtoOrder(uint _productseq) public buyerMod{
order.products[orderedProductseq] = offeredProducts[_productseq];
orderedProductseq++;
}
function queryOrderedProduct(uint _orderedProductseq) public view buyerMod returns(string memory,uint){
return(order.products[_orderedProductseq].productName,order.products[_orderedProductseq].productPrice);
}
function completeOrder() public payable buyerMod returns(string memory,uint){
uint totalPrice=0;
for(uint i=0; i<orderedProductseq;i++){
totalPrice+= order.products[i].productPrice*1000000000000000000;
}
if(msg.value > totalPrice){
owner.transfer(msg.value);
return ("Order is completed", buyerAd.balance);
}
else{
revert();
return ("Order is not completed", buyerAd.balance);
}
}
//
function sendtoOwner(uint256 value) public payable buyerMod{
owner.transfer(value);
}
function setPrice(uint _price) public ownerMod {
price = _price;
}
function getPrice() public view returns(uint) {
return price;
}
function getContractValue() public payable returns(uint){
return owner.balance;
}
}
我可以毫无问题地运行您的代码。当您调用以下功能时
function sendtoOwner(uint256 value) public payable buyerMod{
owner.transfer(value);
}
你是用业主账户还是买家账户打电话。在执行此函数时,您需要以买家的身份调用,因为您有一个修饰符来检查其买家或所有者。如果它不是买家,那么它将恢复,这就是我预计在你的情况下会发生的事情。