What you do: Transfer money from a virtual account on Remix to a smart contract account you wrote

Prerequisite Basics: Some basic understanding of Solidity and some understanding of ethereum’s account mechanisms.


account

The unique identifier of an account in Ethereum is an address.

In solidity this is the address type. But there are two types of address: Address and Address payable. The most common member variable for the address type is balance, which is the current account balance. See the documentation for specific member variables

Address Payable is the same as Address but also has member transfer() and send() so that it can receive transfers to the account.

The receive () function

Receive () external payable {… }

The receive function is executed when there is no additional data called to the contract (usually a transfer to the contract). For example, with.send() or.transfer(), if the receive function does not exist but fallback function is available for payable for the pure Ether transfer, fallback function will be called.

If neither function is available, the contract cannot receive Ethernet through a regular transfer transaction (throwing an exception).

Fallback () function

Fallback () {fallback() external [payable]{… }, this function must not take arguments or return values. (Advanced version can be, documentation)

Function: When calling a function that does not exist in the contract, or when calling an empty method, or when using the built-in transfoer() and send() functions at the contract address, fallback() of the target contract is executed without the receive() function.


So in our case of transferring to a contract account, we must include the fallback() function, otherwise the run will fail. If written properly, you need to include the receive() and fallback() functions


Code:

// spdx-license-identifier: gpl-3.0 pragma solidity ^0.7.0; Contract transferTest {function transderToContract() payable public { payable(address(this)).transfer(msg.value); Function uint256 public view returns (uint256) {return address(this).balance; } fallback() external payable {} receive() external payable {}} // Use compiler version 0.7.5Copy the code

The code is relatively simple, but a few points need to be noted:

  • Get account contract address use:address(this)
  • addressaddress payableIs an explicit conversion:payable(address)
  • address payableaddressIs an implicit conversion.
  • msg.valueIs passed in by the caller

Operation:

  1. Click Compile to compile the contract code

  2. Click DEPLOY&RUN TRANSACTIONS, and click Deploy to Deploy

  3. A Deployed contract should appear under Deployed Contracts and we can call the function by clicking on transderToContract().

    Let’s say we need to transfer 10 Ether coins to a contract address

    In the picture, we transfer 10 Ether coins to a contract user. In the lower right console, we can see the hash code of the transaction, and we can also debug to see the operation details.

  4. Click getBalanceOfContract() to see the balance in the current contract user.

    This is in wei, so there’s a whole bunch of zeros.


So that’s the whole demo, if there’s anything wrong, you’re welcome to discuss it.