工具

Geth
web3j
remix

使用solidity编写智能合约

pragma solidity 0.4.25;

contract KYC { 
 
 //自定义的结构类型
 struct customer {
  string name; //姓名
  uint8  age;  //年龄
 }
 
 //映射EOA与数据 
 mapping(uint => customer) private customers;
 
 //将信息记录在日志中  
 event InsertEvn(address indexed _from, uint id, string name);
 
 //添加客户
 function doInsert(uint id, string name, uint8 age) public {  
  customers[id].name = name;  
  customers[id].age = age;
  emit InsertEvn(msg.sender, id, name);
 }
 
 function queryName(uint id) public view returns (string) {
  return customers[id].name;
 }

 function queryAge(uint id) public view returns (uint8) {
  return customers[id].age;
 } 
}

编译
复制其中的ABI和字节码文本后,存储器为YourContract.abi,YourContract.bin文件

使用web3j开发包生成智能合约的封装对象

下载地址:百度网盘 提取码:2y30

将工作目录切换到web3j-3.5.0目录下,同时将这两个文件复制到当前的工作路径再执行下列指令:

java -cp .;.\lib\* org.web3j.codegen.SolidityFunctionWrapperGenerator ./YourContract.bin ./YourContract.abi -o ./java -p com.alc

运行结束后:将会生成合约java文件:./java/com/alc/YourContract.java

部署智能合约

方法一:

使用loadScript(“YourContract.js”)命令部署,运行成功后会出现
在这里插入图片描述

方法二:

package sec02;

import java.math.BigInteger;

import org.web3j.crypto.Credentials;
import org.web3j.crypto.WalletUtils;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.http.HttpService;

import com.KYC;

public class deloyCon {

    public static void main(String[] args) {
        try {
            // 连接区块链节点
            String blockchainNode = "http://127.0.0.1:8545/";
            Web3j web3 = Web3j.build(new HttpService(blockchainNode));

            // 指定密钥文件并验证账户和密码
            String coinBaseFile = "F:\\MyGeth\\node01\\keystore\\UTC--2021-09-26T08-40-23.249859700Z--1efe2ce884c1837bee9423878f56d9c95d486167";
            String myPWD = "123456";
            Credentials credentials = WalletUtils.loadCredentials(myPWD, coinBaseFile);

            // 通过合约封装对象进行部署
            long startTime = System.currentTimeMillis();
            KYC contract = KYC
                    .deploy(web3, credentials, KYC.GAS_PRICE, KYC.GAS_LIMIT).send();
            long endTime = System.currentTimeMillis();

            // 获取合约地址
            String addr = contract.getContractAddress();
            System.out.println("合约部署地址:" + addr + ",花费:" + (endTime - startTime) + " ms");

            //KYC contract2 = KYC.load(addr, web3, credentials, KYC.GAS_PRICE, KYC.GAS_LIMIT);
            System.out.println("合约有效性:" + contract.isValid());

        } catch (Exception e) {
            System.out.println("交易错误:" + e);
        }

        // System.exit(0);
    }
}

调用智能合约

package sec02;

import java.math.BigInteger;
import java.util.List;

import org.web3j.crypto.Credentials;
import org.web3j.crypto.WalletUtils;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.response.Log;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.protocol.http.HttpService;

import com.KYC;

public class KYCInsertExample {
    public static void main(String[] args) {
        try {
            // 连接区块链节点
            String blockchainNode = "http://127.0.0.1:8545/";
            Web3j web3 = Web3j.build(new HttpService(blockchainNode));

            // 指定密钥文件并进行账户密码验证
            String coinBaseFile = "F:\\MyGeth\\node02\\keystore\\UTC--2021-09-26T13-57-17.921141400Z--992523c0411a4118564a8bfb29b1f8f0af9d849c";
            String myPWD = "123456";
            Credentials credentials = WalletUtils.loadCredentials(myPWD, coinBaseFile);

            // 获取合约封装对象
            String contractAddr = "0x49383001c9bbd3f06c1b2a977705492dd8e96ae5";
            KYC contract = KYC.load(contractAddr, web3, credentials, KYC.GAS_PRICE, KYC.GAS_LIMIT);

            // 合约函数的参数设置
            BigInteger id = new BigInteger("" + 1024);
            String name = "bit";
            BigInteger age = new BigInteger("" + 16);

            // 调用合约函数,并获取交易序号
            long startTime = System.currentTimeMillis();
            TransactionReceipt recp = contract.doInsert(id, name, age).send();

            long endTime = System.currentTimeMillis();
            System.out.println("运行时间:" + (endTime - startTime) + " ms");

            String txnHash = recp.getTransactionHash();

            System.out.println("txnHash:" + txnHash);
            System.out.println("blockNum:" + recp.getBlockNumber());
            List<Log> list = recp.getLogs();
            if (list != null && list.size() > 0) {
                for (Log log : list) {
                    System.out.println("log data:" + log.getData());
                }
            }

            // 查询数据
            String rtnName = contract.queryName(id).send();
            System.out.println("name:" + rtnName);

            BigInteger rtnAge = contract.queryAge(id).send();
            System.out.println("age:" + rtnAge);

        } catch (Exception e) {
            System.out.println("交易错误:" + e);
        }
    }
}

运行结果:

运行时间:15201 ms
txnHash:0x96088bb2bb0d577f028f6b1d42bcf8cdea3c6d73577f63cc96a3be288c8c6a5b
blockNum:3317
log data:0x0000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000036269740000000000000000000000000000000000000000000000000000000000
name:bit
age:16
Logo

为所有Web3兴趣爱好者提供学习成长、分享交流、生态实践、资源工具等服务,作为Anome Land原住民可不断优先享受各种福利,共同打造全球最大的Web3 UGC游戏平台。

更多推荐