Skip to content

Commit

Permalink
Added abi.decode_boc
Browse files Browse the repository at this point in the history
  • Loading branch information
mdorofeev committed Mar 1, 2023
1 parent 03cec34 commit 1700b21
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/main/kotlin/ee/nx01/tonclient/abi/AbiModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(<ParamType>) 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)
}
}


10 changes: 10 additions & 0 deletions src/main/kotlin/ee/nx01/tonclient/abi/Types.kt
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,14 @@ data class ParamsOfGetSignatureData(
data class ResultOfGetSignatureData(
val signature: String,
val unsigned: String
)

data class ParamsOfDecodeBoc(
val params: List<AbiParam>,
val boc: String,
val allow_partial: Boolean
)

data class ResultOfDecodeBoc(
val data: Any
)
12 changes: 12 additions & 0 deletions src/test/kotlin/ee/nx01/tonclient/abi/AbiModuleTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

}

})

0 comments on commit 1700b21

Please sign in to comment.