Как прочитать открытую переменную в солидности с кодами теста трюфеля? - PullRequest
0 голосов
/ 02 мая 2019

Я пытаюсь получить значение из моей публичной переменной в солидности с помощью трюфельной консоли, но я не знаю правильный синтаксис.

трюфельная версия Трюфель v5.0.14 (ядро: 5.0.14) Твердость - 0,5,4 (solc-js) Узел v11.10.1 Web3.js v1.0.0-beta.37

Вот что я уже пробовал.

  1. Я установил трюфель с помощью следующей команды.
$ npm install truffle -g
$ truffle init
$ truffle develop
  1. У меня есть контракт с именем ProxyContract, и я настроил 2_deploy_migration.js
const ProxyContract = artifacts.require("./ProxyContract.sol");

module.exports = function(deployer) {
   deployer.deploy(ProxyContract).then( () => console.log("ProxyContract: " + ProxyContract.address))
}
  1. перенесенный контракт.
truffle(develop)> migrate
  1. Когда я набрал ProxyContract, чтобы получить подсказку, я увидел часть информации какого-то объекта, и все выглядело нормально.

  2. и я попытался получить доступ к переменной 'CommitteeStatus', как это, но только что произошли некоторые коды ошибок, даже нет параметра для 'CommitteeStatus'

truffle(develop)> var proxyContract = await ProxyContract.deployed()
undefined

truffle(develop)> proxyContract.committeeStatus.call().then(function (res) {console.log(res)})
Thrown:
Error: Invalid number of parameters for "committeeStatus". Got 0 expected 1!
    at processTicksAndRejections (internal/process/next_tick.js:81:5)
    at Promise (/usr/local/lib/node_modules/truffle/build/webpack:/packages/truffle-contract/lib/execute.js:128:1)
    at Object._createTxObject (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3-eth-contract/src/index.js:699:1)
    at Object.InvalidNumberOfParams (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3-eth-contract/~/web3-core-helpers/src/errors.js:32:1)

Как получить значение публичной переменной с помощью тестовых кодов трюфелей?

Что я попробовал дополнительные вещи ...

truffle(develop)> proxyContract.committeeStatus
{ [Function]
  call: [Function],
  sendTransaction: [Function],
  estimateGas: [Function],
  request: [Function] }
truffle(develop)> proxyNemodax.committeeStatus.toString()
function() {     
    var params = {};
    var defaultBlock = "latest";
    var args = Array.prototype.slice.call(arguments);
    var lastArg = args[args.length - 1];

    // Extract defaultBlock parameter
    if (execute.hasDefaultBlock(args, lastArg, methodABI.inputs)) {
        defaultBlock = args.pop();
    }
      // Extract tx params
      if (execute.hasTxParams(lastArg)) {
        params = args.pop();
      }

      params.to = address;
      params = utils.merge(constructor.class_defaults, params);

      return new Promise(async (resolve, reject) => {
        let result;
        try {
          await constructor.detectNetwork();
          args = utils.convertToEthersBN(args);
          result = await fn(...args).call(params, defaultBlock);
          result = reformat.numbers.call(
            constructor,
            result,
            methodABI.outputs
          );
          resolve(result);
        } catch (err) {
          reject(err);
        }
      });
    }'
truffle(develop)> proxyNemodax.committeeStatus.call(0)
Thrown:
{ Error: invalid address (arg="", coderType="address", value=0)
    at ABICoder.encodeParameters (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3-eth-abi/src/index.js:96:1)
    at AbiCoder.encode (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3-eth-abi/~/ethers/utils/abi-coder.js:897:1)
    at CoderTuple.encode (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3-eth-abi/~/ethers/utils/abi-coder.js:764:1)
    at pack (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3-eth-abi/~/ethers/utils/abi-coder.js:604:1)
    at Array.forEach (<anonymous>)
    at /usr/local/lib/node_modules/truffle/build/webpack:/~/web3-eth-abi/~/ethers/utils/abi-coder.js:605:21
    at CoderAddress.encode (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3-eth-abi/~/ethers/utils/abi-coder.js:467:1)
    at Object.throwError (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3-eth-abi/~/ethers/utils/errors.js:68:1)
  reason: 'invalid address',
  code: 'INVALID_ARGUMENT',
  arg: '',
  coderType: 'address',
  value: 0 }

Вот мой код солидности


pragma solidity 0.5.4;


/**
 * @title SafeMath
 * @dev Math operations with safety checks that throw on error
 */
library SafeMath {

  /**
  * @dev Multiplies two numbers, throws on overflow.
  */
  function mul(uint256 a, uint256 b) internal pure returns (uint256 c) {
    // Gas optimization: this is cheaper than asserting 'a' not being zero, but the
    // benefit is lost if 'b' is also tested.
    // See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522
    if (a == 0) {
      return 0;
    }

    c = a * b;
    assert(c / a == b);
    return c;
  }

  /**
  * @dev Integer division of two numbers, truncating the quotient.
  */
  function div(uint256 a, uint256 b) internal pure returns (uint256) {
    // assert(b > 0); // Solidity automatically throws when dividing by 0
    // uint256 c = a / b;
    // assert(a == b * c + a % b); // There is no case in which this doesn't hold
    return a / b;
  }

  /**
  * @dev Subtracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).
  */
  function sub(uint256 a, uint256 b) internal pure returns (uint256) {
    assert(b <= a);
    return a - b;
  }

  /**
  * @dev Adds two numbers, throws on overflow.
  */
  function add(uint256 a, uint256 b) internal pure returns (uint256 c) {
    c = a + b;
    assert(c >= a);
    return c;
  }
}

/**
 * @title MultiOwnable
 *
 * @dev Require majority approval of multiple owners to use and access to features
 *      when restrictions on access to critical functions are required.
 *
 */

contract MultiOwnable {
    using SafeMath for uint8;

    struct CommitteeStatusPack{
      /**
       * Key informations for decisions.
       * To save some gas, choosing the struct.
       */
        uint8 numOfOwners;
        uint8 numOfVotes;
        uint8 numOfMinOwners;
        bytes proposedFuncData;
    }
    CommitteeStatusPack public committeeStatus;

    address[] public ballot; // To make sure if it already was voted
    mapping(address => bool) public owner;

    event Vote(address indexed proposer, bytes indexed proposedFuncData);
    event Propose(address indexed proposer, bytes indexed proposedFuncData);
    event Dismiss(address indexed proposer, bytes indexed proposedFuncData);
    event AddedOwner(address newOwner);
    event RemovedOwner(address removedOwner);
    event TransferOwnership(address from, address to);


    /**
     * Organize initial committee.
     *
     * @notice committee must be 3 at least.
     *         you have to use this contract to be inherited because it is internal.
     *
     * @param _coOwner1 _coOwner2 _coOwner3 _coOwner4 _coOwner5 committee members
     */
    constructor(address _coOwner1, address _coOwner2, address _coOwner3, address _coOwner4, address _coOwner5) internal {
        require(_coOwner1 != address(0x0) &&
                _coOwner2 != address(0x0) &&
                _coOwner3 != address(0x0) &&
                _coOwner4 != address(0x0) &&
                _coOwner5 != address(0x0));
        require(_coOwner1 != _coOwner2 &&
                _coOwner1 != _coOwner3 &&
                _coOwner1 != _coOwner4 &&
                _coOwner1 != _coOwner5 &&
                _coOwner2 != _coOwner3 &&
                _coOwner2 != _coOwner4 &&
                _coOwner2 != _coOwner5 &&
                _coOwner3 != _coOwner4 &&
                _coOwner3 != _coOwner5 &&
                _coOwner4 != _coOwner5); // SmartDec Recommendations
        owner[_coOwner1] = true;
        owner[_coOwner2] = true;
        owner[_coOwner3] = true;
        owner[_coOwner4] = true;
        owner[_coOwner5] = true;
        committeeStatus.numOfOwners = 5;
        committeeStatus.numOfMinOwners = 5;
        emit AddedOwner(_coOwner1);
        emit AddedOwner(_coOwner2);
        emit AddedOwner(_coOwner3);
        emit AddedOwner(_coOwner4);
        emit AddedOwner(_coOwner5);
    }


    modifier onlyOwner() {
        require(owner[msg.sender]);
        _;
    }

    /**
     * Pre-check if it's decided by committee
     *
     * @notice If there is a majority approval,
     *         the function with this modifier will not be executed.
     */
    modifier committeeApproved() {
      /* check if proposed Function Name and real function Name are correct */
      require( keccak256(committeeStatus.proposedFuncData) == keccak256(msg.data) ); // SmartDec Recommendations

      /* To check majority */
      require(committeeStatus.numOfVotes > committeeStatus.numOfOwners.div(2));
      _;
      _dismiss(); //Once a commission-approved proposal is made, the proposal is initialized.
    }


    /**
     * Suggest the functions you want to use.
     *
     * @notice To use some importan functions, propose function must be done first and voted.
     */
    function propose(bytes memory _targetFuncData) onlyOwner public {
      /* Check if there're any ongoing proposals */
      require(committeeStatus.numOfVotes == 0);
      require(committeeStatus.proposedFuncData.length == 0);

      /* regist function informations that proposer want to run */
      committeeStatus.proposedFuncData = _targetFuncData;
      emit Propose(msg.sender, _targetFuncData);
    }

    /**
     * Proposal is withdrawn
     *
     * @notice When the proposed function is no longer used or deprecated,
     *         proposal is discarded
     */
    function dismiss() onlyOwner public {
      _dismiss();
    }

    /**
     * Suggest the functions you want to use.
     *
     * @notice 'dismiss' is executed even after successfully executing the proposed function.
     *          If 'msg.sender' want to pass permission, he can't pass the 'committeeApproved' modifier.
     *          internal functions are required to enable this.
     */

    function _dismiss() internal {
      emit Dismiss(msg.sender, committeeStatus.proposedFuncData);
      committeeStatus.numOfVotes = 0;
      committeeStatus.proposedFuncData = "";
      delete ballot;
    }


    /**
     * Owners vote for proposed item
     *
     * @notice if only there're proposals, 'vote' is processed.
     *         the result must be majority.
     *         one ticket for each owner.
     */

    function vote() onlyOwner public {
      // Check duplicated voting list.
      uint length = ballot.length; // SmartDec Recommendations
      for(uint i=0; i<length; i++) // SmartDec Recommendations
        require(ballot[i] != msg.sender);

      //onlyOnwers can vote, if there's ongoing proposal.
      require( committeeStatus.proposedFuncData.length != 0 );

      //Check, if everyone voted.
      //require(committeeStatus.numOfOwners > committeeStatus.numOfVotes); // SmartDec Recommendations
      committeeStatus.numOfVotes++;
      ballot.push(msg.sender);
      emit Vote(msg.sender, committeeStatus.proposedFuncData);
    }


    /**
     * Existing owner transfers permissions to new owner.
     *
     * @notice It transfers authority to the person who was not the owner.
     *           Approval from the committee is required.
     */
    function transferOwnership(address _newOwner) onlyOwner committeeApproved public {
        require( _newOwner != address(0x0) ); // callisto recommendation
        require( owner[_newOwner] == false );
        owner[msg.sender] = false;
        owner[_newOwner] = true;
        emit TransferOwnership(msg.sender, _newOwner);
    }

    /**
     * Add new Owner to committee
     *
     * @notice Approval from the committee is required.
     *
     */
    function addOwner(address _newOwner) onlyOwner committeeApproved public {
        require( _newOwner != address(0x0) );
        require( owner[_newOwner] != true );
        owner[_newOwner] = true;
        committeeStatus.numOfOwners++;
        emit AddedOwner(_newOwner);
    }

    /**
     * Remove the Owner from committee
     *
     * @notice Approval from the committee is required.
     *
     */
    function removeOwner(address _toRemove) onlyOwner committeeApproved public {
        require( _toRemove != address(0x0) );
        require( owner[_toRemove] == true );
        require( committeeStatus.numOfOwners > committeeStatus.numOfMinOwners ); // must keep Number of Minimum Owners at least.
        owner[_toRemove] = false;
        committeeStatus.numOfOwners--;
        emit RemovedOwner(_toRemove);
    }
}

contract Pausable is MultiOwnable {
    event Pause();
    event Unpause();

    bool internal paused;

    modifier whenNotPaused() {
        require(!paused);
        _;
    }

    modifier whenPaused() {
        require(paused);
        _;
    }

    modifier noReentrancy() {
        require(!paused);
        paused = true;
        _;
        paused = false;
    }

    /* When you discover your smart contract is under attack, you can buy time to upgrade the contract by
       immediately pausing the contract.
     */
    function pause() public onlyOwner committeeApproved whenNotPaused {
        paused = true;
        emit Pause();
    }

    function unpause() public onlyOwner committeeApproved whenPaused {
        paused = false;
        emit Unpause();
    }
}

/**
 * Contract Managing TokenExchanger's address used by ProxyNemodax
 */
contract RunningContractManager is Pausable {
    address public implementation; //SmartDec Recommendations

    event Upgraded(address indexed newContract);

    function upgrade(address _newAddr) onlyOwner committeeApproved external {
        require(implementation != _newAddr);
        implementation = _newAddr;
        emit Upgraded(_newAddr); // SmartDec Recommendations
    }

    /* SmartDec Recommendations
    function runningAddress() onlyOwner external view returns (address){
        return implementation;
    }
    */
}




/**
 * @title NemodaxStorage
 *
 * @dev This is contract for proxyNemodax data order list.
 *      Contract shouldn't be changed as possible.
 *      If it should be edited, please add from the end of the contract .
 */

contract NemodaxStorage is RunningContractManager {

    // Never ever change the order of variables below!!!!
    // Public variables of the token
    string public name;
    string public symbol;
    uint8 public decimals = 18;    // 18 decimals is the strongly suggested default, avoid changing it
    uint256 public totalSupply;

    /* This creates an array with all balances */
    mapping (address => uint256) public balances;
    mapping (address => mapping (address => uint256)) public allowed;
    mapping (address => bool) public frozenExpired; // SmartDec Recommendations

    bool private initialized;

    uint256 public tokenPerEth;
    bool public opened = true;
}

/**
 * @title ProxyNemodax
 *
 * @dev The only fallback function will forward transaction to TokenExchanger Contract.
 *      and the result of calculation would be stored in ProxyNemodax
 *
 */

contract ProxyNemodax is NemodaxStorage {

    /* Initialize new committee. this will be real committee accounts, not from TokenExchanger contract */
    constructor(address _coOwner1,
                address _coOwner2,
                address _coOwner3,
                address _coOwner4,
                address _coOwner5)
        MultiOwnable( _coOwner1, _coOwner2, _coOwner3, _coOwner4, _coOwner5) public {}

    function () payable external {
        address localImpl = implementation;
        require(localImpl != address(0x0));

        assembly {
            let ptr := mload(0x40)

            switch calldatasize
            case 0 {  } // just to receive ethereum

            default{
                calldatacopy(ptr, 0, calldatasize)

                let result := delegatecall(gas, localImpl, ptr, calldatasize, 0, 0)
                let size := returndatasize
                returndatacopy(ptr, 0, size)
                switch result

                case 0 { revert(ptr, size) }
                default { return(ptr, size) }
            }
        }
    }
}

Ответы [ 2 ]

1 голос
/ 03 мая 2019
Error: Invalid number of parameters for "committeeStatus". Got 0 expected 1!

Выше указано, что вашей функции CommitteeStatus требовался 1 аргумент во время вызова функции, но вы не указали ни одного. Поэтому попробуйте указать этот аргумент в качестве обязательного аргумента. proxyContract.committeeStatus.call().then(function (res) {console.log(res)})

0 голосов
/ 07 мая 2019

Я проверил ваш контракт на remix.ethereum.org.

Аби комитета CommitteeStatus (без входных данных):

{
    "constant": true,
    "inputs": [],
    "name": "committeeStatus",
    "outputs": [
        {
            "name": "numOfOwners",
            "type": "uint8"
        },
        {
            "name": "numOfVotes",
            "type": "uint8"
        },
        {
            "name": "numOfMinOwners",
            "type": "uint8"
        },
        {
            "name": "proposedFuncData",
            "type": "bytes"
        }
    ],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
},

Так что вы можете просто позвонить: proxyNemodax.committeeStatus.call () в этом контракте.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...