10. Crowdfunding contract realization

Keywords: Blockchain


Article directory

Introduction to ICO crowdfunding

ICO (initial crypto token offering, first token crowdfunding) is considered to be a new type of investment and financing mode generated by the block chain ecology. The concept originated from IPO, but the raised currency became general digital currency such as bitcoin and Ethereum, so as to support the development cost of the project.

At present, there is no unified definition of ICO. Generally speaking, ICO refers to the behavior that a blockchain start-up project issues a unique crypto token on the blockchain platform, and the investor purchases tokens for the project by using the specified digital currency (such as bitcoin and etheric currency) for crowdfunding. According to different projects, tokens represent the right to use and vote in the future. With the recognition of the project achievements and the increase of the number of users, the value of token as a trading medium or equity has been continuously improved.

Mastercoin (now called Omni), which raised 5000 bitcoins in July 2013, is the first recorded ICO, while Ethereum's ICO of more than $15 million in July 2014 started the rapid development of ICO. In 2015, The DAO raised as much as $150 million, but later failed due to hacker attacks. Since 2016, the speed of ICO crowdfunding has been fast, and the amount of money raised has been increasing, which often leads to the situation of looting all

Crowdfunding contract initialization

pragma solidity >=0.6.0 <0.7.0;

contract CrowdDemo{
    
    // Create investor structure
    struct Funder{
        address addr;  // Investor address
        uint amount;   // Investment amount
    }
    
    // Crowdfunding products
    struct Product{
        address addr;  // If crowdfunding is successful, the amount goes to the current address
        uint goal;   // Expected crowdfunding objectives
        uint amount;   // Actual crowdfunding objectives
        uint funderNum;   // Number of investors
        // Create relationship between products and investors
        mapping(uint => Funder) funders;
    }
    
    // Platform releases crowdfunding products
    Product[] public products;
    
    // Release product information to be crowdfunded
    function candidate(address addr,uint goal) public returns (uint){
        products.push(Product(addr,goal,0,0));
        return products.length;
    }
    
}

Crowdfunding contract realization

If crowdfunding is successful, the amount of crowdfunding will be transferred to the address to be provided by crowdfunding products. You can consider how to realize the refund function if crowdfunding fails!

pragma solidity >=0.6.0 <0.7.0;

contract CrowdDemo{
    
    // Create investor structure
    struct Funder{
        address addr;  // Investor address
        uint amount;   // Investment amount
    }
    
    // Crowdfunding products
    struct Product{
        address payable addr;  // If crowdfunding is successful, the amount goes to the current address
        uint goal;   // Expected crowdfunding objectives
        uint amount;   // Actual crowdfunding objectives
        uint funderNum;   // Number of investors
        // Create relationship between products and investors
        mapping(uint => Funder) funders;
    }
    
    // Platform releases crowdfunding products
    Product[] public products;
    
    // Release product information to be crowdfunded
    function candidate(address payable addr,uint goal) public returns (uint){
        products.push(Product(addr,goal*10**18,0,0));
        return products.length;
    }
    
    // Write function to realize crowdfunding function
    function vote(uint index) public payable{
        // Index products to be crowdfunded
        Product storage p = products[index];   // a = b
        p.funders[p.funderNum++]= Funder({addr:msg.sender,amount:msg.value});
        // Add crowdfunding amount to amount
        p.amount += msg.value;
    }
    
    // Check if a product is crowdfunding successful
    function check(uint index) public payable returns(bool){
        Product storage p = products[index];
        // Whether the current crowdfunding amount is greater than the set amount
        if(p.amount < p.goal){
            return false;
        }
        // The crowdfunding is successful, and the amount is transferred to the corresponding address of the product
        uint amount = p.amount;
        p.addr.transfer(amount);  // amount to the caller of the transfer function
        p.amount = 0;
    }
}

138 original articles published, praised 19, visited 20000+
Private letter follow

Posted by smartsley on Tue, 10 Mar 2020 23:18:43 -0700