From 69f89b1e91d6ef66de6feef5e4acbb12d49d33c5 Mon Sep 17 00:00:00 2001 From: astinz <28899947+astinz@users.noreply.github.com> Date: Mon, 15 Jul 2024 16:47:43 +0300 Subject: [PATCH] improve apt txn sample... --- sample/jvmApp/src/main/kotlin/APTTransfer.kt | 95 +++++++++++++++++++ sample/jvmApp/src/main/kotlin/Transaction.kt | 98 -------------------- 2 files changed, 95 insertions(+), 98 deletions(-) create mode 100644 sample/jvmApp/src/main/kotlin/APTTransfer.kt delete mode 100644 sample/jvmApp/src/main/kotlin/Transaction.kt diff --git a/sample/jvmApp/src/main/kotlin/APTTransfer.kt b/sample/jvmApp/src/main/kotlin/APTTransfer.kt new file mode 100644 index 00000000..42f20402 --- /dev/null +++ b/sample/jvmApp/src/main/kotlin/APTTransfer.kt @@ -0,0 +1,95 @@ +/* + * Copyright 2024 McXross + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package xyz.mcxross.kaptos.sample + +import xyz.mcxross.kaptos.Aptos +import xyz.mcxross.kaptos.account.Account +import xyz.mcxross.kaptos.extension.toStructTag +import xyz.mcxross.kaptos.model.* +import xyz.mcxross.kaptos.util.runBlocking + +const val FUNDING_AMOUNT = 100_000_000L +const val SEND_AMOUNT_APT = 0.5f +const val UNIT_CONVERSION = 100_000_000 +const val SEND_AMOUNT_UNITS = (SEND_AMOUNT_APT * UNIT_CONVERSION) +const val SEND_AMOUNT = 1_000_000UL + +/** + * This example demonstrates how to transfer APT from one account to another. + * + * Each run generates and creates new accounts on-chain using faucet funding. After funding, the APT + * balance of each account is printed; if funding fails, an error is thrown. + * + * Next, a transaction is constructed to send 0.5 APT from Alice to Bob. The transaction is then + * signed and submitted using the one-step `signAndSubmitTransaction` method. We wait for the + * transaction to complete and print the updated balances of Alice and Bob. If the transaction + * fails, an error is thrown. + */ +fun main() = runBlocking { + val aptos = Aptos(AptosConfig(AptosSettings(network = Network.TESTNET))) + + println("Generating Alice and Bob's accounts") + + val alice = Account.generate() + val bob = Account.generate() + + aptos.fundAccount(alice.accountAddress, FUNDING_AMOUNT).expect("Failed to fund Alice's account") + aptos.fundAccount(bob.accountAddress, FUNDING_AMOUNT).expect("Failed to fund Bob's account") + + println("Created accounts on chain") + println("Alice's balance: ${aptos.getAccountAPTAmount(alice.accountAddress)}") + println("Bob's balance: ${aptos.getAccountAPTAmount(bob.accountAddress)}") + println("=============================================") + println( + "Building transaction to send ${SEND_AMOUNT / 100_000_000u} APT to Bob: ${bob.accountAddress}" + ) + + val txn = + aptos.buildTransaction.simple( + sender = alice.accountAddress, + data = + entryFunctionData { + function = "0x1::coin::transfer" + typeArguments = typeArguments { + +TypeTagStruct(type = "0x1::aptos_coin::AptosCoin".toStructTag()) + } + functionArguments = functionArguments { + +MoveString(bob.accountAddress.toString()) + +U64(SEND_AMOUNT_UNITS.toULong()) + } + }, + ) + + // Sign and submit the transaction + val commitedTransaction = aptos.signAndSubmitTransaction(alice, txn) + + val executedTransaction = + aptos.waitForTransaction( + HexInput.fromString(commitedTransaction.expect("Transaction failed").hash) + ) + + println( + "Transaction wait response: $executedTransaction\n=============================================" + ) + + val aliceNewBalance = + aptos.getAccountAPTAmount(alice.accountAddress).expect("Alice's account does not exist") + val bobNewBalance = + aptos.getAccountAPTAmount(bob.accountAddress).expect("Bob's account does not exist") + + println("Alice's new balance: $aliceNewBalance") + println("Bob's new balance: $bobNewBalance") +} diff --git a/sample/jvmApp/src/main/kotlin/Transaction.kt b/sample/jvmApp/src/main/kotlin/Transaction.kt deleted file mode 100644 index cfc092a8..00000000 --- a/sample/jvmApp/src/main/kotlin/Transaction.kt +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright 2024 McXross - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package xyz.mcxross.kaptos.sample - -import xyz.mcxross.kaptos.Aptos -import xyz.mcxross.kaptos.account.Account -import xyz.mcxross.kaptos.extension.asAccountAddress -import xyz.mcxross.kaptos.extension.asPrivateKey -import xyz.mcxross.kaptos.extension.toStructTag -import xyz.mcxross.kaptos.model.* - -const val FUNDING_AMOUNT = 1_000_000_000L -const val SEND_AMOUNT = 100_000_000UL - -suspend fun transfer() { - val aptos = Aptos() - - // Let's create a private key from a hex string - val alicePrivateKey = - "0x1893cc5626444a03978480882de32faf9a820afb31b8075159dda107db1b470d".asPrivateKey() - - // And now that we have a private key, let's create an account from it - val aliceAccount = Account from alicePrivateKey - - // Assuming we have an account, let's check the balance - val aliceInitialBalance = - when (val aliceInitialBalance = aptos.getAccountAPTAmount(aliceAccount.accountAddress)) { - is Option.None -> throw IllegalStateException("Alice's account does not exist") - is Option.Some -> aliceInitialBalance.expect("Alice's account does not exist") - } - - println("Alice's initial balance: $aliceInitialBalance") - println( - "Bob's initial balance: ${aptos.getAccountAPTAmount("0x088698359f12ef2b19ba3bda04e129173d0672b5de8d77ce9e8eb0a149c23f04".asAccountAddress()).expect("Bob's account does not exist")}" - ) - println("=============================================") - - // Yes, we have an account, but let's see if we need to fund it - if (aliceInitialBalance < SEND_AMOUNT.toLong() + 1_000) { - aptos.fundAccount(aliceAccount.accountAddress, FUNDING_AMOUNT) - println( - "Alice's new balance after funding: ${aptos.getAccountAPTAmount(aliceAccount.accountAddress)}" - ) - } - - val bobAccountAddress = - "0x088698359f12ef2b19ba3bda04e129173d0672b5de8d77ce9e8eb0a149c23f04".asAccountAddress() - - println( - "Building transaction to send ${SEND_AMOUNT / 100_000_000u} APT to Bob: $bobAccountAddress" - ) - - val txn = - aptos.buildTransaction.simple( - sender = aliceAccount.accountAddress, - data = - inputEntryFunctionData { - function = "0x1::coin::transfer" - typeArguments = typeArguments { - +TypeTagStruct(type = "0x1::aptos_coin::AptosCoin".toStructTag()) - } - functionArguments = functionArguments { - +MoveString(bobAccountAddress.value) - +U64(SEND_AMOUNT) - } - }, - ) - - // Sign and submit the transaction - val commitedTransaction = aptos.signAndSubmitTransaction(aliceAccount, txn) - - val executedTransaction = - aptos.waitForTransaction( - HexInput.fromString(commitedTransaction.expect("Transaction failed").hash) - ) - - println("Transaction wait response: $executedTransaction") - println("=============================================") - println( - "Alice's new balance: ${aptos.getAccountAPTAmount(aliceAccount.accountAddress).expect("Alice's account does not exist")}" - ) - println( - "Bob's new balance: ${aptos.getAccountAPTAmount(bobAccountAddress).expect("Bob's account does not exist")}" - ) -}