Skip to content

Commit

Permalink
create universal transfer endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mlake committed Jan 9, 2024
1 parent fb16799 commit 5fe34b0
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/kotlin/bybit/sdk/rest/ByBitRestClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) }
Expand Down
20 changes: 20 additions & 0 deletions src/main/kotlin/bybit/sdk/rest/asset/ByBitAssetClient.kt
Original file line number Diff line number Diff line change
@@ -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<CreateUniversalTransferResponse>
) = coroutineToRestCallback(callback, { createUniversalTransfer(params) })


}
51 changes: 51 additions & 0 deletions src/main/kotlin/bybit/sdk/rest/asset/CreateUniversalTransfer.kt
Original file line number Diff line number Diff line change
@@ -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()
35 changes: 35 additions & 0 deletions src/test/kotlin/bybit/sdk/rest/AssetClientTest.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 5fe34b0

Please sign in to comment.