package com.demo.web3j;
import cn.hutool.json.JSONUtil;
import com.demo.utils.Test3;
import org.bitcoinj.crypto.MnemonicException;
import org.junit.Test;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.response.EthTransaction;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.Contract;
import org.web3j.tx.gas.DefaultGasProvider;
import java.math.BigDecimal;
import java.math.BigInteger;
/**
* 测试数据
*
* @Autor Tricky
* @Date 2021-04-01 22:06:36
*/
public class EthTestData {
// {"address":"0xdb4065e4151933606924171dfafdf4a56cc3807d","privateKey":"6317dac0d278314b0a4c2f026aef25c1321e6956b245e931357c887e26a336a8"}
private String privateKey="0x204b7f58cbaaff80e11aa58895ef1020b0d90c34cfcbbab900e334ab9951e738";
private String myAddress = "0xdb4065e4151933606924171dfafdf4a56cc3807d";
//rinkeby上面的测试币 erc20-usdt同款
private String contract="0xf805ed280cadeadc2aa135808688e06fef5a9b71";
private Web3j web3j ;
{
try{
//如果这个地址不知道怎么获取 可以参考 https://blog.csdn.net/sail331x/article/details/115395131
web3j = Web3j.build(new HttpService("https://mainnet.infura.io/v3/6e2e7a0f108840b989df9acab441543e"));
}catch (Throwable t){
t.printStackTrace();
}
}
public static void main(String[] arg) throws Exception {
String privateKey="0x671da391061849f5dd736caab8d79a671d4b71e909e5c75908e2abd3aa96da12";
Credentials credentials = Credentials.create(privateKey);
Web3j web3j = Web3j.build(new HttpService("http://127.0.0.1:8545"));
// System.out.println("查询ETH:"+EthUtils.balanceOf(web3j,credentials.getAddress()));
System.out.println("查询ERC20:"+EthUtils.balanceOfErc20(web3j,"0x066d8E6D7a0218e1E71f784B88724212Aa3919c9",credentials.getAddress()));
EthTransaction tx = EthUtils.getTransaction(web3j, "0x066d8E6D7a0218e1E71f784B88724212Aa3919c9");
System.out.println("查询交易:"+ JSONUtil.toJsonStr(tx));
//
BigInteger gasPrices = DefaultGasProvider.GAS_PRICE;
BigInteger gasLimit =DefaultGasProvider.GAS_LIMIT;
;
System.out.println(credentials.getAddress());
//注意 这里是发布合约 每次发布都要燃气费 所以java发布合约时记得只发布一次 或者通过remix发布,后面直接调用即可
-----------------------------***--------------------------------------------------
// 部署合约,这里Test_sol_testEth是由web3j生成的java文件的类,需要改成你自己的类名
Test3 deployContract = Test3.deploy(web3j, credentials, gasPrices, gasLimit).send();
// 部署完成后打印合约地址
System.out.println(deployContract.getContractAddress());
// 判断部署的合约是否可用
System.out.println(deployContract.isValid());
-----------------------------***--------------------------------------------------
// 调用合约
Test3 testContract = new Test3(deployContract.getContractAddress(),web3j,
credentials,gasPrices,gasLimit);
Boolean send = testContract.isEqual(BigInteger.valueOf(2), BigInteger.valueOf(2)).send();
System.out.println("-----*****-------");
System.out.println(send);
System.out.println("******----******");
}
/**
* 创建地址
*/
@Test
public void createAddress() throws MnemonicException.MnemonicLengthException {
System.out.println("创建地址:"+JSONUtil.toJsonStr(EthUtils.createAddress()));
}
/**
* 查询eth数量
*/
@Test
public void balanceOf(){
System.out.println("查询ETH:"+EthUtils.balanceOf(web3j,myAddress));
}
/**
* 查询ERC20数量
*/
@Test
public void balanceOfErc20(){
System.out.println("查询ERC20:"+EthUtils.balanceOfErc20(web3j,contract,myAddress));
}
/**
* 发送ERC20
*/
@Test
public void sendErc20(){
String txid = EthUtils.sendErc20(web3j, contract, privateKey, myAddress, BigInteger.valueOf(10000000));
System.out.println("发送ERC20:"+txid);
}
/**
* 发送以太坊
*/
@Test
public void sendEth(){
String txid = EthUtils.sendEth(web3j, privateKey, myAddress, new BigDecimal("0.001"));
System.out.println("发送ETH:"+txid);
}
@Test
public void getTransaction(){
//合约
String txid="0x29d96b351be4ab1c29912a1c26c1c8f9205fc35fb9ea2395c53c5c2e1884c421";
//eth
String txid2="0xef3c06f56085187d6a43edec2bb399a7fe98572aad63bcd5bd80e5e5dab153b3";
EthTransaction tx = EthUtils.getTransaction(web3j, txid2);
System.out.println("查询交易:"+ JSONUtil.toJsonStr(tx));
}
}
package com.demo.web3j;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.ImmutableList;
import lombok.extern.slf4j.Slf4j;
import org.bitcoinj.crypto.*;
import org.bitcoinj.wallet.DeterministicSeed;
import org.web3j.abi.FunctionEncoder;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Address;
import org.web3j.abi.datatypes.Function;
import org.web3j.abi.datatypes.Type;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.crypto.*;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.DefaultBlockParameterNumber;
import org.web3j.protocol.core.methods.response.*;
import org.web3j.utils.Numeric;
import sun.security.provider.SecureRandom;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.*;
/**
* 以太坊工具类
*
* @Autor Tricky
* @Date 2021-04-01 21:02:11
*/
@Slf4j
public class EthUtils {
public static final BigDecimal ETH_DECIMALS = new BigDecimal(1_000_000_000_000_000_000L);
public static final BigInteger ETH_GAS_LIMIT = new BigInteger("100000");
private final static ImmutableList BIP44_ETH_ACCOUNT_ZERO_PATH =
ImmutableList.of(new ChildNumber(44, true), new ChildNumber(60, true),
ChildNumber.ZERO_HARDENED, ChildNumber.ZERO);
/**
* 获取区块数据
*
* @param web3j
* @param block 块高
* @param fullTransactionObjects 是否需要交易数据
* @return
*/
public static EthBlock getBlock(Web3j web3j, long block, boolean fullTransactionObjects) {
try {
return web3j.ethGetBlockByNumber(new DefaultBlockParameterNumber(block), fullTransactionObjects).send();
} catch (Throwable t) {
log.error(String.format("Get Block Error %d", block), t);
}
return null;
}
/**
* 获取当前块高
*
* @param web3j
* @return
*/
public static long getNowBlockNumber(Web3j web3j) {
try {
EthBlockNumber send = web3j.ethBlockNumber().send();
return send.getBlockNumber().longValue();
} catch (Throwable t) {
log.error("GetBlockNumberError", t);
}
return -1;
}
/**
* 发送erc20
*
* @param web3j
* @param contractAddress 合约地址
* @param privateKey 私钥
* @param to 收款地址
* @param value 额度
* @return
*/
public static String sendErc20(Web3j web3j, String contractAddress, String privateKey,
String to, BigInteger value) {
String from = getAddressByPrivateKey(privateKey);
log.info(String.format("Start:SendErc20 from:%s to:%s amount:%s erc20:%s", from, to, value.toString(), contractAddress));
try {
//加载转账所需的凭证,用私钥
Credentials credentials = Credentials.create(privateKey);
//获取nonce,交易笔数
BigInteger nonce = getNonce(web3j, from);
if (nonce == null) {
log.error(String.format("END:GetNonceError from:%s to:%s amount:%s erc20:%s", from, to, value.toString(), contractAddress));
return null;
}
//gasPrice和gasLimit 都可以手动设置
BigInteger gasPrice = getGasPrice(web3j);
if (gasPrice == null) {
log.error(String.format("END:GetGasPriceError from:%s to:%s amount:%s erc20:%s", from, to, value.toString(), contractAddress));
return null;
}
//BigInteger.valueOf(4300000L) 如果交易失败 很可能是手续费的设置问题
BigInteger gasLimit = BigInteger.valueOf(60000L);
//ERC20代币合约方法
Function function = new Function(
"transfer",
Arrays.asList(new Address(to), new Uint256(value)),
Collections.singletonList(new TypeReference() {
}));
//创建RawTransaction交易对象
String encodedFunction = FunctionEncoder.encode(function);
RawTransaction rawTransaction = RawTransaction.createTransaction(nonce, gasPrice, gasLimit,
contractAddress, encodedFunction);
//签名Transaction
byte[] signMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
String hexValue = Numeric.toHexString(signMessage);
//发送交易
EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).sendAsync().get();
String hash = ethSendTransaction.getTransactionHash();
if (hash != null) {
return hash;
}
log.error(String.format("END:HashIsNull from:%s to:%s amount:%s erc20:%s", from, to, value.toString(), contractAddress));
} catch (Throwable t) {
log.error(String.format("发送ERC20失败 from=%s to=%s erc20=%s amount=%s",
from, to, contractAddress, value.toString()), t);
}
return null;
}
/**
* 列出交易信息
*
* @param block 区块高度
* @param filter 过滤器
* @return
*/
public static List getTransactions(Web3j web3j, long block, java.util.function.Function filter) {
EthBlock send = getBlock(web3j, block, true);
if (send == null) {
log.error(String.format("GetBlockDataError:%d", block));
return Collections.emptyList();
}
List transactions = send.getBlock().getTransactions();
if (filter != null) {
List result = new ArrayList<>();
for (EthBlock.TransactionResult e : transactions) {
try {
if (filter.apply(e)) {
result.add(e);
}
} catch (Throwable t) {
log.error(t.getMessage(), t);
}
}
return result;
}
return transactions;
}
/**
* 根据私钥获取地址
*
* @param privateKey
* @return
*/
public static String getAddressByPrivateKey(String privateKey) {
ECKeyPair ecKeyPair = ECKeyPair.create(new BigInteger(privateKey, 16));
return "0x" + Keys.getAddress(ecKeyPair).toLowerCase();
}
/**
* 创建地址
*
* @return
*/
public static EthAddress createAddress() throws MnemonicException.MnemonicLengthException {
SecureRandom secureRandom = new SecureRandom();
byte[] entropy = new byte[DeterministicSeed.DEFAULT_SEED_ENTROPY_BITS / 8];
secureRandom.engineNextBytes(entropy);
//生成12位助记词
List str = MnemonicCode.INSTANCE.toMnemonic(entropy);
//使用助记词生成钱包种子
byte[] seed = MnemonicCode.toSeed(str, "");
DeterministicKey masterPrivateKey = HDKeyDerivation.createMasterPrivateKey(seed);
DeterministicHierarchy deterministicHierarchy = new DeterministicHierarchy(masterPrivateKey);
DeterministicKey deterministicKey = deterministicHierarchy
.deriveChild(BIP44_ETH_ACCOUNT_ZERO_PATH, false, true, new ChildNumber(0));
byte[] bytes = deterministicKey.getPrivKeyBytes();
ECKeyPair keyPair = ECKeyPair.create(bytes);
//通过公钥生成钱包地址
String address = Keys.getAddress(keyPair.getPublicKey());
String privateKey = "0x" + keyPair.getPrivateKey().toString(16);
String publicKey = keyPair.getPublicKey().toString(16);
address = "0x" + address;
return EthAddress.builder().privateKey(privateKey).publicKey(publicKey).address(address).mnemonic(str).build();
}
/**
* 查询地址以太坊数量
*
* @param web3j
* @param address 查询地址
* @return
*/
public static BigDecimal balanceOf(Web3j web3j, String address) {
try {
EthGetBalance balance = web3j.ethGetBalance(address, DefaultBlockParameterName.LATEST).send();
BigInteger amount = balance.getBalance();
if (amount == null || amount.compareTo(BigInteger.ZERO) <= 0) {
return BigDecimal.ZERO;
}
return new BigDecimal(amount).divide(ETH_DECIMALS, 18, RoundingMode.FLOOR);
} catch (Throwable t) {
log.error(String.format("获取以太坊数量出错 %s", address), t);
}
return BigDecimal.ZERO;
}
/**
* 转换成最小单位 Wei
*
* @param ethAmount
* @return
*/
public static BigInteger toWei(BigDecimal ethAmount) {
return ethAmount.multiply(ETH_DECIMALS).toBigInteger();
}
/**
* wei to eth
*
* @param wei
* @return
*/
public static BigDecimal toEth(BigInteger wei) {
return new BigDecimal(wei).divide(ETH_DECIMALS, 18, RoundingMode.FLOOR);
}
/**
* 查询erc20的余额
*
* @param web3j
* @param contract 合约地址
* @param address 查询地址
* @return
*/
public static BigInteger balanceOfErc20(Web3j web3j, String contract, String address) {
try {
final String DATA_PREFIX = "0x70a08231000000000000000000000000";
String value = web3j.ethCall(org.web3j.protocol.core.methods.request.Transaction.createEthCallTransaction(address,
contract, DATA_PREFIX + address.substring(2)), DefaultBlockParameterName.PENDING).send().getValue();
if (StrUtil.isEmptyIfStr(value)) {
return BigInteger.ZERO;
}
return new BigInteger(value.substring(2), 16);
} catch (Throwable t) {
log.error(String.format("查询ERC20失败 contract:%s address:%s", contract, address), t);
}
return BigInteger.ZERO;
}
/**
* 获取gas-price
*
* @param web3j
* @return
*/
public static BigInteger getGasPrice(Web3j web3j) {
try {
EthGasPrice ethGasPrice = web3j.ethGasPrice().sendAsync().get();
if (ethGasPrice == null) {
log.error("GetGasPriceError");
return null;
}
return ethGasPrice.getGasPrice();
} catch (Throwable t) {
log.error(t.getMessage(), t);
}
return null;
}
/**
* 获取nonce
*
* @param web3j
* @param address
* @return
*/
public static BigInteger getNonce(Web3j web3j, String address) {
try {
EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(address, DefaultBlockParameterName.PENDING).send();
if (ethGetTransactionCount == null) {
log.error("GetNonceError:" + address);
return null;
}
return ethGetTransactionCount.getTransactionCount();
} catch (Throwable t) {
log.error("GetNonceError:" + address);
}
return null;
}
/**
* 发送以太坊
*
* @param web3j
* @param privateKey 发送者私钥
* @param to 收款地址
* @param wei wei为单位的数量
* @param gasPrice gas-price
* @param gasLimit gas-limit
* @return
*/
public static String sendEth(Web3j web3j, String privateKey, String to, BigInteger wei, BigInteger gasPrice, BigInteger gasLimit) {
String from = getAddressByPrivateKey(privateKey);
try {
//加载转账所需的凭证,用私钥
Credentials credentials = Credentials.create(privateKey);
//获取nonce,交易笔数
BigInteger nonce = getNonce(web3j, from);
//创建RawTransaction交易对象
RawTransaction rawTransaction = RawTransaction.createEtherTransaction(nonce, gasPrice, gasLimit, to, wei);
//签名Transaction,这里要对交易做签名
byte[] signMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
String hexValue = Numeric.toHexString(signMessage);
//发送交易
EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).sendAsync().get();
return ethSendTransaction.getTransactionHash();
} catch (Throwable t) {
log.error(String.format("发送ETH失败 from:%s to:%s amount-eth:%s", from, to, toEth(wei).toString()));
}
return null;
}
/**
* 发送以太坊
*
* @param web3j
* @param privateKey 发送者私钥
* @param to 收款地址
* @param wei wei为单位的数量
* @return
*/
public static String sendEth(Web3j web3j, String privateKey, String to, BigInteger wei) {
return sendEth(web3j, privateKey, to, wei, getGasPrice(web3j), ETH_GAS_LIMIT);
}
/**
* 发送以太坊
*
* @param web3j
* @param privateKey 发送者私钥
* @param to 收款地址
* @param eth wei为单位的数量
* @param gasPrice gas-price
* @param gasLimit gas-limit
* @return
*/
public static String sendEth(Web3j web3j, String privateKey, String to, BigDecimal eth, BigInteger gasPrice, BigInteger gasLimit) {
return sendEth(web3j, privateKey, to, toWei(eth), gasPrice, gasLimit);
}
/**
* 发送以太坊
*
* @param web3j
* @param privateKey 发送者私钥
* @param to 收款地址
* @param eth wei为单位的数量
* @return
*/
public static String sendEth(Web3j web3j, String privateKey, String to, BigDecimal eth) {
return sendEth(web3j, privateKey, to, toWei(eth), getGasPrice(web3j), ETH_GAS_LIMIT);
}
/**
* 根据hash获取交易信息
*
* @param web3j
* @param hash
* @return
*/
public static EthTransaction getTransaction(Web3j web3j, String hash) {
try {
EthTransaction tx = web3j.ethGetTransactionByHash(hash).send();
return tx;
} catch (Throwable t) {
log.error("GetTransactionError:" + hash, t);
}
return null;
}
}
package com.demo.web3j;
import lombok.Builder;
import lombok.Data;
import java.util.List;
@Builder
@Data
public class EthAddress {
private String address;
private String privateKey;
private String publicKey;
private List mnemonic;
}