-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b306a73
commit 4086b55
Showing
17 changed files
with
885 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
@file:Suppress("PackageDirectoryMismatch") | ||
|
||
package org.ton.kotlin.config | ||
|
||
import org.ton.bigint.div | ||
import org.ton.bigint.times | ||
import org.ton.bigint.toBigInt | ||
import org.ton.block.AddrStd | ||
import org.ton.block.Coins | ||
import org.ton.cell.CellBuilder | ||
import org.ton.cell.CellSlice | ||
import org.ton.kotlin.cell.CellContext | ||
import org.ton.tlb.TlbCodec | ||
|
||
public data class BurningConfig( | ||
val blackholeAddress: AddrStd?, | ||
val feeBurnNum: Int, | ||
val feeBurnDenom: Int | ||
) { | ||
init { | ||
require(feeBurnDenom >= 1) { "feeBurnDenom must be at least 1, actual: $feeBurnDenom" } | ||
require(feeBurnNum in 0..feeBurnDenom) { "feeBurnNum must be in 0..$feeBurnDenom, actual: $feeBurnNum" } | ||
} | ||
|
||
public fun calculateBurnedFees(value: Coins): Coins { | ||
if (value == Coins.ZERO) return value | ||
return Coins(value.amount.value.times(feeBurnNum.toBigInt()).div(feeBurnDenom.toBigInt())) | ||
} | ||
|
||
public companion object : TlbCodec<BurningConfig> by BurningConfigTlbCodec | ||
} | ||
|
||
private object BurningConfigTlbCodec : TlbCodec<BurningConfig> { | ||
override fun loadTlb(slice: CellSlice, context: CellContext): BurningConfig { | ||
val tag = slice.loadUInt(8).toInt() | ||
require(tag == 0x01) { "Invalid BurningConfig tag: ${tag.toHexString()}" } | ||
val blackholeAddress = if (slice.loadBoolean()) AddrStd(-1, slice.loadByteArray(32)) else null | ||
val feeBurnNum = slice.loadUInt(32).toInt() | ||
val feeBurnDenom = slice.loadUInt(32).toInt() | ||
return BurningConfig(blackholeAddress, feeBurnNum, feeBurnDenom) | ||
} | ||
|
||
override fun storeTlb(builder: CellBuilder, value: BurningConfig) { | ||
builder.storeUInt(0x01, 8) | ||
if (value.blackholeAddress != null) { | ||
builder.storeBoolean(true) | ||
builder.storeBitString(value.blackholeAddress.address) | ||
} else { | ||
builder.storeBoolean(false) | ||
} | ||
builder.storeUInt(value.feeBurnNum, 32) | ||
builder.storeUInt(value.feeBurnDenom, 32) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package org.ton.block.config | ||
|
||
import org.ton.bigint.* | ||
import org.ton.block.Coins | ||
import org.ton.block.StorageUsedShort | ||
import org.ton.cell.CellBuilder | ||
import org.ton.cell.CellSlice | ||
import org.ton.kotlin.cell.CellContext | ||
import org.ton.tlb.TlbCodec | ||
|
||
/** | ||
* Storage prices for some interval. | ||
*/ | ||
public data class StoragePrices( | ||
/** | ||
* Unix timestamp in seconds since which this prices are used. | ||
*/ | ||
val validSince: Long, | ||
|
||
/** | ||
* Bit price in base workchain. | ||
*/ | ||
val bitPrice: Long, | ||
|
||
/** | ||
* Cell price in base workchain. | ||
*/ | ||
val cellPrice: Long, | ||
|
||
/** | ||
* Bit price in masterchain. | ||
*/ | ||
val mcBitPrice: Long, | ||
|
||
/** | ||
* Cell price in masterchain. | ||
*/ | ||
val mcCellPrice: Long, | ||
) { | ||
/** | ||
* Computes the amount of fees for storing [stats] data for [delta] seconds. | ||
*/ | ||
public fun computeStorageFee( | ||
isMasterchain: Boolean, | ||
delta: Long, | ||
stats: StorageUsedShort | ||
): Coins { | ||
var result = if (isMasterchain) { | ||
(stats.cellCount.toBigInt().times(mcCellPrice.toBigInt())).plus( | ||
(stats.bitCount.toBigInt().times(mcBitPrice.toBigInt())) | ||
) | ||
} else { | ||
(stats.cellCount.toBigInt().times(cellPrice.toBigInt())).plus( | ||
(stats.bitCount.toBigInt().times(bitPrice.toBigInt())) | ||
) | ||
} | ||
result = result.times(delta.toBigInt()) | ||
val r = result.and(0xFFFF.toBigInt()).toLong() != 0L | ||
result = result.shr(16) | ||
if (r) { | ||
result = result.plus(1.toBigInt()) | ||
} | ||
return Coins(result) | ||
} | ||
|
||
public companion object : TlbCodec<StoragePrices> by StoragePricesTlbCodec | ||
} | ||
|
||
private object StoragePricesTlbCodec : TlbCodec<StoragePrices> { | ||
override fun loadTlb(slice: CellSlice, context: CellContext): StoragePrices { | ||
val tag = slice.loadUInt(8).toInt() | ||
require(tag == 0xCC) { "Invalid StorageUsedShort tag: ${tag.toHexString()}" } | ||
val validSince = slice.loadUInt(32).toLong() | ||
val bitPrice = slice.loadULong().toLong() | ||
val cellPrice = slice.loadULong().toLong() | ||
val mcBitPrice = slice.loadULong().toLong() | ||
val mcCellPrice = slice.loadULong().toLong() | ||
return StoragePrices(validSince, bitPrice, cellPrice, mcBitPrice, mcCellPrice) | ||
} | ||
|
||
override fun storeTlb(builder: CellBuilder, value: StoragePrices, context: CellContext) { | ||
builder.storeUInt(0xCC, 8) | ||
builder.storeUInt(value.validSince, 32) | ||
builder.storeULong(value.bitPrice.toULong(), 64) | ||
builder.storeULong(value.cellPrice.toULong(), 64) | ||
builder.storeULong(value.mcBitPrice.toULong(), 64) | ||
builder.storeULong(value.mcCellPrice.toULong(), 64) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.ton.kotlin.examples | ||
|
||
import org.ton.block.AccountActive | ||
import org.ton.kotlin.examples.contract.config.ConfigContract | ||
import org.ton.kotlin.examples.contract.config.ConfigData | ||
import org.ton.kotlin.examples.provider.LiteClientProvider | ||
import org.ton.kotlin.examples.provider.liteClientMainnet | ||
|
||
private val provider = LiteClientProvider(liteClientMainnet()) | ||
|
||
suspend fun main() { | ||
val configContract = ConfigContract(provider) | ||
|
||
val account = configContract.getState()?.loadAccount() ?: error("cant load account not found") | ||
val data = (account.state as AccountActive).value.data.value?.cell ?: error("cant load data") | ||
val configData = ConfigData.loadTlb(data.beginParse()) | ||
println("seqno: ${configData.seqno}") | ||
println("public key: ${configData.publicKey}") | ||
println("voteDict: ${configData.voteDict}") | ||
for (entry in configData.voteDict) { | ||
println("proposal: ${entry.key}") | ||
println(entry.value) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.ton.kotlin.examples | ||
|
||
import org.ton.api.pk.PrivateKeyEd25519 | ||
import org.ton.block.AddrStd | ||
import org.ton.block.Coins | ||
import org.ton.block.CurrencyCollection | ||
import org.ton.contract.wallet.WalletTransferBuilder | ||
import org.ton.contract.wallet.WalletV4R2Contract | ||
import org.ton.kotlin.account.balance | ||
import org.ton.kotlin.examples.faucet.TestnetFaucet | ||
import org.ton.kotlin.examples.provider.LiteClientProvider | ||
import org.ton.kotlin.examples.provider.liteClientTestnet | ||
import kotlin.random.Random | ||
|
||
private val provider = LiteClientProvider(liteClientTestnet()) | ||
|
||
suspend fun main() { | ||
val key = PrivateKeyEd25519(Random(42)) | ||
val wallet = WalletV4R2Contract(provider.liteClient, WalletV4R2Contract.address(key)) | ||
if (provider.getAccount(wallet.address)?.loadAccount().balance == CurrencyCollection.ZERO) { | ||
TestnetFaucet(provider).topUpContract(wallet.address, Coins.of(1)) | ||
} | ||
val transfer = WalletTransferBuilder().apply { | ||
destination = AddrStd("0QCaLIqf03-qGREcf-Novb6H3UOXmIB1cLXxAJDxqrwe3rbP") | ||
currencyCollection = CurrencyCollection(Coins.ofNano(1)) | ||
bounceable = false | ||
} | ||
|
||
val res = wallet.transfer(key, transfer.build()) | ||
println(res) | ||
} |
Oops, something went wrong.