Nuxtstop

For all things nuxt.js

How to create a smart contract to receive payments

How to create a smart contract to receive payments
54 14

With the ascendancy of blockchains and cryptocurrencies you do not want to be left out of this right?

In this article I will teach to you how to create a smart contract to receive donations using solidity.

What is Solidity?

Solidity is an object-oriented programming language for writing smart contracts. It is used for implementing smart contracts on various blockchain platforms, most notably, Ethereum. Wikipedia

Pragma Version

First thing we will do after creating a .sol file(solidity extension) is define a pragma version, this is for solidity to understand which version our contract is in and to compile it correctly.

DonateContract.sol

pragma solidity ^0.8.0;
Enter fullscreen mode Exit fullscreen mode

Starting the contract

Now we are going to start our contract, for this we need to call contract + ContractName for solidity to understand where our contract code will be.

DonateContract.sol

pragma solidity ^0.8.0;

contract DonateContract {
 //the code will stay here
}

Enter fullscreen mode Exit fullscreen mode

Variables

Solidity supports three types of variables:

  1. State variables: variables whose values are permanently stored in a contract storage.
  2. Local variables: variables whose values are present till function is executing.
  3. Global variables: special variables exists in the global namespace used to get information about the blockchain.

We are going to create two types of state variables:

The variables are written as follows: type + variableName

  1. totalDonations: totalDonations is a uint that stores the amount of donations that have already been made.
  2. owner: owner is of the payable address type that will be the creator of the contract that will receive the donations.

Obs: all addresses that will accept payment or make payment must be of the payable type.

DonateContract.sol

pragma solidity ^0.8.0;

contract DonateContract {

  uint totalDonations; // the amount of donations
  address payable owner; // contract creator's address

}

Enter fullscreen mode Exit fullscreen mode

Using the constructor

In our solidity contract we have a constructor to set up our contract and set some standards.

We will define who will be the owner of our contract and that it will be of the payable type, in this case the creator of the contract.

DonateContract.sol

pragma solidity ^0.8.0;

contract DonateContract {

  uint totalDonations; // the amount of donations
  address payable owner; // contract creator's address

  //contract settings
  constructor() {
    owner = payable(msg.sender); // setting the contract creator
  }

}
Enter fullscreen mode Exit fullscreen mode

Creating our first function

Now we are going to create a simple function to return the amount of donations.

DonateContract.sol

pragma solidity ^0.8.0;

contract DonateContract {

  uint totalDonations; // the amount of donations
  address payable owner; // contract creator's address

  //contract settings
  constructor() {
    owner = payable(msg.sender); // setting the contract creator
  }

  // public function to return the amount of donations
  function getTotalDonations() view public returns(uint) {
    return totalDonations;
  }
}
Enter fullscreen mode Exit fullscreen mode

Our function takes three parameters:

  1. public: This function is of the public type and can be called by anyone.
  2. view: It means that this function is a view-only function and does not do any transactions and you don't need to pay gas to use it.
  3. returns(uint): Here we are saying that the function will return something of type uint.

Creating function to make donation

Now we are going to make a function to make the donation, we need say that it is public and payable.

DonateContract.sol

pragma solidity ^0.8.0;

contract DonateContract {

  uint totalDonations; // the amount of donations
  address payable owner; // contract creator's address

  //contract settings
  constructor() {
    owner = payable(msg.sender); // setting the contract creator
  }

  //public function to make donate
  function donate() public payable {
    (bool success,) = owner.call{value: msg.value}("");
    require(success, "Failed to send money");
  }

  // public function to return total of donations
  function getTotalDonations() view public returns(uint) {
    return totalDonations;
  }
}
Enter fullscreen mode Exit fullscreen mode

Obs: you can use nonReentrant to give more secure to your contract.

In our donate function we use owner.call{value: msg.value}("") to make a payment to owner of contract, where msg.value(global variable) is the value we want to send as a donation.

Then we get the call return to check if the transfer was successful using require.

The require takes as the first parameter the variable success saying whether a transaction was successful or not, thus returning an error or proceeding. The second parameter will be the message sent in case of error.

Ready!! we have our first contract ready. 😁🥳

For more content you can follow me here and on my twitter:
Twitter