The first smart contract program Faucet.sol

Code first

pragma solidity 0.6.4;

contract Faucet {
    
    
    // Accept any incoming amount
    receive() external payable {
    
    }

    // 这个函数的名称是withdraw,它接收一个名为withdraw_amount、类型为无符号整型(uint)的参数
    function withdraw(uint withdraw_amount) public {
    
    
        //设定提币的上限,它使用内置的Solidity函数require来测试一个前提条件,
        //即withdraw_amount小于或等于100000000000000000 wei
        //如果使用大于该数量的withdraw_amount调用withdraw函数,
        //则此处的require函数将导致合约执行停止并因异常而失败
        require(withdraw_amount <= 100000000000000000);

        //msg对象是一个所有合约都可以访问的输入,它代表触发这个合约执行的交易。
        //sender属性就是发起这个交易的发起方地址。
        //transfer函数是一个内置的函数,用来把以太币从合约转账到调用这个合约的交易地址
        //将以太币转给触发这个合约执行的交易消息的发送地址。
        //transfer函数只接收一个数额参数。
        //把之前定义的withdraw_amount这个变量的值作为参数传递给withdraw函数。
        msg.sender.transfer(withdraw_amount);
    }
}

Compile Faucet contract

We have completed the first contract, and now we need to use the Solidity compiler to convert the Solidity code into EVM bytecode so that it can be executed by the EVM on the blockchain.
Solidity's compiler is an independent executable program, usually included in various programming frameworks, and also integrated in some IDEs. For the sake of simplicity, we use Remix, a popular IDE.
1. Open the Chrome browser with the MetaMask plug-in installed and visit the Chinese version of Remix IDE at http://remix.app.hubwiz.com/
2. We add a new file by clicking the plus button on the left side of the toolbar. Name the new file Faucet.sol.
Insert image description here
3. Copy the code and compile the program; note that the compiler version must be consistent with the one declared in the code.
Insert image description here
4. If the compilation is successful, a Faucet contract name will appear.
Insert image description here

Deploy Faucet contract

We've got a contract and compiled it into bytecode. Now you need to "register" this contract to the Ethereum blockchain. We still use the Ropsten test network to test the contract, so Ropsten is the target where we need to register the contract.
1. First, switch to the "Run" tab, and in the "Environment" drop-down menu, select "Injected Web3". This will establish a connection between the Remix IDE and the MetaMask wallet, and in turn connect to the Ropsten test network through MetaMask. After completing this setting, you will see the word Ropsten in the "Environment" column. Again, with your MetaMask wallet address appearing in the account selection input box
Insert image description here
, Remix IDE will build a special "creation" transaction, and MetaMask will ask for your approval. As you can see in MetaMask, the transaction created by this contract does not contain ether, it is only 258 bytes (compiled contract), and the gas for this transaction requires 10 gwei. Click "SUBMIT" to approve the transaction (register the contract on the blockchain).
3. Please note that the Faucet contract now has an address of its own, which is shown in Remix as "Faucet at 0x72e...c7829" (due to different addresses, these random letters and numbers will be different). The little clipboard icon on the right allows you to copy the contract address to the clipboard. We will use this contract address later. Now that
Insert image description here
we have a contract recorded on the blockchain, we can view it by its address. We can see it on the block explorer https://ropsten.etherscan.io .
Click the clipboard icon to copy the contract address, and copy the contract address in the search box. You'll see the contract's history
Insert image description here
. So far, this contract has only one transaction history: its own contract creation transaction. As you can see, this contract also doesn’t have any ether (the balance is zero). This is because we did not include any ether in the transaction that created the contract.

Top up the contract

We can send some ether to this contract. You should still have the contract address in your clipboard (if not, copy it again from Remix). Opening MetaMask
is the same as other transfer operations, sending 1 ether to the contract address.
Soon, if you refresh the Etherscan block explorer, you will see a new transaction under the contract address, and the account balance is updated to 1 ether.

When you send a transaction to the contract address, if you do not specify which function of the contract to call, it will call the default function. Because we added the payable attribute when declaring the function, this function can receive ether and deposit the coins into the contract balance. This transaction triggers the contract to be executed on the EVM and updates the balance. In this way we have completed the recharge!

Withdraw coins from the contract

Next, we try to withdraw ether from the contract. In order to withdraw coins, we need to construct a transaction that calls the withdraw function and pass withdraw_amount as a parameter. To keep it simple, we can use Remix to build this transaction, then use MetaMask to send it and get our approval.
Return to the Remix interface and find the contract on the Run tab. Click to open the deployed contract, labeled withdraw with field input tag uint256 withdraw_amount.
Insert image description here

This is the interface of the contract in Remix. It allows us to construct a transaction that calls functions defined in the contract. We enter a withdraw_amount value and click the "withdraw" button to generate the transaction.
1. First, let’s take a look at the withdraw_amount parameter. We try to withdraw 0.1 ETH from the contract, which is the maximum withdrawal allowed by the contract. Remember that all currency units within Ethereum should be represented by wei, right? Therefore, the withdraw_amount parameter must also use the wei format. The 0.1 ETH here corresponds to 100000000000000000 wei.
2. Click "withdraw" in Remix to create a currency withdrawal transaction. MetaMask will pop up a transaction window requesting your confirmation. Click "SUBMIT" to make a withdrawal call to the contract. 3. Etherscan shows that the transaction calls the withdraw function of the contract
.
We can see that this transaction uses the contract as the address and does not contain any ether. After spending 0.1 ETH, the balance of the contract has become 0.9 ETH. But strangely, we don’t see an “outbound payment” transaction displayed in the contract address history.
Insert image description here
4. Where is this external payment transaction? A new area will appear on the contract address history page called "Internal Txns". Because the transfer of 0.1 ETH is initiated by the contract, this type of transaction is called an internal transaction (also called a message). Click on the "Internal Txns" tab to see internal transactions
Insert image description here

Summarize

We used Solidity to write a Faucet contract and used Remix IDE to compile the contract into EVM bytecode. We use Remix to build a transaction, and then register the contract to the Ropsten test blockchain network. After the registration is completed, this contract will have an Ethereum address, and we can send Ether coins to this address. Finally, we constructed a call to the withdraw function and successfully extracted 0.1 ETH. After checking our request, the contract used an internal transaction to send 0.1 ETH back to our calling address.
This seems quite simple. What we have just done is to use software to successfully implement digital currency transfer operations on a decentralized world computer.

Guess you like

Origin blog.csdn.net/qq_42042158/article/details/125725788