Skip to content

Commit ca205a1

Browse files
committed
add
1 parent 7a6d12e commit ca205a1

2 files changed

Lines changed: 155 additions & 4 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.ethjava;
2+
3+
/**
4+
* filter相关
5+
* 监听区块、交易
6+
*/
7+
public class Filter {
8+
public static void main(String[] args){
9+
10+
}
11+
}

src/main/java/com/ethjava/TokenClient.java

Lines changed: 144 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,22 @@
44
import org.web3j.abi.FunctionEncoder;
55
import org.web3j.abi.FunctionReturnDecoder;
66
import org.web3j.abi.TypeReference;
7-
import org.web3j.abi.datatypes.Address;
8-
import org.web3j.abi.datatypes.Function;
9-
import org.web3j.abi.datatypes.Type;
10-
import org.web3j.abi.datatypes.Utf8String;
7+
import org.web3j.abi.datatypes.*;
118
import org.web3j.abi.datatypes.generated.Uint256;
129
import org.web3j.abi.datatypes.generated.Uint8;
1310
import org.web3j.protocol.Web3j;
11+
import org.web3j.protocol.admin.Admin;
12+
import org.web3j.protocol.admin.methods.response.PersonalUnlockAccount;
1413
import org.web3j.protocol.core.DefaultBlockParameterName;
1514
import org.web3j.protocol.core.methods.request.Transaction;
1615
import org.web3j.protocol.core.methods.response.EthCall;
16+
import org.web3j.protocol.core.methods.response.EthGetTransactionCount;
17+
import org.web3j.protocol.core.methods.response.EthSendTransaction;
1718
import org.web3j.protocol.http.HttpService;
19+
import org.web3j.utils.Convert;
1820

1921
import java.io.IOException;
22+
import java.math.BigDecimal;
2023
import java.math.BigInteger;
2124
import java.util.ArrayList;
2225
import java.util.List;
@@ -29,6 +32,8 @@ public class TokenClient {
2932

3033
private static Web3j web3j;
3134

35+
private static Admin admin;
36+
3237
private static String fromAddress = "0x7b1cc408fcb2de1d510c1bf46a329e9027db4112";
3338

3439
private static String contractAddress = "0x4c1ae77bc2df45fb68b13fa1b4f000305209b0cb";
@@ -37,9 +42,16 @@ public class TokenClient {
3742

3843
public static void main(String[] args) {
3944
web3j = Web3j.build(new HttpService(Environment.RPC_URL));
45+
admin = Admin.build(new HttpService(Environment.RPC_URL));
4046
getTokenBalance(web3j, fromAddress, contractAddress);
4147
System.out.println(getTokenName(web3j, contractAddress));
4248
System.out.println(getTokenDecimals(web3j, contractAddress));
49+
System.out.println(getTokenSymbol(web3j, contractAddress));
50+
System.out.println(getTokenTotalSupply(web3j, contractAddress));
51+
System.out.println(sendTokenTransaction(
52+
fromAddress,"yzw",
53+
"0x6c0f49aF552F2326DD851b68832730CB7b6C0DaF",contractAddress,
54+
BigInteger.valueOf(100000)));
4355
}
4456

4557
/**
@@ -72,6 +84,13 @@ public static BigInteger getTokenBalance(Web3j web3j, String fromAddress, String
7284
return balanceValue;
7385
}
7486

87+
/**
88+
* 查询代币名称
89+
*
90+
* @param web3j
91+
* @param contractAddress
92+
* @return
93+
*/
7594
public static String getTokenName(Web3j web3j, String contractAddress) {
7695
String methodName = "name";
7796
String name = null;
@@ -99,6 +118,47 @@ public static String getTokenName(Web3j web3j, String contractAddress) {
99118
return name;
100119
}
101120

121+
/**
122+
* 查询代币符号
123+
*
124+
* @param web3j
125+
* @param contractAddress
126+
* @return
127+
*/
128+
public static String getTokenSymbol(Web3j web3j, String contractAddress) {
129+
String methodName = "symbol";
130+
String symbol = null;
131+
String fromAddr = emptyAddress;
132+
List<Type> inputParameters = new ArrayList<>();
133+
List<TypeReference<?>> outputParameters = new ArrayList<>();
134+
135+
TypeReference<Utf8String> typeReference = new TypeReference<Utf8String>() {
136+
};
137+
outputParameters.add(typeReference);
138+
139+
Function function = new Function(methodName, inputParameters, outputParameters);
140+
141+
String data = FunctionEncoder.encode(function);
142+
Transaction transaction = Transaction.createEthCallTransaction(fromAddr, contractAddress, data);
143+
144+
EthCall ethCall;
145+
try {
146+
ethCall = web3j.ethCall(transaction, DefaultBlockParameterName.LATEST).sendAsync().get();
147+
List<Type> results = FunctionReturnDecoder.decode(ethCall.getValue(), function.getOutputParameters());
148+
symbol = results.get(0).getValue().toString();
149+
} catch (InterruptedException | ExecutionException e) {
150+
e.printStackTrace();
151+
}
152+
return symbol;
153+
}
154+
155+
/**
156+
* 查询代币精度
157+
*
158+
* @param web3j
159+
* @param contractAddress
160+
* @return
161+
*/
102162
public static int getTokenDecimals(Web3j web3j, String contractAddress) {
103163
String methodName = "decimals";
104164
String fromAddr = emptyAddress;
@@ -125,4 +185,84 @@ public static int getTokenDecimals(Web3j web3j, String contractAddress) {
125185
}
126186
return decimal;
127187
}
188+
189+
/**
190+
* 查询代币发行总量
191+
*
192+
* @param web3j
193+
* @param contractAddress
194+
* @return
195+
*/
196+
public static BigInteger getTokenTotalSupply(Web3j web3j, String contractAddress) {
197+
String methodName = "totalSupply";
198+
String fromAddr = emptyAddress;
199+
BigInteger totalSupply = BigInteger.ZERO;
200+
List<Type> inputParameters = new ArrayList<>();
201+
List<TypeReference<?>> outputParameters = new ArrayList<>();
202+
203+
TypeReference<Uint256> typeReference = new TypeReference<Uint256>() {
204+
};
205+
outputParameters.add(typeReference);
206+
207+
Function function = new Function(methodName, inputParameters, outputParameters);
208+
209+
String data = FunctionEncoder.encode(function);
210+
Transaction transaction = Transaction.createEthCallTransaction(fromAddr, contractAddress, data);
211+
212+
EthCall ethCall;
213+
try {
214+
ethCall = web3j.ethCall(transaction, DefaultBlockParameterName.LATEST).sendAsync().get();
215+
List<Type> results = FunctionReturnDecoder.decode(ethCall.getValue(), function.getOutputParameters());
216+
totalSupply = (BigInteger) results.get(0).getValue();
217+
} catch (InterruptedException | ExecutionException e) {
218+
e.printStackTrace();
219+
}
220+
return totalSupply;
221+
}
222+
223+
/**
224+
* 代币转账
225+
*/
226+
public static String sendTokenTransaction(String fromAddress, String password, String toAddress, String contractAddress,BigInteger amount) {
227+
String txHash = null;
228+
229+
try {
230+
PersonalUnlockAccount personalUnlockAccount = admin.personalUnlockAccount(
231+
fromAddress, password, BigInteger.valueOf(10)).send();
232+
if (personalUnlockAccount.accountUnlocked()) {
233+
String methodName = "transfer";
234+
List<Type> inputParameters = new ArrayList<>();
235+
List<TypeReference<?>> outputParameters = new ArrayList<>();
236+
237+
Address tAddress = new Address(toAddress);
238+
239+
Uint256 value = new Uint256(amount);
240+
inputParameters.add(tAddress);
241+
inputParameters.add(value);
242+
243+
TypeReference<Bool> typeReference = new TypeReference<Bool>() {
244+
};
245+
outputParameters.add(typeReference);
246+
247+
Function function = new Function(methodName, inputParameters, outputParameters);
248+
249+
String data = FunctionEncoder.encode(function);
250+
251+
EthGetTransactionCount ethGetTransactionCount = web3j
252+
.ethGetTransactionCount(fromAddress, DefaultBlockParameterName.LATEST).sendAsync().get();
253+
BigInteger nonce = ethGetTransactionCount.getTransactionCount();
254+
BigInteger gasPrice = Convert.toWei(BigDecimal.valueOf(5), Convert.Unit.GWEI).toBigInteger();
255+
256+
Transaction transaction = Transaction.createFunctionCallTransaction(fromAddress, nonce, gasPrice,
257+
BigInteger.valueOf(60000), contractAddress, data);
258+
259+
EthSendTransaction ethSendTransaction = web3j.ethSendTransaction(transaction).sendAsync().get();
260+
txHash = ethSendTransaction.getTransactionHash();
261+
}
262+
} catch (Exception e) {
263+
e.printStackTrace();
264+
}
265+
266+
return txHash;
267+
}
128268
}

0 commit comments

Comments
 (0)