Ethereum builds and truffle compiles and deploys smart contracts

Ethereum builds and compiles and deploys smart contracts

Since the company is going to build an nft project, it tries to build an Ethereum private chain in centos and ubuntu, all of which are successful, and can be compiled and deployed using truffle. The following records the problems encountered and the solutions. Beginners suggest to look at the Ethereum account and contract first
. concept

centos build

  1. Install related dependencies

yum update -y && yum install git wget bzip2 vim gcc-c++ ntp epel-release nodejs tree -y

  1. Download the installation package, you can choose the version you want here https://geth.ethereum.org/downloads/, copy the link to wget

wget -c https://gethstore.blob.core.windows.net/builds/geth-alltools-linux-amd64-1.9.24-cc05b050.tar.gz ./

  1. decompress

tar -zxvf geth-alltools-linux-amd64-1.9.24-cc05b050.tar.gz

  1. Copy the relevant commands directly into the bin

cp ./abigen /usr/local/bin/abigen
cp ./bootnode /usr/local/bin/bootnode
cp ./clef /usr/local/bin/clef
cp ./evm /usr/local/bin/evm
cp ./geth /usr/local/bin/geth
cp ./puppeth /usr/local/bin/puppeth
cp ./rlpdump /usr/local/bin/rlpdump
5.查看版本
geth version

build on ubuntu

add-apt-repository -y ppa:ethereum/ethereum
apt-get update
apt-get install ethereum
geth version

Start Ethereum

  1. Create a genesis file before starting to initialize the genesis block, which is equivalent to the head node of the blockchain

    mkdir private_Eth //Create a folder to put related files
    cd private_Eth //Create a json file in this path
    vim genesie.json //The system comes with vi, vim needs to be downloaded by itself

    The content of the file is as follows:

{
“config”: {
“chainId”: 666,
“homesteadBlock”: 0,
“eip150Block”: 0,
“eip150Hash”: “0x0000000000000000000000000000000000000000000000000000000000000000”,
“eip155Block”: 0,
“eip158Block”: 0,
“byzantiumBlock”: 0,
“constantinopleBlock”: 0,
“petersburgBlock”: 0,
“istanbulBlock”: 0,
“ethash”: {}
},
“nonce”: “0x0”,
“timestamp”: “0x5ddf8f3e”,
“extraData”: “0x0000000000000000000000000000000000000000000000000000000000000000”,
“gasLimit”: “0xffffffff”,
“difficulty”: “0x00002”,
“mixHash”: “0x0000000000000000000000000000000000000000000000000000000000000000”,
“coinbase”: “0x0000000000000000000000000000000000000000”,
“alloc”: {
},
“number”: “0x0”,
“gasUsed”: “0x0”,
“parentHash”: “0x0000000000000000000000000000000000000000000000000000000000000000”
}

  1. Initialize the creation block parameter with the above file – datadir specifies the folder used to store data

mkdir data
geth --datadir data init genesis.json

  1. Start Ethereum, with explanation of some parameters

geth --datadir dataA --nodiscover --networkid 333 --ipcdisable --port 1111 --rpcport 2222 --http --http.corsdomain=“” --http.port 8545 --http.addr 0.0.0.0 --allow-insecure-unlock console

geth --datadir data --networkid 1337 --http --http.corsdomain=“*” --http.port 8545 --http.addr “0.0.0.0” --allow-insecure-unlock --rpc.allow-unprotected-txs --port 30303 --dev --dev.period 1 console 2>>geth.log

Specify the data directory – datadir
allows unlocking the account – allow-insecure-unlock (if this parameter is not added, unlocking the account will fail when executing personal.unlockAccount(), and the contract cannot be deployed) connection
port – http.port 8545
to enter the interactive console Console
specifies the log file >>geth.log
For more parameter explanations, refer to this https://blog.csdn.net/pulong0748/article/details/109027085

Compile and deploy smart contracts with truffle

You can directly refer to the introductory tutorial on the official website of truffle https://trufflesuite.com/docs/truffle/quickstart
Here, it is recommended to install truffle directly with ubuntu. There are too many environmental problems in windows. I use the ubuntu virtual machine. How to install the virtual machine is skipped.

  1. Install npm first

apt install npm
npm -g install npm //upgrade npm

  1. install truffle

npm install -g truffle

  1. Get project templates with truffle

mkdir MetaCoin
cd MetaCoin
truffle unbox metacoin //There is a network problem here, and the project template cannot be obtained directly. Go directly to git to download the zip file and decompress the git link
. There is another way, directly truffle init can generate the simplest project template

The project directory structure after decompression is shown in the figure below, truffle-config.js is empty by default Project directory structure5. Compile

truffle compile

insert image description here

  1. Deployment: If you do not change the truffle-config.js configuration file, it will be deployed to the built-in chain of truffle by default (the first account that comes with truffle is used for deployment by default), enter truffle develop to enter the interactive mode, and then enter migrate to deploy up.
    insert image description here
    If you want to deploy on the private chain of the server (I use centos), you need to change the truffle-config.js configuration file, then create an account in the centos private chain, unlock the account, and let the miners in the chain start mining, otherwise The configuration file will not be successfully deployed , and the ip and port of the server will be added

    insert image description here

centos create an account
insert image description here

view all accountsview account

Unlock the account
insert image description here
and start mining
insert image description here

Use web3j to compile,
deploy, and call the contract. If you want to use web3j to compile and deploy in java, and call the contract, you need to generate the java version of the contract. There are several ways to generate it. Here I choose to use the web3j command line tool to generate it in Ubuntu (the premise is to Equipped with jdk, and truffle)
install JDK

https://blog.csdn.net/qq_37034181/article/details/120673562


The simplest way to install the Web3j CLI is via the following script :

curl -L get.web3j.io | sh && source ~/.web3j/source.sh

web3j generates the java file of the contract.
Before generating it, you need to compile the solidity file with truffles (the operation method is like the truffle compile above), so that you will get the json file of the contract (stored in the build folder)
insert image description here

After that call the web3j command

web3j generate truffle --truffle-json ./MetaCoin.json -o /home/hqz/Desktop -p com.hqz //-o is output, specifying the output path, -p is package specifying the package name

Here is a simple contract file I wrote for testing
insert image description here

insert image description here

After getting the java file of the contract, put it into the java project and call
insert image description here
this method to deploy the contract and call the contract method, but the data seems to be unable to be placed in the transaction record, so directly calling the transfer interface of Ethereum can put the data in the transaction record , because the data is in hexadecimal in Ethereum, so it is necessary to encode the string: convert the string to unicode and then convert it to hexadecimal. The following is the transaction record
Transaction Record

Interconnection of blockchain nodes

admin.nodeInfo.enode //Current node information
admin.addPeer() //Add node command

Encountered a lot of problems, there have been many times when the connection failed, and finally concluded that the genesis files of different nodes need to be the same, and the parameters of the startup command should be paid attention to

{
“config”: {
“chainId”: 666,
“homesteadBlock”: 0,
“eip150Block”: 0,
“eip150Hash”: “0x0000000000000000000000000000000000000000000000000000000000000000”,
“eip155Block”: 0,
“eip158Block”: 0,
“byzantiumBlock”: 0,
“constantinopleBlock”: 0,
“petersburgBlock”: 0,
“istanbulBlock”: 0,
“ethash”: {}
},
“nonce”: “0x0”,
“timestamp”: “0x5ddf8f3e”,
“extraData”: “0x0000000000000000000000000000000000000000000000000000000000000000”,
“gasLimit”: “0xffffffff”,
“difficulty”: “0x00002”,
“mixHash”: “0x0000000000000000000000000000000000000000000000000000000000000000”,
“coinbase”: “0x0000000000000000000000000000000000000000”,
“alloc”: {
},
“number”: “0x0”,
“gasUsed”: “0x0”,
“parentHash”: “0x0000000000000000000000000000000000000000000000000000000000000000”
}

geth --datadir dataB --nodiscover --networkid 333 --ipcdisable --port 3333 --rpcport 4444 --http --http.corsdomain=“” --http.port 8545 --http.addr 0.0.0.0 --allow-insecure-unlock --http.api “eth,net,web3,txpool” console

–rpc.txfeecap 0 --rpc.gascap 0

Supplement the solution that java cannot call the blockchain interface
– http.api “eth, net, web3, txpool, personal”

Guess you like

Origin blog.csdn.net/Heqinze/article/details/124432658