diff --git a/lib/src/commonTest/kotlin/xyz/mcxross/kaptos/e2e/TransactionTest.kt b/lib/src/commonTest/kotlin/xyz/mcxross/kaptos/e2e/TransactionTest.kt index 8a4ba185..a665de77 100644 --- a/lib/src/commonTest/kotlin/xyz/mcxross/kaptos/e2e/TransactionTest.kt +++ b/lib/src/commonTest/kotlin/xyz/mcxross/kaptos/e2e/TransactionTest.kt @@ -19,27 +19,83 @@ package xyz.mcxross.kaptos.e2e import kotlin.test.Test import kotlin.test.assertTrue import xyz.mcxross.kaptos.Aptos +import xyz.mcxross.kaptos.account.Account import xyz.mcxross.kaptos.model.* +import xyz.mcxross.kaptos.unit.TRANSFER_AMOUNT +import xyz.mcxross.kaptos.util.FUND_AMOUNT import xyz.mcxross.kaptos.util.runBlocking class TransactionTest { + private val aptos = Aptos() + private val aptosLocal = Aptos(AptosConfig(AptosSettings(network = Network.LOCAL))) + @Test - fun testGetGasPriceEstimation() { - runBlocking { - val aptos = Aptos(AptosConfig(AptosSettings(network = Network.LOCAL))) - val response = aptos.getGasPriceEstimation() - assertTrue(response.gasEstimate > 0, "Gas estimate should be greater than 0") - } + fun testGetGasPriceEstimation() = runBlocking { + val response = aptosLocal.getGasPriceEstimation() + assertTrue(response.gasEstimate > 0, "Gas estimate should be greater than 0") } @Test fun `it queries for transactions on the chain`() = runBlocking { - val aptos = Aptos() val txns = aptos.getTransactions().expect("Couldn't retrieve transactions.") - assertTrue( - txns.isNotEmpty(), - "Transactions on the chain should have zero size of transactions.", - ) + assertTrue(txns.isNotEmpty(), "Chain should contain transactions.") + } + + @Test + fun `it queries for transactions by version`() = runBlocking { + val (alice, bob) = createAndFundAccounts() + val wait = submitAndWaitForTransaction(alice, bob) + + val respByVersion = + aptos + .getTransactionByVersion(wait.version.toLong()) + .expect("Could not retrieve txn by version.") + + assertTrue(respByVersion == wait) + } + + @Test + fun `it queries for transactions by hash`() = runBlocking { + val (alice, bob) = createAndFundAccounts() + val wait = submitAndWaitForTransaction(alice, bob) + + val resp = aptos.getTransactionByHash(wait.hash).expect("Unable to retrieve txn by hash.") + + assertTrue(resp == wait) + } + + @Test + fun `it fetches block data by block height`() = runBlocking { + val blockHeight = 1L + val blockData = aptos.getBlockByHeight(blockHeight).expect("Cannot fetch block data by height.") + assertTrue(blockData.blockHeight.toLong() == blockHeight) + } + + @Test + fun `it fetches block data by block version`() = runBlocking { + val blockVersion = 1L + val blockData = + aptos.getBlockByVersion(blockVersion).expect("Cannot fetch block data by version.") + assertTrue(blockData.blockHeight.toLong() == blockVersion) + } + + private suspend fun createAndFundAccounts(): Pair { + val alice = Account.generate() + val bob = Account.generate() + aptos.fundAccount(alice.accountAddress, FUND_AMOUNT) + return alice to bob + } + + private suspend fun submitAndWaitForTransaction( + alice: Account, + bob: Account, + ): UserTransactionResponse { + val txn = + aptos.transferCoinTransaction(alice.accountAddress, bob.accountAddress, TRANSFER_AMOUNT) + val sub = + aptos.signAndSubmitTransaction(alice, txn).expect("Could not sign and transfer transaction.") + return aptos.waitForTransaction(HexInput(sub.hash)).expect("Unable to wait for transaction.") + as UserTransactionResponse } }