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);
}
}
}