Get started with Ethereum smart contract development, and learn to deploy contracts with Remix in five steps.
Original title: “Deploy your first Ethereum smart contract”
Author: Tan Guopeng, founder of Ownbit
The learning of Ethereum smart contracts requires a combination of theory and practice. Recently there have been many messages asking how to learn smart contracts. I think getting started is the most effective way to learn smart contracts.
Preliminary preparation
We will deploy your first smart contract on the Ethereum mainnet, so you need an ETH account, a certain amount of ETH, and an Ethereum wallet that can deploy contracts.
This article uses Ownbit wallet as an example to explain.
Write contract
We write a very simple contract: MyFirst. It is a simple contract account that can receive and send ETH. Beginners can use the Remix tool to write contracts, which has rich compilation and debugging functions.
pragma solidity ^ 0.4.26 ; // This is the Ownbit 5.0 Contract account. // copyright @2020 ownbit.io contract MyFirst { address private owner ; // An event sent when funds are received. event Funded ( uint new_balance ); // An event sent when a spend is triggered to the given address. event Withdrawn ( address to , uint transfer ); constructor () public payable { owner = msg . sender ; } // The fallback function for this contract. function () public payable { emit Funded ( address ( this ). balance ); } modifier onlyOwner () { require ( msg . sender == owner , "Operation only for the owner!" ); _ ; } //get the current owner address function getOwner () public view returns ( address ) { return owner ; } //withdraw ETH to the destination function withdraw ( address destination , uint256 value ) public onlyOwner { //check amount require ( address ( this ). balance >= value && address ( this ). balance > 0 , "balance or widthdraw value invalid" ); //value 0 means withdraw all if ( value == 0 ) { value = address ( this ). balance ; } //transfer will throw if fails destination . transfer ( value ); emit Withdrawn ( destination , value ); } //destroy this contract and release some gas function destroyContract () public onlyOwner { //transfer ether if any if ( address ( this ). balance > 0 ) { owner . transfer ( address ( this ). balance ); } selfdestruct ( owner ); } }
Some explanations:
modifier is the modification of the method, where _; means to execute the method. If it fails to execute _; then the entire method body will not be executed;
view means it is a read-only method and does not modify the state;
require means verification, if verification fails, revert. For details, see Technology|Revert(), Assert() and Require() usage .
Compile the contract
Click “Compile first.sol” in Remix to compile the source code:
After the compilation is complete, click “Compilation Details” to view the binary code (BYTECODE) and ABI (Application Binary Interface) of the contract.
Deployment contract
Send a transaction to the Ethereum mainnet, leave to empty, and fill in the bytecode obtained in the previous step as data to create a contract. Specific steps are as follows:
1. Choose to send ETH transaction in Ownbit, and the receiver fills in: deploy-contract
2. Fill in 0.01 ETH in the amount. We send some ETH to the newly created contract. If you don’t want to send ETH in this step, you can also fill in 0.
3. Paste the bytecode obtained in the previous step in the advanced options, and adjust the Gas Limit to about 1,000,000.
4. Enter the password and the transaction is sent successfully.
5. After the transaction is confirmed, enter the etherscan query to confirm that the contract has been successfully created.
So far, the contract has been successfully deployed to the Ethereum mainnet, and we have obtained the newly created contract account. In the example: 0x164ce3a1d29355e803a0bed9c9e7f8cbbac25139.
Interact with the contract
When we deployed the contract, we transferred 0.01 ETH to the new contract, so the new contract has this amount of balance. Next we transfer the balance to another account. This requires calling the contract method. In our example, MyFirst.sol provides the withdraw method, which allows us to transfer the ETH balance of the contract to any address.
For security reasons, we restrict only the creator of the contract (owner) to call the widthraw method. Therefore, the following steps must still use the address of the deployment contract to operate:
1. Get the function hash of the widthdraw method in Remix. In this example, the hash is: f3fef3a3.
2. Use the owner address to send ETH transactions in Ownbit, and fill in the contract address with the recipient address:
3. Fill in the sending amount with 0, fill in the following HEX string in the advanced options (remove the explanation and blank lines), and fill in the Gas Limit appropriately (such as 500,000):
0xf3fef3a3 --> Method hash 00000000000000000000000097B65aD59C8c96F2dD786751e6279a1A6D34A481 --> Recipient address, with 64 bits 0000000000000000000000000000000000000000000000000000000000000000 --> 0 means sending all
4. After the transaction is confirmed, enter the etherscan query and see the record of the successful call:
5. You can view the details of the transaction sending parameters in etherscan:
The above example of interacting (invoking) with the contract is mainly based on explaining the principle, thus adopting the method of manually encapsulating parameters. You can also use web3 js and ABI in Remix to directly write programs to call contracts without manually encapsulating parameters.
Conclusion
The above explained the detailed process of deploying a contract account and interacting with it. Through practice, with sensory understanding, and adding theoretical knowledge, you can get twice the result with half the effort!
The example contract address in this article.
As for where to learn theories, there will be many on Google. Here are two simple recommendations: