Installation:
composer require ranabd36/larathereum
Publish Config:
php artisan vendor:publish --provider="Larathereum\LarathereumServiceProvider"
config/larathereum.php to define:
node_url (e.g., https://mainnet.infura.io/v3/YOUR_KEY)port (if applicable, often omitted for HTTP/HTTPS nodes)private_key (for signing transactions, stored securely in .env or encrypted config).First Use Case: Fetch an account balance:
use Larathereum\Larathereum;
$balance = Larathereum::balance('0x123...abc');
dd($balance); // Returns wei as integer
Contract Interactions (ERC20):
// Approve tokens (spender, amount in wei)
Larathereum::approve('0xContractAddress', '0xSpender', $amount);
// Transfer tokens
Larathereum::transfer('0xContractAddress', '0xRecipient', $amount);
TokenService) to abstract gas/fee logic.Transaction Handling:
Larathereum::sendTransaction([
'to' => '0xRecipient',
'value' => $weiAmount,
'gas' => 21000,
'gasPrice' => Larathereum::gasPrice(), // Auto-fetch
]);
Bus queue for async transactions (e.g., SendTransactionJob).Event Listening:
Transfer):
Larathereum::on('0xContractAddress', 'Transfer', function ($event) {
// Log or process event data
});
Event facade for cross-cutting concerns.Multi-Network Support:
config(['larathereum.node_url' => 'https://goerli.infura.io/v3/KEY']);
Larathereum with traits for reusable logic:
trait TokenHandler {
public function mintTokens($address, $amount) {
return Larathereum::callContract(
'0xMinterContract',
'mint',
[$address, $amount],
['from' => auth()->user()->walletAddress]
);
}
}
laravel/web3-php mocks or local nodes (e.g., Ganache) for unit tests:
$this->partialMock(Larathereum::class, function ($mock) {
$mock->shouldReceive('balance')->andReturn(1000);
});
Gas Estimation:
Larathereum::estimateGas() before sending:
$gas = Larathereum::estimateGas([
'to' => '0xContract',
'data' => '0x...',
]);
Private Key Security:
.env and use Laravel’s env():
config(['larathereum.private_key' => env('ETH_PRIVATE_KEY')]);
Rate Limits:
try {
Larathereum::call(...);
} catch (RateLimitException $e) {
sleep(2 ** $attempt++);
retry();
}
Event Log Parsing:
web3-php's Log class or ABI tools:
$event = Larathereum::decodeLog($log, $abi);
Deprecated Methods:
src/Larathereum.php for undocumented features (e.g., callContractRaw).Enable Logging:
Add to config/larathereum.php:
'debug' => env('APP_DEBUG', false),
Logs appear in storage/logs/larathereum.log.
Common Errors:
| Error | Cause | Solution |
|---|---|---|
Invalid JSON RPC response |
Node connection issues | Verify node_url and network |
Nonce too low |
Stale nonce | Use Larathereum::getNonce() |
Insufficient funds |
Wrong account or balance | Check balance() and gas costs |
Custom Contracts: Extend the package by adding contract-specific methods:
// In a service class
public function stake($address, $amount) {
return Larathereum::callContract(
'0xStakingContract',
'stake',
[$address, $amount],
['gas' => 300000]
);
}
Middleware: Add transaction validation middleware:
public function handle($request, Closure $next) {
if ($request->input('tx_hash')) {
$receipt = Larathereum::getTransactionReceipt($request->tx_hash);
if (!$receipt->status) throw new TransactionFailedException();
}
return $next($request);
}
Fallback Nodes: Implement multi-node redundancy:
public function safeCall(callable $callback) {
foreach (config('larathereum.nodes') as $node) {
config(['larathereum.node_url' => $node]);
try {
return $callback();
} catch (Exception $e) {
continue;
}
}
throw new NodeUnavailableException();
}
How can I help you explore Laravel packages today?