我尝试使用truffle console从solidity中的公共变量获取一个值,但我不知道正确的语法。
truffle版本truffle v5.0.14(核心:5.0.14)坚固性-0.5.4(solc-js)节点v11.10.1网站3.jsv1.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提示时,我可以看到一些对象的信息,它看起来很好。
1.我尝试像这样访问'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测试代码获取公共变量值?
我尝试了额外的东西...
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条答案
按热度按时间q9rjltbz1#
错误:“committeeStatus”的参数数目无效。获得0,应为1!
上面的例子告诉我们committeeStatus函数在调用过程中需要1个参数,但是您没有提供任何参数,因此,请尝试为该调用提供所需的参数
iibxawm42#
我在remix.ethereum.org上测试了你的合约。
committeeStatus的abi(无输入数据):
所以你可以打电话:proxyNemodax.committeeStatus.call()在本合同中。