Installation
composer require amashukov/ton-php
Ensure your environment meets requirements: PHP 8.3+, ext-gmp, ext-sodium, and ext-bcmath.
First Use Case: Wallet Creation & Transaction
use Amashukov\TonWallet\WalletV4R2;
use Amashukov\TonWallet\Mnemonic;
use Amashukov\Toncenter\ToncenterClient;
use Amashukov\Toncenter\ToncenterWalletRpc;
use Amashukov\HttpClient\CurlHttpClient;
// 1. Generate a mnemonic and derive a wallet
$mnemonic = Mnemonic::generate();
$wallet = WalletV4R2::fromMnemonic($mnemonic);
// 2. Set up toncenter client
$httpClient = new CurlHttpClient();
$toncenter = new ToncenterClient($httpClient, 'https://toncenter.com/api/v2/jsonRPC');
$walletRpc = new ToncenterWalletRpc($toncenter, $wallet->getAddress());
// 3. Get wallet balance and send a transaction
$balance = $walletRpc->getBalance();
$walletRpc->sendTransfer('EQD...', 1000000000); // 1 TON
Key Files to Explore
src/Amashukov/TonWallet/WalletV4R2.php (Wallet logic)src/Amashukov/TonCell/Builder.php (Cell construction)src/Amashukov/Toncenter/ToncenterClient.php (TON Center API)Wallet Management
Mnemonic::generate() and WalletV4R2::fromMnemonic() for secure wallet creation.WalletV4R2::sign() for transaction signing before broadcasting.ToncenterWalletRpc to abstract wallet operations (balance, transfers) via TON Center.Cell & BOC Handling
Builder to construct TON cells (e.g., for smart contract interactions).
$builder = new Builder();
$builder->storeUint(123, 32); // Store a 32-bit unsigned integer
$cell = $builder->asCell();
Slice to decode BOC-encoded cells (e.g., from contract state).Typed Toncenter Client
ToncenterClient with any PSR-18 HTTP client (e.g., CurlHttpClient).ToncenterClient::getWalletBalance() returns Balance).Address Parsing
Address::parse() to validate and parse TON addresses (UQ/EQ, bounceable flags).
$address = Address::parse('EQD...'); // Returns Address object
public function register()
{
$this->app->singleton(ToncenterClient::class, function ($app) {
return new ToncenterClient(
new CurlHttpClient(),
config('toncenter.endpoint')
);
});
}
wallet.transfer.sent).ToncenterWalletRpc for unit tests by implementing WalletRpcInterface.BOC Canonical Encoding
Builder must match @ton/core byte-for-byte. Test with:
$boc = $cell->toBoc();
$expectedBoc = file_get_contents('expected.boc');
assert($boc === $expectedBoc);
Mnemonic Security
Toncenter Rate Limits
use Symfony\Component\HttpClient\RetryableHttpClient;
$httpClient = new RetryableHttpClient(
new CurlHttpClient(),
[
'max_retries' => 3,
'delay' => 100,
]
);
Address Formats
Address::parse('EQD...', true)).Address::isValid() to catch malformed addresses early.Builder::debug() to visualize cell structure:
$builder->debug(); // Outputs human-readable cell layout
ToncenterClient:
$toncenter = new ToncenterClient($httpClient, 'https://toncenter.com/api/v2/jsonRPC', [
'logger' => new \Monolog\Logger('toncenter'),
]);
$keyPair = $wallet->getKeyPair();
$signature = $keyPair->sign('message');
$keyPair->verify('message', $signature); // Returns bool
Custom RPC Clients
WalletRpcInterface for alternative backends (e.g., Jetton, Tonkeeper).class CustomWalletRpc implements WalletRpcInterface {
public function getBalance(): Balance { ... }
public function sendTransfer(string $to, int $amount): void { ... }
}
Smart Contract Interactions
Builder to support custom contract messages:class MyContractBuilder extends Builder {
public function buildCustomMessage(): Cell {
// Add contract-specific fields
return $this->asCell();
}
}
Symfony Integration
blockchain-context-bundle to wire the SDK into Symfony services:# config/packages/amashukov_blockchain.yaml
amashukov_blockchain:
ton:
wallet: '@Amashukov\TonWallet\WalletV4R2'
rpc: '@Amashukov\Toncenter\ToncenterWalletRpc'
Testing Utilities
ToncenterClient for tests:$mockToncenter = $this->createMock(ToncenterClient::class);
$mockToncenter->method('getWalletBalance')
->willReturn(new Balance(1000000000));
```markdown
### Config Quirks
- **HTTP Client**: The SDK expects a PSR-18 client. Avoid `GuzzleHttp\Client` directly; use `amashukov/http-client-php` for compatibility.
- **Toncenter Endpoint**: Hardcode endpoints in config or use environment variables:
```php
$toncenter = new ToncenterClient($httpClient, env('TONCENTER_ENDPOINT'));
gmp extension is enabled and configured for high precision.Builder instances for multiple operations to avoid overhead.ToncenterClient::batch() to reduce API calls:
$toncenter->batch([
'getWalletBalance',
'getTransaction',
]);
How can I help you explore Laravel packages today?