0

I am new to blockchain development. I am trying to understand where, when, and how a transaction is sent to the blockchain to be recorded.

Here is the sample code I am studying:

pragma solidity ^0.4.21;

import "./EIP20Interface.sol";


contract EIP20 is EIP20Interface {

    uint256 constant private MAX_UINT256 = 2**256 - 1;
    mapping (address => uint256) public balances;
    mapping (address => mapping (address => uint256)) public allowed;
    /*
    NOTE:
    The following variables are OPTIONAL vanities. One does not have to include them.
    They allow one to customise the token contract & in no way influences the core functionality.
    Some wallets/interfaces might not even bother to look at this information.
    */
    string public name;                   //fancy name: eg Simon Bucks
    uint8 public decimals;                //How many decimals to show.
    string public symbol;                 //An identifier: eg SBX

    function EIP20(
        uint256 _initialAmount,
        string _tokenName,
        uint8 _decimalUnits,
        string _tokenSymbol
    ) public {
        balances[msg.sender] = _initialAmount;               // Give the creator all initial tokens
        totalSupply = _initialAmount;                        // Update total supply
        name = _tokenName;                                   // Set the name for display purposes
        decimals = _decimalUnits;                            // Amount of decimals for display purposes
        symbol = _tokenSymbol;                               // Set the symbol for display purposes
    }

    function transfer(address _to, uint256 _value) public returns (bool success) {
        require(balances[msg.sender] >= _value);
        balances[msg.sender] -= _value;
        balances[_to] += _value;
        emit Transfer(msg.sender, _to, _value); //solhint-disable-line indent, no-unused-vars
        return true;
    }

    function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
        uint256 allowance = allowed[_from][msg.sender];
        require(balances[_from] >= _value && allowance >= _value);
        balances[_to] += _value;
        balances[_from] -= _value;
        if (allowance < MAX_UINT256) {
            allowed[_from][msg.sender] -= _value;
        }
        emit Transfer(_from, _to, _value); //solhint-disable-line indent, no-unused-vars
        return true;
    }

    function balanceOf(address _owner) public view returns (uint256 balance) {
        return balances[_owner];
    }

    function approve(address _spender, uint256 _value) public returns (bool success) {
        allowed[msg.sender][_spender] = _value;
        emit Approval(msg.sender, _spender, _value); //solhint-disable-line indent, no-unused-vars
        return true;
    }

    function allowance(address _owner, address _spender) public view returns (uint256 remaining) {
        return allowed[_owner][_spender];
    }
}

Where in this code does it send the transaction to the blockchain? Is it the event in the transfer function?

Thank you so much for your help!

2 Answers 2

1

Everything (no exceptions) starts with a transaction signed by an externally owned account (EOA).

Signed transactions are sent to the network and included in a block. Blocks establish the canonical transaction order. (Peer discovery, gossip and mining.)

Ordered transactions are processed by nodes as the blocks arrive. To deploy that contract, first, it is compiled (dev does it, off-chain) into ByteCode which is the native executable format. ByteCode is similar to compiled assembler but the "CPU" and other concerns are not defined by processor/hardware/platform. Instead, they are defined in the Ethereum protocol as logical instructions. In case it helps, it is modeled as a stack machine with a single stack.

A contract is deployed by sending a transaction:

  • crafted a specific way to include the instructions for deployment itself (basically a wrapper that includes a preamble and any arguments that are to be passed into the constructor, if any).
  • addressed to address 0x0, a.k.a. null address.

Properly formed create transactions will:

  • pass the arguments into the constructor and run it
  • If the constructor doesn't abort, then ...
  • deploy the bytecode to a deterministic address

They do this because the ByteCode (remember the wrapper) tells the EVM to that, and every node that verifies the blockchain will see that this is the only thing that could have happened. Therefore, everyone will agree that there is a contract, with that ByteCode, at a certain address.

This line:

balances[msg.sender] = _initialAmount;

Says that msg.sender is the only user with any tokens after deployment, so they are the only signer who can do anything important. Everybody else's balance is 0 because nothing is explicitly written to their slots (every possible address) in the mapping.

Msg.sender can

  • put the function, say transfer and the arguments (e.g. recipient and amount, which are uint and address) into the msg.data.
  • address it to the contract
  • sign it
  • send it to the network

When the transaction is mined, the network will process function transfer() with those inputs.

The network itself is concerned with low-level concerns, like:

  • is it signed?
  • Is it properly formed?
  • What came before? (mining is concerned with transaction order)

the contract is concerned with application-level concerns, like:

  • does the sender have enough tokens?

require(balances[msg.sender] >= _value);

  • Accounting:

balances[msg.sender] -= _value; balances[_to] += _value;

Is it possible a client will be interested in that?

emit Transfer(msg.sender, _to, _value);

Is there something to say to other contracts, if msg.sender is another contract?

return true;

If everything is okay, sent tokens will be subtracted from the sender's balance, added to the receiver's balance, an event will be emitted and the function will return true.

Hope it helps.

0
0

The code you pasted is code for creating a smart contract. So that is a program running inside the Ethereum blockchain and users of the blockchain can interact with the smart contract. Everything the contract does happens inside the blockchain.

Now if I just copypaste that code (or any other code to create a smart contract) to some editor it obviously is just a piece of code and nothing more. Then I have to compile it to bytecode and issue a transaction to the blockchain which contains the bytecode data. That transaction is relayed to the blockchain through blockchain clients called nodes.

So the code you provided doesn't include any information about how the contract is actually deployed to the blockchain - there are various tools to do it (for example Truffle).

Your Answer

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

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