Installation:
composer require amashukov/ton-cell-php
Ensure ext-gmp is enabled in your PHP environment.
First Use Case: Build a simple cell for a Jetton transfer:
use Amashukov\TonCell\Builder;
$cell = (new Builder())
->storeUint(0xF8A7EA5, 32) // Jetton transfer opcode
->storeUint(12345, 64) // Query ID
->storeCoins('1000000') // Amount in nanoTON
->storeAddress(new AddressData(0, 'hashPart32')) // Destination address
->endCell();
Key Classes to Explore:
Builder: For constructing cells.Slice: For parsing cells.Boc: For serializing cells to BOC format.AddressData: For handling TON addresses.Use nested builders for hierarchical data:
$nestedCell = (new Builder())
->storeUint(1, 8)
->endCell();
$mainCell = (new Builder())
->storeRef($nestedCell)
->storeUint(2, 8)
->endCell();
Mirror the builder structure in parsing:
$slice = $cell->beginParse();
$op = $slice->loadUint(32);
$queryId = $slice->loadUint(64);
$amount = $slice->loadCoins();
$address = $slice->loadAddress();
Use decimal strings for values exceeding PHP_INT_MAX:
$builder->storeUint('12345678901234567890', 256); // Big integer
$slice->loadUintString(256); // Returns string for large values
Serialize cells to BOC format for network transmission:
$bocBytes = Boc::encode($cell);
$bocBase64 = Boc::encodeBase64($cell); // Ready for toncenter API
Use AddressData for address operations:
$addressData = new AddressData(0, 'hashPart32');
$builder->storeAddress($addressData);
$parsedAddress = $slice->loadAddress();
Compute hashes for signing or address derivation:
$hash = $cell->hash(); // 32-byte SHA-256 hash
Service Provider: Bind the package classes to Laravel's container for dependency injection:
public function register()
{
$this->app->bind(Builder::class, function () {
return new Builder();
});
}
Facades (Optional): Create facades for cleaner syntax:
// app/Facades/TonCell.php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
use Amashukov\TonCell\Builder;
class TonCell extends Facade
{
protected static function getFacadeAccessor()
{
return Builder::class;
}
}
Jobs/Commands: Use the package in Laravel jobs or Artisan commands for background processing:
use Amashukov\TonCell\Builder;
use Amashukov\TonCell\Boc;
class ProcessTonTransaction implements ShouldQueue
{
public function handle()
{
$cell = (new Builder())->storeUint(1, 8)->endCell();
$boc = Boc::encodeBase64($cell);
// Send to TON network via toncenter-client-php
}
}
amashukov/toncenter-client-php for sending BOCs:
use Amashukov\TonCenterClient\Client;
$client = new Client('https://toncenter.com/api/v2/jsonRPC');
$bocBase64 = Boc::encodeBase64($cell);
$response = $client->sendBoc($bocBase64);
storeUint with incorrect bit lengths) will corrupt the cell.addr_std$10 requires 256 bits for the hash part).PHP_INT_MAX must be passed as strings (e.g., '12345678901234567890').gmp_strval for debugging large integers.AddressData only handles the addr_std$10 format. Full address parsing (e.g., EQ... or UQ... base64) requires additional logic.amashukov/ton-wallet-php for full address handling.offset_byte_size from 1 to 2 bytes if the cell data exceeds 255 bytes.hash() method returns a 32-byte SHA-256 hash of the recursive TLB representation, not the raw bytes.amashukov/ton-crypto-php).Slice::load* methods throw RuntimeException if they overrun the cell's bit length.remainingBits() and remainingRefs() before parsing.Use Slice to debug cell contents:
$slice = $cell->beginParse();
while (!$slice->isEmpty()) {
echo "Remaining bits: " . $slice->remainingBits() . "\n";
$value = $slice->loadUint(8); // Load 1 byte at a time
echo "Byte: " . dechex($value) . "\n";
}
@ton/coreValidate outputs against the TypeScript SDK:
# Example: Compare BOC outputs
node -e "const { Cell } = require('@ton/core'); const cell = Cell.fromBoc('...'); console.log(cell.toBoc({ idx: false }).toString('base64'));"
Log bit-level operations to catch misalignments:
$builder->storeUint(123, 8)->storeUint(456, 16); // Log these operations
Extend Builder or Slice to support custom TLB types:
class CustomBuilder extends Builder
{
public function storeCustomType($value)
{
// Implement custom logic
return $this->storeUint($value, 32);
}
}
Override Boc::encode() for non-canonical formats:
class CustomBoc
{
public static function encode(Cell $cell): string
{
// Custom BOC logic (e.g., no CRC32C)
return 'custom-boc-' . $cell->hash();
}
}
Extend AddressData for full address support:
class FullAddressData extends AddressData
{
public function __construct(string $address)
{
// Parse EQ/UQ base64, validate CRC-16, etc.
parent::__construct(0, 'hashPart32');
}
}
Use BitReader for low-level parsing of non-TLB data:
$reader = new BitReader($binaryString, $bitLength);
How can I help you explore Laravel packages today?