Search in sources :

Example 1 with BitfinexDepositWithdrawalHistoryResponse

use of org.knowm.xchange.bitfinex.v1.dto.account.BitfinexDepositWithdrawalHistoryResponse in project XChange by knowm.

the class BitfinexAdapters method adaptFundingHistory.

public static List<FundingRecord> adaptFundingHistory(BitfinexDepositWithdrawalHistoryResponse[] bitfinexDepositWithdrawalHistoryResponses) {
    final List<FundingRecord> fundingRecords = new ArrayList<>();
    for (BitfinexDepositWithdrawalHistoryResponse responseEntry : bitfinexDepositWithdrawalHistoryResponses) {
        String address = responseEntry.getAddress();
        String description = responseEntry.getDescription();
        Currency currency = Currency.getInstance(responseEntry.getCurrency());
        FundingRecord.Status status = FundingRecord.Status.resolveStatus(responseEntry.getStatus());
        if (status == null && responseEntry.getStatus().equalsIgnoreCase(// there's a spelling mistake in the protocol
        "CANCELED"))
            status = FundingRecord.Status.CANCELLED;
        String txnId = null;
        if (status == null || !status.equals(FundingRecord.Status.CANCELLED)) {
            /*
        sometimes the description looks like this (with the txn hash in it):
        "description":"a9d387cf5d9df58ff2ac4a338e0f050fd3857cf78d1dbca4f33619dc4ccdac82","address":"1Enx...

        and sometimes like this (with the address in it as well as the txn hash):
        "description":"3AXVnDapuRiAn73pjKe7gukLSx5813oFyn, txid: aa4057486d5f73747167beb9949a0dfe17b5fc630499a66af075abdaf4986987","address":"3AX...

        and sometimes when cancelled
        "description":"3LFVTLFZoDDzLCcLGDDQ7MNkk4YPe26Yva, expired","address":"3LFV...
         */
            String cleanedDescription = description.replace(",", "").replace("txid:", "").trim().toLowerCase();
            // Address will only be present for crypto payments. It will be null for all fiat payments
            if (address != null) {
                cleanedDescription = cleanedDescription.replace(address.toLowerCase(), "").trim();
            }
            // check its just some hex characters, and if so lets assume its the txn hash
            if (cleanedDescription.matches("^(0x)?[0-9a-f]+$")) {
                txnId = cleanedDescription;
            }
        }
        FundingRecord fundingRecordEntry = new FundingRecord(address, responseEntry.getTimestamp(), currency, responseEntry.getAmount(), String.valueOf(responseEntry.getId()), txnId, responseEntry.getType(), status, null, null, description);
        fundingRecords.add(fundingRecordEntry);
    }
    return fundingRecords;
}
Also used : FundingRecord(org.knowm.xchange.dto.account.FundingRecord) Currency(org.knowm.xchange.currency.Currency) BitfinexTickerFundingCurrency(org.knowm.xchange.bitfinex.v2.dto.marketdata.BitfinexTickerFundingCurrency) ArrayList(java.util.ArrayList) BitfinexDepositWithdrawalHistoryResponse(org.knowm.xchange.bitfinex.v1.dto.account.BitfinexDepositWithdrawalHistoryResponse)

Example 2 with BitfinexDepositWithdrawalHistoryResponse

use of org.knowm.xchange.bitfinex.v1.dto.account.BitfinexDepositWithdrawalHistoryResponse in project XChange by knowm.

the class BitfinexAdaptersTest method testAdaptFundingHistory.

@Test
public void testAdaptFundingHistory() throws IOException {
    // Read in the JSON from the example resources
    InputStream is = BitfinexAdaptersTest.class.getResourceAsStream("/org/knowm/xchange/bitfinex/v1/dto/account/example-deposit-withdrawal-info-data.json");
    // Use Jackson to parse it
    ObjectMapper mapper = new ObjectMapper();
    BitfinexDepositWithdrawalHistoryResponse[] response = mapper.readValue(is, BitfinexDepositWithdrawalHistoryResponse[].class);
    List<FundingRecord> fundingRecords = BitfinexAdapters.adaptFundingHistory(response);
    for (FundingRecord record : fundingRecords) {
        if (record.getType().name().equalsIgnoreCase(FundingRecord.Type.DEPOSIT.name())) {
            assertThat(record.getStatus()).isEqualTo(FundingRecord.Status.PROCESSING);
            assertEquals(new BigDecimal("0.01"), record.getAmount());
            assertEquals("jlsd98087sdfkjldsflj432kjlsdf8", record.getAddress());
            assertEquals(null, record.getBlockchainTransactionHash());
            assertEquals(Currency.BTC, record.getCurrency());
        } else {
            assertThat(record.getStatus()).isEqualTo(FundingRecord.Status.COMPLETE);
            assertEquals(new BigDecimal("0.07"), record.getAmount());
            assertEquals("3QXYWgRGX2BPYBpUDBssGbeWEa5zq6snBZ", record.getAddress());
            assertEquals("3QXYWgRGX2BPYBpUDBssGbeWEa5zq6snBZ, txid: offchain transfer", record.getDescription());
            assertEquals(null, record.getBlockchainTransactionHash());
            assertEquals("3QXYWgRGX2BPYBpUDBssGbeWEa5zq6snBZ, txid: offchain transfer", record.getDescription());
            assertEquals(Currency.BTC, record.getCurrency());
        }
    }
}
Also used : InputStream(java.io.InputStream) FundingRecord(org.knowm.xchange.dto.account.FundingRecord) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) BitfinexDepositWithdrawalHistoryResponse(org.knowm.xchange.bitfinex.v1.dto.account.BitfinexDepositWithdrawalHistoryResponse) BigDecimal(java.math.BigDecimal) BitfinexFeesJSONTest(org.knowm.xchange.bitfinex.v1.dto.account.BitfinexFeesJSONTest) BitfinexWalletJSONTest(org.knowm.xchange.bitfinex.v1.dto.account.BitfinexWalletJSONTest) Test(org.junit.Test)

Aggregations

BitfinexDepositWithdrawalHistoryResponse (org.knowm.xchange.bitfinex.v1.dto.account.BitfinexDepositWithdrawalHistoryResponse)2 FundingRecord (org.knowm.xchange.dto.account.FundingRecord)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 InputStream (java.io.InputStream)1 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1 BitfinexFeesJSONTest (org.knowm.xchange.bitfinex.v1.dto.account.BitfinexFeesJSONTest)1 BitfinexWalletJSONTest (org.knowm.xchange.bitfinex.v1.dto.account.BitfinexWalletJSONTest)1 BitfinexTickerFundingCurrency (org.knowm.xchange.bitfinex.v2.dto.marketdata.BitfinexTickerFundingCurrency)1 Currency (org.knowm.xchange.currency.Currency)1