Skip to content

Commit

Permalink
Update to SDK 1.20.0 and added some missed methods (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdorofeev authored Jul 25, 2021
1 parent 150a8b6 commit 8d43825
Show file tree
Hide file tree
Showing 12 changed files with 283 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ repositories {
```

```groovy
implementation "ee.nx-01.tonclient:ton-client-kotlin:0.0.32"
implementation "ee.nx-01.tonclient:ton-client-kotlin:0.0.34"
```

## Supported OS
Expand Down
10 changes: 10 additions & 0 deletions src/main/kotlin/ee/nx01/tonclient/abi/AbiModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,14 @@ class AbiModule(private val tonClient: TonClient) {
suspend fun attachSignatureToMessageBody(params: ParamsOfAttachSignatureToMessageBody): ResultOfAttachSignatureToMessageBody {
return tonClient.request("abi.attach_signature_to_message_body", params)
}

/**
* Decodes account data using provided data BOC and ABI.
Note: this feature requires ABI 2.1 or higher.
*/
suspend fun decodeAccountData(params: ParamsOfDecodeAccountData): ResultOfDecodeData {
return tonClient.request("abi.decode_account_data", params)
}
}

11 changes: 10 additions & 1 deletion src/main/kotlin/ee/nx01/tonclient/abi/Types.kt
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,13 @@ data class ResultOfEncodeInternalMessage(
val message: String,
val address: String,
val messageId: String
)
)

data class ParamsOfDecodeAccountData(
val abi: Abi,
val data: String
)

data class ResultOfDecodeData(
val data: Any
)
154 changes: 151 additions & 3 deletions src/main/kotlin/ee/nx01/tonclient/net/NetModule.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package ee.nx01.tonclient.net

import ee.nx01.tonclient.TonClient
import ee.nx01.tonclient.abi.Abi
import ee.nx01.tonclient.abi.DecodedMessageBody
import ee.nx01.tonclient.types.QueryOrderByInput

class NetModule(private val tonClient: TonClient) {

Expand Down Expand Up @@ -109,6 +106,157 @@ class NetModule(private val tonClient: TonClient) {
return tonClient.request("net.query_transaction_tree", params)
}

/**
* ## create_block_iterator
Creates block iterator.
Block iterator uses robust iteration methods that guaranties that every
block in the specified range isn't missed or iterated twice.
Iterated range can be reduced with some filters:
- `start_time` – the bottom time range. Only blocks with `gen_utime`
more or equal to this value is iterated. If this parameter is omitted then there is
no bottom time edge, so all blocks since zero state is iterated.
- `end_time` – the upper time range. Only blocks with `gen_utime`
less then this value is iterated. If this parameter is omitted then there is
no upper time edge, so iterator never finishes.
- `shard_filter` – workchains and shard prefixes that reduce the set of interesting
blocks. Block conforms to the shard filter if it belongs to the filter workchain
and the first bits of block's `shard` fields matches to the shard prefix.
Only blocks with suitable shard are iterated.
Items iterated is a JSON objects with block data. The minimal set of returned
fields is:
```text
id
gen_utime
workchain_id
shard
after_split
after_merge
prev_ref {
root_hash
}
prev_alt_ref {
root_hash
}
```
Application can request additional fields in the `result` parameter.
Application should call the `remove_iterator` when iterator is no longer required.
*/
suspend fun createBlockIterator(params: ParamsOfCreateBlockIterator): RegisteredIterator {
return tonClient.request("net.create_block_iterator", params)
}

/**
* ## resume_block_iterator
Resumes block iterator.
The iterator stays exactly at the same position where the `resume_state` was catched.
Application should call the `remove_iterator` when iterator is no longer required.
*/
suspend fun resumeBlockIterator(params: ParamsOfResumeBlockIterator): RegisteredIterator {
return tonClient.request("net.resume_block_iterator", params)
}

/**
* Creates transaction iterator.
Transaction iterator uses robust iteration methods that guaranty that every transaction in the specified range isn't missed or iterated twice.
Iterated range can be reduced with some filters:
start_time – the bottom time range. Only transactions with now more or equal to this value are iterated. If this parameter is omitted then there is no bottom time edge, so all the transactions since zero state are iterated.
end_time – the upper time range. Only transactions with now less then this value are iterated. If this parameter is omitted then there is no upper time edge, so iterator never finishes.
shard_filter – workchains and shard prefixes that reduce the set of interesting accounts. Account address conforms to the shard filter if it belongs to the filter workchain and the first bits of address match to the shard prefix. Only transactions with suitable account addresses are iterated.
accounts_filter – set of account addresses whose transactions must be iterated. Note that accounts filter can conflict with shard filter so application must combine these filters carefully.
Iterated item is a JSON objects with transaction data. The minimal set of returned fields is:
id
account_addr
now
balance_delta(format:DEC)
bounce { bounce_type }
in_message {
id
value(format:DEC)
msg_type
src
}
out_messages {
id
value(format:DEC)
msg_type
dst
}
Application can request an additional fields in the result parameter.
Another parameter that affects on the returned fields is the include_transfers. When this parameter is true the iterator computes and adds transfer field containing list of the useful TransactionTransfer objects. Each transfer is calculated from the particular message related to the transaction and has the following structure:
message – source message identifier.
isBounced – indicates that the transaction is bounced, which means the value will be returned back to the sender.
isDeposit – indicates that this transfer is the deposit (true) or withdraw (false).
counterparty – account address of the transfer source or destination depending on isDeposit.
value – amount of nano tokens transferred. The value is represented as a decimal string because the actual value can be more precise than the JSON number can represent. Application must use this string carefully – conversion to number can follow to loose of precision.
Application should call the remove_iterator when iterator is no longer required.
*/
suspend fun createTransactionIterator(params: ParamsOfCreateTransactionIterator): RegisteredIterator {
return tonClient.request("net.create_transaction_iterator", params)
}


/**
* Resumes transaction iterator.
The iterator stays exactly at the same position where the resume_state was caught. Note that resume_state doesn't store the account filter. If the application requires to use the same account filter as it was when the iterator was created then the application must pass the account filter again in accounts_filter parameter.
Application should call the remove_iterator when iterator is no longer required.
*/
suspend fun resumeTransactionIterator(params: ParamsOfResumeTransactionIterator): RegisteredIterator {
return tonClient.request("net.resume_transaction_iterator", params)
}

/**
* ## iterator_next
Returns next available items.
In addition to available items this function returns the `has_more` flag
indicating that the iterator isn't reach the end of the iterated range yet.
This function can return the empty list of available items but
indicates that there are more items is available.
This situation appears when the iterator doesn't reach iterated range
but database doesn't contains available items yet.
If application requests resume state in `return_resume_state` parameter
then this function returns `resume_state` that can be used later to
resume the iteration from the position after returned items.
The structure of the items returned depends on the iterator used.
See the description to the appropriated iterator creation function.
*/
suspend fun iteratorNext(params: ParamsOfIteratorNext): ResultOfIteratorNext {
return tonClient.request("net.iterator_next", params)
}

/**
* ## remove_iterator
Removes an iterator
Frees all resources allocated in library to serve iterator.
Application always should call the `remove_iterator` when iterator
is no longer required.
*/
suspend fun removeIterator(params: RegisteredIterator) {
return tonClient.request("net.remove_iterator", params)
}

}

Expand Down
41 changes: 41 additions & 0 deletions src/main/kotlin/ee/nx01/tonclient/net/Types.kt
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,45 @@ data class ParamsOfQueryTransactionTree(
data class ResultOfQueryTransactionTree(
val messages: List<MessageNode>,
val transactions: List<TransactionNode>
)

data class ParamsOfResumeBlockIterator(
val resume_state: Any
)

data class RegisteredIterator(
val handle: Long
)

data class ParamsOfCreateBlockIterator(
val startTime: Long? = null,
val endTime: Long? = null,
val shardFilter: List<String>? = null,
val result: String? = null
)

data class ParamsOfIteratorNext(
val iterator: Long,
val limit: Long? = null,
val returnResumeState: Boolean? = null
)

data class ResultOfIteratorNext(
val items: List<Any>,
val hasMore: Boolean,
val resumeState: Any? = null
)

data class ParamsOfCreateTransactionIterator(
val startTime: Long? = null,
val endTime: Long? = null,
val shardFilter: List<String>? = null,
val accountsFilter: List<String>? = null,
val result: String? = null,
val includeTransfers: Boolean? = null
)

data class ParamsOfResumeTransactionIterator(
val resumeState: Any,
val accountsFilter: List<String>? = null
)
28 changes: 28 additions & 0 deletions src/main/kotlin/ee/nx01/tonclient/utils/UtilsModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,34 @@ class UtilsModule(private val tonClient: TonClient) {

return decompressed
}

/**
* Validates and returns the type of any TON address.
Address types are the following
0:919db8e740d50bf349df2eea03fa30c385d846b991ff5542e67098ee833fc7f7 - standart TON address most commonly used in all cases.
Also called as hex addres 919db8e740d50bf349df2eea03fa30c385d846b991ff5542e67098ee833fc7f7 - account ID.
A part of full address. Identifies account inside particular workchain EQCRnbjnQNUL80nfLuoD+jDDhdhGuZH/VULmcJjugz/H9wam - base64 address.
Also called "user-friendly". Was used at the beginning of TON. Now it is supported for compatibility
*/
suspend fun getAddressType(param: ParamsOfGetAddressType): ResultOfGetAddressType {
return tonClient.request("utils.get_address_type", param)
}
}

data class ParamsOfGetAddressType(
val address: String
)

data class ResultOfGetAddressType(
val addressType: AccountAddressType
)

enum class AccountAddressType {
AccountId,
Hex,
Base64
}

data class ParamsOfDecompressZstd(
Expand Down
Binary file modified src/main/resources/natives/linux_64/libtonclientjni.so
Binary file not shown.
Binary file modified src/main/resources/natives/osx_64/libtonclientjni.dylib
Binary file not shown.
Binary file modified src/main/resources/natives/windows_64/tonclientjni.dll
Binary file not shown.
8 changes: 4 additions & 4 deletions src/test/kotlin/ee/nx01/tonclient/TestConstants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package ee.nx01.tonclient
object TestConstants {
val WALLET_PHRASE = "defense talent layer admit setup virus deer buffalo timber ethics symbol cover"
val WALLET_ADDRESS = "0:7f458ae01e28573a181e2227dc77710d6421d4e103fdd3e023200aa4bce83950"
val MESSAGE_ID = "8fad5a4e275f921ac19b22d00533c48cee277b93bea309317470340b64294baf"
val BLOCK_ID = 452651f
val TRANSACTION_ID = "4f5a7c06d287f0c41b3cb7795605a96491fb275ae2edf02612638c30ef430576"
val KEY_BLOCK_ID = "f2cfde69312a330f70f671f66ce13bdc5a514f30844c698643ac39aee4e2b890"
val MESSAGE_ID = "240e0219663b7253c41e76d581919a62cf447d0b8b5330b6da133b77acabc64b"
val BLOCK_ID = 35664f
val TRANSACTION_ID = "c289bae33734e680b73d5ec61c98baa6c8bc929b4536a2d43e7940d193d87e3c"
val KEY_BLOCK_ID = "3fbe311e2bb04c758d041e6890a000039039de3177f13aff0897587aeb25bb0e"
val WALLET_SECRET = "db16e21ee91b5064f6e31d9a2fb4771ce7f8acf14fe6d6ffade8ffcbeec31d69"
val WALLET_PUBLIC = "335c317bfef32545749a665bfec97f692fdc107d0867a1a1e1b2d2906b40d24c"

Expand Down
22 changes: 22 additions & 0 deletions src/test/kotlin/ee/nx01/tonclient/net/NetModuletTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import ee.nx01.tonclient.TonClient
import ee.nx01.tonclient.TonClientConfig
import ee.nx01.tonclient.types.AccountFilterInput
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe
import kotlinx.coroutines.sync.Mutex

Expand Down Expand Up @@ -84,4 +85,25 @@ class NetModuletTest : StringSpec({
response.messages shouldNotBe null

}

"Should be able get block with block iterator" {
val client = TonClient(TonClientConfig(NetworkConfig(endpoints = listOf("main.ton.dev"))))

val iterator = client.net.createBlockIterator(ParamsOfCreateBlockIterator(shardFilter = listOf("0:8000000000000000")))

val next = ParamsOfIteratorNext(iterator= iterator.handle, returnResumeState = true)

val listItem = mutableListOf<ResultOfIteratorNext>()

for (i in 1..10) {
listItem += client.net.iteratorNext(next)
}

client.net.removeIterator(iterator)

listItem.size shouldBe 10

}


})
16 changes: 16 additions & 0 deletions src/test/kotlin/ee/nx01/tonclient/utils/UtilsModuleTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,20 @@ class UtilsModuleTest : StringSpec({

String(Base64.getDecoder().decode(response)) shouldBe "Test string"
}

"Should be able get address type in Hex" {
val client = TonClient()

val response = client.utils.getAddressType(ParamsOfGetAddressType("0:919db8e740d50bf349df2eea03fa30c385d846b991ff5542e67098ee833fc7f7"))

response.addressType shouldBe AccountAddressType.Hex
}

"Should be able get address type in base 64" {
val client = TonClient()

val response = client.utils.getAddressType(ParamsOfGetAddressType("EQCRnbjnQNUL80nfLuoD+jDDhdhGuZH/VULmcJjugz/H9wam"))

response.addressType shouldBe AccountAddressType.Base64
}
})

0 comments on commit 8d43825

Please sign in to comment.