How to write your first smart contract in solidity

  1. You will learn how to use browser-solidity.
  2. And how to write a simple token sale contract and call its methods.
  1. Getting started with browser-solidity
  1. The Contract Tab is where the transactions take place. You can select the development environment, the accounts and the gas limit and value.
  2. The environment tab allows you to select between running the contract on a web3 provider , injected Web3 and javascript VM. A web3 provider connects to an actual node or a localhost. The injected Web3 uses the environment provided by mist. We will use javascript VM where everything is local and in memory of browser-solidity.
  3. You can select the user accounts available, according to your environment in the accounts list. Selecting an account from the list is equivalent to ‘logging into’ the account. Transactions will take place from the account thereon. The addresses can be copied by using the button adjacent to the dropdown.
  4. Gas limit sets the maximum gas allowed for each transaction.
  5. Value corresponds to msg.value, or the ethers, the default cryptocurrency used in the ethereum blockchain, that will be transferred with each payable function. The default currency we will use here is Wei. Make sure that the amount you enter in the value box trails by the tag ‘wei’. For ex-: ’10 wei’.
  6. The settings tab deals with compiler related options. You don’t need to deal with it, unless you want to change the compiler version or if you need to compile the code (when auto compile is switched off)
  7. Under the files tab you have option to publish all your files to a public github gist or to copy it to another browser-solidity instance.
  8. The debugger will allow you to run the code line by line in case you get stuck somewhere with your smart contract. It will become a very powerful tool when you start writing your own smart contracts.
  1. A Simple Token Sale Contract
  1. msg.value points to the ‘value’ in Contract tab dialog box. That is, when you call the function, msg.value will be equal to the amount you have entered in the ‘value’ dialog box.
  2. msg.sender is the account you have selected from the drop down list.
  1. The launch debugger opens up the debugger tab allowing you to understand the flow of the code. It becomes easy to identify errors when you face one.
  2. The copy address button sounds self explanatory. It will copy the address of the voting smart contract. Useful for sending ether to the contract, etc. In this case, you will copy the address of the contract to check the ether balance using the checkBalance function after tokens have been bought.
  3. Transaction costs are the costs for sending the contract code to the ethereum blockchaindepending on the size of the contract.
  4. Execution costs cover the costs for storing global variables and the runtime of the method calls.
  1. Select any of the addresses from the dropdown list to make them the transacting account to start buying tokens.
  2. After selecting the address, enter the amount you want to spend to buy tokens in the ‘value’ dialog box. 1 wei fetches you 10 tokens (set by the tokenSwap variable). Write the respective value in wei (for ex:- 10 wei) in the value dialog box. Make sure you write ‘Wei’ after the amount. And make sure the amount is lesser than or equal to 1000 wei.
  3. Now call the function by simply clicking on the buyTokens function.

--

--

--

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

Recommended from Medium

NEM’s next generation core (Codename “Catapult”) unveils its power for productive use - confirmed…

GamingBlock Commercial White Paper

How DeFi has shifted the power in hands of consumers

What is Blockchain

Ethereum 2.0 : The Merge to be tested on a second test network

The Merge : with this merger, Ethereum is about to become Ethereum 2.0

Public Nodes vs Getblock Shared Nodes vs Getblock Dedicated Nodes: How Do They Differ?

RFP: Another programming language for smart contracts

What Crypto Has in Store for the Future

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
ABHISHEK KUMAR

ABHISHEK KUMAR

More from Medium

Solidity Gotchas — Part 3: Compile Size Limitations and Libraries

Course 2 Project: Design and Development of Smart Contract

The Ethernaut Challenge #2 Solution — Fallout

How to interact with the smart contract using web3.js