0

I'm confused about how the Smart Contracts work internally.
I've read this:
https://blog.zeppelin.solutions/the-hitchhikers-guide-to-smart-contracts-in-ethereum-848f08001f05

TLTR: there is an example of a smart contract "ProofOfExistence" saving the hash for some string into an array

contract ProofOfExistence3 {
  mapping (bytes32 => bool) private proofs;
  // store a proof of existence in the contract state
  function storeProof(bytes32 proof) {
    proofs[proof] = true;
  }
  // calculate and store the proof for a document
  function notarize(string document) {
    var proof = proofFor(document);
    storeProof(proof);
  }
  // helper function to get a document's sha256
  function proofFor(string document) constant returns (bytes32) {
    return sha256(document);
  }
  // check if a document has been notarized
  function checkDocument(string document) constant returns (bool) {
    var proof = proofFor(document);
    return hasProof(proof);
  }
  // returns true if proof is stored
  function hasProof(bytes32 proof) constant returns(bool) {
    return proofs[proof];
  }
}

My questions:
1) What happens when I deploy that contract? in terms of the Ethereum blockchain. The code is saved in a new block on the Ethereum network? I mean in Bitcoin we send some BTC from one address to another, and that info is saved in a new block. Is there any analogy to this for Ethereum and Smart Contracts?

2) Is there any cost to deploy the smart contract?

3) How can I use that contract? Let's say I want to check if some string exists there, how can I do that? the same if I want to save a new string. What is the workflow to execute some function from the smart contract (and to get the result)?

4) Do we need ETH to execute some function on the Smart Contract? why? what is happening on the blockchain when executing it (not deploying)?

I know these could be a bit long to reply, but I'm pretty sure will be a good reference for many newbies like me :)

1 Answer 1

1
  1. Send tx to create a new account, a contract account this is stored in the world state (which is a mapping of the accounts and account state) . This creates a contract storage, our smart contract's bytecode gets stored/referenced. Blocks contain roothashes of the current state, transactions in the block, and transaction receipts (among some other things).
  2. Deploying contracts costs gas indeed, a minimum of 32 000 (CREATE Opcode). This amount increases depending on the size of the bytecode and constructor functions being executed on deployment.
  3. There's a few different methods. You can use geth console and interact with it over RPC. Or use truffle framework and truffle console , which gives you a web3 instance. Any contracts methods that are public/external can be called. There's also libraries available like web3js or ethersjs to build client side applications with your favorite JS frameworks like react, vue or angular.
  4. We need to pay gas (which is a tiny amount of ether) for each transaction. If we call a contract we add calldata to our transaction. This contains the encoded functions and parameters we want to call on our contract (send transaction with the calldata to the contract address). Each operation costs a specific amount of gas, so depending on the complexity of the logic the gas costs can increase. This gas is used to pay for the nodes processing your transaction (executing the call). Gas is there mainly to avoid spam and turing halting problem (blocks have gas limit).

The ethereum white and yellow paper are a really good place to start. Also : https://medium.com/@preethikasireddy/how-does-ethereum-work-anyway-22d1df506369

2
  • I'm still confused about 3. Every smart contract has an address? (not sure if it's a normal Ethereum address, like a wallet?), so I guess to call a function we need that address and the name of the function (some of them are normalized if it's ERC20). And we need to pay the gas to execute the function (we won't know how much is this right? that's why we put a limit?). So in this example, every user with and Ethereum account and ETH (to pay gas) who knows the address to the smart contract could execute the functions? and every time we call a function a new block is added to the Ethereum chain?
    – Enrique
    Commented Jun 15, 2018 at 11:10
  • 1
    It's a normal ethereum address but the difference compared to a user address is that it has bytecode associated with it (see extcodesize() in the yellow paper). Contracts can hold ethereum as well. There is a limit on the gas due to there being a gas limit for blocks so that an operation can't loop forever. If the gas limit is reached or a transaction runs out of gas an 'out of gas'-exception is thrown, reverting the transaction and sending the unspent gas back to the user. Calling a function means sending a transaction with calldata to a contract address. Commented Jun 15, 2018 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.