-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from onflow/get-transaction-example
Examples use-cases - get transaction
- Loading branch information
Showing
16 changed files
with
628 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
java-example/src/main/java/org/onflow/examples/java/getEvent/GetEventAccessAPIConnector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package org.onflow.examples.java.getEvent; | ||
|
||
import kotlin.ranges.LongRange; | ||
import org.onflow.flow.sdk.*; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
public class GetEventAccessAPIConnector { | ||
private final FlowAccessApi accessAPI; | ||
|
||
public GetEventAccessAPIConnector(FlowAccessApi accessAPI) { | ||
this.accessAPI = accessAPI; | ||
} | ||
|
||
public List<FlowEventResult> getEventsForHeightRange(String eventType, long startHeight, long endHeight) { | ||
LongRange range = new LongRange(startHeight, endHeight); | ||
FlowAccessApi.AccessApiCallResponse<List<FlowEventResult>> response = accessAPI.getEventsForHeightRange(eventType, range); | ||
if (response instanceof FlowAccessApi.AccessApiCallResponse.Success) { | ||
return ((FlowAccessApi.AccessApiCallResponse.Success<List<FlowEventResult>>) response).getData(); | ||
} else if (response instanceof FlowAccessApi.AccessApiCallResponse.Error) { | ||
FlowAccessApi.AccessApiCallResponse.Error errorResponse = (FlowAccessApi.AccessApiCallResponse.Error) response; | ||
throw new RuntimeException(errorResponse.getMessage(), errorResponse.getThrowable()); | ||
} else { | ||
throw new RuntimeException("Unknown response type"); | ||
} | ||
} | ||
|
||
public List<FlowEventResult> getEventsForBlockIds(String eventType, List<FlowId> blockIds) { | ||
Set<FlowId> blockIdSet = new HashSet<>(blockIds); | ||
FlowAccessApi.AccessApiCallResponse<List<FlowEventResult>> response = accessAPI.getEventsForBlockIds(eventType, blockIdSet); | ||
if (response instanceof FlowAccessApi.AccessApiCallResponse.Success) { | ||
return ((FlowAccessApi.AccessApiCallResponse.Success<List<FlowEventResult>>) response).getData(); | ||
} else if (response instanceof FlowAccessApi.AccessApiCallResponse.Error errorResponse) { | ||
throw new RuntimeException(errorResponse.getMessage(), errorResponse.getThrowable()); | ||
} else { | ||
throw new RuntimeException("Unknown response type"); | ||
} | ||
} | ||
|
||
public FlowTransactionResult getTransactionResult(FlowId txID) { | ||
FlowAccessApi.AccessApiCallResponse<FlowTransactionResult> response = accessAPI.getTransactionResultById(txID); | ||
if (response instanceof FlowAccessApi.AccessApiCallResponse.Success) { | ||
return ((FlowAccessApi.AccessApiCallResponse.Success<FlowTransactionResult>) response).getData(); | ||
} else if (response instanceof FlowAccessApi.AccessApiCallResponse.Error errorResponse) { | ||
throw new RuntimeException(errorResponse.getMessage(), errorResponse.getThrowable()); | ||
} else { | ||
throw new RuntimeException("Unknown response type"); | ||
} | ||
} | ||
|
||
public List<FlowEventResult> getAccountCreatedEvents(long startHeight, long endHeight) { | ||
return getEventsForHeightRange("flow.AccountCreated", startHeight, endHeight); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...ava/org/onflow/examples/java/getNetworkParams/GetNetworkParametersAccessAPIConnector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.onflow.examples.java.getNetworkParams; | ||
|
||
import org.onflow.flow.sdk.FlowAccessApi; | ||
import org.onflow.flow.sdk.FlowChainId; | ||
|
||
public class GetNetworkParametersAccessAPIConnector { | ||
|
||
private final FlowAccessApi accessAPI; | ||
|
||
public GetNetworkParametersAccessAPIConnector(FlowAccessApi accessAPI) { | ||
this.accessAPI = accessAPI; | ||
} | ||
|
||
public FlowChainId getNetworkParameters() { | ||
FlowAccessApi.AccessApiCallResponse<FlowChainId> response = accessAPI.getNetworkParameters(); | ||
if (response instanceof FlowAccessApi.AccessApiCallResponse.Success) { | ||
return ((FlowAccessApi.AccessApiCallResponse.Success<FlowChainId>) response).getData(); | ||
} else { | ||
FlowAccessApi.AccessApiCallResponse.Error errorResponse = (FlowAccessApi.AccessApiCallResponse.Error) response; | ||
throw new RuntimeException(errorResponse.getMessage(), errorResponse.getThrowable()); | ||
} | ||
} | ||
} | ||
|
35 changes: 35 additions & 0 deletions
35
...c/main/java/org/onflow/examples/java/getTransaction/GetTransactionAccessAPIConnector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.onflow.examples.java.getTransaction; | ||
|
||
import org.onflow.flow.sdk.FlowAccessApi; | ||
import org.onflow.flow.sdk.FlowId; | ||
import org.onflow.flow.sdk.FlowTransaction; | ||
import org.onflow.flow.sdk.FlowTransactionResult; | ||
|
||
public class GetTransactionAccessAPIConnector { | ||
|
||
private final FlowAccessApi accessAPI; | ||
|
||
public GetTransactionAccessAPIConnector(FlowAccessApi accessAPI) { | ||
this.accessAPI = accessAPI; | ||
} | ||
|
||
public FlowTransaction getTransaction(FlowId txID) { | ||
FlowAccessApi.AccessApiCallResponse<FlowTransaction> response = accessAPI.getTransactionById(txID); | ||
if (response instanceof FlowAccessApi.AccessApiCallResponse.Success) { | ||
return ((FlowAccessApi.AccessApiCallResponse.Success<FlowTransaction>) response).getData(); | ||
} else { | ||
FlowAccessApi.AccessApiCallResponse.Error errorResponse = (FlowAccessApi.AccessApiCallResponse.Error) response; | ||
throw new RuntimeException(errorResponse.getMessage(), errorResponse.getThrowable()); | ||
} | ||
} | ||
|
||
public FlowTransactionResult getTransactionResult(FlowId txID) { | ||
FlowAccessApi.AccessApiCallResponse<FlowTransactionResult> response = accessAPI.getTransactionResultById(txID); | ||
if (response instanceof FlowAccessApi.AccessApiCallResponse.Success) { | ||
return ((FlowAccessApi.AccessApiCallResponse.Success<FlowTransactionResult>) response).getData(); | ||
} else { | ||
FlowAccessApi.AccessApiCallResponse.Error errorResponse = (FlowAccessApi.AccessApiCallResponse.Error) response; | ||
throw new RuntimeException(errorResponse.getMessage(), errorResponse.getThrowable()); | ||
} | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
...ample/src/test/java/org/onflow/examples/java/getEvent/GetEventAccessAPIConnectorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package org.onflow.examples.java.getEvent; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.onflow.examples.java.AccessAPIConnector; | ||
import org.onflow.flow.common.test.*; | ||
import org.onflow.flow.sdk.*; | ||
import org.onflow.flow.sdk.crypto.Crypto; | ||
import org.onflow.flow.sdk.crypto.PublicKey; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@FlowEmulatorProjectTest(flowJsonLocation = "../flow/flow.json") | ||
public class GetEventAccessAPIConnectorTest { | ||
@FlowServiceAccountCredentials | ||
private TestAccount serviceAccount; | ||
|
||
@FlowTestAccount | ||
private TestAccount testAccount; | ||
|
||
@FlowTestClient | ||
private FlowAccessApi accessAPI; | ||
|
||
private GetEventAccessAPIConnector connector; | ||
|
||
private FlowId txID; | ||
|
||
@BeforeEach | ||
void setup() { | ||
AccessAPIConnector accessAPIConnector = new AccessAPIConnector(serviceAccount.getPrivateKey(), accessAPI); | ||
connector = new GetEventAccessAPIConnector(accessAPI); | ||
|
||
// Send a sample transaction to create an account and capture the transaction ID | ||
PublicKey publicKey = Crypto.generateKeyPair(SignatureAlgorithm.ECDSA_P256).getPublic(); | ||
txID = accessAPIConnector.sendSampleTransaction( | ||
serviceAccount.getFlowAddress(), | ||
publicKey | ||
); | ||
} | ||
|
||
@Test | ||
void testGetEventsForHeightRange() { | ||
List<FlowEventResult> events = connector.getEventsForHeightRange("flow.AccountCreated", 0, 30); | ||
assertNotNull(events, "Events should not be null"); | ||
assertFalse(events.isEmpty(), "Expected account created events but found none."); | ||
assertEquals(3, events.size(), "Expected 3 account created events."); | ||
} | ||
|
||
@Test | ||
void testGetEventsForBlockIds() { | ||
FlowBlockHeader latestBlock; | ||
|
||
// Get the latest block header from the access API | ||
FlowAccessApi.AccessApiCallResponse<FlowBlockHeader> latestBlockResponse = accessAPI.getLatestBlockHeader(true); | ||
|
||
// Handle the response | ||
if (latestBlockResponse instanceof FlowAccessApi.AccessApiCallResponse.Success) { | ||
latestBlock = ((FlowAccessApi.AccessApiCallResponse.Success<FlowBlockHeader>) latestBlockResponse).getData(); | ||
} else if (latestBlockResponse instanceof FlowAccessApi.AccessApiCallResponse.Error errorResponse) { | ||
throw new RuntimeException(errorResponse.getMessage(), errorResponse.getThrowable()); | ||
} else { | ||
throw new RuntimeException("Unknown response type"); | ||
} | ||
|
||
List<FlowId> blockIds = List.of(latestBlock.getId()); | ||
List<FlowEventResult> events = connector.getEventsForBlockIds("flow.AccountCreated", blockIds); | ||
|
||
assertNotNull(events, "Events should not be null"); | ||
assertFalse(events.isEmpty(), "Expected events for the provided block IDs but found none."); | ||
assertEquals(1, events.size(), "Expected 1 account created event."); | ||
} | ||
|
||
@Test | ||
void testAccountCreatedEvents() { | ||
List<FlowEventResult> events = connector.getAccountCreatedEvents(0, 30); | ||
assertNotNull(events, "Events should not be null"); | ||
assertFalse(events.isEmpty(), "Expected account created events but found none."); | ||
for (FlowEventResult block : events) { | ||
for (FlowEvent event : block.getEvents()) { | ||
assertEquals("flow.AccountCreated", event.getType()); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
void testTransactionResultEvents() { | ||
FlowTransactionResult txResult = connector.getTransactionResult(txID); | ||
assertNotNull(txResult, "Transaction result should not be null"); | ||
assertFalse(txResult.getEvents().isEmpty(), "Expected events in transaction result but found none."); | ||
|
||
Set<String> expectedEventTypes = Set.of("Withdrawn", "TokensDeposited", "Deposited", "AccountCreated", "AccountKeyAdded", "TokensWithdrawn", "StorageCapabilityControllerIssued", "CapabilityPublished"); | ||
|
||
for (FlowEvent event : txResult.getEvents()) { | ||
String eventType = event.getType().split("\\.")[event.getType().split("\\.").length - 1]; | ||
assertTrue(expectedEventTypes.contains(eventType), "Unexpected event type: " + eventType); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...org/onflow/examples/java/getNetworkParams/GetNetworkParametersAccessAPIConnectorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package org.onflow.examples.java.getNetworkParams; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.onflow.flow.common.test.FlowEmulatorProjectTest; | ||
import org.onflow.flow.common.test.FlowTestClient; | ||
import org.onflow.flow.sdk.FlowAccessApi; | ||
import org.onflow.flow.sdk.FlowChainId; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@FlowEmulatorProjectTest(flowJsonLocation = "../flow/flow.json") | ||
public class GetNetworkParametersAccessAPIConnectorTest { | ||
|
||
@FlowTestClient | ||
private FlowAccessApi accessAPI; | ||
|
||
private GetNetworkParametersAccessAPIConnector networkParametersConnector; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
networkParametersConnector = new GetNetworkParametersAccessAPIConnector(accessAPI); | ||
} | ||
|
||
@Test | ||
public void canFetchNetworkParameters() { | ||
FlowChainId networkParams = networkParametersConnector.getNetworkParameters(); | ||
assertNotNull(networkParams, "Network parameters should not be null"); | ||
assertFalse(networkParams.getId().isEmpty(), "Network parameters should have a valid ID"); | ||
assertEquals(FlowChainId.EMULATOR, networkParams, "Network parameters should match EMULATOR"); | ||
assertEquals("flow-emulator", networkParams.getId(), "Network ID should be 'flow-emulator'"); | ||
} | ||
} |
Oops, something went wrong.