Skip to content

Commit

Permalink
Merge pull request #4956 from bigscoop/bitget
Browse files Browse the repository at this point in the history
[bitget] Extend functionality
  • Loading branch information
timmolter authored Nov 20, 2024
2 parents 586be46 + c23df6e commit 3222e96
Show file tree
Hide file tree
Showing 41 changed files with 1,643 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.experimental.UtilityClass;
import org.knowm.xchange.bitget.dto.account.BitgetAccountType;
import org.knowm.xchange.bitget.dto.account.BitgetBalanceDto;
import org.knowm.xchange.bitget.dto.account.BitgetDepositWithdrawRecordDto;
import org.knowm.xchange.bitget.dto.account.BitgetDepositWithdrawRecordDto.DepositType;
import org.knowm.xchange.bitget.dto.account.BitgetDepositWithdrawRecordDto.RecordType;
import org.knowm.xchange.bitget.dto.account.params.BitgetMainSubTransferHistoryParams;
import org.knowm.xchange.bitget.dto.account.params.BitgetMainSubTransferHistoryParams.Role;
import org.knowm.xchange.bitget.dto.marketdata.BitgetMarketDepthDto;
import org.knowm.xchange.bitget.dto.marketdata.BitgetSymbolDto;
import org.knowm.xchange.bitget.dto.marketdata.BitgetSymbolDto.Status;
import org.knowm.xchange.bitget.dto.marketdata.BitgetTickerDto;
import org.knowm.xchange.bitget.dto.trade.BitgetFillDto;
import org.knowm.xchange.bitget.dto.trade.BitgetOrderInfoDto;
import org.knowm.xchange.bitget.dto.trade.BitgetOrderInfoDto.BitgetOrderStatus;
import org.knowm.xchange.bitget.dto.trade.BitgetPlaceOrderDto;
Expand All @@ -24,12 +31,15 @@
import org.knowm.xchange.dto.Order.OrderStatus;
import org.knowm.xchange.dto.Order.OrderType;
import org.knowm.xchange.dto.account.Balance;
import org.knowm.xchange.dto.account.FundingRecord;
import org.knowm.xchange.dto.account.FundingRecord.Type;
import org.knowm.xchange.dto.account.Wallet;
import org.knowm.xchange.dto.marketdata.OrderBook;
import org.knowm.xchange.dto.marketdata.Ticker;
import org.knowm.xchange.dto.meta.InstrumentMetaData;
import org.knowm.xchange.dto.trade.LimitOrder;
import org.knowm.xchange.dto.trade.MarketOrder;
import org.knowm.xchange.dto.trade.UserTrade;
import org.knowm.xchange.instrument.Instrument;

@UtilityClass
Expand Down Expand Up @@ -215,4 +225,61 @@ public BitgetPlaceOrderDto toBitgetPlaceOrderDto(MarketOrder marketOrder) {
.size(marketOrder.getOriginalAmount())
.build();
}

public UserTrade toUserTrade(BitgetFillDto bitgetFillDto) {
return new UserTrade(
bitgetFillDto.getOrderSide(),
bitgetFillDto.getAssetAmount(),
toCurrencyPair(bitgetFillDto.getSymbol()),
bitgetFillDto.getPrice(),
toDate(bitgetFillDto.getUpdatedAt()),
bitgetFillDto.getTradeId(),
bitgetFillDto.getOrderId(),
bitgetFillDto.getFeeDetail().getTotalFee().abs(),
bitgetFillDto.getFeeDetail().getCurrency(),
null);
}

public String toString(BitgetAccountType bitgetAccountType) {
return Optional.ofNullable(bitgetAccountType).map(BitgetAccountType::getValue).orElse(null);
}

public String toString(BitgetMainSubTransferHistoryParams.Role role) {
return Optional.ofNullable(role).map(Role::getValue).orElse(null);
}

public FundingRecord toFundingRecord(BitgetDepositWithdrawRecordDto record) {
return new FundingRecord.Builder()
.setInternalId(record.getOrderId())
.setBlockchainTransactionHash(record.getTradeId())
.setCurrency(record.getCurrency())
.setType(toFundingRecordType(record))
.setAmount(record.getSize())
.setFee(record.getFee())
.setStatus(record.getStatus())
.setAddress(record.getToAddress())
.setAddressTag(record.getToAddressTag())
.setDate(toDate(record.getUpdatedAt()))
.build();
}

public FundingRecord.Type toFundingRecordType(BitgetDepositWithdrawRecordDto record) {
if (record.getDepositType() == DepositType.ON_CHAIN
&& record.getType() == RecordType.WITHDRAW) {
return Type.WITHDRAWAL;
}
if (record.getDepositType() == DepositType.ON_CHAIN && record.getType() == RecordType.DEPOSIT) {
return Type.DEPOSIT;
}
if (record.getDepositType() == DepositType.INTERNAL_TRANSFER
&& record.getType() == RecordType.WITHDRAW) {
return Type.INTERNAL_WITHDRAWAL;
}
if (record.getDepositType() == DepositType.INTERNAL_TRANSFER
&& record.getType() == RecordType.DEPOSIT) {
return Type.INTERNAL_DEPOSIT;
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
import org.knowm.xchange.bitget.dto.BitgetException;
import org.knowm.xchange.bitget.dto.BitgetResponse;
import org.knowm.xchange.bitget.dto.account.BitgetBalanceDto;
import org.knowm.xchange.bitget.dto.account.BitgetDepositWithdrawRecordDto;
import org.knowm.xchange.bitget.dto.account.BitgetMainSubTransferRecordDto;
import org.knowm.xchange.bitget.dto.account.BitgetSubBalanceDto;
import org.knowm.xchange.bitget.dto.account.BitgetTransferRecordDto;
import org.knowm.xchange.bitget.dto.trade.BitgetFillDto;
import org.knowm.xchange.bitget.dto.trade.BitgetOrderInfoDto;
import org.knowm.xchange.bitget.dto.trade.BitgetPlaceOrderDto;
import si.mazi.rescu.ParamsDigest;
Expand All @@ -25,6 +30,16 @@ public interface BitgetAuthenticated {
@GET
@Path("api/v2/spot/account/assets")
BitgetResponse<List<BitgetBalanceDto>> balances(
@HeaderParam("ACCESS-KEY") String apiKey,
@HeaderParam("ACCESS-SIGN") ParamsDigest signer,
@HeaderParam("ACCESS-PASSPHRASE") String passphrase,
@HeaderParam("ACCESS-TIMESTAMP") SynchronizedValueFactory<Long> timestamp,
@QueryParam("coin") String currency)
throws IOException, BitgetException;

@GET
@Path("api/v2/spot/account/subaccount-assets")
BitgetResponse<List<BitgetSubBalanceDto>> subBalances(
@HeaderParam("ACCESS-KEY") String apiKey,
@HeaderParam("ACCESS-SIGN") ParamsDigest signer,
@HeaderParam("ACCESS-PASSPHRASE") String passphrase,
Expand All @@ -51,4 +66,98 @@ BitgetResponse<BitgetOrderInfoDto> createOrder(
@HeaderParam("ACCESS-TIMESTAMP") SynchronizedValueFactory<Long> timestamp,
BitgetPlaceOrderDto bitgetPlaceOrderDto)
throws IOException, BitgetException;

@GET
@Path("api/v2/spot/trade/fills")
BitgetResponse<List<BitgetFillDto>> fills(
@HeaderParam("ACCESS-KEY") String apiKey,
@HeaderParam("ACCESS-SIGN") ParamsDigest signer,
@HeaderParam("ACCESS-PASSPHRASE") String passphrase,
@HeaderParam("ACCESS-TIMESTAMP") SynchronizedValueFactory<Long> timestamp,
@QueryParam("symbol") String symbol,
@QueryParam("limit") Integer limit,
@QueryParam("orderId") String orderId,
@QueryParam("startTime") Long startTime,
@QueryParam("endTime") Long endTime,
@QueryParam("idLessThan") String idLessThan)
throws IOException, BitgetException;

@GET
@Path("api/v2/spot/account/transferRecords")
BitgetResponse<List<BitgetTransferRecordDto>> transferRecords(
@HeaderParam("ACCESS-KEY") String apiKey,
@HeaderParam("ACCESS-SIGN") ParamsDigest signer,
@HeaderParam("ACCESS-PASSPHRASE") String passphrase,
@HeaderParam("ACCESS-TIMESTAMP") SynchronizedValueFactory<Long> timestamp,
@QueryParam("coin") String currency,
@QueryParam("limit") Integer limit,
@QueryParam("clientOid") String clientOid,
@QueryParam("fromType") String fromType,
@QueryParam("startTime") Long startTime,
@QueryParam("endTime") Long endTime,
@QueryParam("idLessThan") String idLessThan)
throws IOException, BitgetException;

@GET
@Path("api/v2/spot/account/sub-main-trans-record")
BitgetResponse<List<BitgetMainSubTransferRecordDto>> mainSubTransferRecords(
@HeaderParam("ACCESS-KEY") String apiKey,
@HeaderParam("ACCESS-SIGN") ParamsDigest signer,
@HeaderParam("ACCESS-PASSPHRASE") String passphrase,
@HeaderParam("ACCESS-TIMESTAMP") SynchronizedValueFactory<Long> timestamp,
@QueryParam("coin") String currency,
@QueryParam("limit") Integer limit,
@QueryParam("clientOid") String clientOid,
@QueryParam("role") String role,
@QueryParam("subUid") String subAccountUid,
@QueryParam("startTime") Long startTime,
@QueryParam("endTime") Long endTime,
@QueryParam("idLessThan") String idLessThan)
throws IOException, BitgetException;

@GET
@Path("api/v2/spot/wallet/deposit-records")
BitgetResponse<List<BitgetDepositWithdrawRecordDto>> depositRecords(
@HeaderParam("ACCESS-KEY") String apiKey,
@HeaderParam("ACCESS-SIGN") ParamsDigest signer,
@HeaderParam("ACCESS-PASSPHRASE") String passphrase,
@HeaderParam("ACCESS-TIMESTAMP") SynchronizedValueFactory<Long> timestamp,
@QueryParam("coin") String currency,
@QueryParam("limit") Integer limit,
@QueryParam("orderId") String orderId,
@QueryParam("startTime") Long startTime,
@QueryParam("endTime") Long endTime,
@QueryParam("idLessThan") String idLessThan)
throws IOException, BitgetException;

@GET
@Path("api/v2/spot/wallet/subaccount-deposit-records")
BitgetResponse<List<BitgetDepositWithdrawRecordDto>> subDepositRecords(
@HeaderParam("ACCESS-KEY") String apiKey,
@HeaderParam("ACCESS-SIGN") ParamsDigest signer,
@HeaderParam("ACCESS-PASSPHRASE") String passphrase,
@HeaderParam("ACCESS-TIMESTAMP") SynchronizedValueFactory<Long> timestamp,
@QueryParam("coin") String currency,
@QueryParam("limit") Integer limit,
@QueryParam("subUid") String subAccountUid,
@QueryParam("startTime") Long startTime,
@QueryParam("endTime") Long endTime,
@QueryParam("idLessThan") String idLessThan)
throws IOException, BitgetException;

@GET
@Path("api/v2/spot/wallet/withdrawal-records")
BitgetResponse<List<BitgetDepositWithdrawRecordDto>> withdrawalRecords(
@HeaderParam("ACCESS-KEY") String apiKey,
@HeaderParam("ACCESS-SIGN") ParamsDigest signer,
@HeaderParam("ACCESS-PASSPHRASE") String passphrase,
@HeaderParam("ACCESS-TIMESTAMP") SynchronizedValueFactory<Long> timestamp,
@QueryParam("coin") String currency,
@QueryParam("limit") Integer limit,
@QueryParam("orderId") String orderId,
@QueryParam("clientOid") String clientOid,
@QueryParam("startTime") Long startTime,
@QueryParam("endTime") Long endTime,
@QueryParam("idLessThan") String idLessThan)
throws IOException, BitgetException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.knowm.xchange.bitget.config.converter;

import com.fasterxml.jackson.databind.util.StdConverter;
import java.util.Locale;
import org.knowm.xchange.dto.account.FundingRecord.Status;

/** Converts string to {@code FundingRecord.Status} */
public class StringToFundingRecordStatusConverter extends StdConverter<String, Status> {

@Override
public Status convert(String value) {
switch (value.toUpperCase(Locale.ROOT)) {
case "PENDING":
return Status.PROCESSING;
case "FAIL":
return Status.FAILED;
case "SUCCESS":
return Status.COMPLETE;
default:
throw new IllegalArgumentException("Can't map " + value);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.knowm.xchange.bitget.dto.account;

import com.fasterxml.jackson.annotation.JsonValue;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum BitgetAccountType {
SPOT("spot"),
P2P("p2p"),
COIN_FUTURES("coin_futures"),
USDT_FUTURES("usdt_futures"),
USDC_FUTURES("usdc_futures"),
CROSSED_MARGIN("crossed_margin"),
ISOLATED_MARGIN("isolated_margin");

@JsonValue private final String value;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package org.knowm.xchange.bitget.dto.account;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import java.math.BigDecimal;
import java.time.Instant;
import lombok.Builder;
import lombok.Data;
import lombok.extern.jackson.Jacksonized;
import org.knowm.xchange.bitget.config.converter.StringToCurrencyConverter;
import org.knowm.xchange.bitget.config.converter.StringToFundingRecordStatusConverter;
import org.knowm.xchange.currency.Currency;
import org.knowm.xchange.dto.account.FundingRecord.Status;

@Data
@Builder
@Jacksonized
public class BitgetDepositWithdrawRecordDto {

@JsonProperty("orderId")
private String orderId;

@JsonProperty("tradeId")
private String tradeId;

@JsonProperty("coin")
@JsonDeserialize(converter = StringToCurrencyConverter.class)
private Currency currency;

@JsonProperty("clientOid")
private String clientOid;

@JsonProperty("type")
private RecordType type;

@JsonProperty("dest")
private DepositType depositType;

@JsonProperty("size")
private BigDecimal size;

@JsonProperty("fee")
private BigDecimal fee;

@JsonProperty("status")
@JsonDeserialize(converter = StringToFundingRecordStatusConverter.class)
private Status status;

@JsonProperty("fromAddress")
private String fromAddress;

@JsonProperty("toAddress")
private String toAddress;

@JsonProperty("chain")
private String chain;

@JsonProperty("confirm")
private Integer confirmCount;

@JsonProperty("tag")
private String toAddressTag;

@JsonProperty("cTime")
private Instant createdAt;

@JsonProperty("uTime")
private Instant updatedAt;

public static enum RecordType {
@JsonProperty("withdraw")
WITHDRAW,

@JsonProperty("deposit")
DEPOSIT
}

public static enum DepositType {
@JsonProperty("on_chain")
ON_CHAIN,

@JsonProperty("internal_transfer")
INTERNAL_TRANSFER
}
}
Loading

0 comments on commit 3222e96

Please sign in to comment.