Write your first contract using Solidity

 Contract code:

// SPDX-License-Identifier: CC-BY-SA-4.0

// Version of Solidity compiler this program was written for
pragma solidity 0.6.4;

contract Faucet {
    //Purpose:receive ETH coin 
    receive() external payable {}


    // Give out ether to anyone who asks
    function withdraw(uint withdraw_amount) public {
        // Limit withdrawal amount  0.1eth => 100000000000000000 wei
        require(withdraw_amount <= 100000000000000000);

        // Send the amount to the address that requested it
        msg.sender.transfer(withdraw_amount);
    }
}

Compile the contract:

  Use RemixIDE to compile the contract into EVM bytecode

Registration contract:

    Registering the contract on the blockchain network requires a special transaction. The target address of this transaction is 0x00000000000000000, also known as the zero address. This is a special address used to tell Ethereum that blockchain users want to pass this To register the contract , RemixIDE will automatically handle these details and send the generated transaction to MetaMask.
    Switch to the DEPLOY&RUN TRANSCATIONS tab, select Injected Web3 for ENVIRONMENT , establish a connection between the MetaMask wallet and RemixIDE, and then connect to the Ropsten test network through MetaMask.

Recharge ETH to the contract address

The process of recharging ETH is too simple and I won’t go into details (see the MetaMask tutorial for details)

Withdraw 0.1eth from the contract address (all currency units in Ethereum are in wei, 0.1ETH = 100000000000000000wei)

Contract address transaction bill:

Contract address: 0x3CfFb89465d0eC6ed9a94D3c3D73a6Bf41f49Bed

Use the wallet address of the test interaction: 0x4ea691c9400dCfA6B2e3eECF22B088a6CF3822C5

The three transactions are explained as follows:

  • 44mins ago Register the contract address to the blockchain test network and you can see that the receiver is Contract Creation. This step is done for us by RemixIDE
  • 41mins ago Send 4 ETH from my wallet address to the contract address (why it shows 3.99986, because eth transactions need to charge fees)
  • 18 mins ago Withdraw 0.1 eth from the contract address to my wallet account, but the value here shows 0, this is because the transfer of 0.1 ETH is initiated by the contract, so this type of transaction is called an internal transaction (also called for message)


 

Summarize:

  1. Use RemixIDE to write simple contracts
  2. Register the contract into the blockchain network
  3. Wallet address -> transfer -> contract address
  4. Operate contract code -> transfer -> wallet address

 

Fantasies about knowledge:

After modifying the contract code, it needs to be redeployed to the blockchain

  • The old contract code corresponds to the old contract address
  • The new contract code corresponds to the new contract address

I removed the withdrawal limit in the new contract code and republished it on the chain. The old contract code was limited to 0.1eth

Later, I transferred 1eth from the wallet to the new contract address for subsequent test extraction

Test to extract 1 eth at one time, 0.1ETH = 100000000000000000wei 1eth=1000000000000000000wei (a 0 is added here, I don’t understand why the author of Ethereum uses such a small unit, it takes half a day to count 0)

OK, it failed. I don’t know why the follow-up test failed to extract 0.2-0.9 amount of ETH. I can only extract 0.1 eth at a time. I have clearly removed the code that limits the amount. up

 

Guess you like

Origin blog.csdn.net/claysystem/article/details/118050834