1+ package org.ton.block.config
2+
3+ import org.ton.bigint.*
4+ import org.ton.block.Coins
5+ import org.ton.block.StorageUsedShort
6+ import org.ton.cell.CellBuilder
7+ import org.ton.cell.CellSlice
8+ import org.ton.kotlin.cell.CellContext
9+ import org.ton.tlb.TlbCodec
10+
11+ /* *
12+ * Storage prices for some interval.
13+ */
14+ public data class StoragePrices (
15+ /* *
16+ * Unix timestamp in seconds since which this prices are used.
17+ */
18+ val validSince : Long ,
19+
20+ /* *
21+ * Bit price in base workchain.
22+ */
23+ val bitPrice : Long ,
24+
25+ /* *
26+ * Cell price in base workchain.
27+ */
28+ val cellPrice : Long ,
29+
30+ /* *
31+ * Bit price in masterchain.
32+ */
33+ val mcBitPrice : Long ,
34+
35+ /* *
36+ * Cell price in masterchain.
37+ */
38+ val mcCellPrice : Long ,
39+ ) {
40+ /* *
41+ * Computes the amount of fees for storing [stats] data for [delta] seconds.
42+ */
43+ public fun computeStorageFee (
44+ isMasterchain : Boolean ,
45+ delta : Long ,
46+ stats : StorageUsedShort
47+ ): Coins {
48+ var result = if (isMasterchain) {
49+ (stats.cellCount.toBigInt().times(mcCellPrice.toBigInt())).plus(
50+ (stats.bitCount.toBigInt().times(mcBitPrice.toBigInt()))
51+ )
52+ } else {
53+ (stats.cellCount.toBigInt().times(cellPrice.toBigInt())).plus(
54+ (stats.bitCount.toBigInt().times(bitPrice.toBigInt()))
55+ )
56+ }
57+ result = result.times(delta.toBigInt())
58+ val r = result.and (0xFFFF .toBigInt()).toLong() != 0L
59+ result = result.shr(16 )
60+ if (r) {
61+ result = result.plus(1 .toBigInt())
62+ }
63+ return Coins (result)
64+ }
65+
66+ public companion object : TlbCodec <StoragePrices > by StoragePricesTlbCodec
67+ }
68+
69+ private object StoragePricesTlbCodec : TlbCodec<StoragePrices> {
70+ override fun loadTlb (slice : CellSlice , context : CellContext ): StoragePrices {
71+ val tag = slice.loadUInt(8 ).toInt()
72+ require(tag == 0xCC ) { " Invalid StorageUsedShort tag: ${tag.toHexString()} " }
73+ val validSince = slice.loadUInt(32 ).toLong()
74+ val bitPrice = slice.loadULong().toLong()
75+ val cellPrice = slice.loadULong().toLong()
76+ val mcBitPrice = slice.loadULong().toLong()
77+ val mcCellPrice = slice.loadULong().toLong()
78+ return StoragePrices (validSince, bitPrice, cellPrice, mcBitPrice, mcCellPrice)
79+ }
80+
81+ override fun storeTlb (builder : CellBuilder , value : StoragePrices , context : CellContext ) {
82+ builder.storeUInt(0xCC , 8 )
83+ builder.storeUInt(value.validSince, 32 )
84+ builder.storeULong(value.bitPrice.toULong(), 64 )
85+ builder.storeULong(value.cellPrice.toULong(), 64 )
86+ builder.storeULong(value.mcBitPrice.toULong(), 64 )
87+ builder.storeULong(value.mcCellPrice.toULong(), 64 )
88+ }
89+ }
0 commit comments