mirror of
https://github.com/XRPLF/xrpl-dev-portal.git
synced 2025-11-21 04:05:49 +00:00
Update tutorial for xrpl4j v3
This PR updates all Java examples to utilize xrpl4j v3, which has slight different contracts as compared to v2.
This commit is contained in:
@@ -1,12 +1,8 @@
|
||||
// Example Credentials --------------------------------------------------------
|
||||
WalletFactory walletFactory = DefaultWalletFactory.getInstance();
|
||||
Wallet testWallet = walletFactory
|
||||
.fromSeed("sn3nxiW7v8KXzPzAqzyHXbSSKNuN9", true)
|
||||
.wallet();
|
||||
// Create a KeyPair
|
||||
KeyPair randomKeyPair = Seed.ed25519Seed().deriveKeyPair();
|
||||
|
||||
// Get the Classic address from testWallet
|
||||
Address classicAddress = testWallet.classicAddress();
|
||||
System.out.println(classicAddress); // "rMCcNuTcajgw7YTgBy1sys3b89QqjUrMpH"
|
||||
Address classicAddress = randomKeyPair.publicKey().deriveAddress();
|
||||
|
||||
// Connect --------------------------------------------------------------------
|
||||
HttpUrl rippledUrl = HttpUrl.get("https://s.altnet.rippletest.net:51234/");
|
||||
@@ -15,9 +11,9 @@ XrplClient xrplClient = new XrplClient(rippledUrl);
|
||||
// Prepare transaction --------------------------------------------------------
|
||||
// Look up your Account Info
|
||||
AccountInfoRequestParams requestParams = AccountInfoRequestParams.builder()
|
||||
.ledgerIndex(LedgerIndex.VALIDATED)
|
||||
.account(classicAddress)
|
||||
.build();
|
||||
.ledgerSpecifier(LedgerSpecifier.VALIDATED)
|
||||
.build();
|
||||
AccountInfoResult accountInfoResult = xrplClient.accountInfo(requestParams);
|
||||
UnsignedInteger sequence = accountInfoResult.accountData().sequence();
|
||||
|
||||
@@ -27,17 +23,15 @@ XrpCurrencyAmount openLedgerFee = feeResult.drops().openLedgerFee();
|
||||
|
||||
// Get the latest validated ledger index
|
||||
LedgerIndex validatedLedger = xrplClient.ledger(
|
||||
LedgerRequestParams.builder()
|
||||
.ledgerIndex(LedgerIndex.VALIDATED)
|
||||
.build()
|
||||
)
|
||||
LedgerRequestParams.builder()
|
||||
.ledgerSpecifier(LedgerSpecifier.VALIDATED)
|
||||
.build()
|
||||
)
|
||||
.ledgerIndex()
|
||||
.orElseThrow(() -> new RuntimeException("LedgerIndex not available."));
|
||||
|
||||
// Workaround for https://github.com/XRPLF/xrpl4j/issues/84
|
||||
UnsignedInteger lastLedgerSequence = UnsignedInteger.valueOf(
|
||||
validatedLedger.plus(UnsignedLong.valueOf(4)).unsignedLongValue().intValue()
|
||||
);
|
||||
// LastLedgerSequence is the current ledger index + 4
|
||||
UnsignedInteger lastLedgerSequence = validatedLedger.plus(UnsignedInteger.valueOf(4)).unsignedIntegerValue();
|
||||
|
||||
// Construct a Payment
|
||||
Payment payment = Payment.builder()
|
||||
@@ -46,75 +40,61 @@ Payment payment = Payment.builder()
|
||||
.destination(Address.of("rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe"))
|
||||
.sequence(sequence)
|
||||
.fee(openLedgerFee)
|
||||
.signingPublicKey(testWallet.publicKey())
|
||||
.signingPublicKey(randomKeyPair.publicKey())
|
||||
.lastLedgerSequence(lastLedgerSequence)
|
||||
.build();
|
||||
System.out.println("Constructed Payment: " + payment);
|
||||
|
||||
// Sign transaction -----------------------------------------------------------
|
||||
// Construct a SignatureService to sign the Payment
|
||||
PrivateKey privateKey = PrivateKey.fromBase16EncodedPrivateKey(
|
||||
testWallet.privateKey().get()
|
||||
);
|
||||
SignatureService signatureService = new SingleKeySignatureService(privateKey);
|
||||
SignatureService<PrivateKey> signatureService = new BcSignatureService();
|
||||
|
||||
// Sign the Payment
|
||||
SignedTransaction<Payment> signedPayment = signatureService.sign(
|
||||
KeyMetadata.EMPTY,
|
||||
payment
|
||||
);
|
||||
SingleSignedTransaction<Payment> signedPayment = signatureService.sign(randomKeyPair.privateKey(), payment);
|
||||
System.out.println("Signed Payment: " + signedPayment.signedTransaction());
|
||||
|
||||
// Submit transaction ---------------------------------------------------------
|
||||
SubmitResult<Transaction> prelimResult = xrplClient.submit(signedPayment);
|
||||
System.out.println(prelimResult);
|
||||
SubmitResult<Payment> paymentSubmitResult = xrplClient.submit(signedPayment);
|
||||
System.out.println(paymentSubmitResult);
|
||||
|
||||
// Wait for validation --------------------------------------------------------
|
||||
TransactionResult<Payment> transactionResult = null;
|
||||
|
||||
boolean transactionValidated = false;
|
||||
boolean transactionExpired = false;
|
||||
while (!transactionValidated && !transactionExpired) {
|
||||
Thread.sleep(4 * 1000);
|
||||
LedgerIndex latestValidatedLedgerIndex = xrplClient.ledger(
|
||||
LedgerRequestParams.builder().ledgerIndex(LedgerIndex.VALIDATED).build()
|
||||
)
|
||||
.ledgerIndex()
|
||||
.orElseThrow(() ->
|
||||
new RuntimeException("Ledger response did not contain a LedgerIndex.")
|
||||
);
|
||||
|
||||
TransactionResult<Payment> transactionResult = xrplClient.transaction(
|
||||
TransactionRequestParams.of(signedPayment.hash()),
|
||||
Payment.class
|
||||
);
|
||||
LedgerIndex latestValidatedLedgerIndex = xrplClient.ledger(
|
||||
LedgerRequestParams.builder()
|
||||
.ledgerSpecifier(LedgerSpecifier.VALIDATED)
|
||||
.build()
|
||||
)
|
||||
.ledgerIndex()
|
||||
.orElseThrow(() -> new RuntimeException("Ledger response did not contain a LedgerIndex."));
|
||||
|
||||
transactionResult = xrplClient.transaction(TransactionRequestParams.of(signedPayment.hash()), Payment.class);
|
||||
|
||||
if (transactionResult.validated()) {
|
||||
System.out.println("Payment was validated with result code " +
|
||||
transactionResult.metadata().get().transactionResult());
|
||||
System.out.println("Payment was validated with result code " + transactionResult.metadata().get().transactionResult());
|
||||
transactionValidated = true;
|
||||
} else {
|
||||
boolean lastLedgerSequenceHasPassed = FluentCompareTo.
|
||||
is(latestValidatedLedgerIndex.unsignedLongValue())
|
||||
.greaterThan(UnsignedLong.valueOf(lastLedgerSequence.intValue()));
|
||||
boolean lastLedgerSequenceHasPassed = FluentCompareTo.is(latestValidatedLedgerIndex.unsignedIntegerValue())
|
||||
.greaterThan(UnsignedInteger.valueOf(lastLedgerSequence.intValue()));
|
||||
if (lastLedgerSequenceHasPassed) {
|
||||
System.out.println("LastLedgerSequence has passed. Last tx response: "
|
||||
transactionResult);
|
||||
);
|
||||
System.out.println("LastLedgerSequence has passed. Last tx response: " + transactionResult);
|
||||
transactionExpired = true;
|
||||
} else {
|
||||
System.out.println("Payment not yet validated.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check transaction results --------------------------------------------------
|
||||
// Check transaction results
|
||||
System.out.println(transactionResult);
|
||||
System.out.println("Explorer link: https://testnet.xrpl.org/transactions/" +
|
||||
signedPayment.hash());
|
||||
System.out.println("Explorer link: https://testnet.xrpl.org/transactions/" + signedPayment.hash());
|
||||
transactionResult.metadata().ifPresent(metadata -> {
|
||||
System.out.println("Result code: " + metadata.transactionResult());
|
||||
|
||||
metadata.deliveredAmount().ifPresent(deliveredAmount ->
|
||||
System.out.println("XRP Delivered: " +
|
||||
((XrpCurrencyAmount) deliveredAmount).toXrp())
|
||||
);
|
||||
});
|
||||
System.out.println("XRP Delivered: " + ((XrpCurrencyAmount) deliveredAmount).toXrp()));
|
||||
}
|
||||
);
|
||||
Reference in New Issue
Block a user