From 5fe34b0c954a98c2a4cbb752fd907d2a85444db6 Mon Sep 17 00:00:00 2001 From: Michael Lake Date: Tue, 9 Jan 2024 18:30:28 +0100 Subject: [PATCH] create universal transfer endpoint --- .../kotlin/bybit/sdk/rest/ByBitRestClient.kt | 3 ++ .../bybit/sdk/rest/asset/ByBitAssetClient.kt | 20 ++++++++ .../sdk/rest/asset/CreateUniversalTransfer.kt | 51 +++++++++++++++++++ .../kotlin/bybit/sdk/rest/AssetClientTest.kt | 35 +++++++++++++ 4 files changed, 109 insertions(+) create mode 100644 src/main/kotlin/bybit/sdk/rest/asset/ByBitAssetClient.kt create mode 100644 src/main/kotlin/bybit/sdk/rest/asset/CreateUniversalTransfer.kt create mode 100644 src/test/kotlin/bybit/sdk/rest/AssetClientTest.kt diff --git a/src/main/kotlin/bybit/sdk/rest/ByBitRestClient.kt b/src/main/kotlin/bybit/sdk/rest/ByBitRestClient.kt index a16822c..c3e49d2 100644 --- a/src/main/kotlin/bybit/sdk/rest/ByBitRestClient.kt +++ b/src/main/kotlin/bybit/sdk/rest/ByBitRestClient.kt @@ -5,6 +5,7 @@ import bybit.sdk.DefaultJvmHttpClientProvider import bybit.sdk.HttpClientProvider import bybit.sdk.Version import bybit.sdk.rest.account.ByBitAccountClient +import bybit.sdk.rest.asset.ByBitAssetClient import bybit.sdk.rest.market.ByBitMarketClient import bybit.sdk.rest.order.ByBitOrderClient import bybit.sdk.rest.position.ByBitPositionClient @@ -38,6 +39,8 @@ constructor( val httpClientProvider: HttpClientProvider = DefaultCIOHttpClientProvider() ) { + val assetClient by lazy { ByBitAssetClient(this) } + val accountClient by lazy { ByBitAccountClient(this) } val marketClient by lazy { ByBitMarketClient(this) } diff --git a/src/main/kotlin/bybit/sdk/rest/asset/ByBitAssetClient.kt b/src/main/kotlin/bybit/sdk/rest/asset/ByBitAssetClient.kt new file mode 100644 index 0000000..7c4378f --- /dev/null +++ b/src/main/kotlin/bybit/sdk/rest/asset/ByBitAssetClient.kt @@ -0,0 +1,20 @@ +package bybit.sdk.rest.asset + +import bybit.sdk.ext.coroutineToRestCallback +import bybit.sdk.rest.ByBitRestApiCallback +import bybit.sdk.rest.ByBitRestClient +import kotlinx.coroutines.runBlocking + +class ByBitAssetClient +internal constructor(internal val byBitRestClient: ByBitRestClient) { + + fun createUniversalTransferBlocking(params: CreateUniversalTransferParams): + CreateUniversalTransferResponse = runBlocking { createUniversalTransfer(params) } + + fun createUniversalTransfer( + params: CreateUniversalTransferParams, + callback: ByBitRestApiCallback + ) = coroutineToRestCallback(callback, { createUniversalTransfer(params) }) + + +} diff --git a/src/main/kotlin/bybit/sdk/rest/asset/CreateUniversalTransfer.kt b/src/main/kotlin/bybit/sdk/rest/asset/CreateUniversalTransfer.kt new file mode 100644 index 0000000..339e33a --- /dev/null +++ b/src/main/kotlin/bybit/sdk/rest/asset/CreateUniversalTransfer.kt @@ -0,0 +1,51 @@ +package bybit.sdk.rest.asset + +import bybit.sdk.rest.APIResponseV5 +import bybit.sdk.shared.AccountType +import io.ktor.http.* +import kotlinx.serialization.Serializable +import lombok.Builder + +suspend fun ByBitAssetClient.createUniversalTransfer( + params: CreateUniversalTransferParams +): CreateUniversalTransferResponse = + byBitRestClient.call({ + path( + "v5", + "asset", + "transfer", + "universal-transfer", + ) + + params.transferId.let { parameters["transferId"] = it.toString() } + params.coin.let { parameters["coin"] = it.toString() } + params.amount.let { parameters["amount"] = it.toString() } + + params.fromMemberId.let { parameters["fromMemberId"] = it.toString() } + params.toMemberId.let { parameters["toMemberId"] = it.toString() } + + params.fromAccountType.let { parameters["fromAccountType"] = it.toString() } + params.toAccountType.let { parameters["toAccountType"] = it.toString() } + + }, HttpMethod.Post, false) + +@Builder +data class CreateUniversalTransferParams( + val transferId: String, // "be7a2462-1138-4e27-80b1-62653f24925e", + val coin: String, // "ETH" + val amount: String, // "0.4" + val fromMemberId: Int, + val toMemberId: Int, + val fromAccountType: AccountType, + val toAccountType: AccountType +) + +@Serializable +data class CreateUniversalTransferResult( + val transferId: String +) + +@Serializable +data class CreateUniversalTransferResponse( + val result: CreateUniversalTransferResult, +) : APIResponseV5() diff --git a/src/test/kotlin/bybit/sdk/rest/AssetClientTest.kt b/src/test/kotlin/bybit/sdk/rest/AssetClientTest.kt new file mode 100644 index 0000000..66c434f --- /dev/null +++ b/src/test/kotlin/bybit/sdk/rest/AssetClientTest.kt @@ -0,0 +1,35 @@ +package bybit.sdk.rest + + +import bybit.sdk.rest.asset.CreateUniversalTransferParams +import bybit.sdk.shared.AccountType +import java.util.* +import kotlin.test.Test +import kotlin.test.assertEquals + +internal class AssetClientTest { + + private var restClient: ByBitRestClient = + ByBitRestClient( + apiKey = System.getenv("BYBIT_API_KEY"), + secret = System.getenv("BYBIT_SECRET"), + testnet = true, + httpClientProvider = okHttpClientProvider) + + @Test + fun createUniversalTransfer() { + val resp = restClient.assetClient.createUniversalTransferBlocking( + CreateUniversalTransferParams( + transferId = UUID.randomUUID().toString(), + coin = "BTC", + amount = "0.001", + fromMemberId = 100488137, + toMemberId = 408568, + fromAccountType = AccountType.CONTRACT, + toAccountType = AccountType.CONTRACT + ) + ) + assertEquals(0, resp.retCode) + assertEquals("success", resp.retMsg) + } +}