0xCODE
Published in

0xCODE

Functions Calls and Transaction Objects

Function Calls And Transaction Objects In Smart Contracts Using Solidity

When you execute a smart contract on the Ethereum blockchain, you can invoke a function call and not incur the cost of gas. It is possible when viewing information that is already stored on the blockchain from previous transactions. However, when you send a new transaction object then that is the time costs are incurred.

Source Code

Let us use the following code as an example (function_transaction.sol).

pragma solidity >=0.4.0 <0.6.0;contract dataStorage {   uint dataStore;

function set(uint x) public {
dataStore = x;
}

function get() public view returns (uint) {
return dataStore;
}
}

We begin by issuing a pragma directive to use compiler version greater than version 0.4.0 but less than version 0.6.0.

This is a smart contract called dataStorage. It simply takes the value of variable declaration uint (unsigned integer) called dataStore and stores it on the blockchain. It is viewable to the public, therefore the value can be accessed using a function call.

There are two simple functions in the smart contract.

The first function is set(), which allows a data input to the variable dataStore. It is viewable to the public and the value of x (input) is stored to dataStore. This creates a transaction object which changes the world state of the blockchain when the value is set for dataStore.

function set(uint x) public {
dataStore = x;
}

The second function is get(), which is a function call to view the value that was set in dataStore from the set() function. The statement when issued, returns the value of dataStore. It also has visibility to the public like the first function, so both statements when deployed on the network can be viewed by everyone.

function get() public view returns (uint) {
return dataStore;
}

Using REMIX IDE, the source code is compiled into bytecode on the EVM (Ethereum Virtual Machine).

{
"linkReferences": {},
"object": "6060604052341561000f57600080fd5b60cb8061001d6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806360fe47b11460465780636d4ce63c14606657600080fd5b3415605057600080fd5b60646004808035906020019091905050608c565b005b3415607057600080fd5b60766096565b6040518082815260200191505060405180910390f35b8060008190555050565b600080549050905600a165627a7a72305820a7c43f822a741570738f5b46ce1dd49cc65ebd5e29c37993fc809ac6b2a3ad3c0029",
"opcodes": "PUSH1 0x60 PUSH1 0x40 MSTORE CALLVALUE ISZERO PUSH2 0xF JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST PUSH1 0xCB DUP1 PUSH2 0x1D PUSH1 0x0 CODECOPY PUSH1 0x0 RETURN STOP PUSH1 0x60 PUSH1 0x40 MSTORE PUSH1 0x0 CALLDATALOAD PUSH29 0x100000000000000000000000000000000000000000000000000000000 SWAP1 DIV PUSH4 0xFFFFFFFF AND DUP1 PUSH4 0x60FE47B1 EQ PUSH1 0x46 JUMPI DUP1 PUSH4 0x6D4CE63C EQ PUSH1 0x66 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST CALLVALUE ISZERO PUSH1 0x50 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST PUSH1 0x64 PUSH1 0x4 DUP1 DUP1 CALLDATALOAD SWAP1 PUSH1 0x20 ADD SWAP1 SWAP2 SWAP1 POP POP PUSH1 0x8C JUMP JUMPDEST STOP JUMPDEST CALLVALUE ISZERO PUSH1 0x70 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST PUSH1 0x76 PUSH1 0x96 JUMP JUMPDEST PUSH1 0x40 MLOAD DUP1 DUP3 DUP2 MSTORE PUSH1 0x20 ADD SWAP2 POP POP PUSH1 0x40 MLOAD DUP1 SWAP2 SUB SWAP1 RETURN JUMPDEST DUP1 PUSH1 0x0 DUP2 SWAP1 SSTORE POP POP JUMP JUMPDEST PUSH1 0x0 DUP1 SLOAD SWAP1 POP SWAP1 JUMP STOP LOG1 PUSH6 0x627A7A723058 KECCAK256 0xa7 0xc4 0x3f DUP3 0x2a PUSH21 0x1570738F5B46CE1DD49CC65EBD5E29C37993FC809A 0xc6 0xb2 LOG3 0xad EXTCODECOPY STOP 0x29 ",
"sourceMap": "32:186:0:-;;;;;;;;;;;;;;;;;"
}

The generated bytecode contains opcodes that the EVM will execute in the smart contract. These are the machine readable statements of the source code.

The smart contract dataStorage is deployed using a JVM (Javascript Virtual Environment) since this is for testing purposes only. It is basically a simulated version of the EVM implemented in Javascript to run inside a browser window.

The account being used, provided for testing by the JVM, has an EOA address of:

0xca35b7d915458ef540ade6068dfe2f44e8fa733c

Smart Contract Deployment

To deploy the contract, the Deploy button must be clicked. There is an option available if this is to deploy an existing smart contract that already has a contract address. This smart contract does not yet have an address of its own.

Initially the account has a total of 100 ether. Every time there is a change in state, there is a cost deducted from the account. The costs include the deployment of the smart contract and execution of transaction objects.

I incurred a total cost of gas measured in units of Wei amounting to 106712. A Wei is 1e-18 ether.

The smart contract obtains an address once it has been deployed. In this example the address is:

0x692a70d2e424a56d2c6c27aa97d1a86395877b3a

I set a value of 50 for dataStore by typing the value on the input box next to the set button. Clicking the button executes the opcode for the set() function. The get button was also clicked and that executes the function call get(), to return the value of set in dataStore.

Breakdown Analysis

Behind the scenes, the debug window shows what happened.

I incurred the cost of gas, which was set to a limit of 3000000. The transaction object was nowhere near that value. The transaction cost for this simple instruction was just 41621 gas, and the execution cost 20157 gas. The account is charged the cost for computation and so the amount of ether is reduced. Whenever the set() function is called and there is a change in value, a cost will be incurred. A hash of the transaction is then created with a value of :

0x45d36df9fc3d34d95a045e5f98154005966f9b1f92dfa6a55bb7bf180bb90a44

Now the costs are not fixed and results vary from each environment. In this example, deploying the smart contract and sending the transaction object for execution cost a total of 106712 + 41621 = 148333 gas. That is subtracted from the initial 100 ether and now the account has 99.000000000000851667 ether.

When we use the get() function, we are returned the value of dataStore.

No cost was incurred here, unless it was called by another smart contract. From the debug window the decoded output shows the return value of 50.

Synopsis

When calling functions to get the value of a transaction, there are no gas costs. This does not cause a change in the world state of the blockchain. When sending a transaction object to the EVM, this creates a change in the world state. An appropriate gas cost will be charged by the network and it will be reflected in the user’s account.

--

--

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