Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

bsc-php

A PHP library for interacting with BNB Smart Chain (BSC).


Architecture

All blockchain interactions go through the ProxyApi interface.

It defines:

  • sending raw transactions
  • querying balances
  • eth_call
  • fetching nonces
  • reading blocks and transaction receipts

Implementations

  • BscscanApi – uses BscScan HTTP endpoints
  • NodeApi – uses JSON-RPC nodes

Wallets

  • Wallet generation: BIP39
  • Key derivation: BIP44

Private keys are local only. Addresses derived with elliptic curves + keccak hashing.


Low-level Helpers

Utils.php includes:

  • hex encoding/decoding
  • keccak hashing
  • big number math
  • wei ⇄ ether conversion
  • decimal handling
  • small HTTP helpers

ABI Formatting

Formatter.php converts method signatures (e.g. balanceOf(address)) into ABI-encoded call data (function selector + padded parameters) for eth_call and contract calls.


Transactions

Native BNB

Bnb.php builds raw transactions, signs them using the web3p/ethereum-tx library, and sends them via ProxyApi.

BEP20 Tokens

BEP20.php handles balances and transfers.


Installation

composer require web3p/ethereum-tx kornrunner/keccak phpseclib/phpseclib guzzlehttp/guzzle

Examples

Create a New Wallet

require __DIR__ . '/vendor/autoload.php';
use Binance\Wallet;

$account = Wallet::newAccountByMnemonic();

echo $account['mnemonic'] . PHP_EOL;
echo $account['address']  . PHP_EOL;
echo $account['key']      . PHP_EOL;

Restore Wallet From Private Key

use Binance\Wallet;

$account = Wallet::revertAccountByPrivateKey('0xYOUR_PRIVATE_KEY');

echo $account['address'] . PHP_EOL;

Call BEP20 Balance Using BscScan

require __DIR__ . '/vendor/autoload.php';
use Binance\BscscanApi;
use Binance\Formatter;

$bsc = new BscscanApi('YOUR_BSCSCAN_API_KEY', 'mainnet');

$address  = '0xYourAddress';
$contract = '0x55d398326f99059fF775485246999027B3197955'; // USDT

$method = Formatter::toMethodFormat('balanceOf(address)');
$addr   = Formatter::toAddressFormat($address);

$params = [
    'to'   => $contract,
    'data' => '0x' . $method . $addr
];

$result = $bsc->ethCall($params);
var_dump($result);

Send Native BNB Using JSON-RPC (Testnet)

require __DIR__ . '/vendor/autoload.php';
use Binance\NodeApi;
use Binance\Bnb;

$node = new NodeApi(
    'https://data-seed-prebsc-1-s1.binance.org:8545/',
    null,
    null,
    'testnet'
);

$bnb = new Bnb($node);

$txHash = $bnb->transfer(
    '0xYOUR_PRIVATE_KEY',
    '0xRecipientAddress',
    0.01
);

var_dump($txHash);

Disclaimer:

This project is not affiliated with, endorsed by, or maintained by Binance or BNB Chain.

About

A lightweight PHP library for interacting with BNB Smart Chain (BSC). Send raw BNB, interact with BEP20 tokens, generate BIP39 wallets offline, and query chain data via BscScan or JSON-RPC.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages