Error
嘗試編譯合約時在我的合約中創建的函式顯示了這一點;TypeError:引用的聲明既不是修飾符也不是基類
//SPDX-Licence-Identifier: MIT pragma solidity ^0.8.11; contract PurchaseAgreement { // VARIABLES uint public value; address payable public seller; address payable public buyer; enum State { Created, Locked, Release, Inactive} State public state; constructor() payable { seller = payable(msg.sender); value = msg.value / 2; // buyer = payable(msg.sender); } /// The function can not be called at the current state error InvalidState(); /// Only buyer can call this function error onlyBuyer(); /// Only buyer can call this function error onlySeller(); modifier inState(State state_) { if (state != state_) { revert InvalidState(); } _; } modifier onlyBuyer() { if (msg.sender != buyer) { revert onlyBuyer(); } _; } modifier onlySeller() { if (msg.sender != seller) { revert onlySeller(); } _; } function confirmPurchase() external inState(State.Created) payable { require(msg.value == (2 * value), "please send the 2x the purchase amount" ); buyer = payable(msg.sender); state = State.Locked; } function confirmReceived() external onlyBuyer inState(State.Locked) { state = State.Release; buyer.transfer(value); } function paySeller() external onlySeller inState(State.Release) { state = State.Inactive; seller.transfer( 3 * value); } }
這是由於修飾符和 and 的自定義錯誤之間的命名
onlyBuyer
衝突onlySeller
。我建議將錯誤重命名為
OnlyBuyer
andOnlySeller
。完整的程式碼是:
//SPDX-Licence-Identifier: MIT pragma solidity ^0.8.11; contract PurchaseAgreement { // VARIABLES uint public value; address payable public seller; address payable public buyer; enum State { Created, Locked, Release, Inactive} State public state; constructor() payable { seller = payable(msg.sender); value = msg.value / 2; // buyer = payable(msg.sender); } /// The function can not be called at the current state error InvalidState(); /// Only buyer can call this function error OnlyBuyer(); /// Only buyer can call this function error OnlySeller(); modifier inState(State state_) { if (state != state_) { revert InvalidState(); } _; } modifier onlyBuyer() { if (msg.sender != buyer) { revert OnlyBuyer(); } _; } modifier onlySeller() { if (msg.sender != seller) { revert OnlySeller(); } _; } function confirmPurchase() external inState(State.Created) payable { require(msg.value == (2 * value), "please send the 2x the purchase amount" ); buyer = payable(msg.sender); state = State.Locked; } function confirmReceived() external onlyBuyer inState(State.Locked) { state = State.Release; buyer.transfer(value); } function paySeller() external onlySeller inState(State.Release) { state = State.Inactive; seller.transfer( 3 * value); } }