I was following a tutorial for a Crowdfunding Smart Contract that acts like Kickstarter from Programtheblockchain, however I'm always getting a MetaMask Error which says "Exception thrown in Contract Code" when trying to send Ether to the Contract. It compiles and deploys just fine, but I can't send Ether to it. Is the code maybe outdated or am I doing something wrong on my end?

pragma solidity ^0.4.19;

contract Crowdfunding {
    address owner;
    uint256 deadline;
    uint256 goal;
    mapping(address => uint256) public pledgeOf;

    function Crowdfunding(uint256 numberOfDays, uint256 _goal) public {
        owner = msg.sender;
        deadline = now + (numberOfDays * 1 days);
        goal = _goal;

    function pledge(uint256 amount) public payable {
        require(now < deadline);                // in the fundraising period
        require(msg.value == amount);

        pledgeOf[msg.sender] += amount;

    function claimFunds() public {
        require(address(this).balance >= goal); // funding goal met
        require(now >= deadline);               // in the withdrawal period
        require(msg.sender == owner);


    function getRefund() public {
        require(address(this).balance < goal);  // funding goal not met
        require(now >= deadline);               // in the withdrawal period

        uint256 amount = pledgeOf[msg.sender];
        pledgeOf[msg.sender] = 0;

And my deployment code

var Crowdfunding = artifacts.require("Crowdfunding");

module.exports = function(deployer){
    const numberOfDays = 1;
    const goal = web3.toWei(10, 'ether');   
deployer.deploy(Crowdfunding, numberOfDays, goal);
  • 1
    Where exactly do you deploy the contract and where is your Metamask connected? Is it Mainnet, Rinkeby, or local development environment? – Utgarda Sep 18 '18 at 16:42
  • 1
    Cool, looks like we're getting somewhere :) I remember having error messages like this when I forgot to update contract related files in the web app after updating and re-deploying the contract. – Utgarda Sep 18 '18 at 16:47
  • For now I'm just using the Ganache + MetaMask combination to test the Contract, so I don't have a web app just yet, or what do you mean with the web app? Maybe I'm misunderstanding something here :) But yes, I have seen these kinds of errors and they were fixable by resetting the account in MetaMask or re-compiling / deploying, but this time nothing seems to fix the issue unfortunately – Chris Sep 18 '18 at 16:58

Your contract does not have a fallback payable function. The only payable function you have is the pledge function which is not a fallback function.

Therefore, if you are only trying to send Ether to the contract without explicitly calling the pledge function your transaction will fail as the contract cannot accept Ether without explicit call to the pledge function.

You can read more about fallback function here: https://www.bitdegree.org/learn/solidity-fallback-functions/


There are a few things that may be going wrong here:

  1. If you are only sending Ether (without adding any additional data), it will not work because you don't have a fallback function (function ()). I suggest adding a fallback function to your code.

  2. Your amount may not be equivalent to the msg.value. Remember, msg.value is in Wei, so if you are trying to send 1 Ether, then msg.value == 1000000000000000000 Wei. On a side note, is there a reason you have the line require(msg.value == amount);, instead of simply using msg.value in the line below it?

  • Yes. Prior to the require statement, you can add amount = amount * 1 ether, provided that the amount you pass in will always in in Ether. (see here) – Shane Fontaine Sep 18 '18 at 17:52

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.