簡體   English   中英

Solidity:tx.origin == address1 不匹配,即使兩個地址相同

[英]Solidity : tx.origin == address1 does not match, even when both address are same

我正在嘗試檢查智能合約是否由特定地址發送交易,但下面提到的一些如何在 Remix IDE 上工作,但在使用 python 部署合約時失敗。以下是智能合約:

pragma solidity ^0.4.24;
contract ethersend {
    address vin;
    address awa;
    address test;
    
    function say(address address1, address address2) public {
        test = tx.origin;
        vin = address1;
        awa = address2;
        if(tx.origin != address1){
        revert("Error Say");
        }
        else
        {
                revert("Hurray Say");
        }
    }
    
        function speak(address address1, address address2) public {
        test = tx.origin;
        vin = address1;
        awa = address2;
        require(tx.origin == address1,"Error Speak");
        revert("Hurray Speak");
        }
    
}

//////////////////////////////////////////////// /////////////////////////////////////////// 下面是 python代碼:

def registerStation1():
    print("\n***********************************************************************************************************")
    print("Test registerStation Method of Smart Contract")
    print("***********************************************************************************************************\n")
    # call transactional method
    #function registerStation(address stationAddress, address ownerAddress, string memory pwd, string memory stationDetails, string memory location, string memory availability,uint priceRate, uint rating, uint ratedby)

    ownerAddress =  "0x9E498002E0e6f7b8208941cF17611de82b64Fec1" #MetaMask Third Account
    ownerPrivateKey = ""
    contract_func1 = contract.functions.speak(ownerAddress, "0xf886D7306336007136D87398B1Ceb4AE194949D2")

    try:
        transaction = contract_func1.buildTransaction()
        nonce = w3.eth.getTransactionCount(ownerAddress)
        transaction['nonce'] = nonce
        #print(transaction)
        signed_Transaction = w3.eth.account.signTransaction(transaction, ownerPrivateKey);
        txn_hash = w3.eth.sendRawTransaction(signed_Transaction.rawTransaction);
        print('txn_hash={} waiting for receipt..'.format(txn_hash.hex()))

        tx_receipt = w3.eth.waitForTransactionReceipt(txn_hash, timeout=240)

        greeting_Event = contract.events.errorLog()
        result = greeting_Event.processReceipt(tx_receipt) # Modification
        print(result[0]['args'])
        print("Receipt accepted. gasUsed={gasUsed} blockNumber={blockNumber}". format(**tx_receipt))

    except Exception as err:
        print("Exception Occured: ",err)
        #raise Exception(err)
    print("\n***********************************************************************************************************")
    print("Test registerStation Method End")
    print("***********************************************************************************************************\n")


registerStation1()

錯誤:revert("Error Say") revert("Error Speak");

嘗試返回 tx.Origin 和 address1 變量來檢查錯誤。 它在我的安全帽測試環境中完美運行。

暫無
暫無

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

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