我正在通过Remix和Metamask部署。
我正在使用这个SafeMath函数,它似乎阻止了我的合同初始化它的所有状态变量(契约部署,但是所有的vars都是零):
function add(uint256 a, uint256 b) internal constant returns (uint256) {
uint256 c = a + b;
assert(c >= a);
return c;
}库函数似乎导致了以下函数中的问题。要明确的是,下面的deposit函数甚至在部署期间都没有被调用。但是,当我使用启用if块(使用.add SafeMath公式)中的第一行部署合同时,契约会部署,但没有设置任何状态变量。
当我使用第二行时,一切都很好。
我是否不正确地使用SafeMath库?
function deposit(address addr) payable {
uint256 amt = msg.value;
uint alreadyDeposited = deposits[addr]; // deposits is an address => uint mapping
if (alreadyDeposited > 0) {
// deposits[addr] = alreadyDeposited.add(amt); // When this line is enabled, my contract doesn't initialize (for example, "ownerWallet", which should be msg.sender, is not set when the contract deploys).
deposits[addr] = alreadyDeposited + amt; // this line lets contract vars initialize appropriately.
}
sendFunds();
}有人能解释一下add如何知道如何使用自己作为第一个参数吗?
发布于 2017-08-11 02:26:18
我认为这个错误来自于您的if代码,当调用deposite函数时,地址存储的值为零,因为无法初始化地址的值。我去掉if条件,一切都好。这是我的测试代码,如下:
pragma solidity ^0.4.13;
import "./SafeMath.sol";
contract Test{
using SafeMath for uint;
mapping (address => uint) deposits;
function deposit(address addr) payable {
uint256 amt = msg.value;
uint alreadyDeposited = deposits[addr]; // deposits is an address => uint mapping
// if (alreadyDeposited > 0) {
deposits[addr] = alreadyDeposited.add(amt);// When this line is enabled, my contract doesn't initialize (for example, "ownerWallet", which should be msg.sender, is not set when the contract deploys).
// deposits[addr] = alreadyDeposited + amt; // this line lets contract vars initialize appropriately.
// }
//sendFunds();
}
function getBalance(address _addr) constant returns (uint){
return deposits[_addr];
}
}希望能帮上忙~
https://ethereum.stackexchange.com/questions/24126
复制相似问题