Solidity

如何訪問事務中的solidity函式變數

  • September 2, 2022

我試圖address dogOwner在我的單元測試中斷言它等於部署者地址,但是當我嘗試從中獲取dogOwner變數時txReceipt它返回:這是部署者地址,前面0x0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266有一堆數字*(主要是 0) 。*我得到的地址dogOwner不正確嗎?或者這只是由於如何安排可靠儲存而返回值的方式?

這是測試和終端輸出的螢幕截圖:

圖片

與測試相關的功能:

function requestNft() public payable returns (uint256 requestId) {
       if (msg.value < i_mintFee) {
           revert RandomIpfsNft__NeedMoreEthSent();
       }
       requestId = i_vrfCoordinator.requestRandomWords(
           i_gasLane,
           i_subscriptionId,
           REQUEST_CONFIRMATIONS,
           i_callbackGasLimit,
           NUM_WORDS
       );
       s_requestIdToSender[requestId] = msg.sender;
       emit NftRequested(requestId, msg.sender);
   }

   function fulfillRandomWords(uint256 requestId, uint256[] memory randomWords)
       internal
       override
   {
       address dogOwner = s_requestIdToSender[requestId];
       uint256 newTokenId = s_tokenCounter;
       // What does this token look like?

       uint256 moddedRng = randomWords[0] & MAX_CHANCE_VALUE;

       Breed dogBreed = getBreedFromModdedRng(moddedRng);

       _safeMint(dogOwner, newTokenId);
       _setTokenURI(newTokenId, s_dogTokenUris[uint256(dogBreed)]);
       emit NftMinted(dogBreed, dogOwner);
   }

該地址只是用零填充到 32 個字節,並以 0x 開頭

類型是什麼Breed?一個結構?契約?

當我解碼“0x00000000000000000000000000000000000000000000000000020000000000000000000000000f39fd6e51aad88f6f4ce6ab88627279cfffb2”時:

web3.eth.abi.decodeParameters(["uint256", "address"], "0x0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266")

它返回:

{
 "0":"2",
 "1":"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
 "__length__":2
}

所以,我想知道那個“2”可能是什麼。我對其進行解碼,將第一部分視為 a uint256,將第二部分視為address.

您可以進行解碼並像這樣使用它:

const decoded = web3.eth.abi.decodeParameters(["uint256", "address"], "0x0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266");

console.log(deployer.address === decoded["1"]);

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