Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

web3j example #609

Merged
merged 11 commits into from
May 13, 2024
27 changes: 16 additions & 11 deletions web3j-ext/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
- Use java version: 11 <= v <= 18
- Visit https://adoptopenjdk.net/ site
- Download OpenJDK

## Install Web3j Klaytn extension

To add the [Gradle Library](https://docs.gradle.org/current/userguide/getting_started.html) to your project:
Expand Down Expand Up @@ -53,7 +53,7 @@ import org.web3j.protocol.klaytn.core.method.response.TransactionReceipt;

public class FeeDelegatedValueTransferExample implements keySample {

public static void run() throws IOException {
public static void run() throws Exception {
Web3j web3j = Web3j.build(new HttpService(keySample.BAOBAB_URL));
KlayCredentials credentials = KlayCredentials.create(keySample.LEGACY_KEY_privkey);
KlayCredentials credentials_feepayer = KlayCredentials.create(keySample.LEGACY_KEY_FEEPAYER_privkey);
Expand Down Expand Up @@ -89,17 +89,22 @@ public class FeeDelegatedValueTransferExample implements keySample {
EthSendTransaction transactionResponse = web3j.ethSendRawTransaction(hexValue).send();
System.out.println("TxHash : \n " + transactionResponse.getResult());
String txHash = transactionResponse.getResult();
try {
Thread.sleep(2000);
}
catch(Exception e) {
System.out.println(e);
}

int DEFAULT_POLLING_ATTEMPTS_PER_TX_HASH = 40;
int DEFAULT_BLOCK_TIME = 1 * 1000;
long DEFAULT_POLLING_FREQUENCY = DEFAULT_BLOCK_TIME;
TransactionReceiptProcessor transactionReceiptProcessor = new PollingTransactionReceiptProcessor(web3j,
DEFAULT_POLLING_FREQUENCY, DEFAULT_POLLING_ATTEMPTS_PER_TX_HASH);
org.web3j.protocol.core.methods.response.TransactionReceipt ethReceipt = transactionReceiptProcessor
.waitForTransactionReceipt(txHash);
System.out.println("Receipt from eth_getTransactionReceipt : \n" + ethReceipt);
TransactionReceipt receipt = web3j.klayGetTransactionReceipt(txHash).send().getResult();
System.out.print("receipt : \n" + receipt);
System.out.println("Receipt from klay_getTransactionReceipt : \n" + receipt);
web3j.shutdown();

TxTypeValueTransfer rawTransaction = TxTypeValueTransfer.decodeFromRawTransaction(hexValue);
TxTypeFeeDelegatedValueTransfer rawTransaction = TxTypeFeeDelegatedValueTransfer
.decodeFromRawTransaction(hexValue);
System.out.println("TxType : " + rawTransaction.getKlayType());
}
}
````
Expand All @@ -111,7 +116,7 @@ import org.web3j.example.FeeDelegatedValueTransferExample;

public class quickstart {
public static void main(String[] args) throws Exception {
FeeDelegatedValueTransferExample.run();
FeeDelegatedValueTransferExample.run();
}
}
````
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package org.web3j.example.accountKey;

import org.web3j.tx.response.PollingTransactionReceiptProcessor;
import org.web3j.tx.response.TransactionReceiptProcessor;
import org.web3j.example.keySample;
import java.io.IOException;
import java.math.BigInteger;
import org.web3j.crypto.KlayCredentials;
import org.web3j.crypto.KlayRawTransaction;
import org.web3j.crypto.KlayTransactionEncoder;
import org.web3j.crypto.transaction.account.AccountKeyLegacy;
import org.web3j.crypto.transaction.type.TxType;
import org.web3j.crypto.transaction.type.TxTypeAccountUpdate;
import org.web3j.crypto.transaction.type.TxType.Type;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.response.EthChainId;
import org.web3j.protocol.core.methods.response.EthSendTransaction;
import org.web3j.protocol.klaytn.Web3j;
import org.web3j.protocol.http.HttpService;
import org.web3j.utils.Numeric;
import org.web3j.protocol.klaytn.core.method.response.TransactionReceipt;

public class AccountUpdateWithLegacyExample {

public static void run() throws Exception {

KlayCredentials credentials = KlayCredentials.create(keySample.LEGACY_KEY_privkey);
Web3j web3j = Web3j.build(new HttpService(keySample.BAOBAB_URL));

BigInteger GAS_PRICE = BigInteger.valueOf(50000000000L);
BigInteger GAS_LIMIT = BigInteger.valueOf(6721950);
String from = credentials.getAddress();
EthChainId EthchainId = web3j.ethChainId().send();
long chainId = EthchainId.getChainId().longValue();
BigInteger nonce = web3j.ethGetTransactionCount(from, DefaultBlockParameterName.LATEST).send()
.getTransactionCount();

AccountKeyLegacy accountkey = AccountKeyLegacy.create();

TxType.Type type = Type.ACCOUNT_UPDATE;

KlayRawTransaction raw = KlayRawTransaction.createTransaction(
type,
nonce,
GAS_PRICE,
GAS_LIMIT,
from,
accountkey);

byte[] signedMessage = KlayTransactionEncoder.signMessage(raw, chainId, credentials);
String hexValue = Numeric.toHexString(signedMessage);
EthSendTransaction transactionResponse = web3j.ethSendRawTransaction(hexValue).send();
System.out.println("TxHash : \n " + transactionResponse.getResult());
String txHash = transactionResponse.getResult();

int DEFAULT_POLLING_ATTEMPTS_PER_TX_HASH = 40;
int DEFAULT_BLOCK_TIME = 1 * 1000;
long DEFAULT_POLLING_FREQUENCY = DEFAULT_BLOCK_TIME;
TransactionReceiptProcessor transactionReceiptProcessor = new PollingTransactionReceiptProcessor(web3j,
DEFAULT_POLLING_FREQUENCY, DEFAULT_POLLING_ATTEMPTS_PER_TX_HASH);
org.web3j.protocol.core.methods.response.TransactionReceipt ethReceipt = transactionReceiptProcessor
.waitForTransactionReceipt(txHash);
System.out.println("Receipt from eth_getTransactionReceipt : \n" + ethReceipt);
TransactionReceipt receipt = web3j.klayGetTransactionReceipt(txHash).send().getResult();
System.out.println("Receipt from klay_getTransactionReceipt : \n" + receipt);
web3j.shutdown();

TxTypeAccountUpdate rawTransaction = TxTypeAccountUpdate.decodeFromRawTransaction(signedMessage);

System.out.println("TxType : " + rawTransaction.getKlayType());

}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.web3j.example.accountKey;

import org.web3j.tx.response.PollingTransactionReceiptProcessor;
import org.web3j.tx.response.TransactionReceiptProcessor;
import org.web3j.example.keySample;
import java.io.IOException;
import java.math.BigInteger;
Expand All @@ -24,66 +26,71 @@

public class AccountUpdateWithMultiSigExample {

public static void run(KlayCredentials credentials) throws IOException {

Web3j web3j = Web3j.build(new HttpService(keySample.BAOBAB_URL));
KlayCredentials new_credentials1 = KlayCredentials.create(keySample.MULTI_KEY_privkey1);
KlayCredentials new_credentials2 = KlayCredentials.create(keySample.MULTI_KEY_privkey2);
KlayCredentials new_credentials3 = KlayCredentials.create(keySample.MULTI_KEY_privkey3);

BigInteger GAS_PRICE = BigInteger.valueOf(50000000000L);
BigInteger GAS_LIMIT = BigInteger.valueOf(6721950);
String from = credentials.getAddress();
EthChainId EthchainId = web3j.ethChainId().send();
long chainId = EthchainId.getChainId().longValue();
BigInteger nonce = web3j.ethGetTransactionCount(from, DefaultBlockParameterName.LATEST).send()
.getTransactionCount();

BigInteger newPubkey1 = new_credentials1.getEcKeyPair().getPublicKey();
WeightedPublicKey weightedAccountKey1 = WeightedPublicKey.create(BigInteger.ONE,
AccountKeyPublic.create(newPubkey1));
BigInteger newPubkey2 = new_credentials1.getEcKeyPair().getPublicKey();
WeightedPublicKey weightedAccountKey2 = WeightedPublicKey.create(BigInteger.ONE,
AccountKeyPublic.create(newPubkey2));
BigInteger newPubkey3 = new_credentials1.getEcKeyPair().getPublicKey();
WeightedPublicKey weightedAccountKey3 = WeightedPublicKey.create(BigInteger.ONE,
AccountKeyPublic.create(newPubkey3));

// make list with weightedAccountKey1, weightedAccountKey2, weightedAccountKey3
List<WeightedPublicKey> weightedAccountKeyList = List.of(weightedAccountKey1, weightedAccountKey2,
weightedAccountKey3);

AccountKeyWeightedMultiSig accountkey = AccountKeyWeightedMultiSig.create(BigInteger.TWO,
weightedAccountKeyList);

TxType.Type type = Type.ACCOUNT_UPDATE;

KlayRawTransaction raw = KlayRawTransaction.createTransaction(
type,
nonce,
GAS_PRICE,
GAS_LIMIT,
from,
accountkey);

byte[] signedMessage = KlayTransactionEncoder.signMessage(raw, chainId, credentials);
String hexValue = Numeric.toHexString(signedMessage);
EthSendTransaction transactionResponse = web3j.ethSendRawTransaction(hexValue).send();
System.out.println("TxHash : \n " + transactionResponse.getResult());
String txHash = transactionResponse.getResult();
try {
Thread.sleep(2000);
} catch (Exception e) {
System.out.println(e);
}
TransactionReceipt receipt = web3j.klayGetTransactionReceipt(txHash).send().getResult();
System.out.println("receipt : \n" + receipt);
web3j.shutdown();

TxTypeAccountUpdate rawTransaction = TxTypeAccountUpdate.decodeFromRawTransaction(signedMessage);
public static void run() throws Exception {

Web3j web3j = Web3j.build(new HttpService(keySample.BAOBAB_URL));
KlayCredentials credential1 = KlayCredentials.create(keySample.MULTI_KEY_privkey1, keySample.MULTI_KEY_address);
KlayCredentials credential2 = KlayCredentials.create(keySample.MULTI_KEY_privkey2, keySample.MULTI_KEY_address);
KlayCredentials credential3 = KlayCredentials.create(keySample.MULTI_KEY_privkey3, keySample.MULTI_KEY_address);

BigInteger GAS_PRICE = BigInteger.valueOf(50000000000L);
BigInteger GAS_LIMIT = BigInteger.valueOf(6721950);
String from = credential1.getAddress();
EthChainId EthchainId = web3j.ethChainId().send();
long chainId = EthchainId.getChainId().longValue();
BigInteger nonce = web3j.ethGetTransactionCount(from, DefaultBlockParameterName.LATEST).send()
.getTransactionCount();

BigInteger newPubkey1 = credential1.getEcKeyPair().getPublicKey();
WeightedPublicKey weightedAccountKey1 = WeightedPublicKey.create(BigInteger.ONE,
AccountKeyPublic.create(newPubkey1));
BigInteger newPubkey2 = credential2.getEcKeyPair().getPublicKey();
WeightedPublicKey weightedAccountKey2 = WeightedPublicKey.create(BigInteger.ONE,
AccountKeyPublic.create(newPubkey2));
BigInteger newPubkey3 = credential3.getEcKeyPair().getPublicKey();
WeightedPublicKey weightedAccountKey3 = WeightedPublicKey.create(BigInteger.ONE,
AccountKeyPublic.create(newPubkey3));

// make list with weightedAccountKey1, weightedAccountKey2, weightedAccountKey3
List<WeightedPublicKey> weightedAccountKeyList = List.of(weightedAccountKey1, weightedAccountKey2,
weightedAccountKey3);

AccountKeyWeightedMultiSig accountkey = AccountKeyWeightedMultiSig.create(BigInteger.TWO,
weightedAccountKeyList);

TxType.Type type = Type.ACCOUNT_UPDATE;

KlayRawTransaction raw = KlayRawTransaction.createTransaction(
type,
nonce,
GAS_PRICE,
GAS_LIMIT,
from,
accountkey);

byte[] signedMessage = KlayTransactionEncoder.signMessage(raw, chainId, credential1);
signedMessage = KlayTransactionEncoder.signMessage(raw, chainId, credential2);
String hexValue = Numeric.toHexString(signedMessage);
EthSendTransaction transactionResponse = web3j.ethSendRawTransaction(hexValue).send();
System.out.println("TxHash : \n " + transactionResponse.getResult());
String txHash = transactionResponse.getResult();

int DEFAULT_POLLING_ATTEMPTS_PER_TX_HASH = 40;
int DEFAULT_BLOCK_TIME = 1 * 1000;
long DEFAULT_POLLING_FREQUENCY = DEFAULT_BLOCK_TIME;
TransactionReceiptProcessor transactionReceiptProcessor = new PollingTransactionReceiptProcessor(web3j,
DEFAULT_POLLING_FREQUENCY, DEFAULT_POLLING_ATTEMPTS_PER_TX_HASH);
org.web3j.protocol.core.methods.response.TransactionReceipt ethReceipt = transactionReceiptProcessor
.waitForTransactionReceipt(txHash);
System.out.println("Receipt from eth_getTransactionReceipt : \n" + ethReceipt);
TransactionReceipt receipt = web3j.klayGetTransactionReceipt(txHash).send().getResult();
System.out.println("Receipt from klay_getTransactionReceipt : \n" + receipt);
web3j.shutdown();

TxTypeAccountUpdate rawTransaction = TxTypeAccountUpdate.decodeFromRawTransaction(signedMessage);

System.out.println("TxType : " + rawTransaction.getKlayType());

System.out.println("TxType : " + rawTransaction.getKlayType());

}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.web3j.example.accountKey;

import org.web3j.tx.response.PollingTransactionReceiptProcessor;
import org.web3j.tx.response.TransactionReceiptProcessor;
import org.web3j.example.keySample;
import java.io.IOException;
import java.math.BigInteger;
Expand All @@ -20,52 +22,55 @@

public class AccountUpdateWithPubKeyExample {

public static void run(KlayCredentials credentials) throws IOException {
public static void run() throws Exception {

Web3j web3j = Web3j.build(new HttpService(keySample.BAOBAB_URL));
KlayCredentials new_credentials = KlayCredentials.create(keySample.PUBLIC_KEY_privkey,
keySample.PUBLIC_KEY_address);
KlayCredentials credentials = KlayCredentials.create(keySample.PUBLIC_KEY_privkey, keySample.PUBLIC_KEY_address);
Web3j web3j = Web3j.build(new HttpService(keySample.BAOBAB_URL));

BigInteger GAS_PRICE = BigInteger.valueOf(50000000000L);
BigInteger GAS_LIMIT = BigInteger.valueOf(6721950);
String from = credentials.getAddress();
EthChainId EthchainId = web3j.ethChainId().send();
long chainId = EthchainId.getChainId().longValue();
BigInteger nonce = web3j.ethGetTransactionCount(from, DefaultBlockParameterName.LATEST).send()
.getTransactionCount();
BigInteger GAS_PRICE = BigInteger.valueOf(50000000000L);
BigInteger GAS_LIMIT = BigInteger.valueOf(6721950);
String from = credentials.getAddress();
EthChainId EthchainId = web3j.ethChainId().send();
long chainId = EthchainId.getChainId().longValue();
BigInteger nonce = web3j.ethGetTransactionCount(from, DefaultBlockParameterName.LATEST).send()
.getTransactionCount();

BigInteger newPubkey = new_credentials.getEcKeyPair().getPublicKey();
BigInteger newPubkey = credentials.getEcKeyPair().getPublicKey();

AccountKeyPublic accountkey = AccountKeyPublic.create(newPubkey);
AccountKeyPublic accountkey = AccountKeyPublic.create(newPubkey);

TxType.Type type = Type.ACCOUNT_UPDATE;
TxType.Type type = Type.ACCOUNT_UPDATE;

KlayRawTransaction raw = KlayRawTransaction.createTransaction(
type,
nonce,
GAS_PRICE,
GAS_LIMIT,
from,
accountkey);
KlayRawTransaction raw = KlayRawTransaction.createTransaction(
type,
nonce,
GAS_PRICE,
GAS_LIMIT,
from,
accountkey);

byte[] signedMessage = KlayTransactionEncoder.signMessage(raw, chainId, credentials);
String hexValue = Numeric.toHexString(signedMessage);
EthSendTransaction transactionResponse = web3j.ethSendRawTransaction(hexValue).send();
System.out.println("TxHash : \n " + transactionResponse.getResult());
String txHash = transactionResponse.getResult();
try {
Thread.sleep(2000);
} catch (Exception e) {
System.out.println(e);
}
TransactionReceipt receipt = web3j.klayGetTransactionReceipt(txHash).send().getResult();
System.out.println("receipt : \n" + receipt);
web3j.shutdown();
byte[] signedMessage = KlayTransactionEncoder.signMessage(raw, chainId, credentials);
String hexValue = Numeric.toHexString(signedMessage);
EthSendTransaction transactionResponse = web3j.ethSendRawTransaction(hexValue).send();
System.out.println("TxHash : \n " + transactionResponse.getResult());
String txHash = transactionResponse.getResult();

int DEFAULT_POLLING_ATTEMPTS_PER_TX_HASH = 40;
int DEFAULT_BLOCK_TIME = 1 * 1000;
long DEFAULT_POLLING_FREQUENCY = DEFAULT_BLOCK_TIME;
TransactionReceiptProcessor transactionReceiptProcessor = new PollingTransactionReceiptProcessor(web3j,
DEFAULT_POLLING_FREQUENCY, DEFAULT_POLLING_ATTEMPTS_PER_TX_HASH);
org.web3j.protocol.core.methods.response.TransactionReceipt ethReceipt = transactionReceiptProcessor
.waitForTransactionReceipt(txHash);
System.out.println("Receipt from eth_getTransactionReceipt : \n" + ethReceipt);
TransactionReceipt receipt = web3j.klayGetTransactionReceipt(txHash).send().getResult();
System.out.println("Receipt from klay_getTransactionReceipt : \n" + receipt);
web3j.shutdown();

TxTypeAccountUpdate rawTransaction = TxTypeAccountUpdate.decodeFromRawTransaction(signedMessage);
TxTypeAccountUpdate rawTransaction = TxTypeAccountUpdate.decodeFromRawTransaction(signedMessage);

System.out.println("TxType : " + rawTransaction.getKlayType());
System.out.println("TxType : " + rawTransaction.getKlayType());

}
}

}
Loading
Loading