Solidity Tutorial: How to build and deploy a smart contract to send Ether from one account to another
Tags: solidity, web3, programming, tutorial
Published: 2022-04-22 13:53:00


Before we jump right in, I wrote an in-depth tutorial on how to [deploy a smart contract to Rinkeby testnet,](https://dev.to/sparklesix/writing-and-deploying-a-solidity-smart-contract-to-rinkeby-test-network-2d0k) you can go read it up.

So let's goooo!!

Below are the table of contents.

## Intro
## What tools do we need?
## Writing a smart contract.
## Compiling and deploying to the JavaScript VM.
## Testing the smart contract.


## Intro

Oi, so here we going to be building a smart contract that can be able to send funds(ether) from one Ethereum account/address to another. Also, we will be adding a feature that allows only the owner(deployer) of the contract to be able to send these funds(ether).

## What tools do we need?

The tools we will be needing for this tutorial is nothing other than the [remix Ethereum](http://remix.ethereum.org/) web IDE for quick and rapid development and deployment of smart contracts written in Solidity.

You don't need to know anything about solidity or smart contracts in order to follow up with this tutorial as this is beginner friendly, and I try my best to explain every line of code as I write them.

## Writing a smart contract 
 
In order to get started, you have to jump over to this website known as [Remix Ethereum](http://remix.ethereum.org/). The below is a screenshot of the interface of a website you might get some annoying pop-ups so just hit the decline or accept button to get in.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i3kmynr3nzoi8fqxn6uo.jpg)
- Once we are here, head over to the left sidebar and locate the file icon. Click on it and it should pull up a makeshift file manager.
- Locate the contract folder and click on it.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/asgpkvkwu8bbbuygb95d.PNG)

Just right above the contract folder you should see a file icon at the very top just beside the folder icon. 

- Click on it and it should generate a fresh file which you can name anything. But in this case, I'm going to name it **sendEther.sol**

Great, now let's get started with the good stuff.

Write the below on the first line of your solidity file. 

`// SPDX-License-Identifier: MIT`

This tells the Solidity compiler that we are using the MIT license for this program.
SPDX means "Software Package Data Exchange" an open standard for software bill of materials.
SPDX allows the expression of components, licenses, copyrights, security references and other metadata relating to software.
You can read more about this [here](https://en.wikipedia.org/wiki/Software_Package_Data_Exchange).

Next, let's define the version of Solidity we are going to use by writing the below.

`pragma solidity ^0.8.11;`

This tells the compiler that we are going to be using the 0.8.11 and above version of Solidity.
Ps: Solidity is a strongly typed compiled language so the semi colons are necessary.

Let's write a smart contract by using the Contract keyword like below.

`contract SendEth{}`

This is kind of the entry point where you can start writing out the functionalities, related state variables and logic of your contract. It's kind of conventional to capitalize the contract identifier. It can be any name you want, doesn't matter.

This smart contract is going to be needing two state variables named amount (for holding the amount to be sent) and the owner address.

Write the below inside your already defined contract body.

```solidity
uint public amount;
address payable owner;
```

Solidity just like other languages have datatypes and uint is one of them. It actually means unsigned integer (no negatives say -20,nah we don't do that here)

Some other Solidity datatypes include bool, struct, mapping, string, address…

`address payable owner;`

Since we are sending ether from our Ethereum address we need to add the "payable" keyword right before our identifier.

Now, let's write the constructor just below our state variables.

```solidity
constructor () {
     owner = payable(msg.sender); // set the deployer of contract as the owner
   }
```

The constructor allows us to define some logic to be set during the deployment of our smart contract. Let's say for example you want instantiate say a name variable during deployment, you do that in the constructor.

`owner = payable(msg.sender);`

Here, we are calling the owner address that we declared as a state variable and assigning it to **msg.sender**. Then we cast the msg.sender as a payable address.
**msg.sender** is kind of a global variable that you get access to in your contract and in this case, it holds the payable (can receive ether) address of the person who deployed the contract.

This might be confusing tho, I will clear the air once we get to the deployment stage so you can see it for yourself.

Your file right now should contain the below.

```solidity
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.11;

contract SendEth {
   uint public amount;
   address payable owner;
   constructor (){
     owner = payable(msg.sender); // set the deployer of contract as the owner
   }
}
```

Next, let's write a function to enable us send Ether to any address.

```solidity
function sendEth(address payable receiver) payable public{
     }
```

Here we are declaring a function with name of **sendEth **and a parameter of a **payable address.** Any address sent in to this function should be able to receive funds (payable).
Also, since the function allows one to be able to send ether to another account we need to add the **payable** keyword to it.
The **public ** keyword ensures that this function is able to seen and be called anywhere within the smart contract.

Now, add the below statement into the function body.

```solidity
require(owner == msg.sender, "Only the owner can send funds");
amount = msg.value;
receiver.transfer(amount);
```

The first line ensures that only the owner of the contract who deployed it can be able to call this function. That means an outsider with a different Ethereum address can't send funds by calling this function.

`amount = msg.value;`

Here, we are setting the value the owner of the contract sent to the variable amount. 

**msg.value** is another global variable that is available to you.

`receiver.transfer(amount);`

Then we send the ether to the address passed into the function as an argument using the **transfer()** function.

And that's it!! Your whole code should be like this below.

```solidity

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.11;

contract Sender {
   uint public amount;
   address payable owner;

   constructor (){
     owner = payable(msg.sender); // set the deployer of contract as the owner
   }
   function sendEth(address payable receiver) payable public{
     require(owner == msg.sender, "Only the owner can send funds");
     amount = msg.value;
     receiver.transfer(amount);
}
}
```

Now, it's time to deploy.
Go to the left of your sidebar and click on the solidity icon.
You should see the below.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i27czliq6zm51oovjnoq.PNG)

If the compiler version is not on the version you wrote your contract in, you can change it here by switching to your preferred compiler.

- Also, check the auto compile button so it auto compiles your code.
- Click the "compile" button to compile it.

Deploying.

Cool, now we are going to deploy the contract on the JavaScript VM. This is a makeshift in-memory blockchain that remix avails us with.

- Click the icon below the compile one and you should get the screen below.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jzi6nwmx3se2g151ls81.PNG)

Once here there are couple of things to do:

- Leave the environment as JavaScript VM.
- Select the eth account that you want to deploy the contract with. The first eth account is good enough.
- Leave the gas fees and stuff as is.(Submit to the process)


Finally, click on the red deploy button.
If you see this green light in the console, then it was successful. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6j6oht0t9wev34021xh9.PNG)

_Phew!! You can beat your chest right now and change your Twitter bio to "Smart Contract/Blockchain developer"_

## Testing the smart contract

- Scroll down below and you should see some text that says "Deployed contract"
- Expand the accordion and you should see two buttons. The "sendEth" function and "amount" variable we created.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3zjzhhjqfxj0z66t3q1b.PNG)

- Now go up to the account dropdown and switch to a new account(Seecond eth address).
- Copy the account address by clicking on the copy icon beside it.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zygon8c2h7jbfi7z6x01.PNG)

- Change back to the account which you deployed your contract with. In my case, it was the first account.

**NOTE:** _It is pertinent that you use the same Eth account that you used to deploy or else it won't work._ Remember the **require()** that we added to the top of the **sendEth **function? Yo, you do.

- Copy the account address (second address) and paste into the **sendEth** textfield.
- On the "Value" dropdown change the 0 to any number of your choice and change the "wei" to "ether".
- Scroll down and smash the sendEth button. 

Bam!! You just sent ether to another eth address.

Check the other account where you copied the address and it should increment by the amount of ether you sent in.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wul47c0phmz7n6oage89.PNG)

Now switch the account to a new one and try sending some eth to it. You will encounter an error that says "Only the owner of the contract can send funds" (Its our handwork o.)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/21gebewowuo6xshgs24t.PNG)
And there you have it, you were able to write a smart contract that can send ether to a different account and learnt the basics of solidity while at it.

You might be asking "Is it because of this 7 lines of code that you wrote this term paper of an article?"

Well, I assumed you had no prior knowledge of Solidity so I tried to explain every line of code to avoid any confusion.

_You can follow me on [GitHub ](https://github.com/sparkle666)I want to see what projects you are working on._

Sayonara...