Smart contracts are first compiled and converted into bytecode when the contract is deployed. This bytecode is then stored on the blockchain, and an address is assigned to it. The contract address is determined based on the person's address creating the contract (sender) and the number of transactions the creator has sent (nonce). The sender address and nonce are
RLP encoded and hashed with the
keccak-256 algorithm.
Smart contracts, which are overwhelmingly written in Solidity, are primarily deployed and tested using
Ethereum Remix IDE; Remix IDE is an open-source browser-based IDE for Ethereum smart contracts. Let’s try to deploy a contract using Remix IDE; we’ll deploy our contract on the Ropsten Testnet. For that, we’ll need some test ETH. You will need the
MetaMask browser extension to create an ETH wallet, and some test ETH to get started. You can get test ETH by going to the
Ropsten faucet. You'll need to select the
Ropsten Test Network on your MetaMask wallet and copy-paste the wallet address into the text field in the faucet. Then click “Send me test Ether”.
Head over to the Ethereum Remix IDE and make a new Solidity file-- for example, `contract.sol`.
Paste the following code into your new Solidity script:
how are smart contracts deployed
// SPDX-License-Identifier: MIT
pragma solidity 0.8.1;
contract test {
uint256 private count = 0;
function increment() public {
count += 1;
}
function getCount() public view returns (uint256) {
return count;
}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.1;
contract test {
uint256 private count = 0;
function increment() public {
count += 1;
}
function getCount() public view returns (uint256) {
return count;
}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.1;
contract test {
uint256 private count = 0;
function increment() public {
count += 1;
}
function getCount() public view returns (uint256) {
return count;
}
}
Explanation of the code above.
Line 1: Specifying
SPDX license type, which is an addition after Solidity ^0.6.8.
Whenever the source code of a smart contract is made available to the public, these licenses can help resolve/avoid copyright issues. If you do not wish to specify any license type, you can use a special license UNLICENSED or simply skip the whole comment (it won’t result in an error, just a warning).
Line 2: Declaring the Solidity version.
Line 4: Starting our contract named test.
Line 6: Declaring a private variable count of unsigned type integer and assigning it zero value.
Line 8-10: Declaring a function increment() of type public which means it can be accessed outside the function and by other contracts, incrementing the value of count by 1.
Line 12-14: Declaring a function getCount() of type public and view as it’s immutability value, which means that it can only read blockchain status, and not write anything. Using the returns keyword to specify that the function returns a value, in the form of an unsigned integer here. Returning the value of count to the function.
Compile the smart contract by first clicking on the second icon (Solidity icon) on the left menu, and then clicking on “Compile contract.sol”.
A green tick will be added to the compiler tab on the left menu on a successful compilation of the contract. Now to deploy the contract, click on the third icon from the left menu, and select injected Web3 from the dropdown under “Environment,” and then click on “Deploy”(make sure to select Ropsten testnet on MetaMask before Deploying the contract). Approve the transaction from MetaMask.
Now that your contract is deployed, you can locate it under the "Deployed Contracts" section.
Smart contracts can also be deployed using
truffle or other Ethereum libraries.