Learn Solidity: Globally Available Variables & Functions

In this post, we will understand the globally available variables & functions in solidity programming language.

Notice: This is one of the multi-post series of Learn Solidity - Build Decentralized Application in Ethereum. This is an attempt to teach you all about Solidity - A Programming Language for Ethereum Based Smart Contracts. If you want to take this as a video course please signup using below button.

[pastacode lang=”javascript” manual=”pragma%20solidity%200.4.8%3B%20%0A%0A%2F*%0A*%20%40title%20Learn%20Solidity%3A%20Globally%20Available%20Variables%20%26%20Functions%20in%20Solidity%20Programming%20Language%0A*%20%40author%20Toshendra%20Sharma%0A*%20%40notice%20Example%20for%20the%20Learn%20Solidity%20Series%0A*%2F%0A%0A%2F%2F%20There%20are%20many%20global%20%0A%0Acontract%20GlobalVariablesAndFunctions%20%7B%0A%0A%20%20%20%20%2F%2F%20msg%20represent%20the%20current%20message%20received%20by%20the%20contracts%20during%20the%20execution.%20%0A%20%20%20%20%2F%2F%20So%20when%20you%20as%20a%20user%20send%20a%20message%20to%20contract%20or%20make%20a%20function%20call%20from%20drop%20down%20menu%20of%20mist%20wallet%0A%20%20%20%20%2F%2F%20you%20catually%20send%20a%20message%20to%20contract%20which%20can%20be%20accessed%20by%20using%20msg%0A%0A%20%20%20%20%2F%2F%20using%20msg.sender%20you%20can%20access%20then%20address%20of%20the%20sender%0A%20%20%20%20msg.sender%3B%0A%0A%20%20%20%20%2F%2F%20amount%20of%20ether%20provided%20to%20this%20contract%20in%20wei%20can%20be%20accessed%20like%20this%0A%20%20%20%20msg.value%3B%20%0A%0A%20%20%20%20%2F%2F%20complete%20call%20data%20in%20bytes%20can%20be%20accessed%20like%20this%0A%20%20%20%20msg.data%3B%20%0A%0A%20%20%20%20%2F%2F%20you%20can%20access%20the%20remaining%20gas%20like%20this%0A%20%20%20%20msg.gas%3B%20%0A%0A%20%20%20%20%2F%2F%20you%20can%20return%20the%20first%20four%20bytes%20of%20the%20call%20data%20like%20this%0A%20%20%20%20msg.sig%3B%20%0A%0A%20%20%20%20%0A%0A%20%20%20%20%2F%2F%20similarly%20tx%20represent%20the%20current%20transaction%20in%20smart%20contract%0A%0A%20%20%20%20%2F%2F%20you%20can%20access%20address%20of%20sender%20of%20the%20transaction%20like%20this%0A%20%20%20%20tx.origin%3B%20%0A%0A%20%20%20%20%2F%2F%20you%20can%20access%20the%20gas%20price%20of%20the%20transaction%0A%20%20%20%20tx.gasprice%3B%20%0A%0A%0A%20%20%20%20%0A%20%20%20%20%2F%2F%20now%20will%20give%20you%20the%20current%20time%20approximately%20in%20unix%20timestamp%20format%0A%20%20%20%20now%3B%20%0A%0A%20%20%20%20%2F%2F%20like%20msg%20%26%20tx%2C%20block%20represent%20the%20information%20about%20the%20current%20Block%0A%0A%20%20%20%20block.number%3B%20%2F%2F%20current%20block%20number%0A%20%20%20%20block.difficulty%3B%20%2F%2F%20current%20block%20difficulty%0A%20%20%20%20block.blockhash(1)%3B%20%2F%2F%20returns%20bytes32%2C%20only%20works%20for%20most%20recent%20256%20blocks%0A%20%20%20%20block.gasLimit()%3B%20%2F%2Freturn%20Gas%20limit%0A%20%20%20%20block.coinbase%20()%3B%20%2F%2F%20return%20current%20block%20miner%E2%80%99s%20address%0A%0A%20%20%20%20%2F%2F%20there%20are%20manu%20athematical%20and%20Cryptographic%20Functions%20as%20well%20%0A%20%20%20%20%2F%2F%20which%20you%20can%20access%20anywhere%20in%20smart%20contracts%0A%20%20%20%20assert(bool%20condition)%3B%20%2F%2F%20throws%20an%20exception%20if%20the%20condition%20is%20not%20met.%0A%20%20%20%20addmod(uint%20x%2C%20uint%20y%2C%20uint%20k)%3B%20%2F%2F%20returns%20(uint)%3B%20%20%2F%2Fcompute%20(x%20%2B%20y)%20%25%20k%20where%20the%20addition%20is%20performed%20with%20arbitrary%20precision%20and%20does%20not%20wrap%20around%20at%202**256.%0A%20%20%20%20mulmod(uint%20x%2C%20uint%20y%2C%20uint%20k)%3B%2F%2F%20returns%20(uint)%3B%20%2F%2F%20compute%20(x%20*%20y)%20%25%20k%20where%20the%20multiplication%20is%20performed%20with%20arbitrary%20precision%20and%20does%20not%20wrap%20around%20at%202**256.%0A%20%20%20%20keccak256(…)%3B%20%2F%2F%20returns%20(bytes32)%3B%20%2F%2F%20compute%20the%20Ethereum-SHA-3%20(Keccak-256)%20hash%20of%20the%20(tightly%20packed)%20arguments%0A%20%20%20%20sha3(…)%3B%20%2F%2F%20returns%20(bytes32)%3B%20%2F%2F%20alias%20to%20keccak256()%0A%20%20%20%20sha256(…)%3B%20%2F%2F%20returns%20(bytes32)%20%2F%2F%20compute%20the%20SHA-256%20hash%20of%20the%20(tightly%20packed)%20arguments%0A%20%20%20%20ripemd160(…)%3B%20%2F%2F%20returns%20(bytes20)%20%2F%2F%20compute%20RIPEMD-160%20hash%20of%20the%20(tightly%20packed)%20arguments%0A%20%20%20%20ecrecover(bytes32%20hash%2C%20uint8%20v%2C%20bytes32%20r%2C%20bytes32%20s)%3B%20%2F%2F%20returns%20(address)%20%2F%2F%20recover%20the%20address%20associated%20with%20the%20public%20key%20from%20elliptic%20curve%20signature%20or%20return%20zero%20on%20error%0A%20%20%20%20revert()%3B%20%2F%2F%20abort%20execution%20and%20revert%20state%20changes%0A%20%20%20%20%2F%2F%20sha3(%22ab%22%2C%20%22c%22)%20%3D%3D%20sha3(%22abc%22)%20%3D%3D%20sha3(0x616263)%20%3D%3D%20sha3(6382179)%20%3D%20sha3(97%2C%2098%2C%2099)%0A%0A%20%20%20%20%2F%2F%20Address%20Related%0A%20%20%20%20%3Caddress%3E.balance%3B%20%2F%2F%20returns%20(uint256)%3B%20%2F%2F%20balance%20of%20the%20Address%20in%20Wei%0A%20%20%20%20%3Caddress%3E.send(uint256%20amount)%3B%20%2F%2F%20returns%20(bool)%3B%20%2F%2Fsend%20given%20amount%20of%20Wei%20to%20Address%2C%20returns%20false%20on%20failure%0A%20%20%20%20%3Caddress%3E.transfer(uint256%20amount)%3B%20%2F%2F%20send%20given%20amount%20of%20Wei%20to%20Address%2C%20but%20throws%20on%20failure%0A%0A%20%20%20%20%2F%2F%20Contracts%20Related%0A%20%20%20%20this%3B%20%2F%2F%20(current%20contract%E2%80%99s%20type)%20the%20current%20contract%2C%20explicitly%20convertible%20to%20Address%0A%20%20%20%20selfdestruct(address%20recipient)%3B%20%2F%2F%20destroy%20the%20current%20contract%2C%20sending%20its%20funds%20to%20the%20given%20Address%0A%0A%0A%0A%20%20%20%20%2F%2F%20Define%20consutruct%20here%0A%20%20%20%20function%20GlobalVariablesAndFunctions(uint%20initialCoins)%20%7B%0A%20%20%20%20%20%20%20%2F%2F%20Initialize%20state%20variables%20here%0A%20%20%20%20%7D%0A%0A%7D” message=”” highlight=”” provider=”manual”/]



[otw_shortcode_button href=”https://www.toshblocks.com/solidity/ether-units-time-units-solidity-programming-language/” size=”large” icon_type=”general foundicon-left-arrow” icon_position=”left” shape=”radius” color_class=”otw-blue”]Previous: Ether Units & Time Units in Solidity?[/otw_shortcode_button][otw_shortcode_button href=”https://www.toshblocks.com/solidity/operators-arithmetic-logical-bitwise/” size=”large” icon_type=”general foundicon-right-arrow” icon_position=”right” shape=”radius” color_class=”otw-green”]Next: Operators: Arithmetic, Logical & Bitwise Operators[/otw_shortcode_button]