eth와erc20 코드 접속
주소 만들기
public static EthAddress createAddress() {
try {
String seed = UUID.randomUUID().toString();
ECKeyPair ecKeyPair = Keys.createEcKeyPair();
BigInteger privateKeyInDec = ecKeyPair.getPrivateKey();
String sPrivatekeyInHex = privateKeyInDec.toString(16);
WalletFile aWallet = Wallet.createLight(seed, ecKeyPair);
String sAddress = aWallet.getAddress();
EthAddress address = new EthAddress();
address.setAddress("0x" + sAddress);
address.setPrivateKey(sPrivatekeyInHex);
return address;
} catch (Throwable t) {
logger.error(" ", t);
}
return null;
}
조회erc20 잔액
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 (StringUtils.isEmpty(value)) {
return BigInteger.ZERO;
}
return new BigInteger(value.substring(2), 16);
} catch (Throwable t) {
logger.error(String.format(" ERC20 contract:%s address:%s", contract, address), t);
}
return BigInteger.ZERO;
}
erc20 보내기
public static String sendErc20(Web3j web3j, String contractAddress, String privateKey,
String to, BigInteger value) {
String from = getAddressByPrivateKey(privateKey);
logger.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) {
logger.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) {
logger.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;
}
logger.error(String.format("END:HashIsNull from:%s to:%s amount:%s erc20:%s", from, to, value.toString(), contractAddress));
} catch (Throwable t) {
System.out.println(t);
logger.error(String.format(" ERC20 from=%s to=%s erc20=%s amount=%s",
from, to, contractAddress, value.toString()), t);
}
return null;
}
다른 방법은 잠시 보류하고 시간이 있으면 다시 하자
문의 사항: laoxiang829