English
 找回密码
 立即注册

Ethereum’s first transfer

Vitalik 2025-9-18 12:42 8845人围观 ETH

is probably a huge reflex arc. The concept of blockchain has been popular for so long, and I have only recently begun to learn blockchain-related technologies. After learning the basic concepts, my friend recommended the first chain in the universe: Ethe
It is probably a huge reflex arc. The concept of blockchain has been popular for so long, and I have only recently started to learn blockchain-related technologies. I finished learning the basic concepts and was recommended by a friend. 宇宙第一链Ethereum. After studying over the weekend, I finally completed the first goal: the first transfer.

Share the learning process and results for reference only. The knowledge involved in the article comes from official documents, and the address can be searched by yourself. The documentation is available in Chinese, but the code practice part is mostly in English. It is recommended that beginners also read the conceptual documents and do not start coding directly.

test chain


First, we need to build a test chain locally, just like for HTTP interface testing, we need a local test server. Here’s an official reminder from this post:

您应该先理解以太坊堆栈和以太坊网络基础知识才能进入开发网络。

Among the official development tools, I choseGanache, nothing else, because it ranks first. Test chains can be deployed and smart contracts can be deployed, which fully meets my needs.

Install the same software as other software, and then start the interface:


Installation page

For newbies, chooseQUICKSTARTThat's it, you can see the main page. I won’t take up space by putting pictures here. After successful startup, you will get the IP and port of a local service. This is what is in the code. hostvalue. Query on how to obtain the mainnet hostI suggest that beginners skip this part first. There is no need to build one locally.Ethereumclient.

Create Java project


Since I am used to using Java, I will start with Java. Of course, I will try to use Groovy to implement it later when I become proficient. Later, I plan to write it again in Golang, so I won’t go into details here.

I used Java dependencyWeb3j, as far as I have seen there is a lot of informationjsonrpcThe implementation is essentially an HTTP request. I was thinking of using it FunTesterFramework implementation. But the current scenario does not allow me, a blockchain novice, to do this.

Dependency configuration:
        <dependency>
            <groupId>org.web3j</groupId>
            <artifactId>core</artifactId>
            <version>4.3.0</version>
        </dependency>

Then you need a class andmainmethod, nothing else.

Verify Demo


When the preparations are ready, we write some code to callEthereumAPI to verify whether the local test chain is valid.
import com.funtester.frame.SourceCode; 
import org.web3j.protocol.Web3j; 
import org.web3j.protocol.core.methods.response.EthProtocolVersion; 
import org.web3j.protocol.http.HttpService; 
 
import java.io.IOException; 
 
public class Demo extends SourceCode { 
 
    static Web3j drive = Web3j.build(new HttpService("http://127.0.0.1:7545")); 
 
    public static void main(String[] args) throws IOException { 
        EthProtocolVersion send = drive.ethProtocolVersion().send(); 
        output(parse(send)); 
    } 
 
}

When the console outputs the format information, it means that the environment setup work has been completed.
~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~
♨ ♨ {
♨ ♨ ① ➤ "result":"0x3f",
♨ ♨ ① ➤ "protocolVersion":"0x3f",
♨ ♨ ① ➤ "id":0,
♨ ♨ ① ➤ "jsonrpc":"2.0"
♨ ♨ }
~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~

transfer


EthereumThere are many APIs,Web3jThere are corresponding implementation methods. Although the documentation and test chain are not complete, as far as I am currently using it, I have learned enough and have not encountered any missing functions.

Account transfer is a very important API, and it is more troublesome than other query APIs because it involves signature encryption. PS: There is documentation GanacheAllows transfers without signatures. I have not tried this. After all, I will be surfing the mainnet in the future.

The following is a demo of the transfer. The keys are all test accounts on the test chain. You can passGanacheObtained from the small key button on the page.
package com.funtester.ethereum;

import com.funtester.frame.SourceCode;
import org.web3j.crypto.Credentials;
import org.web3j.crypto.RawTransaction;
import org.web3j.crypto.TransactionEncoder;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.response.*;
import org.web3j.protocol.http.HttpService;
import org.web3j.utils.Convert;
import org.web3j.utils.Numeric;

import java.io.IOException;
import java.math.BigInteger;
import java.util.Optional;

public class Demo extends SourceCode {

    static Web3j drive = Web3j.build(new HttpService("http://127.0.0.1:7545"));

    public static void main(String[] args) throws IOException, InterruptedException {
        String privateKey = "0x04c02dc2381e1f69a274fa7cc4851c9eb01358fae8753dd4273014b43e34ab0c";
        Credentials credentials = Credentials.create(privateKey);
        EthGetTransactionCount ethGetTransactionCount = drive.ethGetTransactionCount(credentials.getAddress(), DefaultBlockParameterName.LATEST).send();
        BigInteger nonce = ethGetTransactionCount.getTransactionCount();
        String recipientAddress = "0x9483df5Bb8183548B4D7eb66B44C41FccD7c40A6";
        BigInteger value = Convert.toWei("1", Convert.Unit.ETHER).toBigInteger();
        BigInteger gasLimit = BigInteger.valueOf(22100);
        BigInteger gasPrice = Convert.toWei("11", Convert.Unit.GWEI).toBigInteger();
        RawTransaction rawTransaction = RawTransaction.createEtherTransaction(
                nonce,
                gasPrice,
                gasLimit,
                recipientAddress,
                value);
        byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
        String hexValue = Numeric.toHexString(signedMessage);
        EthSendTransaction ethSendTransaction = drive.ethSendRawTransaction(hexValue).send();
        String transactionHash = ethSendTransaction.getTransactionHash();
        Optional<TransactionReceipt> transactionReceipt = null;
        do {
            EthGetTransactionReceipt ethGetTransactionReceiptResp = drive.ethGetTransactionReceipt(transactionHash).send();
            transactionReceipt = ethGetTransactionReceiptResp.getTransactionReceipt();
            Thread.sleep(3000);
        } while (!transactionReceipt.isPresent());
        System.out.println("deal confirm");
    }

}


The subsequent loop waiting for the response is not elegant enough. Once you become proficient, you can encapsulate a convenient method.

console outputdeal confirmIf so, it means the transfer has been confirmed. can go GanacheMake sure the balance is correct. Of course you can pass EthereumAPI to query whether the changes in the balance of the corresponding account are consistent with expectations.


A young person’s first Ethereum transfer


A young person’s first Ethereum transfer

A young man's firstEthereumThe transfer has been completed.
FunTester original topic recommendation~
  • 900 original collections
  • 2021 original collection
  • 2022 original collection
  • Special topic on interface function testing
  • Performance testing topics
  • Groovy Topics
  • Java、Groovy、Go、Python
  • Single test & white box
  • FunTester community style
  • Testing Theory Chicken Soup
  • FunTester video feature
  • Case sharing: solutions, bugs, crawlers
  • UI Automation Topic
  • Special topic on testing tools
-- By FunTester



精彩评论0
我有话说......
TA还没有介绍自己。