Add API GET /assets/transfers/{assetid}/{address}

This commit is contained in:
catbref 2019-03-25 11:55:03 +00:00
parent 2a45e27048
commit 0fd8ee3547
4 changed files with 102 additions and 5 deletions

View File

@ -623,6 +623,50 @@ public class AssetsResource {
} }
} }
@GET
@Path("/transfers/{assetid}/{address}")
@Operation(
summary = "Asset transfers for specific asset and address combination",
responses = {
@ApiResponse(
description = "Asset transactions",
content = @Content(
array = @ArraySchema(
schema = @Schema(
implementation = TransferAssetTransactionData.class
)
)
)
)
}
)
@ApiErrors({
ApiError.INVALID_ADDRESS, ApiError.INVALID_ASSET_ID, ApiError.REPOSITORY_ISSUE
})
public List<TransferAssetTransactionData> getAssetTransfers(@Parameter(
ref = "assetid"
) @PathParam("assetid") int assetId, @PathParam("address") String address, @Parameter(
ref = "limit"
) @QueryParam("limit") Integer limit, @Parameter(
ref = "offset"
) @QueryParam("offset") Integer offset, @Parameter(
ref = "reverse"
) @QueryParam("reverse") Boolean reverse) {
try (final Repository repository = RepositoryManager.getRepository()) {
if (!repository.getAssetRepository().assetExists(assetId))
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_ASSET_ID);
if (!Crypto.isValidAddress(address))
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_ADDRESS);
return repository.getTransactionRepository().getAssetTransfers(assetId, address, limit, offset, reverse);
} catch (ApiException e) {
throw e;
} catch (DataException e) {
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.REPOSITORY_ISSUE, e);
}
}
@POST @POST
@Path("/order/delete") @Path("/order/delete")
@Operation( @Operation(

View File

@ -10,9 +10,9 @@ import javax.xml.bind.annotation.XmlAccessorType;
public class PaymentData { public class PaymentData {
// Properties // Properties
protected String recipient; private String recipient;
protected long assetId; private long assetId;
protected BigDecimal amount; private BigDecimal amount;
// Constructors // Constructors

View File

@ -6,6 +6,7 @@ import java.util.Map;
import org.qora.api.resource.TransactionsResource.ConfirmationStatus; import org.qora.api.resource.TransactionsResource.ConfirmationStatus;
import org.qora.data.transaction.GroupApprovalTransactionData; import org.qora.data.transaction.GroupApprovalTransactionData;
import org.qora.data.transaction.TransactionData; import org.qora.data.transaction.TransactionData;
import org.qora.data.transaction.TransferAssetTransactionData;
import org.qora.transaction.Transaction.TransactionType; import org.qora.transaction.Transaction.TransactionType;
public interface TransactionRepository { public interface TransactionRepository {
@ -52,12 +53,26 @@ public interface TransactionRepository {
* Returns list of transactions relating to specific asset ID. * Returns list of transactions relating to specific asset ID.
* *
* @param assetId * @param assetId
* @param confirmationStatus
* @param limit * @param limit
* @param offset * @param offset
* @param reverse * @param reverse
* @return list of transactions, or empty if none * @return list of transactions, or empty if none
*/ */
public List<TransactionData> getAssetTransactions(int assetId, ConfirmationStatus confirmationStatus, Integer limit, Integer offset, Boolean reverse) public List<TransactionData> getAssetTransactions(long assetId, ConfirmationStatus confirmationStatus, Integer limit, Integer offset, Boolean reverse)
throws DataException;
/**
* Returns list of TRANSFER_ASSET transactions relating to specific asset ID/address combination.
*
* @param assetId
* @param address
* @param limit
* @param offset
* @param reverse
* @return list of transactions, or empty if none
*/
public List<TransferAssetTransactionData> getAssetTransfers(long assetId, String address, Integer limit, Integer offset, Boolean reverse)
throws DataException; throws DataException;
/** /**

View File

@ -22,6 +22,7 @@ import org.qora.api.resource.TransactionsResource.ConfirmationStatus;
import org.qora.data.PaymentData; import org.qora.data.PaymentData;
import org.qora.data.transaction.GroupApprovalTransactionData; import org.qora.data.transaction.GroupApprovalTransactionData;
import org.qora.data.transaction.TransactionData; import org.qora.data.transaction.TransactionData;
import org.qora.data.transaction.TransferAssetTransactionData;
import org.qora.group.Group; import org.qora.group.Group;
import org.qora.repository.DataException; import org.qora.repository.DataException;
import org.qora.repository.TransactionRepository; import org.qora.repository.TransactionRepository;
@ -446,7 +447,7 @@ public class HSQLDBTransactionRepository implements TransactionRepository {
} }
@Override @Override
public List<TransactionData> getAssetTransactions(int assetId, ConfirmationStatus confirmationStatus, Integer limit, Integer offset, Boolean reverse) public List<TransactionData> getAssetTransactions(long assetId, ConfirmationStatus confirmationStatus, Integer limit, Integer offset, Boolean reverse)
throws DataException { throws DataException {
TransactionType[] transactionTypes = new TransactionType[] { TransactionType[] transactionTypes = new TransactionType[] {
ISSUE_ASSET, TRANSFER_ASSET, CREATE_ASSET_ORDER, CANCEL_ASSET_ORDER ISSUE_ASSET, TRANSFER_ASSET, CREATE_ASSET_ORDER, CANCEL_ASSET_ORDER
@ -531,6 +532,43 @@ public class HSQLDBTransactionRepository implements TransactionRepository {
} }
} }
@Override
public List<TransferAssetTransactionData> getAssetTransfers(long assetId, String address, Integer limit, Integer offset, Boolean reverse)
throws DataException {
String sql = "SELECT creation, tx_group_id, reference, fee, signature, sender, recipient, amount FROM TransferAssetTransactions "
+ "JOIN Transactions USING (signature) "
+ "JOIN Accounts ON public_key = sender "
+ "WHERE asset_id = ? AND ? IN (account, recipient) "
+ "ORDER by creation ";
sql += (reverse == null || !reverse) ? "ASC" : "DESC";
sql += HSQLDBRepository.limitOffsetSql(limit, offset);
List<TransferAssetTransactionData> assetTransfers = new ArrayList<>();
try (ResultSet resultSet = this.repository.checkedExecute(sql, assetId, address)) {
if (resultSet == null)
return assetTransfers;
do {
long timestamp = resultSet.getTimestamp(1, Calendar.getInstance(HSQLDBRepository.UTC)).getTime();
int txGroupId = resultSet.getInt(2);
byte[] reference = resultSet.getBytes(3);
BigDecimal fee = resultSet.getBigDecimal(4).setScale(8);
byte[] signature = resultSet.getBytes(5);
byte[] creatorPublicKey = resultSet.getBytes(6);
String recipient = resultSet.getString(7);
BigDecimal amount = resultSet.getBigDecimal(8);
assetTransfers.add(new TransferAssetTransactionData(timestamp, txGroupId, reference, creatorPublicKey, recipient, amount, assetId, fee, signature));
} while (resultSet.next());
return assetTransfers;
} catch (SQLException e) {
throw new DataException("Unable to fetch asset-transfer transactions from repository", e);
}
}
@Override @Override
public List<TransactionData> getPendingTransactions(Integer txGroupId, Integer limit, Integer offset, Boolean reverse) throws DataException { public List<TransactionData> getPendingTransactions(Integer txGroupId, Integer limit, Integer offset, Boolean reverse) throws DataException {
String[] txTypesNeedingApproval = stream(Transaction.TransactionType.values()) String[] txTypesNeedingApproval = stream(Transaction.TransactionType.values())