This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
CaverExample.java
100 lines (90 loc) · 5.05 KB
/
CaverExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.klaytn.caver.Caver;
import com.klaytn.caver.methods.response.Bytes32;
import com.klaytn.caver.methods.response.TransactionReceipt;
import com.klaytn.caver.transaction.TxPropertyBuilder;
import com.klaytn.caver.transaction.response.PollingTransactionReceiptProcessor;
import com.klaytn.caver.transaction.type.ValueTransfer;
import com.klaytn.caver.utils.Utils;
import com.klaytn.caver.wallet.keyring.SingleKeyring;
import io.github.cdimascio.dotenv.Dotenv;
import okhttp3.Credentials;
import org.web3j.protocol.http.HttpService;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* Example code about "How to send Klay."
* Related reference - Korean: https://ko.docs.klaytn.com/bapp/sdk/caver-java/getting-started#sending-klay-at-a-glance
* Related reference - English: https://docs.klaytn.com/bapp/sdk/caver-java/getting-started#sending-klay-at-a-glance
*/
public class CaverExample {
// You can directly input values for the variables below, or you can enter values in the caver-java-examples/.env file.
private static String nodeApiUrl = ""; // e.g. "https://node-api.klaytnapi.com/v1/klaytn";
private static String accessKeyId = ""; // e.g. "KASK1LVNO498YT6KJQFUPY8S";
private static String secretAccessKey = ""; // e.g. "aP/reVYHXqjw3EtQrMuJP4A3/hOb69TjnBT3ePKG";
private static String chainId = ""; // e.g. "1001" or "8217";
private static String senderAddress = ""; // e.g. "0xeb709d59954f4cdc6b6f3bfcd8d531887b7bd199"
private static String senderPrivateKey = ""; // e.g. "0x42f6375b608c2572fadb2ed9fd78c5c456ca3aa860c43192ad910c3269727fc7"
private static String recipientAddress = ""; // e.g. "0xeb709d59954f4cdc6b6f3bfcd8d531887b7bd199"
public static void main(String[] args) {
try {
loadEnv();
run();
} catch (Exception e) {
e.printStackTrace();
}
}
public static String objectToString(Object value) throws JsonProcessingException {
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
return ow.writeValueAsString(value);
}
public static void loadEnv() {
Dotenv env;
String workingDirectory = System.getProperty("user.dir");
Path workingDirectoryPath = Paths.get(workingDirectory);
String projectRootDirectory = "caver-java-examples";
String currentDirectoryName = workingDirectoryPath.getName(workingDirectoryPath.getNameCount() - 1).toString();
String envDirectory = currentDirectoryName.equals(projectRootDirectory) ?
workingDirectoryPath.toString() :
workingDirectoryPath.getParent().getParent().toString();
// Read `/path/to/caver-java-examples/.env` file.
env = Dotenv.configure().directory(envDirectory).load();
nodeApiUrl = nodeApiUrl.equals("") ? env.get("NODE_API_URL") : nodeApiUrl;
accessKeyId = accessKeyId.equals("") ? env.get("ACCESS_KEY_ID") : accessKeyId;
secretAccessKey = secretAccessKey.equals("") ? env.get("SECRET_ACCESS_KEY") : secretAccessKey;
chainId = chainId.equals("") ? env.get("CHAIN_ID") : chainId;
senderAddress = senderAddress.equals("") ? env.get("SENDER_ADDRESS") : senderAddress;
senderPrivateKey = senderPrivateKey.equals("") ? env.get("SENDER_PRIVATE_KEY") : senderPrivateKey;
recipientAddress = recipientAddress.equals("") ? env.get("RECIPIENT_ADDRESS") : recipientAddress;
}
public static void run() throws Exception {
HttpService httpService = new HttpService(nodeApiUrl);
httpService.addHeader("Authorization", Credentials.basic(accessKeyId, secretAccessKey));
httpService.addHeader("x-chain-id", chainId);
Caver caver = new Caver(httpService);
SingleKeyring senderKeyring = caver.wallet.keyring.create(senderAddress, senderPrivateKey);
caver.wallet.add(senderKeyring);
// Send 1 KLAY.
ValueTransfer vt = caver.transaction.valueTransfer.create(
TxPropertyBuilder.valueTransfer()
.setFrom(senderAddress)
.setTo(recipientAddress)
.setValue(new BigInteger(
caver.utils.convertToPeb(
BigDecimal.valueOf(1),
Utils.KlayUnit.KLAY
)
))
.setGas(BigInteger.valueOf(25000))
);
caver.wallet.sign(senderKeyring.getAddress(), vt);
String txHash = caver.rpc.klay.sendRawTransaction(vt).send().getResult();
PollingTransactionReceiptProcessor receiptProcessor = new PollingTransactionReceiptProcessor(caver, 1000, 15);
TransactionReceipt.TransactionReceiptData receiptData = receiptProcessor.waitForTransactionReceipt(txHash);
System.out.println(objectToString(receiptData));
}
}