Smart Contracts

  • Smart Contracts are the realization of the computational element of Blockchain technology

  • The BitCoin blockchain has a feature known as Pay to Script, which allows for simple conditional transfers of value, which is a very basic smart contract.

  • The Ethereum Blockchain incorporates smart contracts as first class citizens, ranking smart contracts as the dominant feature of that Blockchain

  • There are many other blockchains beyond Ethereum that provide platforms for smart contracts.  The table on the following site is a bit out of date but it still contains a relevant and interesting summary of various smart contract blockchains/platforms around the world:  smart contract summary

  • In Theory, smart contracts can be arbitrarily complex.  However, in the current start-of-the-technology, smart contracts are largely constrained to simple, rule-based, if-then logic.

  • Smart contracts are still in their infancy!  As Smart contracts mature, they hold the promise of reducing the overall cost of trading any and all commodities.  This promise lies in the optimization brought by automating appropriate parts of the "deal cycle", where "the deal cycle" consists of search, negotiation, performance and post-performance.  Optimization of any portion of this cycle will result in increased speed and accuracy of execution, lower risk, and ultimately, overall lower cost.

Example Smart Contract Code For a Kickstarter

The following is a small bit of code that shows an example smart contract.  The code is written in Solidity.

The purpose of this contract is to execute an agreement with performance requirements equivalent to a traditional kickstarter campaign.  Namely, the campaign has a specified duration and fund raising goal.  If the funds are raised within the campaign time window, then the funds are released to the owner of the campaign.  Otherwise, the campaign is considered to be unsuccessful and the funds are released back to the contributors. 

 

This example is a demonstration of the intermediary being taken out of the picture, which in this case would be a traditional kickstarter provider such as kickstarter.com, fundable, rockethub, etc.  These traditional intermediaries charge various fees which are avoided when the contract is executed on the blockchain (although, it should be pointed out that execution on the blockchain is not free, its just less than the traditional route)

pragma solidity ^0.7.4;


contract BuildingBlocksKickstarter {

    uint256 public endTime;
    uint256 public goal;
    uint256 private currentBalance;
    address payable public kickstarterWallet;
    address public administrator;
    mapping (address => uint256) public contributions;
    address[] contributors = new address[](1024);
    uint public maxCampaignLength = 45;

    //Constructor. 
    constructor (uint256 inputEndTime, uint256 inputGoal,

                        address payable wallet)           public {
        
        // A few basic checks
        require (inputEndTime > block.timestamp);
        require (inputGoal > 0);
    

        // Conditionally limit the end time for the campaign
        if (inputEndTime > block.timestamp + 45 days){
            endTime = block.timestamp + 45 days;
        }
        else {
             endTime = inputEndTime;
        }
        
        goal = inputGoal;
        kickstarterWallet = wallet;
        administrator = msg.sender;    
    }
    
    //Check current block time against the Kickstarter endTime
    function campaignTimeHasExpired() internal view returns(bool){
        if (block.timestamp >= endTime) {
            return true;
        } 
        else {
            return false;
        }
    }
    
    function goalReached() internal view returns(bool) {
        if (currentBalance >= goal) {
            return true;
        }
        else {
            return false;
            
        }
    }
    
    // Contribute funds
    function contribute() public payable {
        
        //Don't accept contributions after the campaign has expired
        require (!campaignTimeHasExpired());

        // Map Contributor to Amount, Accumulating in case the same 
        // Person contributes multiple times
        contributions[msg.sender] += msg.value;
        
        // Solidity has no way to get Map Keys, so keep track of the 
        // keys in a separate array
        contributors.push(msg.sender);
        
        // Current balance held by the Contract
        currentBalance += msg.value;
    }
    
    //  Return contributions if the campaign has ended and the goal was not met
    //  This function is "active" on the contributors side.  There will also
    //  be a passive return to the contributor that is triggered by the campaign
    //  ending without reaching its goal.
    function withdrawContribution() public {
        
        require (campaignTimeHasExpired());
        require (!goalReached());
        
        require(contributions[msg.sender] > 0);

        //Return contribution to user and zero-out their balance
        msg.sender.transfer(contributions[msg.sender]);
        
        currentBalance -= contributions[msg.sender];
        
        contributions[msg.sender] = 0;

    }
    
    //Authenticate Owner
    modifier onlyOwner() {
        require (msg.sender == administrator);
        _;
    }


    function cancel() public onlyOwner {
       
        require (!campaignTimeHasExpired());
        
        // Return funds being held by the campaign
        for (uint i=0;  i < contributors.length; i++ ) {
            address id = contributors[i];
            msg.sender.transfer(contributions[id]);
            currentBalance -= contributions[id];
            contributions[id] = 0;
        }
        

        // Force the campaign to expire
        endTime = block.timestamp;

    }
    function withdraw() public onlyOwner {
        
        // If the goal was reached, release the funds!
        require (goalReached());
        kickstarterWallet.transfer(currentBalance);
        
        // This Kickstarter is complete!
        endTime = block.timestamp;

    }
    
}

 

©2020 BUILDINGBLOCKSTECHNOLOGIES,LLC