Solidity

從基本 Solidity 合約(安全帽、乙太幣)返回的意外對象

  • August 29, 2022

我無法弄清楚我在這裡做錯了什麼。在使用安全帽測試執行時,為什麼我不能從契約中返回一個簡單的 uint?

契約.sol:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import 'hardhat/console.sol';
import "@openzeppelin/contracts/access/AccessControl.sol";

contract Contract is AccessControl {
   using SafeMath for uint256;

   constructor() {
       _grantRole(DEFAULT_ADMIN_ROLE, msg.sender);
   }

   function doIt() public returns (uint256) {
       console.log("Returning 1 from contract");
       return 1;
   }

}

測試.js

const { expect, assert } = require("chai");
const { ethers, upgrades } = require("hardhat");

describe("Contract", function () {  
 let acontract;

 beforeEach(async function () {
   // Deploying contract
   const aContract = await ethers.getContractFactory("Contract");
   acontract = await aContract.deploy();
   await acontract.deployed();
   console.log(`Contract deployed to ${acontract.address}`);
 });

 describe("test contract", function () {
   it("doIt", async function () {
       console.log(JSON.stringify(await acontract.doIt()));
   });
 });
});

輸出:

npx hardhat test test-test/test.js

 Contract
   test contract
Contract deployed to 0x5FbDB2315678afecb367f032d93F642f64180aa3
Returning 1 from contract
{"hash":"0x2e7cd7d48db5bf437d117ca6c2de3d067c4299fba02b55b69d7a0037a01de74f","type":2,"accessList":[],"blockHash":"0xb15cbb79171f086b59bfa277aa643510022bc787098c4e3a77309c939ae36885","blockNumber":2,"transactionIndex":0,"confirmations":1,"from":"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266","gasPrice":{"type":"BigNumber","hex":"0x6978bc25"},"maxPriorityFeePerGas":{"type":"BigNumber","hex":"0x3b9aca00"},"maxFeePerGas":{"type":"BigNumber","hex":"0x9756ae4a"},"gasLimit":{"type":"BigNumber","hex":"0x01bad458"},"to":"0x5FbDB2315678afecb367f032d93F642f64180aa3","value":{"type":"BigNumber","hex":"0x00"},"nonce":1,"data":"0xb29f0835","r":"0x1c77fabb8795afe562dd6772bd3c26db9b73f94d0be5774dfc3488a3a1e6d299","s":"0x30a98924f50a02f6f06698edf29b14d1be2f3fddea60cba276a4e60a3198e33d","v":0,"creates":null,"chainId":31337}
     ✓ doIt

任何幫助表示讚賞。謝謝

當您呼叫非視圖/純函式時,ethers 會創建並簽署交易。然後,乙太幣返回交易數據,而不是你的 solidity 函式返回的值(該值實際上是在交易雜湊中編碼的)。您可以通過以下方式獲取返回值:

  • 發出一個事件
  • 將函式的類型更改為view如果它不更改合約的狀態,或者pure如果它不訪問狀態變數

更詳細的解釋可以在這裡找到

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