A Preliminary Study on the Method of Ethereum Smart Contract

statement

This article is one of the series of tutorials on interaction between Vue3.0 +Quasar+ ethers.js and Ethereum smart contracts  .

In the previous section: Blockchain browser and contract code – code cool,  we saw the code of a smart contract on the blockchain browser, and today we began to understand these contract codes in the blockchain browser.

Of course, I am not here to teach you how to develop a contract, so we only need to know part of the contract method.

Before studying this section, you need to have:

  1. A metamask wallet
  2. A blockchain wallet account

start

read and write method

We open the USDT token contract: https://etherscan.io/token/0xdac17f958d2ee523a2206206994597c13d831ec7

阅读 合约 The and  in the above figure 编写合约 are actually all the methods in the smart contract. The reason why it is divided into two categories is because they have obvious differences.

Just like GET and POST in our general front-end development, GET is generally used to call the back-end interface to obtain data, and POST, we transfer data through the back-end interface.

The reading method and writing method here are explained as follows:

  1. The read method is used to query data by calling the contract method
  2. The write method is to write data by calling the contract method

The biggest difference between them is:

  1. The read method does not require authorization. You only need to use the web3js library to connect to the contract and call it directly. Calling this method will not consume gas fees (that is, it does not cost money)
  2. The writing method requires authorization, and like the reading method, it needs to be called after connecting to the contract, but calling each step of the method requires pop-up metamask or other wallets to allow the user to authorize, and calling such methods will consume gas fees (it costs money)

example

read method

Here we click on the read method tab and expand the balances method:

This method is used to query the USDT balance of the account. This method requires us to pass in a wallet address. Here we fill in our own wallet address:

Result: 0

You can see intuitively: calling this method only needs to pass in parameters, click the button, and does not require any authorization

write method

We switch to the write contract, and expand the authorization method:

This method is used for authorization. When we are developing, for example, a mall, if we use blockchain payment and it is USDT payment, then we must call this method once before starting payment: it means user authorization How much amount we can use.

Here we need to pass in two parameters:

  1. _spender authorized user
  2. _value authorized amount

It can be seen from here that calling the write method must link the wallet, because all operations of the write method require user confirmation and signature, otherwise the transaction cannot be completed.

We still write our own address and authorized amount:

After clicking the button, if you have installed metamask, you will find a pop-up window and let you authorize.

Notice

This series of tutorials has no interest in any tokens, token institutions, third-party software, plug-ins, etc. mentioned in the tutorials. The examples here are only for technical communication.

This series of tutorials is only for technical exchanges, and operations such as currency speculation and token trading are prohibited.

Guess you like

Origin blog.csdn.net/qq_22502303/article/details/126328947