Platus
Platus

Reputation: 1497

What is the meaning of deploying a Smart Contract?

I am working on an Ethereum solidity blockchain and I deploy smart contracts using truffle.js, I am a bit confused about how smart contracts work.

Is deploying a smart contract equivalent to instantiating it (I mean calling its constructor)?

Therefore each time I need to create a new instance of a smart contract, I actually need to deploy it?

The address resulting from the deployment is a pointer to an instance of a smart contract and not to the smart contract itself?

Upvotes: 0

Views: 1190

Answers (4)

Mehran Ra
Mehran Ra

Reputation: 101

Yes, when you deploy a smart contract, you are instantiating it. This means that you are creating a new instance of the contract on the Ethereum network, with its own unique address.

Each time you want to create a new instance of the same smart contract, you will need to deploy a new contract to the network. The deployment process involves sending a transaction to the network that contains the smart contract code, and it results in a new contract being created on the Ethereum network. The address resulting from the deployment is indeed a pointer to this instance of the smart contract, and not to the smart contract code itself.

Note that if you deploy a smart contract with the same bytecode (the compiled code of the contract), it will have the same behavior and logic, but each deployment will still result in a new and separate instance of the contract with its own address.

Upvotes: 0

Yilmaz
Yilmaz

Reputation: 49182

Deploying a smart contract means sending a transaction with a blank to field. When Ethereum sees a transaction with a blank to field, it will create a new contract. If you send token to someone, "to" field will be the address of the receiver, but for the contract creation transaction to is empty. There is also data field in transaction object which is empty if you just send token. However, you send contract creation transaction, data will include the init code to set up the contract and deploy the contract

this is an example in Ganache

enter image description here

that txdata representation of your contract. If you paste this bytecode on this decompiler it will create solidity code close to your contract code. (variable names and function names are removed)

The bytecode that sent has two parts. first part is init code and second part is your smart contract's code. Init code does not get stored in blockchain, it is a starter code to initialize, calling the constructor so that initial state gets set.

EVM is stack based machine. It has opcodes instructions (operaiton codes) to execute code and those opcodes reads the arguments from the stack. Stack has maximum depth of 1024 slots. Every opcode has its hexademical representation. For example, If you look at the image above, 6080604052 is the same for all contracts. If you decompile this

  0x60806040526040518060400160405280600e81526020017f446563656e7472616c2042616e6b000000000000000000000000000000000000815250600090805190602001906200005192919062000150565b503480156200005f57600080fd5b50604051620014cb380380620014cb8339818101604052810190620000859190620002c3565b81600360006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555080600260006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555033600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050506200036f565b8280546200015e9062000339565b90600052602060002090601f016020900481019282620001825760008555620001ce565b82601f106200019d57805160ff1916838001178555620001ce565b82800160010185558215620001ce579182015b82811115620001cd578251825591602001919060010190620001b0565b5b509050620001dd9190620001e1565b5090565b5b80821115620001fc576000816000905550600101620001e2565b5090565b600080fd5b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000620 

I copied till opcode 23 which is Invalid opcode represents that unreachable code. you will get that

contract Contract {
    function main() {
        memory[0x40:0x60] = 0x80;
        var temp0 = memory[0x40:0x60];
        memory[0x40:0x60] = temp0 + 0x40;
        memory[temp0:temp0 + 0x20] = 0x0e;
        memory[temp0 + 0x20:temp0 + 0x20 + 0x20] = 0x446563656e7472616c2042616e6b000000000000000000000000000000000000;
        var var0 = 0x000051;
        var var1 = 0x00;
        var var2 = temp0 + 0x20;
        var var3 = memory[temp0:temp0 + 0x20];
        var var4 = var1;
        var var5 = 0x00015e;
        var var6 = storage[var4];
        // Unhandled termination
    }
}

this code sets state variables for the contract. After init code is finished, it returns a pointer to the contract code and then EVM copies the contract bytecode with CODECOPY opcode. this code is called runtime bytecode and stored on the blockchain.

Later on when you interact with the contract, evm will change state of the contract based on instructions store on the runtime bytecode

Upvotes: 0

Daniel Pernia
Daniel Pernia

Reputation: 31

When you deploy a smart contract, you convert your Solidity code to binary code and copy that code to a network, it can be a local network, a testnet or a mainnet.

Every time you display your code create a new address.

You can deploy 10 copies of your code and generate 10 different independent addresses from the same code.

Upvotes: 0

C S
C S

Reputation: 1525

Everything happens on the Ethereum blockchain by sending a message from one account to another account. Each account is identified by an address. Smart contracts are precisely the accounts that store executable bytecode.

So in order to use your smart contract, you need to "upload" your compiled bytecode to the blockchain and store it at an address. This is deployment.

Thinking of class instances and such is probably not a useful way of thinking about this process. The Ethereum Virtual Machine (EVM) doesn't actually know about classes. That's a construct of the Solidity language that is used to simplify the creation of smart contract bytecode.

Upvotes: 0

Related Questions