Skip to content

Commit

Permalink
Added encode boc method with test (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdorofeev authored Apr 9, 2021
1 parent 10220db commit 23f95b1
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 41 deletions.
51 changes: 10 additions & 41 deletions src/main/kotlin/ee/nx01/tonclient/boc/BocModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ class BocModule(private val tonClient: TonClient) {

/**
* #parse_shardstate
Parses shardstate boc into a JSON
Parses shardstate boc into a JSON
JSON structure is compatible with GraphQL API shardstate object
JSON structure is compatible with GraphQL API shardstate object
*/
suspend fun parseShardstate(params: ParamsOfParseShardstate): ResultOfParse {
return tonClient.request("boc.parse_shardstate", params)
}

/**
* #get_code_from_tvc
Extracts code from TVC contract image
Extracts code from TVC contract image
*/
suspend fun getCodeFromTvc(params: ParamsOfGetCodeFromTvc): ResultOfGetCodeFromTvc {
return tonClient.request("boc.get_code_from_tvc", params)
Expand All @@ -77,42 +77,11 @@ class BocModule(private val tonClient: TonClient) {
}


}

data class ResultOfGetBocHash(
val hash: String
)

data class ParamsOfGetBocHash(
val boc: String
)

data class ParamsOfGetCodeFromTvc(
val tvc: String
)

data class ResultOfGetCodeFromTvc(
val code: String
)

data class ParamsOfParseShardstate(
val boc: String,
val id: String,
val workchainId: Int
)

data class ResultOfGetBlockchainConfig(
val configBoc: String
)

data class ParamsOfGetBlockchainConfig(
val blockBoc: String
)

data class ParamsOfParse(
val boc: String
)
/**
* Encodes BOC from builder operations.
*/
suspend fun encodeBoc(params: ParamsOfEncodeBoc): ResultOfEncodeBoc {
return tonClient.request("boc.encode_boc", params)
}

data class ResultOfParse(
val parsed: Map<String, Any>
)
}
115 changes: 115 additions & 0 deletions src/main/kotlin/ee/nx01/tonclient/boc/Types.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package ee.nx01.tonclient.boc

data class BocCacheType(
val type: BocCacheTypeEnum = BocCacheTypeEnum.Unpinned,
val pin: String? = null
)

enum class BocCacheTypeEnum {
Pinned, Unpinned
}

data class ParamsOfEncodeBoc(
val builder: List<BuilderOp>,
val boc_cache: BocCacheType?
)

/**
* Depends on value of the type field.
When type is 'Integer'
Append integer to cell data.
size: number – Bit size of the value.
value: any – Value: - Number containing integer number.
e.g. 123, -123. - Decimal string. e.g. "123", "-123".
- 0x prefixed hexadecimal string.
e.g 0x123, 0X123, -0x123.
When type is 'BitString'
Append bit string to cell data.
value: string – Bit string content using bitstring notation. See TON VM specification 1.0.
Contains hexadecimal string representation:
- Can end with _ tag.
- Can be prefixed with x or X.
- Can be prefixed with x{ or X{ and ended with }.
Contains binary string represented as a sequence
of 0 and 1 prefixed with n or N.
Examples:
1AB, x1ab, X1AB, x{1abc}, X{1ABC}
2D9_, x2D9_, X2D9_, x{2D9_}, X{2D9_}
n00101101100, N00101101100
When type is 'Cell'
Append ref to nested cells
builder: BuilderOp[] – Nested cell builder
When type is 'CellBoc'
Append ref to nested cell
boc: string – Nested cell BOC encoded with base64 or BOC cache key.
Variant constructors:
function builderOpInteger(size: number, value: any): BuilderOp;
function builderOpBitString(value: string): BuilderOp;
function builderOpCell(builder: BuilderOp[]): BuilderOp;
function builderOpCellBoc(boc: string): BuilderOp;
*/
data class BuilderOp(
val type: BuilderOpType,
val size: Int? = null,
val value: Any? = null,
val builder: List<BuilderOp>? = null,
val boc: String? = null
)

enum class BuilderOpType {
Integer, BitString, Cell, CellBoc
}

data class ResultOfEncodeBoc(
val boc: String
)

data class ResultOfGetBocHash(
val hash: String
)

data class ParamsOfGetBocHash(
val boc: String
)

data class ParamsOfGetCodeFromTvc(
val tvc: String
)

data class ResultOfGetCodeFromTvc(
val code: String
)

data class ParamsOfParseShardstate(
val boc: String,
val id: String,
val workchainId: Int
)

data class ResultOfGetBlockchainConfig(
val configBoc: String
)

data class ParamsOfGetBlockchainConfig(
val blockBoc: String
)

data class ParamsOfParse(
val boc: String
)

data class ResultOfParse(
val parsed: Map<String, Any>
)
10 changes: 10 additions & 0 deletions src/test/kotlin/ee/nx01/tonclient/boc/BocModuleTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,14 @@ class BocModuleTest : StringSpec({

}

"Should be able encode boc" {
val client = TonClient()

val response = client.boc.encodeBoc(ParamsOfEncodeBoc(listOf(BuilderOp(BuilderOpType.Integer, size = 4, value = "123")), BocCacheType()))

response shouldNotBe null
response.boc shouldBe "*72aa2f825294ca27c3febecaa3d8900331bcd86f359683cf4eb7908a1d5e6c75"

}

})

0 comments on commit 23f95b1

Please sign in to comment.