Blockchain Development — Day 4

In the last article, “Sending ether to a contract and withdrawing” we looked at a working example for using the address variable and msg object. In this article, we are going to extend our contract to store the balance for each addres sending money to the contract using the mapping variable.

Mappings Explained

Mappings act as hash tables which consist of key corresponding to value pairs. In our example, we will map an address to a balance.

mapping (address => uint)

This is the same code as the last contract with the new variable, balanceReceived that maps addresses to balances.

We modify the sendMoneyToContractfunction to increment the amount the address that sent money to the contract and

Lets now add a getBalanceForAddressfunction that gets the balance for a specified address

Finally, lets create a functionwithdrawMoneypassing the address we want to send the money too and the amount to send.

Line 34 : Checks that the msg sender has enough to send, otherwise it will send a message that there is not enough funds to transfer

Line 36: Updates the balance

Line 38: Sends the amount to the specified address

That’s covers extended smart contract to record each addresses balance sent to the smart contract using mapping.

If you found this article helpful, please give me a Clap, as this brightens my day knowing I may have helped someone in their journey in making stuff! Also if you want to be notified for future releases in this series, “Follow” me. You may also send me a message if you need any further help.

--

--

--

A Data & Analytics Consultant who is expanding into developing experiences in XR, Enterprise and Gaming space using Unity www.jeannoelseneque.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Oikos Integrates Chainlink to Secure Access to Decentralized Price Feeds

Newsletter Takes a Byte of the Blockchain Industry

Morning Market Report(Feb.22)

NFTs Will Replace Copyrights and Trademarks

What are smart contracts and how SmartChain.io simplifies the process of smart contracts writing

Michael Wagner delivers visionary speech at largest Game Developer Conference

Meeting top 8 innovators of Oraichain Hackathon 2022

WONO: Bringing Freelancing to The Blockchain

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jean-Noel Seneque

Jean-Noel Seneque

A Data & Analytics Consultant who is expanding into developing experiences in XR, Enterprise and Gaming space using Unity www.jeannoelseneque.com

More from Medium

Exploring Blockchain Development Series — Day 1

I Built a Tool to Sign Contracts on Smart Contracts Without Using Your Private Key

Cryptography 2: Irreversibility and Cryptographic Hashing on the Blockchain

A private key and a public key and a bitcoin address together with the principle of irreversibility

Some Useful GitHub Repositories To Enhance Your Web3 Skills