I've created the following mocked token:
//SPDX-License-Identifier: MIT
pragma solidity 0.8.17;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract Dai is ERC20 {
constructor(uint256 initialSupply) ERC20("Dai Stablecoin", "DAI") {
require(initialSupply > 0, "Initial supply has to be greater than 0");
_mint(msg.sender, initialSupply * 10**18);
}
}
And want to use it in this CrowdfundingCampaign smart-contract:
//SPDX-License-Identifier: MIT
pragma solidity 0.8.17;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
contract CrowdfundingCampaign {
using SafeERC20 for ERC20;
// Custom ERC20 token used for pledging funds
ERC20 public token;
// Address of the contract owner (the campaign creator)
address payable public owner;
// Funding goal of the campaign
uint256 public fundingGoal;
// The total amount of funds pledged
uint256 public totalFundsPledged;
// Mapping of addresses to the amount of funds pledged by each user
mapping(address => uint256) public fundsPledged;
// Event for logging state changes
event FundPledged(address indexed _from, uint256 _amount);
event RefundClaimed(address indexed _to, uint256 _amount);
constructor(ERC20 _token, uint256 _fundingGoal) {
require(
_token.totalSupply() > 0,
"Token contract must have a positive total supply"
);
require(_fundingGoal > 0, "Funding goal must be a positive value");
token = _token;
owner = payable(msg.sender);
fundingGoal = _fundingGoal;
}
// Pledges funds to the campaign
function pledgeFunds(uint256 _amount) public payable {
require(_amount > 0, "Pledge amount must be a positive value");
require(
token.transferFrom(msg.sender, address(this), _amount),
"Token transfer failed"
);
fundsPledged[msg.sender] += _amount;
totalFundsPledged += _amount;
emit FundPledged(msg.sender, _amount);
}
// Claims a refund for pledged funds if the funding goal is not met
function claimRefund() public {
require(
totalFundsPledged < fundingGoal,
"Funding goal has been met, no refunds available"
);
require(fundsPledged[msg.sender] > 0, "You have not pledged any funds");
uint256 refundAmount = fundsPledged[msg.sender];
token.safeTransfer(msg.sender, refundAmount);
fundsPledged[msg.sender] = 0;
totalFundsPledged -= refundAmount;
emit RefundClaimed(msg.sender, refundAmount);
}
// Withdraws any remaining funds and ends the campaign
function endCampaign() public {
require(
msg.sender == owner,
"Only the contract owner can end the campaign"
);
require(
totalFundsPledged >= fundingGoal,
"Funding goal has not been met"
);
// owner.transfer(address(this).balance);
// owner.transferFrom(address(this), address(this), _amount)
token.safeTransfer(owner, address(this).balance);
// token.transferFrom(address(this), owner, address(this).balance);
}
}
I'm testing this smart-contract in Remix, and everything seems to work, but when I do web3.eth.getBalance("contract-adress")
, it always displays 0. endCampaign
also doesn't work because balance aren't changing.
What I'm doing wrong?