1

I'm using the crowdsale contract from the ethereum site but I have made a few changes. I added a tokenWithdraw function, if the beneficiary runs it you can withdraw a set amount of tokens from the contract. I have tested and it works fine.

However the safeWithdraw function which should do the same thing except withdraw ETH rather than the Token does not work when I run it as the beneficiary. The ETH just stays in the contract for some reason and I have no idea why.

pragma solidity ^0.4.2;
contract token { function transfer(address receiver, uint amount); }

contract Crowdsale {
    address public beneficiary;
    uint public fundingGoal; uint public amountRaised; uint public deadline; uint public price; uint public tokenW; uint public ethW; token public tokenReward;
    mapping(address => uint256) public balanceOf;
    bool fundingGoalReached = false;
    event GoalReached(address beneficiary, uint amountRaised);
    event FundTransfer(address backer, uint amount, bool isContribution);
    bool crowdsaleClosed = false;

/* data structure to hold information about campaign contributors */

/*  at initialization, setup the owner */
function Crowdsale(
    address ifSuccessfulSendTo,
    uint fundingGoalInEthers,
    uint durationInMinutes,
    token addressOfTokenUsedAsReward
) {
    beneficiary = ifSuccessfulSendTo;
    fundingGoal = fundingGoalInEthers * 1 ether;
    deadline = now + durationInMinutes * 1 minutes;
    price = 25;
    tokenReward = token(addressOfTokenUsedAsReward);
}

function () payable {
    if (crowdsaleClosed) throw;
    uint amount = msg.value;
    balanceOf[msg.sender] = amount;
    amountRaised += amount;
    tokenReward.transfer(msg.sender, amount * price);
    FundTransfer(msg.sender, amount, true);
}

modifier afterDeadline() { if (now >= deadline) _; }

function checkGoalReached() afterDeadline {
    fundingGoalReached = true;
    GoalReached(beneficiary, amountRaised);
}


function closeCrowdSale()  {
  if (beneficiary == msg.sender) {
  crowdsaleClosed = true;
  }
}

function withdrawTokens(uint tokenW) {
  if (beneficiary == msg.sender) {
    tokenReward.transfer(msg.sender, tokenW);
  }
}

function safeWithdrawal(uint ethW) {
    if (beneficiary == msg.sender) {
      FundTransfer(beneficiary, ethW, false);
    }
 }
}
  • Who are you setting as the beneficiary of this contract? The wallet that deployed the contract? Or a separate wallet?? – asleischow Jul 27 '17 at 23:15
  • When using the safe withdraw function: 1) Does it have to be after the deadline? 2) What do I put in eth w?? The amount I want to send to the beneficiary? 3) Do I have to send eth as well? – asleischow Jul 27 '17 at 23:49
1

In your function:

function safeWithdrawal(uint ethW) {
    if (beneficiary == msg.sender) {
      FundTransfer(beneficiary, ethW, false);
    }
 }

You are only:

1.Checking if the sender is beneficiary:

if (beneficiary == msg.sender) {

2.Publishing a FundTransfer event:

FundTransfer(beneficiary, ethW, false);

There is no code to perform any update on contract state or send transaction. That's why you need to add an instruction that will actually send ether:

beneficiary.send(ethW);
  • Perfect thank you, that makes sense now! I was just displaying the info rather than performing the actual transaction. I added a if (beneficiary.send(ethW) then publish fundtransfer event. Thank you! – Mike Young Jul 18 '17 at 23:29
0

I'm guessing you're using the testrpc..if so, then sending eth from contracts doesn't work. I'd give it a try on Ropsten or the mainnet and you should be good to go

  • no this is on the main net – Mike Young Jul 18 '17 at 21:16
  • for some reason I had to add this if statement to it if (beneficiary.send(ethW)) any idea why? – Mike Young Jul 18 '17 at 22:24
  • beneficiary.send(ethW) sends the ether.. Before you were not sending it. – Thomas Clowes Jul 28 '17 at 0:09
0

For some reason changing it to this worked, anyone able to advise why?

function safeWithdrawal(uint ethW) {
    if (beneficiary == msg.sender) {
      if (beneficiary.send(ethW)) {
            FundTransfer(beneficiary, ethW, false);
          }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.