Contract-Development

如何在買方賣方類型的契約中進行一些乙太幣的交易?

  • January 8, 2019

在此我設置msg.sender == sellerAddress了無效的修飾符 ( )。但在這種情況下,我只想允許並且只允許賣方地址從我的契約中獲取乙太幣。我怎樣才能只允許賣家地址這樣做。以及在執行契約時我必須輸入哪種輸入?

contract ContractDemo {
   address public buyerAddress;
   address public sellerAddress;
   uint public price; //price or charge for delivering parcel

   modifier onlyBuyer() {
       require(msg.sender == buyerAddress);
       _;
   }

   modifier onlySeller() {
       require(msg.sender == sellerAddress);
       _;
   }

   constructor(address payable _sellerAddress) public payable {
       buyerAddress = msg.sender; 
       sellerAddress = _sellerAddress;
       price = msg.value;
   }

   function deliveredParcel(address payable _seller) onlySeller public payable returns(bool) {
       require(msg.value == price);
       _seller.transfer(msg.value); 
       return true;
   }
}

使用賣家地址呼叫該deliverParcel函式,然後修飾符將正常工作。該功能將像

function deliveredParcel()onlySeller public payable returns( bool) {
   require(msg.value == price);
   sellerAddress.transfer(msg.value); 
   return true;
  }

將修飾符更改為此

modifier onlySeller(address seller){
   require(seller == sellerAddress);
   _;
}

該功能將分別如下所示

function deliveredParcel(address payable _seller)onlySeller(_seller) public payable returns( bool) {
   require(msg.value == price);
   _seller.transfer(msg.value); 
   return true;
  }

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