Questions tagged [contract-invocation]

Questions regarding the act of calling or interacting with (a.k.a. invoking) a contract on the Ethereum blockchain by a user or another contract on the blockchain. Includes both local invocation by call and global invocation by transaction.

Local Invocation by Call

A call is a local invocation of a contract function that does not broadcast or publish anything on the blockchain. It is a read-only operation and will not consume any Ether. It simulates what would happen in a transaction, but discards all the state changes when it is done. It is synchronous and the return value of the contract function is returned immediately.

Its web3.js API is web3.eth.call and is what's used for Solidity constant functions. Its underlying JSON-RPC is eth_call.

Global Invocation by Transaction

A transaction is broadcasted to the network, processed by miners, and if valid, is published on the blockchain. It is a write-operation that will affect other accounts, update the state of the blockchain, and consume Ether (unless a miner accepts it with a gas price of zero).

It is asynchronous, because it is possible that no miners will include the transaction in a block (for example, the gas price for the transaction may be too low). Since it is asynchronous, the immediate return value of a transaction is always the transaction's hash. To get the "return value" of a transaction to a function, Events need to be used (unless it's Case4 discussed below).

Its web3.js API is web3.eth.sendTransaction and is used if a Solidity function is not marked constant. Its underlying JSON-RPC is eth_sendTransaction. sendTransaction will be used when a verb is needed, since it is clearer than simply transaction.

Recommendation to Call first, then send Transaction

Since a sendTransaction costs Ether, it is a good practice to "test the waters" by issuing a call first, before sending a transaction. This is a free way to debug and estimate if there will be any problems with the sendTransaction, for example if an Out of Gas exception will be encountered.

This "dry-run" usually works well, but in some cases be aware that call is an estimate, for example a contract function that returns the previous blockhash, will return different results based on when the call was performed, and when the transaction is actually mined.

Finally, note that even though a call does not consume any Ether, sometimes it may be necessary to specify the actual gas amount for the call: the default gas for call in clients such as Geth, may still be insufficient and can still lead to Out of Gas.

Read more: What is the difference between a transaction and a call?

1182 questions
8
votes
2 answers

web3.eth.call. How can I set "data" param?

I deployed my first contract by using browser-solidity to make bytecode and web3.eth.sendRawTransaction to deploy. The following is the transaction and the deployed contract…
zono
  • 1,473
  • 5
  • 17
  • 30
7
votes
4 answers

How to specify gas/value when making a call using abstract contracts?

How does one specify gas or value making a call this way: contract AbstractB { function getX() returns(uint); } contract A { function makeCall(address addressB){ AbstractB(addressB).getX(); } } The following doesn't…
manidos
  • 4,298
  • 3
  • 31
  • 55
7
votes
2 answers

How does one know that one's contract has been executed?

What is the mechanism to know whether miners/validators have indeed executed one's contract, instead of just blindly validating it?
Symeof
  • 1,434
  • 13
  • 19
6
votes
1 answer

Specify sender between contract calls

Is it possible when calling a contract from another one to specify the sender? Something like the API exposed by web3. function sendTokensToContract() { //Send tokens from this contract to this contract (?!) token.transfer(this, amount);…
5
votes
3 answers

Can you call a contract function via a cron job?

I'm trying to automate the calling of a contract function. Every day, I would like to call a function in one of my contract that makes data available to the users of my DApp. I would like to automate this so that I don't have to manually call the…
user16314
  • 51
  • 1
  • 2
4
votes
2 answers

two of transactions initiating the same smart contract in one block during state transition cycle, what happen?

if two of transactions initiating the same smart contract in one block during state transition cycle, what will happen to the contract account balance and the variables(or data) changes through execution of the code? will the later one overwrite the…
Tuhc Tu
  • 51
  • 2
4
votes
2 answers

do smart contracts execute sequentially in each node or in parallel/simulteneously? across all nodes?

do smart contracts execute sequentially in each node or in parallel/simulteneously? across all nodes? 2nd question is - Does it execute on "ALL" nodes including Light nodes as well?
4
votes
1 answer

Calling multiple smart contracts -- how deep can it go?

How many nested calls can an Ethereum transaction do? Can Smart Contract A call Smart Contract B, then Smart Contract C and so on with only one call through Smart Contract A, and is there a limit to this chain of calls?
Nathan Aw
  • 1,954
  • 12
  • 27
3
votes
2 answers

Ethereum Smart Contract execution principle

I have a question that might sound really dumb, but its understanding is crucial for me. Say, I have a Smart Contract, which has a public variable, and a setter to change that variable. If a Smart Contract is executed by 2 addresses simultaneously,…
Ruham
  • 963
  • 2
  • 12
  • 34
3
votes
2 answers

What various components are needed to trigger an Ethereum contract?

From what I understand, you must provide Ether so there's an incentive for miners to run the contract, but what else is provided? Must you sign cryptographically (to make sure an unrelated party doesn't trigger it?) Any other data or variables…
Mark J.
  • 361
  • 2
  • 6
3
votes
1 answer

I created an ERC20 Token long time ago, and someone sent Ether to the contract.

I created an ERC20 Token long time ago, and someone sent Ether to the contract. instead of invoking some token transfer function. Any way to get the ether out (i don't have the ABI, but it was just some standard token contract)?
xgabrielx
  • 836
  • 4
  • 12
2
votes
1 answer

ExtraBalDaoWithdraw

Can someone please explain more than 2300 withdrawals from the DAO Extra Balance contract that took place today? https://etherscan.io/address/0x755cdba6AE4F479f7164792B318b2a06c759833B
kokica2000
  • 23
  • 2
2
votes
1 answer

Where to include arguments for invocation of state-changing function

I have this contract: pragma solidity ^0.4.0; contract Proof { function register(bytes32 hash) { if (hashToDate[hash] != 0) return; hashToDate[hash] = now; } function dateOf(bytes32 hash) constant returns (uint…
mitchkman
  • 225
  • 3
  • 10
2
votes
2 answers

Very confused by the Contract API

TL;DR I'm very confused by the Contract API in web3: https://web3js.readthedocs.io/en/v1.2.6/web3-eth-contract.html myContract.methods not available in geth console either with Geth 1.9.11-stable and 1.9.12-unstable on Ubuntu 16.0.4 (fast sync'd) Is…
Nicola
  • 31
  • 3
2
votes
1 answer

Payable function that returns value: when is this example practical?

To quote an example from a book: contract StockPriceOracle { uint quoteFee = 500; mapping (string => uint) private stockPrices; //... function getStockPrice(string _stockTicker) payable returns (uint _stockPrice) { if (msg.value ==…
1
2 3 4