Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull latest changes from the-nft-company/flow-jvm-sdk #5

Merged
merged 9 commits into from
Jan 26, 2024
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

val javaTargetVersion = "1.8"
val defaultGroupId = "com.nftco"
val defaultVersion = "0.7.1-SNAPSHOT"
val defaultVersion = "0.7.4-SNAPSHOT"

// other variables

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/nftco/flow/sdk/AsyncFlowAccessApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface AsyncFlowAccessApi {

fun ping(): CompletableFuture<Unit>

fun getLatestBlockHeader(): CompletableFuture<FlowBlockHeader>
fun getLatestBlockHeader(sealed: Boolean = true): CompletableFuture<FlowBlockHeader>

fun getBlockHeaderById(id: FlowId): CompletableFuture<FlowBlockHeader?>

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/nftco/flow/sdk/FlowAccessApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interface FlowAccessApi {

fun ping()

fun getLatestBlockHeader(): FlowBlockHeader
fun getLatestBlockHeader(sealed: Boolean = true): FlowBlockHeader

fun getBlockHeaderById(id: FlowId): FlowBlockHeader?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ class AsyncFlowAccessApiImpl(
}
}

override fun getLatestBlockHeader(): CompletableFuture<FlowBlockHeader> {
override fun getLatestBlockHeader(sealed: Boolean): CompletableFuture<FlowBlockHeader> {
return completableFuture(
api.getLatestBlockHeader(
Access.GetLatestBlockHeaderRequest.newBuilder()
.setIsSealed(sealed)
.build()
)
).thenApply {
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/com/nftco/flow/sdk/impl/FlowAccessApiImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ class FlowAccessApiImpl(
)
}

override fun getLatestBlockHeader(): FlowBlockHeader {
override fun getLatestBlockHeader(sealed: Boolean): FlowBlockHeader {
val ret = api.getLatestBlockHeader(
Access.GetLatestBlockHeaderRequest.newBuilder()
.setIsSealed(sealed)
.build()
)
return FlowBlockHeader.of(ret.block)
Expand Down
4 changes: 2 additions & 2 deletions src/test/kotlin/com/nftco/flow/sdk/JsonCadenceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class JsonCadenceTest {
@Test
fun `Can parse new JSON Cadence`() {
val flow = TestUtils.newMainnetAccessApi()
val tx = flow.getTransactionResultById(FlowId("663869d910278d7b6caf793396f6f2c5b91aace7180c2c70cfb3b0b6efd7a049"))!!
val tx = flow.getTransactionResultById(FlowId("273f68ffe175a0097db60bc7cf5e92c5a775d189af3f5636f5432c1206be771a"))!!
val events = tx.events.map { it.payload.jsonCadence }
Assertions.assertThat(events).hasSize(4)
Assertions.assertThat(events).hasSize(7)
}
}
25 changes: 23 additions & 2 deletions src/test/kotlin/com/nftco/flow/sdk/TestExtensionsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,25 @@ class TestExtensionsTest {
@FlowTestAccount
lateinit var account4: TestAccount

@FlowTestAccount(
signAlgo = SignatureAlgorithm.ECDSA_SECP256k1,
balance = 420.0,
contracts = [
FlowTestContractDeployment(
name = "ContractInterface",
code = "pub contract interface ContractInterface { }"
),
FlowTestContractDeployment(
name = "ContractSuccessor",
code = """
import ContractInterface from 0xCONTRACTINTERFACE
pub contract ContractSuccessor : ContractInterface { init() { } }
"""
),
]
)
lateinit var account5: TestAccount

@Test
fun `Test extensions work`() {
accessAPI.ping()
Expand All @@ -90,14 +109,16 @@ class TestExtensionsTest {
assertTrue(account2.isValid)
assertTrue(account3.isValid)
assertTrue(account4.isValid)
assertTrue(account5.isValid)

val addresses = setOf(
account0.flowAddress,
account1.flowAddress,
account2.flowAddress,
account3.flowAddress,
account4.flowAddress
account4.flowAddress,
account5.flowAddress,
)
assertEquals(5, addresses.size)
assertEquals(6, addresses.size)
}
}
32 changes: 18 additions & 14 deletions src/test/kotlin/com/nftco/flow/sdk/TransactionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -107,23 +107,27 @@ class TransactionTest {
fun `Can parse events`() {
val accessApi = TestUtils.newMainnetAccessApi()

// https://flowscan.org/transaction/5e6ef76c524dd131bbab5f9965493b7830bb784561ca6391b320ec60fa5c395e
val tx = accessApi.getTransactionById(FlowId("5e6ef76c524dd131bbab5f9965493b7830bb784561ca6391b320ec60fa5c395e"))
// https://flowscan.org/transaction/8c2e9d37a063240f236aa181e1454eb62991b42302534d4d6dd3839c2df0ef14
val tx = accessApi.getTransactionById(FlowId("8c2e9d37a063240f236aa181e1454eb62991b42302534d4d6dd3839c2df0ef14"))
assertThat(tx).isNotNull

val results = accessApi.getTransactionResultById(FlowId("5e6ef76c524dd131bbab5f9965493b7830bb784561ca6391b320ec60fa5c395e"))!!
assertThat(results.events).hasSize(4)
val results = accessApi.getTransactionResultById(FlowId("8c2e9d37a063240f236aa181e1454eb62991b42302534d4d6dd3839c2df0ef14"))!!
assertThat(results.events).hasSize(12)
assertThat(results.events[0].event.id).isEqualTo("A.0b2a3299cc857e29.TopShot.Withdraw")
assertThat(results.events[1].event.id).isEqualTo("A.c1e4f4f4c4257510.Market.CutPercentageChanged")
assertThat(results.events[2].event.id).isEqualTo("A.0b2a3299cc857e29.TopShot.Deposit")

assertThat(results.events[3].event.id).isEqualTo("A.c1e4f4f4c4257510.Market.MomentListed")
assertThat("id" in results.events[3].event).isTrue
assertThat("price" in results.events[3].event).isTrue
assertThat("seller" in results.events[3].event).isTrue
assertThat("id" in results.events[3].event.value!!).isTrue
assertThat("price" in results.events[3].event.value!!).isTrue
assertThat("seller" in results.events[3].event.value!!).isTrue
assertThat(results.events[1].event.id).isEqualTo("A.0b2a3299cc857e29.TopShot.Deposit")
assertThat(results.events[2].event.id).isEqualTo("A.ead892083b3e2c6c.DapperUtilityCoin.TokensWithdrawn")

assertThat("from" in results.events[2].event).isTrue
assertThat("amount" in results.events[2].event).isTrue

// TODO - fix this test complaining com.fasterxml.jackson.databind.exc.MismatchedInputException: Unknown CadenceType kind: Restriction
franklywatson marked this conversation as resolved.
Show resolved Hide resolved
// assertThat(results.events[8].event.id).isEqualTo("A.b8ea91944fd51c43.OffersV2.OfferCompleted")
// assertThat("nftId" in results.events[8].event).isTrue
// assertThat("nftType" in results.events[8].event).isTrue
// assertThat("offerId" in results.events[8].event).isTrue
// assertThat("offerType" in results.events[8].event.value!!).isTrue
// assertThat("royalties" in results.events[8].event.value!!).isTrue
// assertThat("offerAddress" in results.events[8].event.value!!).isTrue
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ annotation class FlowTestAccount(
@API(status = API.Status.STABLE, since = "5.0")
annotation class FlowTestContractDeployment(
val name: String,
val alias: String = "",
val addToRegistry: Boolean = true,
val code: String = "",
val codeClasspathLocation: String = "",
Expand Down Expand Up @@ -159,6 +160,7 @@ abstract class AbstractFlowEmulatorExtension : BeforeEachCallback, AfterEachCall
override fun beforeEach(context: ExtensionContext) {

Flow.configureDefaults(chainId = FlowChainId.EMULATOR)
Flow.DEFAULT_ADDRESS_REGISTRY.defaultChainId = FlowChainId.EMULATOR

val emulator = launchEmulator(context)
this.process = emulator.process
Expand Down Expand Up @@ -263,7 +265,8 @@ abstract class AbstractFlowEmulatorExtension : BeforeEachCallback, AfterEachCall
.throwOnError()

if (deployable.addToRegistry) {
Flow.DEFAULT_ADDRESS_REGISTRY.register(deployable.name, testAccount.flowAddress, FlowChainId.EMULATOR)
val alias = deployable.alias.ifEmpty { "0x${deployable.name.uppercase()}" }
Flow.DEFAULT_ADDRESS_REGISTRY.register(alias, testAccount.flowAddress, FlowChainId.EMULATOR)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ class FlowEmulatorTestExtension : AbstractFlowEmulatorExtension() {

val args = """
--verbose --grpc-debug
--service-priv-key=${serviceKeyPair.private.hex}
--service-pub-key=${serviceKeyPair.public.hex}
--service-priv-key=${serviceKeyPair.private.hex.replace(Regex("^(00)+"), "")}
--service-sig-algo=${config.signAlgo.name.uppercase()}
--service-hash-algo=${config.hashAlgo.name.uppercase()}
""".trimIndent()
Expand Down
Loading