PHPackages                             iamirnet/sweb3 - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. iamirnet/sweb3

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

iamirnet/sweb3
==============

Web3 library in PHP

0.1.0(3mo ago)02MITPHP

Since Feb 18Pushed 2mo agoCompare

[ Source](https://github.com/iamirnet/sweb3)[ Packagist](https://packagist.org/packages/iamirnet/sweb3)[ RSS](/packages/iamirnet-sweb3/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (6)Versions (2)Used By (0)

SWeb3
=====

[](#sweb3)

[![Latest Version on Packagist](https://camo.githubusercontent.com/78bcd559bdd9718ef833ef66d3e4f3652c3ee926117eef672f8a9b10f79dbed0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f69616d69726e65742f73776562332e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/iamirnet/sweb3)[![Licensed under the MIT License](https://camo.githubusercontent.com/08cef40a9105b6526ca22088bc514fbfdbc9aac1ddbf8d4e6c750e3a88a44dca/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d626c75652e737667)](https://github.com/iamirnet/sweb3/blob/master/LICENSE)[![GitHub last commit](https://camo.githubusercontent.com/d63e87f0be0d5c978e572cd251f268a13f33963e6f5d242994c23f2ae5f8002b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6173742d636f6d6d69742f69616d69726e65742f73776562332e7376673f7374796c653d666c61742d737175617265)](#)[![Packagist Downloads](https://camo.githubusercontent.com/7ce912fa5fd18185e65e1180f679157318f0715e9f635ba710e5a5462c7acbe5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f69616d69726e65742f73776562332e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/iamirnet/sweb3)

A php interface for interacting with the Ethereum blockchain and ecosystem.

Features
========

[](#features)

- PHP &gt;= 7.4
- Customizable curl calls
- Call: get net state
- Send signed transactions
- Batch call requests and signed transactions
- Address &amp; private key creation
- Message signing
- Full ABIv2 encode/decode
- Contract creation
- Contract interaction (call/send)
- Contract Events/logs with filters
- Support for ERC20 contracts with non-nominative decimal values
- Examples provided interacting with simple types, strings, tuples, arrays, arrays of tuples with arrays, multi-dimension arrays...

Install
=======

[](#install)

### Latest stable release

[](#latest-stable-release)

```
composer require iamirnet/sweb3 "^0.1.0"

```

Or you can add this line in composer.json

```
"iamirnet/sweb3": "^0.1.0"

```

### Development (main branch)

[](#development-main-branch)

```
composer require iamirnet/sweb3 dev-master

```

Or you can add this line in composer.json

```
"iamirnet/sweb3": "dev-master"

```

 Click for help with installationInstall Composer
----------------

[](#install-composer)

If the above step didn't work, install composer and try again.

#### Debian / Ubuntu

[](#debian--ubuntu)

```
sudo apt-get install curl php-curl
curl -s http://getcomposer.org/installer | php
php composer.phar install

```

Composer not found? Use this command instead:

```
php composer.phar require "iamirnet/sweb3"

```

#### Installing on Windows

[](#installing-on-windows)

Download and install composer:

1.
2. Create a folder on your drive like C:\\iAmirNet\\Security
3. Run command prompt and type `cd C:\iAmirNet\SWeb3`
4. `composer require iamirnet/sweb3`
5. Once complete copy the vendor folder into your project.

Usage
=====

[](#usage)

### New instance

[](#new-instance)

```
use iAmirNet\SWeb3\SWeb3;
//initialize SWeb3 main object
$sweb3 = new SWeb3('http://ethereum.node.provider:optional.node.port');

//optional if not sending transactions
$from_address = '0x0000000000000000000000000000000000000000';
$from_address_private_key = '345346245645435....';
$sweb3->setPersonalData($from_address, $from_address_private_key);
```

### Convert values

[](#convert-values)

- Most calls return hex or BigNumber to represent numbers.
- Most calls expect numeric parameters represented as BigNumbers.

Hex to Big Number:

```
use iAmirNet\SWeb3\Utils;

$res = $sweb3->call('eth_blockNumber', []);
$bigNum = Utils::hexToBn($res->result);
```

Number to BigNumber:

```
$bigNum = Utils::ToBn(123);
```

Get average-human readable string representation from Big Number:

```
$s_number = $bigNum->toString();
```

Format 1 ether to wei (unit required for ether values in transactions):

```
Utils::toWei('0.001', 'ether');
```

Get average-human readable string representation from a value conversion:

```
$s_val = Utils::fromWeiToString('1001', 'kwei'); // "1.001"

$s_val = Utils::toWeiString('1.001', 'kwei'); // "1001"
```

### General ethereum block information call:

[](#general-ethereum-block-information-call)

```
$res = $sweb3->call('eth_blockNumber', []);
```

### Refresh gas price

[](#refresh-gas-price)

```
$gasPrice = $sweb3->getGasPrice();
```

### Estimate gas price (from send params)

[](#estimate--gas-price-from-send-params)

```
$sweb3->chainId = '0x3';//ropsten
$sendParams = [
    'from' =>	$sweb3->personal->address,
    'to' =>     '0x1111111111111111111111111111111111111111',
    'value' => 	Utils::toWei('0.001', 'ether'),
    'nonce' => 	$sweb3->personal->getNonce()
];
$gasEstimateResult = $sweb3->call('eth_estimateGas', [$sendParams]);
```

### Send 0.001 ether to address

[](#send-0001-ether-to-address)

```
//remember to set personal data first with a valid pair of address & private key
$sendParams = [
    'from' =>   	$sweb3->personal->address,
    'to' =>     	'0x1111111111111111111111111111111111111111',
    'gasLimit' => 	210000,
    'value' => 		Utils::toWei('0.001', 'ether'),
    'nonce' => 		$sweb3->personal->getNonce()
];
$result = $sweb3->send($sendParams);
```

### Batch calls &amp; transactions

[](#batch-calls--transactions)

```
//enable batching
$sweb3->batch(true);

$sweb3->call('eth_blockNumber', []);
$sweb3->call('eth_getBalance', [$sweb3->personal->address], 'latest');

//execute all batched calls in one request
$res = $sweb3->executeBatch();

//batching has to be manually disabled
$sweb3->batch(false);
```

### Account

[](#account)

```
use iAmirNet\SWeb3\Accounts;
use iAmirNet\SWeb3\Account;

//create new account privateKey/address (returns Account)
$account = Accounts::create();

//retrieve account (address) from private key
$account2 = Accounts::privateKeyToAccount('...private_key...');

//sign message with account
$res = $account2->sign('Some data');
```

### Contract interaction

[](#contract-interaction)

```
use iAmirNet\SWeb3\Contract;

$contract = new Contract($sweb3, '0x2222222222222222222222222222222222222222', '[ABI...]'); //'0x2222...' is contract address

// call contract function
$res = $contract->call('autoinc_tuple_a');

// change function state
//remember to set the sign values and chain id first: $sweb3->setPersonalData() & $sweb3->chainId
$extra_data = ['nonce' => $sweb3->personal->getNonce()];
$result = $contract->send('Set_public_uint', 123,  $extra_data);
```

### Contract events (logs)

[](#contract-events-logs)

```
//optional parameters fromBlock, toBlock, topics
//default values -> '0x0', 'latest', null (all events/logs from this contract)
$res = $contract->getLogs();
```

### Contract creation (deployment)

[](#contract-creation-deployment)

```
use iAmirNet\SWeb3\Contract;

$creation_abi = '[abi...]';
$contract = new Contract($sweb3, '', $creation_abi);

//set contract bytecode data
$contract_bytecode = '123456789....';
$contract->setBytecode($contract_bytecode);

//remember to set the sign values and chain id first: $sweb3->setPersonalData() & $sweb3->chainId
$extra_params = ['nonce' => $sweb3->personal->getNonce()];
$result = $contract->deployContract( [123123],  $extra_params);
```

### Usual required includes

[](#usual-required-includes)

```
use iAmirNet\SWeb3\SWeb3;                            //always needed, to create the Web3 object
use iAmirNet\SWeb3\Utils;                            //sweb3 helper classes (for example, hex conversion operations)
use iAmirNet\SWeb3\Contract;                   //contract creation and interaction
use iAmirNet\SWeb3\Accounts;                   		//account creation
use iAmirNet\SWeb3\Account;                   		//single account management (signing)
use phpseclib\Math\BigInteger as BigNumber; //BigInt handling
use stdClass;                               //for object interaction
```

Provided Examples
=================

[](#provided-examples)

In the folder Examples/ there are some extended examples with call &amp; send examples:

- example.call.php
- example.send.php
- example.batch.php
- example.account.php
- example.contract\_creation.php
- example.erc20.php

### Example configuration

[](#example-configuration)

To execute the examples you will need to add some data to the configuration file (example.config.php).

The example is pre-configured to work with an infura endpoint:

```
define('INFURA_PROJECT_ID', 'XXXXXXXXX');
define('INFURA_PROJECT_SECRET', 'XXXXXXXXX');
define('ETHEREUM_NET_NAME', 'ropsten'); //ropsten , mainnet

define('ETHEREUM_NET_ENDPOINT', 'https://'.ETHEREUM_NET_NAME.'.infura.io/v3/'.INFURA_PROJECT_ID);
```

Just add your infura project keys. If you have not configured the api secret key requisite, just ignore it.

If you are using a private endpoint, just ignore all the infura definitions:

```
define('ETHEREUM_NET_ENDPOINT', 'https://123.123.40.123:1234');
```

To enable contract interaction, set the contract data (address &amp; ABI). The file is preconfigured to work with our example contract, already deployed on ropsten.

```
//swp_contract.sol is available on ropsten test net, address: 0x706986eEe8da42d9d85997b343834B38e34dc000
define('SWP_Contract_Address','0x706986eEe8da42d9d85997b343834B38e34dc000');
$SWP_Contract_ABI = '[...]';
define('SWP_Contract_ABI', $SWP_Contract_ABI);
```

To enable transaction sending &amp; signing, enter a valid pair of address and private key. Please take this advises before continuing:

- The address must be active on the ropsten network and have some ether available to send the transactions.
- Double check that you are using a test endpoint, otherwise you will be spending real eth to send the transactions
- Be sure to keep your private key secret!

```
//SIGNING
define('SWP_ADDRESS', 'XXXXXXXXXX');
define('SWP_PRIVATE_KEY', 'XXXXXXXXXX');
```

### Example contract

[](#example-contract)

The solidity contract used in this example is available too in the same folder: swp\_contract.sol

### Example disclaimer

[](#example-disclaimer)

Don't base your code structure on this example. This example does not represent clean / efficient / performant aproach to implement them in a production environment. It's only aim is to show some of the features of Simple Web3 Php.

Modules
=======

[](#modules)

- Utils library forked &amp; extended from web3p/web3.php
- Transaction signing: kornrunner/ethereum-offline-raw-tx
- sha3 encoding: from kornrunner/keccak
- BigNumber interaction: phpseclib\\Math
- Asymetric key handling: simplito/elliptic-php

TODO
====

[](#todo)

- Node accounts creation / interaction

License
=======

[](#license)

MIT

DONATIONS (TRX Or USDT)
=======================

[](#donations-trx-or-usdt)

```
TUE8GiY4vmz831N65McwzZVbA9XEDaLinn 😘❤

```

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance83

Actively maintained with recent releases

Popularity2

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity24

Early-stage or recently created project

 Bus Factor3

3 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

90d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1670938?v=4)[Peyman Jahani](/maintainers/jahani)[@jahani](https://github.com/jahani)

---

Top Contributors

[![drlecks](https://avatars.githubusercontent.com/u/3506116?v=4)](https://github.com/drlecks "drlecks (11 commits)")[![iamirnet](https://avatars.githubusercontent.com/u/68027783?v=4)](https://github.com/iamirnet "iamirnet (8 commits)")[![ofumbi](https://avatars.githubusercontent.com/u/4081256?v=4)](https://github.com/ofumbi "ofumbi (5 commits)")[![cheplv](https://avatars.githubusercontent.com/u/4574587?v=4)](https://github.com/cheplv "cheplv (4 commits)")[![joelcho](https://avatars.githubusercontent.com/u/34619326?v=4)](https://github.com/joelcho "joelcho (4 commits)")[![phanthai12](https://avatars.githubusercontent.com/u/5457016?v=4)](https://github.com/phanthai12 "phanthai12 (4 commits)")[![slawomir-pryczek](https://avatars.githubusercontent.com/u/7211190?v=4)](https://github.com/slawomir-pryczek "slawomir-pryczek (2 commits)")[![scriptoshi](https://avatars.githubusercontent.com/u/189149148?v=4)](https://github.com/scriptoshi "scriptoshi (1 commits)")[![Sigri44](https://avatars.githubusercontent.com/u/9304947?v=4)](https://github.com/Sigri44 "Sigri44 (1 commits)")[![freaker2k7](https://avatars.githubusercontent.com/u/5237609?v=4)](https://github.com/freaker2k7 "freaker2k7 (1 commits)")

---

Tags

phpjsonrpcaccountcontracttransactionethereumethweb3signed transactionABIv2

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/iamirnet-sweb3/health.svg)

```
[![Health](https://phpackages.com/badges/iamirnet-sweb3/health.svg)](https://phpackages.com/packages/iamirnet-sweb3)
```

###  Alternatives

[drlecks/simple-web3-php

Web3 library in PHP

7641.6k2](/packages/drlecks-simple-web3-php)[kornrunner/ethereum-offline-raw-tx

Pure PHP Ethereum Offline Raw Transaction Signer

63192.1k22](/packages/kornrunner-ethereum-offline-raw-tx)[kornrunner/ethereum-token

PHP Ethereum Token Utils

1412.9k1](/packages/kornrunner-ethereum-token)[kornrunner/solidity

Pure PHP implementation of Solidity

1940.5k11](/packages/kornrunner-solidity)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
