216

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 ...


108

DELEGATECALL basically says that I'm a contract and I'm allowing (delegating) you to do whatever you want to my storage. DELEGATECALL is a security risk for the sending contract which needs to trust that the receiving contract will treat the storage well. DELEGATECALL was a new opcode that was a bug fix for CALLCODE which did not preserve msg.sender and ...


70

Summary Your original contract code would not work correctly until I added a constant to the function definition to indicate that this function does not modify the blockchain. I had to use the method signature of double(int256) instead of double(int) to get the eth_call JSON-RPC to work. The Details Run Your geth Instance You can use the Testnet ...


43

The difference between in a call and a transaction is the following: transactions are created by your client, signed and broadcasted to the network. They will eventually alter the state of the blockchain, for example, by manipulating balances or values in smart contracts. calls are transactions executed locally on the user's local machine which alone ...


42

Update per @Girish comment, in Solidity 0.6+ the syntax has changed to: address.function{value:msg.value}(arg1, arg2, arg3) Original The general syntax for calling a function in another contract with arguments and sending funds is: address.func.value(amount)(arg1, arg2, arg3) func needs to have the payable modifier (for Solidity 0.4+). Completing @Edmund'...


41

It is not currently possible to return values from functions which modify the blockchain. To receive a return value, you can mark functions as "pure" or "view". For state-changing functions, the only way to "return" information is by using Solidity Events, which coalesce as LOG opcodes in the Ethereum Virtual Machine.


38

At present the user initiating the transaction must pay the fee. However Serenity is likely to enable 'contract pays' schemes. (See Vitalik's blog post here) A possible work around is a contract which refunds the sender however this would still require the user to hold some Ether and might allow malicious parties to repeatedly call the contract to deplete ...


28

There are a few types of string values that you may be referring to. bytes1 - bytes32: fixed size. bytes or string: dynamically sized. Solidity supports functions with multiple return values. contract MultiReturner { function getData() constant returns (bytes32, bytes32) { bytes32 a = "abcd"; bytes32 b = "wxyz"; return (a, b); ...


27

Yes. The internal modifer means that the function can only be called within the contract itself and any derived contracts. private functions are not available in derived contracts. From the docs: internal: Those functions and state variables can only be accessed internally (i.e. from within the current contract or contracts deriving from it), without using ...


27

Here is a snippet of D using delegatecall on E from Difference between CALL, CALLCODE and DELEGATECALL contract D { uint public n; address public sender; function delegatecallSetN(address _e, uint _n) { _e.delegatecall(bytes4(keccak256("setN(uint256)")), _n); // D's storage is set, E is not modified } } contract E { uint public n; address ...


25

No, a sender with zero ether cannot "ask" a contract to pay for the gas costs. A sender with zero ether cannot even send a transaction. More details: The sender of a transaction must have enough gas to cover execution of the transaction. That gas is required even before a contract can even be called. Once the gas requirement is met (so that the ...


24

result is a BigNumber object that is stringified to something like { [String: '5'] s: 1, e: 0, c: [ 5 ] }. You can use BigNumber methods, like result.toNumber() to see it better. When integers are involved, web3.js uses BigNumber because native Javascript numbers are not large enough. It's out of scope here to go into the internals of a general external ...


24

Use an abstract contract (preferred) Further clarifications to @Edmund's answer: contract A { // This doesn't have to match the real contract name. Call it what you like. function f1(bool arg1, uint arg2) returns(uint); // No implementation, just the function signature. This is just so Solidity can work out how to call it. } contract YourContract { ...


24

Here is an example from What is an ABI and why is it needed to interact with contracts? contract Foo { function baz(uint32 x, bool y) returns (bool r) { r = x > 32 || y; } } If we wanted to call baz() with the parameters 69 and true, we would pass 68 bytes in total, which can be broken down into: 0xcdcd77c0: the Method ID. This is derived as the first ...


22

The internal modifier can be better compared with protected in object-oriented programming languages. Internal functions of the contract C are visible to the code running at the current address (i.e. the current contract instance) but also to contracts derived from C. Note that due to the architecture of the EVM, visibility is something that can be strictly ...


22

There are estimateGas APIs in both the JSON-RPC and Javascript. They are estimates and for developers that want further precision, testing is required, possibly on a private chain. If too much gas is provided, the excess gas is converted to ether and refunded. If too few gas is specified, all the specified gas is forfeited to the miner and the transaction ...


20

Yes, the answer is quite logic. Every node has to verify the results of a transaction which invokes a smart contract. The result is that at least every full node will execute the code. The hash of the transaction isn't relevant until it is being stuffed into a merkle tree. When running the execution, all we care about is the amount of gas, the data being ...


20

This process works as follows: You must compile the same exact code that the contract you are trying to load. Once the compile succeeds, you have to be on the same network that the contract is, by setting your MetaMask account propperly. The last thing to do is paste the contract address on the option: Load from address: and you'll get it on remix to ...


18

They will run out of gas. Each smart contract can only be invoked by a transaction. Transactions costs fees and executing contracts costs gas, which are a kind of additional fees for each executional step. If one smart contract calls another contract, it has to create a new transaction which only works if the contract has access to enough ether to pay the ...


18

You can do: /* MORTAL CONTRACT HERE */ contract Name is mortal{ mapping(address=>string) public text; string public test; function register(string _text){ text[msg.sender] = _text; } } contract Proxy is mortal { address watch_addr = 0xEB1e2c19bd833b7f33F9bd0325B74802DF187935; address user_addr = msg.sender; ...


18

In order to send Ether to another contract while specifying the amount of gas, use the call function. targetAddress.call.gas(200000).value(this.balance)(); will call the fallback function. targetAddress.call.gas(200000).value(this.balance)(bytes4(sha3("pay()"))); will call the pay function.


18

Here's an approach that's simpler and checked by the compiler: contract contractA { function blah(int x, int y) payable {} } contract contractB { function invokeContractA() { contractA a = contractA(0x1234567891234567891234567891234567891234); uint ValueToSend = 1234; a.blah{value: ValueToSend}(2, 3); } }


18

getBalance is a function of the Ether.js blockchain provider object, it is used this way : const balance = await provider.getBalance("address"); Note that you can use contract.address to obtain the address of the contract instance.


17

The total cost of a transaction that creates a contract or executes a contract is based on 2 factors: gasUsed is the total gas that is consumed gasPrice specified in the transaction Total cost = gasUsed * gasPrice gasUsed Each operation in the Ethereum Virtual Machine (EVM) was assigned a number of how much gas it consumes. gasUsed is summing up all ...


17

What happens if Bob, Alice and Eve all call the Greeter at the same time and all three calls are in the same block? The three transactions would get called, not necessarily in sequence, and the last transaction's name will be persisted in the mined block. Does everyone get the proper response with their own name? No. The getName() method would return the ...


17

Functions (aka Methods) are specified by the ABI, and have a Method ID, which is the first 4 bytes of the sha3 (Keccak-256) of the method's signature. Here's an example of invoking someFunction on contract: contract.call(bytes4(sha3("someFunction()"))) Here is a tested function with passing a methodId as a parameter: contract C1 { uint public _n; // ...


17

If the deployed contract doesn’t adhere to the ABI, but you know the contract signature (name and argument types) You could use : contract_address.call(bytes4(sha3("function_name(types)")),parameters_values) for example : contrac_A.call(bytes4(sha3("f()")) while there is no input no parameters in your exemple. replace contract_address,function_name,...


16

Transactions are executed purely sequentially, in an order set arbitrarily by the miner who wins each block. Higher gas priced transactions are more likely to make it into a block, so that contributes to order as well, at the discretion of the miner. Note that each miner chooses separately, but only one miner wins a round. The winning miner determines the ...


16

No. You need to present a function that will return the array length as a uint. Something like function getCount() public view returns(uint count) { return array.length; } It's a pretty common requirement. These storage patterns might save you some time working out how to do things within the constraints we're dealing with: Are there well-solved and ...


16

The return-value of a non-constant (neither pure nor view) function is available only when the function is called on-chain (i.e., from this contract or from another contract). When you call such function from the off-chain (e.g., from an ethers.js script), you need to execute it within a transaction, and the return-value is the hash of that transaction. This ...


Only top voted, non community-wiki answers of a minimum length are eligible