Created
February 9, 2023 21:03
-
-
Save 90K2/70fdad1eaa9b28e9c854697eaa3d204b to your computer and use it in GitHub Desktop.
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
import org.ton.api.pk.PrivateKeyEd25519 | |
import org.ton.bigint.BigInt | |
import org.ton.bitstring.BitString | |
import org.ton.block.* | |
import org.ton.boc.BagOfCells | |
import org.ton.cell.Cell | |
import org.ton.cell.CellBuilder | |
import org.ton.contract.wallet.WalletContract | |
import org.ton.contract.wallet.WalletTransfer | |
import org.ton.crypto.hex | |
import org.ton.hashmap.HashMapE | |
import org.ton.lite.api.LiteApi | |
import org.ton.tlb.CellRef | |
import org.ton.tlb.constructor.AnyTlbConstructor | |
import org.ton.tlb.constructor.tlbCodec | |
import org.ton.tlb.storeTlb | |
import org.ton.tlb.storeRef | |
class HighloadWalletV2( | |
override val privateKey: PrivateKeyEd25519, | |
override val workchain: Int = 0, | |
override val subWalletId: Int = WalletContract.DEFAULT_WALLET_ID + workchain | |
) : AbstractWallet(privateKey, workchain, subWalletId) { | |
private fun generateQueryId(timeout: BigInt): BigInt { | |
return (BigInt.valueOf(utcLongNow()) + timeout).shiftLeft(32) | |
} | |
override fun createDataInit(): Cell = CellBuilder.createCell { | |
storeUInt(subWalletId, 32) // stored_subwallet | |
storeUInt(0, 64) // last_cleaned | |
storeBytes(privateKey.publicKey().key.toByteArray()) | |
storeTlb(HashMapE.tlbCodec(16, Cell.tlbCodec()), HashMapE.empty()) // old_queries | |
} | |
override val code: Cell = CODE | |
override val address: AddrStd = address() | |
companion object { | |
// https://github.com/ton-blockchain/ton/blob/master/crypto/smartcont/highload-wallet-v2-code.fc | |
val CODE = BagOfCells( | |
hex("B5EE9C724101090100E5000114FF00F4A413F4BCF2C80B010201200203020148040501EAF28308D71820D31FD33FF823AA1F5320B9F263ED44D0D31FD33FD3FFF404D153608040F40E6FA131F2605173BAF2A207F901541087F910F2A302F404D1F8007F8E16218010F4786FA5209802D307D43001FB009132E201B3E65B8325A1C840348040F4438AE63101C8CB1F13CB3FCBFFF400C9ED54080004D03002012006070017BD9CE76A26869AF98EB85FFC0041BE5F976A268698F98E99FE9FF98FA0268A91040207A0737D098C92DBFC95DD1F140034208040F4966FA56C122094305303B9DE2093333601926C21E2B39F9E545A") | |
).first() | |
} | |
suspend fun transfer(liteApi: LiteApi, transfers: List<WalletTransfer>) { | |
require(transfers.isNotEmpty() && transfers.size <= 254) { throw BadRequestException("wrong transfers size") } | |
val message = createTransferMessage( | |
address(), createStateInit(), transfers | |
) | |
sendExternalMessage(liteApi, message) | |
} | |
fun createTransferMessage( | |
address: AddrStd, | |
stateInit: StateInit?, | |
transfers: List<WalletTransfer> | |
): Message<Cell> { | |
val info = ExtInMsgInfo( | |
src = AddrNone, | |
dest = address, | |
importFee = Coins() | |
) | |
val maybeStateInit = | |
Maybe.of(stateInit?.let { Either.of<StateInit, CellRef<StateInit>>(null, CellRef(it)) }) | |
val transfersBody = transfers.mapIndexed { index, walletTransfer -> | |
index to walletTransfer | |
}.toMap() | |
val giftBody = CellBuilder.createCell { | |
storeUInt(subWalletId, 32) | |
storeUInt(generateQueryId(BigInt.valueOf(60)), 64) | |
storeRef(serializeMap(transfersBody, 16) { src, cb -> | |
cb.storeUInt(src.sendMode, 8) | |
cb.storeRef(MessageRelaxed.tlbCodec(AnyTlbConstructor), CellRef(createIntMsg(src))) | |
}) | |
} | |
val body = Either.of<Cell, CellRef<Cell>>(null, CellRef(giftBody)) | |
return Message( | |
info = info, | |
init = maybeStateInit, | |
body = body | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment