Skip to content

Commit

Permalink
Get block example - Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
lealobanov committed Aug 15, 2024
1 parent 799fa4d commit fe68568
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.onflow.examples.java.get_block;

import org.onflow.flow.sdk.*;

public class GetBlockAccessAPIConnector {

private final FlowAccessApi accessAPI;

public GetBlockAccessAPIConnector(FlowAccessApi accessAPI) {
this.accessAPI = accessAPI;
}

public FlowBlock getLatestSealedBlock() {
boolean isSealed = true;
FlowAccessApi.AccessApiCallResponse<FlowBlock> response = accessAPI.getLatestBlock(isSealed);

if (response instanceof FlowAccessApi.AccessApiCallResponse.Success) {
return ((FlowAccessApi.AccessApiCallResponse.Success<FlowBlock>) response).getData();
} else {
FlowAccessApi.AccessApiCallResponse.Error errorResponse = (FlowAccessApi.AccessApiCallResponse.Error) response;
throw new RuntimeException(errorResponse.getMessage(), errorResponse.getThrowable());
}
}

public FlowBlock getBlockByID(FlowId blockID) {
FlowAccessApi.AccessApiCallResponse<FlowBlock> response = accessAPI.getBlockById(blockID);

if (response instanceof FlowAccessApi.AccessApiCallResponse.Success) {
return ((FlowAccessApi.AccessApiCallResponse.Success<FlowBlock>) response).getData();
} else {
FlowAccessApi.AccessApiCallResponse.Error errorResponse = (FlowAccessApi.AccessApiCallResponse.Error) response;
throw new RuntimeException(errorResponse.getMessage(), errorResponse.getThrowable());
}
}

public FlowBlock getBlockByHeight(long height) {
FlowAccessApi.AccessApiCallResponse<FlowBlock> response = accessAPI.getBlockByHeight(height);

if (response instanceof FlowAccessApi.AccessApiCallResponse.Success) {
return ((FlowAccessApi.AccessApiCallResponse.Success<FlowBlock>) response).getData();
} else {
FlowAccessApi.AccessApiCallResponse.Error errorResponse = (FlowAccessApi.AccessApiCallResponse.Error) response;
throw new RuntimeException(errorResponse.getMessage(), errorResponse.getThrowable());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.onflow.examples.java.get_block;

import org.junit.jupiter.api.Assertions;
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.FlowBlock;

import java.time.LocalDateTime;

@FlowEmulatorProjectTest(flowJsonLocation = "../flow/flow.json")
public class GetBlockAccessAPIConnectorTest {

@FlowTestClient
private FlowAccessApi accessAPI;

private GetBlockAccessAPIConnector blockAPIConnector;

@BeforeEach
public void setup() {
blockAPIConnector = new GetBlockAccessAPIConnector(accessAPI);
}

@Test
public void testCanFetchLatestSealedBlock() {
FlowBlock block = blockAPIConnector.getLatestSealedBlock();

Assertions.assertNotNull(block.getId(), "Block ID should not be null");
Assertions.assertTrue(block.getHeight() >= 0, "Block height should be non-negative");
Assertions.assertTrue(block.getTimestamp().isBefore(LocalDateTime.now()), "Block timestamp should be in the past");
}

@Test
public void testCanFetchBlockByID() {
FlowBlock latestBlock = blockAPIConnector.getLatestSealedBlock();
FlowBlock blockByID = blockAPIConnector.getBlockByID(latestBlock.getId());

Assertions.assertEquals(latestBlock.getId(), blockByID.getId(), "Block IDs should match");
Assertions.assertEquals(latestBlock.getHeight(), blockByID.getHeight(), "Block heights should match");
Assertions.assertEquals(latestBlock.getTimestamp(), blockByID.getTimestamp(), "Block timestamps should match");
Assertions.assertEquals(latestBlock.getCollectionGuarantees().size(), blockByID.getCollectionGuarantees().size(), "Block should have the same number of collection guarantees");
}

@Test
public void testCanFetchBlockByHeight() {
FlowBlock latestBlock = blockAPIConnector.getLatestSealedBlock();
FlowBlock blockByHeight = blockAPIConnector.getBlockByHeight(latestBlock.getHeight());

Assertions.assertEquals(latestBlock.getId(), blockByHeight.getId(), "Block ID fetched by height should match the latest block ID");
Assertions.assertEquals(latestBlock.getTimestamp(), blockByHeight.getTimestamp(), "Block timestamps should match for the latest block and the block fetched by height");
Assertions.assertEquals(latestBlock.getCollectionGuarantees().size(), blockByHeight.getCollectionGuarantees().size(), "Block should have the same number of collection guarantees");
}
}

0 comments on commit fe68568

Please sign in to comment.