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

Feat/addr Add get transactions by address endpoint #369

Merged
merged 3 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions api-tests/address.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
###
### Get utxos by address
GET {{base_url}}/api/v1/addresses/addr_test1wppg9l6relcpls4u667twqyggkrpfrs5cdge9hhl9cv2upchtch0h/utxos?count=10&page=0&order=desc

> {%
client.test("Get utxos by address", function() {
client.assert(response.status === 200, "Response status is not 200");
client.assert(response.body.length === 10, "No of returned utxos is not 10: " + response.body.length)
});
%}

###
### Get transactions by address and asset
GET {{base_url}}/api/v1/addresses/addr_test1wppg9l6relcpls4u667twqyggkrpfrs5cdge9hhl9cv2upchtch0h/utxos/c2ecc337337cf48720e3747c833c9c179e08d2ea235d9bee7afbcb1741555448?count=2&page=0&order=desc

> {%
client.test("Get utxos by address and asset", function() {
client.assert(response.status === 200, "Response status is not 200");
client.assert(response.body.length === 2, "No of returned utxos is not 2: " + response.body.length)
});
%}

###
### Get transactions by address
GET {{base_url}}/api/v1/addresses/addr_test1wppg9l6relcpls4u667twqyggkrpfrs5cdge9hhl9cv2upchtch0h/transactions?count=10&page=0&order=desc

> {%
client.test("Get transactions by address", function() {
client.assert(response.status === 200, "Response status is not 200");
client.assert(response.body.length === 10, "No of returned transactions is not 10: " + response.body.length)
});
%}

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.bloxbean.cardano.yaci.store.common.domain.TxInput;
import com.bloxbean.cardano.yaci.store.common.domain.UtxoKey;
import com.bloxbean.cardano.yaci.store.common.model.Order;
import com.bloxbean.cardano.yaci.store.utxo.domain.AddressTransaction;
import com.bloxbean.cardano.yaci.store.utxo.storage.UtxoStorageReader;
import com.bloxbean.rocks.types.collection.RocksMap;
import com.bloxbean.rocks.types.collection.RocksMultiZSet;
Expand Down Expand Up @@ -62,6 +63,11 @@ public List<AddressUtxo> findAllByIds(List<UtxoKey> utxoKeys) {
.toList());
}

@Override
public List<AddressTransaction> findTransactionsByAddress(String address, int page, int count, Order order) {
throw new UnsupportedOperationException("Not implemented");
}

@SneakyThrows
@Override
public List<AddressUtxo> findUtxoByAddress(String address, int page, int count, Order order) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.bloxbean.cardano.yaci.store.api.utxo.service.AddressService;
import com.bloxbean.cardano.yaci.store.common.domain.Utxo;
import com.bloxbean.cardano.yaci.store.common.model.Order;
import com.bloxbean.cardano.yaci.store.utxo.domain.AddressTransaction;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.Max;
Expand Down Expand Up @@ -30,7 +31,7 @@ public AddressController(AddressService addressService) {
@GetMapping("{address}/utxos")
@Operation(summary = "Get UTxOs for an address or address verification key hash (addr_vkh). If the address is a stake address, it will return UTXOs for all base addresses associated with the stake address")
public List<Utxo> getUtxos(@PathVariable String address, @RequestParam(required = false, defaultValue = "10") @Min(1) @Max(100) int count,
@RequestParam(required = false, defaultValue = "0") @Min(0) int page, @RequestParam(required = false, defaultValue = "asc") Order order) {
@RequestParam(required = false, defaultValue = "0") @Min(0) int page, @RequestParam(required = false, defaultValue = "desc") Order order) {
//TODO -- Fix pagination index
int p = page;
if (p > 0)
Expand All @@ -48,7 +49,7 @@ public List<Utxo> getUtxos(@PathVariable String address, @RequestParam(required
@GetMapping("{address}/utxos/{asset}")
@Operation(summary = "Get UTxOs for an address or address verification key hash (addr_vkh) for a specific asset. If the address is a stake address, it will return UTXOs for all base addresses associated with the stake address")
public List<Utxo> getUtxosForAsset(@PathVariable String address, @PathVariable String asset, @RequestParam(required = false, defaultValue = "10") @Min(1) @Max(100) int count,
@RequestParam(required = false, defaultValue = "0") @Min(0) int page, @RequestParam(required = false, defaultValue = "asc") Order order) {
@RequestParam(required = false, defaultValue = "0") @Min(0) int page, @RequestParam(required = false, defaultValue = "desc") Order order) {
//TODO -- Fix pagination index
int p = page;
if (p > 0)
Expand All @@ -62,4 +63,16 @@ public List<Utxo> getUtxosForAsset(@PathVariable String address, @PathVariable S
return addressService.getUtxoByAddressAndAsset(address, asset, p, count, order);
}
}

@GetMapping("{address}/transactions")
@Operation(summary = "Get transactions for an address (Base address or Payment address) starting from the latest")
public List<AddressTransaction> getAddressTransactions(@PathVariable String address, @RequestParam(required = false, defaultValue = "10") @Min(1) @Max(100) int count,
@RequestParam(required = false, defaultValue = "0") @Min(0) int page, @RequestParam(required = false, defaultValue = "desc") Order order) {
//TODO -- Fix pagination index
int p = page;
if (p > 0)
p = p - 1;

return addressService.getTransactionsByAddress(address, p, count, order);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.bloxbean.cardano.client.util.HexUtil;
import com.bloxbean.cardano.yaci.store.common.domain.Utxo;
import com.bloxbean.cardano.yaci.store.common.model.Order;
import com.bloxbean.cardano.yaci.store.utxo.domain.AddressTransaction;
import com.bloxbean.cardano.yaci.store.utxo.storage.UtxoStorageReader;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -63,6 +64,10 @@ public List<Utxo> getUtxoByStakeAddressAndAsset(@NonNull String stakeAddress, St
.map(UtxoUtil::addressUtxoToUtxo).collect(Collectors.toList());
}

public List<AddressTransaction> getTransactionsByAddress(@NonNull String address, int page, int count, Order order) {
return utxoStorage.findTransactionsByAddress(address, page, count, order);
}

private static String getPaymentCredential(String address) {
String paymentCredential;
if (address.startsWith(ADDR_VKEY_HASH_PREFIX)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.bloxbean.cardano.yaci.store.utxo.domain;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

@Data
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder(toBuilder = true)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class AddressTransaction {
private String txHash;
private Long blockNumber;
private Long blockTime;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.bloxbean.cardano.yaci.store.common.domain.AddressUtxo;
import com.bloxbean.cardano.yaci.store.common.domain.UtxoKey;
import com.bloxbean.cardano.yaci.store.common.model.Order;
import com.bloxbean.cardano.yaci.store.utxo.domain.AddressTransaction;

import java.util.List;
import java.util.Optional;
Expand All @@ -18,4 +19,6 @@ public interface UtxoStorageReader {
List<AddressUtxo> findUtxoByStakeAddress(String stakeAddress, int page, int count, Order order);
List<AddressUtxo> findUtxoByStakeAddressAndAsset(String stakeAddress, String unit, int page, int count, Order order);
List<AddressUtxo> findAllByIds(List<UtxoKey> utxoKeys);

List<AddressTransaction> findTransactionsByAddress(String address, int page, int count, Order order);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import com.bloxbean.cardano.yaci.store.common.domain.AddressUtxo;
import com.bloxbean.cardano.yaci.store.common.domain.UtxoKey;
import com.bloxbean.cardano.yaci.store.common.model.Order;
import com.bloxbean.cardano.yaci.store.utxo.domain.AddressTransaction;
import com.bloxbean.cardano.yaci.store.utxo.storage.UtxoStorageReader;
import com.bloxbean.cardano.yaci.store.utxo.storage.impl.mapper.UtxoMapper;
import com.bloxbean.cardano.yaci.store.utxo.storage.impl.model.UtxoId;
import com.bloxbean.cardano.yaci.store.utxo.storage.impl.repository.UtxoRepository;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.jooq.DSLContext;
import org.jooq.*;
import org.jooq.impl.DSL;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
Expand Down Expand Up @@ -160,6 +162,45 @@ public List<AddressUtxo> findAllByIds(List<UtxoKey> utxoKeys) {
.toList();
}

@Override
public List<AddressTransaction> findTransactionsByAddress(String address, int page, int count, Order order) {
Pageable pageable = getPageable(page, count, order);

// Define the CTEs using JOOQ
Select<Record3<String, Long, Long>> addressUtxoTx = dsl
.select(ADDRESS_UTXO.TX_HASH, ADDRESS_UTXO.BLOCK, ADDRESS_UTXO.BLOCK_TIME)
.from(ADDRESS_UTXO)
.where(ADDRESS_UTXO.OWNER_ADDR.eq(address));

Select<Record3<String, Long, Long>> spentTx = dsl
.select(TX_INPUT.SPENT_TX_HASH.as("tx_hash"), TX_INPUT.SPENT_AT_BLOCK.as("block"), TX_INPUT.SPENT_BLOCK_TIME.as("block_time"))
.from(TX_INPUT)
.join(ADDRESS_UTXO)
.on(TX_INPUT.TX_HASH.eq(ADDRESS_UTXO.TX_HASH))
.and(TX_INPUT.OUTPUT_INDEX.eq(ADDRESS_UTXO.OUTPUT_INDEX))
.where(ADDRESS_UTXO.OWNER_ADDR.eq(address));

// Use union to combine both CTEs
Select<?> combinedTx = addressUtxoTx
.union(spentTx);

// Fetch distinct tx_hash results with pagination and order by block in descending order
List<AddressTransaction> result = dsl
.selectDistinct(
DSL.field("tx_hash", String.class).as("txHash"),
DSL.field("block", Long.class).as("blockNumber"),
DSL.field("block_time", Long.class).as("blockTime")
)
.from(combinedTx.asTable("combined_tx"))
.orderBy(order.equals(Order.desc) ? DSL.field("block").desc() : DSL.field("block").asc())
.limit(pageable.getPageSize())
.offset((int) pageable.getOffset())
.fetchInto(AddressTransaction.class);

return result;
}


private static PageRequest getPageable(int page, int count, Order order) {
return PageRequest.of(page, count)
.withSort(order.equals(Order.desc) ? Sort.Direction.DESC : Sort.Direction.ASC, "slot", "txHash", "outputIndex");
Expand Down
Loading