Skip to content

Commit

Permalink
support biginteger
Browse files Browse the repository at this point in the history
  • Loading branch information
soaryong-stamper committed May 3, 2023
1 parent 8a3c763 commit 45997ac
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ class SampleViewModel : ViewModel() {

fun getObjectsDetails() = viewModelScope.launch {
_objectInfos.value?.let { objects ->
_objectDetails.postValue(SuiClient.instance.getObjectDetails(objects))
val multiObjects = SuiClient.instance.getMultiObjectDetail(objects.map { it.objectId })
_toastMessage.postValue(Gson().toJson(multiObjects))
}
}

Expand All @@ -74,7 +75,7 @@ class SampleViewModel : ViewModel() {

fun transferObject(objectInfo: SuiObjectInfo, receiver: String, sender: String) = viewModelScope.launch {
val transfer = SuiClient.instance.transferSui(
objectInfo.objectId, receiver, sender, 1000, BigInteger("10000000")
objectInfo.objectId, receiver, sender, BigInteger("10000"), BigInteger("10000000")
)
// val transfer = SuiClient.instance.moveCall(
// _address.value!!, "0x2", "devnet_nft", "mint", listOf(), listOf(
Expand Down
63 changes: 16 additions & 47 deletions suikotlin/src/main/java/io/cosmostation/suikotlin/SuiClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class SuiClient {
return ApiService.create().postJsonRpcRequest(request).body()?.result
}

suspend fun getObjectsByOwner(address: String): List<SuiObjectInfo> {
val request = JsonRpcRequest("suix_getOwnedObjects", listOf(address, SuiObjectResponseQuery(null, SuiObjectDataOptions(showContent = true))))
suspend fun getObjectsByOwner(address: String, options: SuiObjectDataOptions? = SuiObjectDataOptions(showContent = true)): List<SuiObjectInfo> {
val request = JsonRpcRequest("suix_getOwnedObjects", listOf(address, SuiObjectResponseQuery(null, options)))
val result = ApiService.create().postJsonRpcRequest(request).body()?.result
return Gson().fromJson(Gson().toJson(result), SuiObjectDataResult::class.java).data.map { it.data }
}
Expand All @@ -63,17 +63,8 @@ class SuiClient {
return ApiService.create().postJsonRpcRequest(request).body()?.result
}

suspend fun getObjectDetails(objectInfos: List<SuiObjectInfo>): List<String>? {
val request = objectInfos.map { JsonRpcRequest("sui_getObject", listOf(it.objectId)) }
return ApiService.create().postJsonRpcRequests(request).body()?.map { Gson().toJson(it.result) }?.toList()
}

suspend fun getTransactions(
transactionQuery: TransactionQuery, nextOffset: String? = null, limit: Int? = null, descending: Boolean = false
): List<SuiTransaction> {
val request = JsonRpcRequest(
"suix_queryTransactionBlocks", listOf(SuiTransactionQueryFilter(transactionQuery, SuiTransactionBlockResponseOptions(showInput = true, showEffects = true, showBalanceChanges = true)), nextOffset, limit, descending)
)
suspend fun getTransactions(transactionQuery: TransactionQuery, nextOffset: String? = null, limit: Int? = null, descending: Boolean = false, options: SuiTransactionBlockResponseOptions? = SuiTransactionBlockResponseOptions(showInput = true, showEffects = true, showBalanceChanges = true)): List<SuiTransaction> {
val request = JsonRpcRequest("suix_queryTransactionBlocks", listOf(SuiTransactionQueryFilter(transactionQuery, options), nextOffset, limit, descending))
val result = ApiService.create().postJsonRpcRequest(request).body()?.result
return Gson().fromJson(Gson().toJson(result), SuiTransactionDataResult::class.java).data
}
Expand All @@ -89,59 +80,37 @@ class SuiClient {

suspend fun faucet(address: String) = FaucetService.create().faucet(FixedAmountRequest(Recipient(address))).body()

suspend fun transferSui(
objectId: String, receiver: String, sender: String, gasBudget: Int, amount: BigInteger
): SuiWrappedTxBytes? {
val params = mutableListOf(sender, objectId, gasBudget.toString(), receiver, amount)
val result = ApiService.create().postJsonRpcRequest(
JsonRpcRequest("unsafe_transferSui", params)
).body()?.result
suspend fun transferSui(objectId: String, receiver: String, sender: String, gasBudget: BigInteger, amount: BigInteger): SuiWrappedTxBytes? {
val params = mutableListOf(sender, objectId, gasBudget.toString(), receiver, amount.toString())
val result = ApiService.create().postJsonRpcRequest(JsonRpcRequest("unsafe_transferSui", params)).body()?.result
return Gson().fromJson(Gson().toJson(result), SuiWrappedTxBytes::class.java)
}

suspend fun pay(
objectIds: List<String>, receivers: List<String>, sender: String, gasBudget: Int, gasObjectId: String? = null, amounts: List<BigInteger>
): SuiWrappedTxBytes? {
suspend fun pay(objectIds: List<String>, receivers: List<String>, sender: String, gasBudget: BigInteger, gasObjectId: String? = null, amounts: List<BigInteger>): SuiWrappedTxBytes? {
val params: MutableList<Any?> = mutableListOf(sender, objectIds, receivers, amounts.map { it.toString() }, gasObjectId, gasBudget.toString())
val result = ApiService.create().postJsonRpcRequest(JsonRpcRequest("unsafe_pay", params)).body()?.result
return Gson().fromJson(Gson().toJson(result), SuiWrappedTxBytes::class.java)
}

suspend fun paySui(
objectIds: List<String>, receivers: List<String>, sender: String, gasBudget: Int, amounts: List<BigInteger>
): SuiWrappedTxBytes? {
suspend fun paySui(objectIds: List<String>, receivers: List<String>, sender: String, gasBudget: BigInteger, amounts: List<BigInteger>): SuiWrappedTxBytes? {
val params: MutableList<Any?> = mutableListOf(sender, objectIds, receivers, amounts.map { it.toString() }, gasBudget.toString())
val result = ApiService.create().postJsonRpcRequest(
JsonRpcRequest("unsafe_paySui", params)
).body()?.result
val result = ApiService.create().postJsonRpcRequest(JsonRpcRequest("unsafe_paySui", params)).body()?.result
return Gson().fromJson(Gson().toJson(result), SuiWrappedTxBytes::class.java)
}

suspend fun transferObject(
objectId: String, receiver: String, sender: String, gasBudget: Int, gasObjectId: String? = null
): SuiWrappedTxBytes? {
suspend fun transferObject(objectId: String, receiver: String, sender: String, gasBudget: BigInteger, gasObjectId: String? = null): SuiWrappedTxBytes? {
val params = mutableListOf(sender, objectId, gasObjectId, gasBudget.toString(), receiver)
val result = ApiService.create().postJsonRpcRequest(
JsonRpcRequest("unsafe_transferObject", params)
).body()?.result
val result = ApiService.create().postJsonRpcRequest(JsonRpcRequest("unsafe_transferObject", params)).body()?.result
return Gson().fromJson(Gson().toJson(result), SuiWrappedTxBytes::class.java)
}

suspend fun moveCall(
sender: String, packageObjectId: String, module: String, function: String, typeArguments: List<String> = listOf(), arguments: List<String> = listOf(), gasPayment: String? = null, gasBudget: Int
): SuiWrappedTxBytes? {
val params = mutableListOf(
sender, packageObjectId, module, function, typeArguments, arguments, gasPayment, gasBudget
)
val result = ApiService.create().postJsonRpcRequest(
JsonRpcRequest("unsafe_moveCall", params)
).body()?.result
suspend fun moveCall(sender: String, packageObjectId: String, module: String, function: String, typeArguments: List<String> = listOf(), arguments: List<String> = listOf(), gasPayment: String? = null, gasBudget: BigInteger): SuiWrappedTxBytes? {
val params = mutableListOf(sender, packageObjectId, module, function, typeArguments, arguments, gasPayment, gasBudget.toString())
val result = ApiService.create().postJsonRpcRequest(JsonRpcRequest("unsafe_moveCall", params)).body()?.result
return Gson().fromJson(Gson().toJson(result), SuiWrappedTxBytes::class.java)
}

suspend fun executeTransaction(
txBytes: ByteArray, signedBytes: ByteArray, keyPair: EdDSAKeyPair, responseOptions: SuiTransactionBlockResponseOptions? = null
): Any? {
suspend fun executeTransaction(txBytes: ByteArray, signedBytes: ByteArray, keyPair: EdDSAKeyPair, responseOptions: SuiTransactionBlockResponseOptions? = SuiTransactionBlockResponseOptions(showInput = true, showEffects = true, showBalanceChanges = true)): Any? {
val params = mutableListOf(Base64.getEncoder().encodeToString(txBytes), listOf(Base64.getEncoder().encodeToString(byteArrayOf(0x00) + signedBytes + keyPair.publicKey.abyte)), responseOptions, "WaitForLocalExecution")
return ApiService.create().postJsonRpcRequest(JsonRpcRequest("sui_executeTransactionBlock", params)).body()?.result
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,8 @@ package io.cosmostation.suikotlin.model
import java.util.*
import kotlin.math.abs

data class JsonRpcRequest(
val method: String, val jsonrpc: String, val id: Long, val params: List<Any?>
) {
constructor(method: String, params: List<Any?>) : this(
method, "2.0", abs(Random().nextInt()).toLong(), params
)
data class JsonRpcRequest(val method: String, val jsonrpc: String, val id: Long, val params: List<Any?>) {
constructor(method: String, params: List<Any?>) : this(method, "2.0", abs(Random().nextInt()).toLong(), params)
}

data class JsonRpcResponse(
val result: Any, val jsonrpc: String, val id: Long
)
data class JsonRpcResponse(val result: Any, val jsonrpc: String, val id: Long)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ data class SuiObjectInfo(val objectId: String, val version: Int, val digest: Str

data class SuiObjectData(val data: SuiObjectInfo)

//data class SuiObjectDataResult(val data: List<SuiObjectData>, val hasNextPage: Boolean, val nextCursor: String)
data class SuiObjectDataResult(val data: List<SuiObjectData>, val hasNextPage: Boolean)

data class SuiTransactionDataResult(val data: List<SuiTransaction>, val hasNextPage: Boolean, val nextCursor: String)
Expand All @@ -21,7 +20,7 @@ data class ImmOrOwnedMoveObject(val ImmOrOwnedMoveObject: SuiObjectRef)

data class SuiObjectRef(val objectId: String, val version: Int, val digest: String)

data class SuiTransactionQueryFilter(val filter: TransactionQuery, val options: SuiTransactionBlockResponseOptions)
data class SuiTransactionQueryFilter(val filter: TransactionQuery, val options: SuiTransactionBlockResponseOptions?)

data class SuiObjectDataOptions(val showType: Boolean = true, val showContent: Boolean = false, val showDisplay: Boolean = false, val showStorageRebate: Boolean = false, val showPreviousTransaction: Boolean = false, val showOwner: Boolean = false, val showBcs: Boolean = false)

Expand Down

0 comments on commit 45997ac

Please sign in to comment.