簡體   English   中英

require(msg.sender == owner(), 'not owner') 拋出異常,盡管在事件中檢查 msg.sender 等於 owner()

[英]require(msg.sender == owner(), 'not owner') throwing exception although msg.sender equals owner() when inspected in event

我是solidity的新手,所以如果問題很簡單,請原諒,但我沒有找到任何相關的線程。

我有一個繼承自 openzeppelins ownable.sol 的市場合同,我想檢查對 listNFT function 的訪問權限,僅由合同所有者完成。 我嘗試了 onlyOwner 修飾符,它無法引發“調用者不是所有者”錯誤。 然后我試圖了解 msg.sender 和 owner() 的確切值並構建我自己的事件來跟蹤 msg.sender 和 owner 的值。 您可以在此處查看相關代碼。

    # Event to keep track of owner and caller
    event FunctionCall(
        address caller,
        address owner
    );

    // List the NFT on the marketplace
    function listNft( uint256 _tokenId, uint256 _price) public payable nonReentrant {

        # Emit event to inspect caller and owner
        emit FunctionCall(msg.sender ,owner());

        require(_price > 100, "Price must be at least 100 wei");

        # This line doesn't let me through and evaluates to false
        # require(msg.sender == owner(), "Must be owner");

      ...more code
    }

當我在沒有要求所有者聲明的情況下調用合同時,我可以檢查 polyscan 上的事件,它給出以下內容

多邊形掃描事件

事件顯示 msg.sender 和 owner 是同一個地址,之后的 require 語句怎么會評估為 false?

我測試了將 msg.sender 和 owner() 轉換為 address(msg.sender) 和 address(owner()) 但它不起作用。 我錯過了什么?

提前感謝您的幫助!

我找到了答案,謝謝你的意見!

簽名方法有問題。 早些時候,我使用 Web3.py 像這樣手動簽署交易


  transaction = contract.functions.transferFrom(w3.toChecksumAddress(<PublicKey>),w3.toChecksumAddress(to),  token_id).buildTransaction({
        'from': acct.address,
        'nonce': w3.eth.get_transaction_count(acct.address),
    })
    gas = w3.eth.estimate_gas(transaction)
    transaction.update({'gas': int(gas*1.5)})
    signed_transaction = w3.eth.account.sign_transaction(transaction, private_key=<PRIVKEY>)
    tx_hash = w3.eth.send_raw_transaction(signed_transaction.rawTransaction)
    tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)

從 web3.py 切換到 .transact() 方法和construct_sign_and_send_raw_middleware () 后一切正常。 我仍然對這個事件在 polyscan 上給出了正確的 msg.sender 感到困惑,但只要一切正常,我就不會抱怨:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM