PHP code example of roberts / web3-laravel

1. Go to this page and download the library: Download roberts/web3-laravel library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

roberts / web3-laravel example snippets


return [
    'use_database' => env('WEB3_USE_DATABASE', true),
    'default_rpc' => env('WEB3_DEFAULT_RPC', 'https://mainnet.base.org'),
    'default_chain_id' => env('WEB3_DEFAULT_CHAIN_ID', 8453),
    'request_timeout' => env('WEB3_REQUEST_TIMEOUT', 10),
    'networks' => [
        // 1 => 'https://mainnet.infura.io/v3/xxx',
        8453 => 'https://mainnet.base.org',
        // 84532 => 'https://sepolia.base.org',
    ],
    // Optional client tuning
    'rpc' => [
        'retries' => 2,
        'backoff_ms' => 200,
        'headers' => [
            // 'Authorization' => 'Bearer ...',
        ],
    ],
    'confirmations_

use Roberts\Web3Laravel\Protocols\Evm\EvmClientInterface;

// Resolve the native EVM JSON-RPC client
/** @var EvmClientInterface $evm */
$evm = app(EvmClientInterface::class);

$blockHex = $evm->blockNumber();
$gasPriceHex = $evm->gasPrice();

use Roberts\Web3Laravel\Models\Wallet;
use Roberts\Web3Laravel\Models\Contract;

$wallet = Wallet::first();
$balance = $wallet->balance(); // getBalance()
$nonce = $wallet->nonce();     // getTransactionCount()
$gas = $wallet->gasPrice();    // getGasPrice()

// Estimate gas for a potential transaction from this wallet
$estimatedGasHex = $wallet->estimateGas([
	'to' => '0x0000000000000000000000000000000000000000',
	'value' => 1000,
	// 'data' => '0x...', // optional
]);

// Send a transaction (legacy fields; signing library 

use App\Models\User;
use Roberts\Web3Laravel\Models\Wallet;

$user = User::first();

// Create a wallet and associate to a user
$wallet = Wallet::create([
	'address' => '0x...',
	'key' => '0x...', // will be encrypted by the model mutator
	'owner_id' => $user->id,
]);

// Or via the service (recommended): generates keys and sets owner automatically
$wallet = app(Roberts\Web3Laravel\Services\WalletService::class)
	->create([], $user);

// Access the owner and query by owner
$owner = $wallet->user; // belongsTo the configured auth user model
$wallets = Wallet::forUser($user)->get();
bash
php artisan vendor:publish --tag="web3-laravel-migrations"
php artisan migrate

# Optional: seed common chains
php artisan db:seed --class="Roberts\\Web3Laravel\\Database\\Seeders\\BlockchainSeeder"
bash
php artisan vendor:publish --tag="web3-laravel-config"
bash
php artisan web3:ping