It is almost inevitable to escape the term in today’s world. If you have crawled upon this post, then it is probably certain that you understand that there is more to the then .
To completely understand this tutorial, it is important to understand what a even is. If you are already familiar with the topic, feel free to skip ahead, otherwise, I would recommend a quick read .
Dapps (Decentralized applications) are applications that communicate with the via “Smart contracts”, typically with a javascript/html frontend living on a centralized server, and a smart contract living in the . This post will only go over the backend side (smart contract). (Complete Dapp Tutorial coming later)
As this post is not dedicated to general programming, I will not go into details on the programming syntax and logic, however, it is important to know that the only programming language on the with smart contracts is “Solidity”. You can find the docs and more information regarding “Solidity”.
Ready to start?!
Navigate to which is a web-based solidity compiler which allows us to write and test our smart contracts. You should see something like this:
The first thing we need to do is define which version of solidity we will be using. Type this in at the top of the editor to define the version.
pragma solidity 0.5.6;
Our contract will be called “DappFrundraiser”. All of the code that exists in this contract must live within the two brackets. Also, include an empty constructor which we will use in the next block.
contract DappFundraiser
}
So what exactly is our contract going to do?
Variables:
– owner : who is the contract admin (who deployed it)
– contributers: who has contributed to the fundraiser
– contriubterAmount: the amount a user has contributed
– miniumContribution: the least amount somebody can contribute
– fundingGoal: how much funding does the contract need/want to raise
Functions:
– contribute() : contribute to the campaign
– getContributionBalance(): see how much funds have been added to the fundraiser from contributors
Now we can add our variables into the contract using the following syntax:
// address of admin - the one who deployed the contract
address owner;
// addresses of contributers
address[] public contributers;
// address of contributer - also allowing us to see how much was contributed
mapping(address => uint) public contributerAmount;// minimum amount a user can contribute to the fundraiser
uint public minimumContribution;//amount of funding needed before contract has reached its goal
uint public fundingGoal;
Solidity comes with a global variable called msg. We will use the following in this contract:msg.sender (address): sender of the message (current call)msg.value (uint): number of wei sent with the message
When deploying the contract, the constructor will be called one time, and one time only. This is the perfect opportunity to set the owner (admin), minimum contribution amount, as well as the funding goal for the contract.
pragma solidity 0.5.6;
contract DappFundraiser
}
Now let’s create the function that allows us to check to contract Ehter balance. This is important because user funds should not be taken if the fundraising goal has already been met! This will act as a helper function in our contract.
function getContributionBalance() external view returns(uint)
Ok now we are down to the last function before we deploy and test our first smart contract! The last but most import function “contribute()” is responsible for collecting the funds from users, while making sure this rule applies:
* The contribution payment must meet the minimum contribution fund we set in the constructor.
function contribute() external payable
}
When we put it all together, your contract should look like this:
pragma solidity 0.5.6;
contract DappFundraiserfunction getContributionBalance() external view returns(uint)
function contribute() external payable
}
}
Now it is time to compile our smart contract and test it out. To do this, select the 0.5.6 compiler on the top right side and then click “compile”. If all goes as planned, you should see a green box on the middle of the screen on the right side with the contract name. See screenshot below:
Before doing anything, change your environment to the Javascript VM (see image below)
To deploy the contract, you must enter in the two parameters we included in our constructor (minimumcontribution, and fundingGoal). After these two parameters have been entered in the deploy section, press “transact”. You just deployed your first contract!
You should now see a tab under with the contract name “DappFundraiser”. Click the arrow on the left side to open up the contract functions. You will notice that some of the functions are labeled pink, while others are blue. This goes into a much deeper topic in the involving gas fees. The pink labeled functions are functions that require the transaction chains to be changed () which require a small transaction fee. To learn more about this, please click.
To change wallets (users — each belongs to one user, unless of course one user has multiple wallets), navigate through the accounts dropdown.
When testing the contribute() function, make sure to add the minimum payment in the value section. This is imitating sending (x) amount of ether as a contribution. It is set at 0 every time you click a new function, so make sure to always re-enter the minimum contribution value or more when testing the contribute function :).
Congrats!
You have just written your very first smart contract. This was just a quick tutorial on how to get started with contracts, however, if you are interested and you would like to learn more, you can always use this contract as a base and add a few more functions to it. A few ideas..
1) Add an end date to the fundraising project
2) Refund users if the goal was not met before the contract deadline
3) Destroy contract upon completion
4) Connect smart contract to a javacsript/html web app and build your first Dapp!
Thanks for reading my post! If you have any questions or comments, I would love to hear from you!
Best regards
Published at Wed, 24 Apr 2019 20:35:44 +0000