From 1700b21ffbc492a63febb814b2b564a3eda4ea6a Mon Sep 17 00:00:00 2001 From: Maxim Dorofeev Date: Wed, 1 Mar 2023 09:42:04 +0200 Subject: [PATCH] Added abi.decode_boc --- .../kotlin/ee/nx01/tonclient/abi/AbiModule.kt | 18 ++++++++++++++++++ src/main/kotlin/ee/nx01/tonclient/abi/Types.kt | 10 ++++++++++ .../ee/nx01/tonclient/abi/AbiModuleTest.kt | 12 ++++++++++++ 3 files changed, 40 insertions(+) diff --git a/src/main/kotlin/ee/nx01/tonclient/abi/AbiModule.kt b/src/main/kotlin/ee/nx01/tonclient/abi/AbiModule.kt index 6a9506b..89b3f4e 100644 --- a/src/main/kotlin/ee/nx01/tonclient/abi/AbiModule.kt +++ b/src/main/kotlin/ee/nx01/tonclient/abi/AbiModule.kt @@ -161,4 +161,22 @@ class AbiModule(private val tonClient: TonClient) { suspend fun getSignatureData(params: ParamsOfGetSignatureData): ResultOfGetSignatureData { return tonClient.request("abi.get_signature_data", params) } + + + /** + * Decodes BOC into JSON as a set of provided parameters. + * Solidity functions use ABI types for builder encoding. The simplest way to decode such a BOC is to use ABI decoding. + * ABI has it own rules for fields layout in cells so manually encoded BOC can not be described in terms of ABI rules. + * To solve this problem we introduce a new ABI type Ref() which allows to store ParamType ABI parameter + * in cell reference and, thus, decode manually encoded BOCs. This type is available only in decode_boc function and will + * not be available in ABI messages encoding until it is included into some ABI revision. + * Such BOC descriptions covers most users needs. If someone wants to decode some BOC which can not be described by these rules + * (i.e. BOC with TLB containing constructors of flags defining some parsing conditions) then they can decode the fields up to fork condition, + * check the parsed data manually, expand the parsing schema and then decode the whole BOC with the full schema. + */ + suspend fun decodeBoc(params: ParamsOfDecodeBoc): ResultOfDecodeBoc { + return tonClient.request("abi.decode_boc", params) + } } + + diff --git a/src/main/kotlin/ee/nx01/tonclient/abi/Types.kt b/src/main/kotlin/ee/nx01/tonclient/abi/Types.kt index c13ec72..cbe9609 100644 --- a/src/main/kotlin/ee/nx01/tonclient/abi/Types.kt +++ b/src/main/kotlin/ee/nx01/tonclient/abi/Types.kt @@ -277,4 +277,14 @@ data class ParamsOfGetSignatureData( data class ResultOfGetSignatureData( val signature: String, val unsigned: String +) + +data class ParamsOfDecodeBoc( + val params: List, + val boc: String, + val allow_partial: Boolean +) + +data class ResultOfDecodeBoc( + val data: Any ) \ No newline at end of file diff --git a/src/test/kotlin/ee/nx01/tonclient/abi/AbiModuleTest.kt b/src/test/kotlin/ee/nx01/tonclient/abi/AbiModuleTest.kt index 641654f..b9a556f 100644 --- a/src/test/kotlin/ee/nx01/tonclient/abi/AbiModuleTest.kt +++ b/src/test/kotlin/ee/nx01/tonclient/abi/AbiModuleTest.kt @@ -244,4 +244,16 @@ class AbiModuleTest : StringSpec({ } + + "Should be able decode boc " { + val client = TonClient() + + val abiParams = listOf(AbiParam("a", "uint32"), AbiParam("b", "ref(int64)"), AbiParam("c", "bool")) + + val response = client.abi.decodeBoc(ParamsOfDecodeBoc(abiParams, "te6ccgEBAgEAEgABCQAAAADAAQAQAAAAAAAAAHs=", true)) + + response shouldNotBe null + + } + }) \ No newline at end of file