部署
Maven 引用
<dependency>
<groupId>com.taiyuechain</groupId>
<artifactId>taiWeb3j</artifactId>
<version>4.5.5.8</version>
</dependency>
Jar包本地部署
下載最新的Jar包, 在工程主目錄建立src
同級 lib
目錄並將taiWeb3jxxx.jar
拷貝進去,並在 pom.xml
中增加引用
<dependency>
<groupId>taiWeb3j</groupId>
<artifactId>com.taiweb3j</artifactId>
<version>4.5.5.8</version>
<scope>system</scope>
<systemPath>${basedir}/lib/taiWeb3j-4.5.5.8.jar</systemPath>
</dependency>
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>4.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
使用實例
鏈相關
獲取餘額
public BigInteger getBalance(String address) {
BigInteger balance = BigInteger.ZERO;
try {
EthGetBalance ethGetBalance = web3j.ethGetBalance(address, DefaultBlockParameterName.LATEST).send();
if (ethGetBalance != null) {
balance = ethGetBalance.getBalance();
}
} catch (Exception e) {
e.printStackTrace();
}
return balance;
}
獲取交易序號
public BigInteger getTransactionNonce(String address) {
BigInteger nonce = BigInteger.ZERO;
try {
EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(address, DefaultBlockParameterName.PENDING).send();
nonce = ethGetTransactionCount.getTransactionCount();
} catch (IOException e) {
e.printStackTrace();
}
return nonce;
}
獲取交易燃料費
public static BigInteger getGasPrice() {
BigInteger gasPrice = null;
try {
EthGasPrice ethGasPrice = web3j.ethGasPrice().send();
gasPrice = ethGasPrice.getGasPrice();
} catch (Exception e) {
e.printStackTrace();
}
return gasPrice;
}
獲取快鏈最新高度
public BigInteger getCurrentFastNumber() {
BigInteger fastNumber = null;
try {
TaiFastBlockNumber taiFastBlockNumber = new Request<>(
Constant.CURRENT_BLOCK_NUMBER,
Arrays.asList(),
web3jService,
TaiFastBlockNumber.class).send();
fastNumber = taiFastBlockNumber.getBlockNumber();
} catch (Exception e) {
e.printStackTrace();
}
return fastNumber;
}
根據高度獲取塊信息(full 擴展信息)
public FastBlock getFastBlockByNumber(BigInteger fastBlockNumber, boolean full) {
FastBlock fastBlock = null;
try {
TaiFastBlock taiFastBlock = new Request<>(
Constant.BLOCK_BYNUMBER,
Arrays.asList(DefaultBlockParameter.valueOf(fastBlockNumber).getValue(), returnFullTransactionObjects),
web3jService,
TaiFastBlock.class).send();
fastBlock = taiFastBlock.getFastBlock();
} catch (Exception e) {
e.printStackTrace();
}
return fastBlock;
}
根據HASH獲取塊信息(full 擴展信息)
public FastBlock getFastBlockByHash(String fastHash, boolean full) {
FastBlock fastBlock = null;
try {
TaiFastBlock taiFastBlock = new Request<>(
Constant.BLOCK_BYHASH,
Arrays.asList(fastHash, returnFullTransactionObjects),
web3jService,
TaiFastBlock.class).send();
fastBlock = taiFastBlock.getFastBlock();
} catch (Exception e) {
e.printStackTrace();
}
return fastBlock;
}
交易相關
普通交易
public String genRawTransaction() {
Credentials credentials = Credentials.create(fromPrivatekey);
BigInteger nonce = getTransactionNonce(fromAddress);
RawTransaction rawTransaction =
RawTransaction.createEtherTransaction(nonce, Constant.DEFAULT_GASPRICE,
Constant.DEFAULT_CONTRACT_GASLIMIT, toAddress, Constant.DEFAULT_VALUE);
byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, chainId, credentials);
String hexMessage = Numeric.toHexString(signedMessage);
logger.info("genRawTransaction hexMessage ={}", hexMessage);
return hexMessage;
}
public void sendRawTransaction(String hexValue) {
try {
EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).send();
String txHash = ethSendTransaction.getTransactionHash();
if (ethSendTransaction.getError() != null) {
logger.error("sendTransaction error", ethSendTransaction.getError().getMessage());
}
} catch (Exception e) {
e.printStackTrace();
}
}
String hexMessage = transactionClientUsage.genRawTransaction();
sendRawTransaction(hexMessage);
代付交易
public String sendPaymentTx() {
String txHash = null;
try {
EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(fromAddress, DefaultBlockParameterName.LATEST).sendAsync().get();
BigInteger nonce = ethGetTransactionCount.getTransactionCount();
TaiRawTransaction taiRawTransaction = TaiRawTransaction.creattaiPaymentTransaction(nonce, Constant.DEFAULT_GASPRICE,Constant.DEFAULT_GASLIMIT, toAddress, Constant.DEFAULT_VALUE, null, paymentAddress);
TaiTransactionManager taiTransactionManager = new TaiTransactionManager(taiWeb3JRequest, chainId);
TaiSendTransaction taiSendTransaction = taiTransactionManager.signWithFromPaymentAndSend(
taiRawTransaction, fromPrivatekey, paymentPrivateKey);
if (taiSendTransaction != null && taiSendTransaction.hasError()) {
logger.error("sendPaymentTransactionWithSigned error=[{}] ", taiSendTransaction.getError().getMessage());
}
txHash = taiSendTransaction.getTrueTransactionHash();
} catch (Exception e) {
e.printStackTrace();
}
return txHash;
}
ERC20 代幣相關
交易
public void sendTokenTransaction(String contractAddress, String toAddress, String from_privateKey) {
try {
Credentials from_credentials = Credentials.create(from_privateKey);
String from_address = from_credentials.getAddress();
BigInteger amount = Constant.DEFAULT_VALUE;
String methodName = "transfer";
List<Type> inputParameters = new ArrayList<>();
List<TypeReference<?>> outputParameters = new ArrayList<>();
Address tAddress = new Address(toAddress);
Uint256 value = new Uint256(amount);
inputParameters.add(tAddress);
inputParameters.add(value);
TypeReference<Bool> typeReference = new TypeReference<Bool>() {
};
outputParameters.add(typeReference);
Function function = new Function(methodName, inputParameters, outputParameters);
String data = FunctionEncoder.encode(function);
EthGetTransactionCount ethGetTransactionCount = web3j
.ethGetTransactionCount(from_address, DefaultBlockParameterName.PENDING).sendAsync().get();
BigInteger nonce = ethGetTransactionCount.getTransactionCount();
RawTransaction rawTransaction = RawTransaction.createTransaction(
nonce, Constant.DEFAULT_GASPRICE,
Constant.DEFAULT_CONTRACT_GASLIMIT, contractAddress, data);
byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, chainId, from_credentials);
String hexValue = Numeric.toHexString(signedMessage);
EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).sendAsync().get();
String txHash = ethSendTransaction.getTransactionHash();
if (ethSendTransaction.getError() != null) {
logger.error("sendTokenTransaction error" + ethSendTransaction.getError());
}
logger.info("txHash={}", txHash);
} catch (Exception e) {
e.printStackTrace();
}
}
獲取餘額
public BigInteger getTokenBalance(String fromAddress, String contractAddress) {
String methodName = "balanceOf";
List<Type> inputParameters = new ArrayList<>();
List<TypeReference<?>> outputParameters = new ArrayList<>();
Address address = new Address(fromAddress);
inputParameters.add(address);
TypeReference<Uint256> typeReference = new TypeReference<Uint256>() {
};
outputParameters.add(typeReference);
Function function = new Function(methodName, inputParameters, outputParameters);
String data = FunctionEncoder.encode(function);
Transaction transaction = Transaction.createEthCallTransaction(fromAddress, contractAddress, data);
BigInteger balanceValue = BigInteger.ZERO;
try {
EthCall ethCall = web3j.ethCall(transaction, DefaultBlockParameterName.LATEST).send();
List<Type> results = FunctionReturnDecoder.decode(ethCall.getValue(), function.getOutputParameters());
if (ethCall.getError() != null) {
logger.error("getTokenBalance error={}", ethCall.getError().getMessage());
}
if (results.size() == 0) {
logger.error("contractAddress =[{}] is not exist", contractAddress);
return balanceValue;
}
String resultVal = results.get(0).getValue().toString();
balanceValue = new BigInteger(resultVal);
} catch (Exception e) {
e.printStackTrace();
}
return balanceValue;
}
獲取代幣名稱
public String getTokenName(String contractAddress) {
String methodName = "name";
String name = null;
String fromAddr = AddressConstant.EMPTY_ADDRESS;
List<Type> inputParameters = new ArrayList<>();
List<TypeReference<?>> outputParameters = new ArrayList<>();
TypeReference<Utf8String> typeReference = new TypeReference<Utf8String>() {
};
outputParameters.add(typeReference);
Function function = new Function(methodName, inputParameters, outputParameters);
String data = FunctionEncoder.encode(function);
Transaction transaction = Transaction.createEthCallTransaction(fromAddr, contractAddress, data);
try {
EthCall ethCall = web3j.ethCall(transaction, DefaultBlockParameterName.LATEST).sendAsync().get();
List<Type> results = FunctionReturnDecoder.decode(ethCall.getValue(), function.getOutputParameters());
name = results.get(0).getValue().toString();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
return name;
}
獲取代幣位數(小數位)
public int getTokenDecimals(String contractAddress) {
String methodName = "decimals";
String fromAddr = AddressConstant.EMPTY_ADDRESS;
int decimal = 0;
List<Type> inputParameters = new ArrayList<>();
List<TypeReference<?>> outputParameters = new ArrayList<>();
TypeReference<Uint8> typeReference = new TypeReference<Uint8>() {
};
outputParameters.add(typeReference);
Function function = new Function(methodName, inputParameters, outputParameters);
String data = FunctionEncoder.encode(function);
Transaction transaction = Transaction.createEthCallTransaction(fromAddr, contractAddress, data);
try {
EthCall ethCall = web3j.ethCall(transaction, DefaultBlockParameterName.LATEST).sendAsync().get();
List<Type> results = FunctionReturnDecoder.decode(ethCall.getValue(), function.getOutputParameters());
decimal = Integer.parseInt(results.get(0).getValue().toString());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
return decimal;
}
獲取代幣總量
public BigInteger getTokenTotalSupply(String contractAddress) {
String methodName = "totalSupply";
String fromAddr = AddressConstant.EMPTY_ADDRESS;
BigInteger totalSupply = BigInteger.ZERO;
List<Type> inputParameters = new ArrayList<>();
List<TypeReference<?>> outputParameters = new ArrayList<>();
TypeReference<Uint256> typeReference = new TypeReference<Uint256>() {
};
outputParameters.add(typeReference);
Function function = new Function(methodName, inputParameters, outputParameters);
String data = FunctionEncoder.encode(function);
Transaction transaction = Transaction.createEthCallTransaction(fromAddr, contractAddress, data);
try {
EthCall ethCall = web3j.ethCall(transaction, DefaultBlockParameterName.LATEST).sendAsync().get();
List<Type> results = FunctionReturnDecoder.decode(ethCall.getValue(), function.getOutputParameters());
totalSupply = (BigInteger) results.get(0).getValue();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
return totalSupply;
}
委員會相關
獲取當前委員會屆數
public Integer getCurrentCommitteeNumber() {
Integer currentCommitteeNumber = null;
try {
TaiCommitteeNumber taiCommitteeNumber = new Request<>(
Constant.CURRENT_COMMITTEE_NUMBER,
Arrays.asList(),
web3jService,
TaiCommitteeNumber.class).send();
currentCommitteeNumber = taiCommitteeNumber.getCommitteeNumber();
} catch (Exception e) {
e.printStackTrace();
}
return currentCommitteeNumber;
}
查詢委員會
public CommitteeInfo getCommitteeByNumber(BigInteger committeeNumber) {
CommitteeInfo committeeInfo = null;
try {
TaiCommittee taiCommittee = new Request<>(
Constant.COMMITTEE_BY_NUMBER,
Arrays.asList(DefaultBlockParameter.valueOf(committeeNumber).getValue()),
web3jService,
TaiCommittee.class).send();
committeeInfo = taiCommittee.getCommittee();
} catch (Exception e) {
e.printStackTrace();
}
return committeeInfo;
}
使用方法和常量
Web3j 聲明
public TaiWeb3jRequest taiWeb3JRequest = new TaiWeb3jRequest(節點地址);
public Web3j web3j = Web3j.build(new HttpService(節點地址));
建議測試交易執行參數
GasPrice:1200000000
GasLimit:30000(普通交易)/60000(ERC20)