From bb31d4afaef9a9ea6b4ee77187834c49c1e72d19 Mon Sep 17 00:00:00 2001 From: dudiedri Date: Wed, 23 Sep 2026 13:10:34 +0300 Subject: [PATCH] feat(transaction): expose valid_contract and index Nexus began serving both fields on GET /api/transactions/{hash} in Gero-Labs/nexus#1081, verified live on mainnet against tx 4847714e...c6710d25 ("valid_contract": true, "index": 5, both top-level). valid_contract needs an explicit @JsonProperty because the client sets no global naming strategy - the server emits mixed naming (txHash camelCase, block_hash snake_case) - while index binds by field name. valid_contract is transaction-level and distinct from TxPlutusContract.validContract, which is per-script-execution. Both are boxed, so a provider that does not report them leaves null rather than defaulting to false/0. These are what let upstream cardano-client-lib's NexusTransactionService replace its two hardcoded nulls (tc.setIndex(null) / tc.setValidContract(null)). 3 tests: value mapping, index 0 not read as absent, and both staying null when the payload omits them. Co-Authored-By: Claude Opus 5 --- .../api/transaction/model/Transaction.java | 13 ++++++ .../transaction/TransactionServiceTest.java | 41 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/src/main/java/adlabs/nexus/client/backend/api/transaction/model/Transaction.java b/src/main/java/adlabs/nexus/client/backend/api/transaction/model/Transaction.java index 26275bc..5e97161 100644 --- a/src/main/java/adlabs/nexus/client/backend/api/transaction/model/Transaction.java +++ b/src/main/java/adlabs/nexus/client/backend/api/transaction/model/Transaction.java @@ -26,6 +26,19 @@ public class Transaction { private String totalOutput; private String fee; private String deposit; + /** + * Whether the transaction's Plutus scripts all succeeded. Null where the provider does + * not report it. Distinct from {@link TxPlutusContract#getValidContract()}, which is + * per-script-execution; this one is transaction-level. + */ + @JsonProperty("valid_contract") + private Boolean validContract; + /** + * Zero-based position of the transaction within its block, so it can be identified by + * (block, index) rather than by hash alone. Null where the provider does not report it. + * Serialized as {@code index}, not snake_case, so no @JsonProperty is needed. + */ + private Integer index; @JsonProperty("invalid_before") private String invalidBefore; @JsonProperty("invalid_after") diff --git a/src/test/java/adlabs/nexus/client/backend/api/transaction/TransactionServiceTest.java b/src/test/java/adlabs/nexus/client/backend/api/transaction/TransactionServiceTest.java index 3cbd88a..f9853cf 100644 --- a/src/test/java/adlabs/nexus/client/backend/api/transaction/TransactionServiceTest.java +++ b/src/test/java/adlabs/nexus/client/backend/api/transaction/TransactionServiceTest.java @@ -76,4 +76,45 @@ void getTransactionsCbor_postsHashArray() throws Exception { assertEquals("/api/transactions/cbor?network=cardano-mainnet", req.getPath()); assertEquals("[\"h1\",\"h2\"]", req.getBody().readUtf8()); } + + @Test + void getTransaction_mapsValidContractAndIndex() throws Exception { + // Shape verified against mainnet tx 4847714e...c6710d25: both fields are top-level, + // valid_contract is snake_case, index is not. + server.enqueue(new MockResponse().setResponseCode(200).setBody( + "{\"txHash\":\"tx1\",\"valid_contract\":true,\"index\":5,\"fee\":\"170000\"}")); + + Result result = service.getTransaction(Network.MAINNET, "tx1"); + + assertTrue(result.isSuccessful()); + assertEquals(Boolean.TRUE, result.getValue().getValidContract()); + assertEquals(Integer.valueOf(5), result.getValue().getIndex()); + } + + @Test + void getTransaction_indexZeroIsNotReadAsAbsent() throws Exception { + // index 0 is the first transaction in a block, not a missing value. + server.enqueue(new MockResponse().setResponseCode(200).setBody( + "{\"txHash\":\"tx1\",\"valid_contract\":false,\"index\":0}")); + + Result result = service.getTransaction(Network.MAINNET, "tx1"); + + assertTrue(result.isSuccessful()); + assertEquals(Integer.valueOf(0), result.getValue().getIndex()); + assertEquals(Boolean.FALSE, result.getValue().getValidContract()); + } + + @Test + void getTransaction_absentValidContractAndIndexStayNull() throws Exception { + // Providers that do not report them (and networks with no yaci datasource for the + // index enrichment) omit both; they must not default to false/0. + server.enqueue(new MockResponse().setResponseCode(200).setBody( + "{\"txHash\":\"tx1\",\"fee\":\"170000\"}")); + + Result result = service.getTransaction(Network.MAINNET, "tx1"); + + assertTrue(result.isSuccessful()); + assertNull(result.getValue().getValidContract()); + assertNull(result.getValue().getIndex()); + } }