{"_id":"@artman325/simple-web3-php","name":"@artman325/simple-web3-php","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"@artman325/simple-web3-php","version":"1.0.0","description":"[![Latest Version on Packagist](https://img.shields.io/packagist/v/drlecks/simple-web3-php.svg?style=flat-square)](https://packagist.org/packages/drlecks/simple-web3-php) [![Join the chat at https://gitter.im/drlecks/Simple-Web3-Php](https://img.shields.i","main":"index.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"author":"","license":"ISC","_id":"@artman325/simple-web3-php@1.0.0","gitHead":"e13a87b968dadc9924bc68949bfd45e530988896","_nodeVersion":"18.18.0","_npmVersion":"10.1.0","dist":{"integrity":"sha512-IRO8TzkACxqh/vxocu9GKtVKCUz+gxid/A5AN1UK+SElqHErba4PNOJcSdGIhF4np8fAYxj1dj6q13pnHpmS6Q==","shasum":"0372840fb307eab5feda9705f42a02955d39207f","tarball":"https://registry.npmjs.org/@artman325/simple-web3-php/-/simple-web3-php-1.0.0.tgz","fileCount":23,"unpackedSize":147608,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCfYyI8VN1N4LGBvM2SCLIf3SewK379pbrWV89u+tbpFwIhAL+Uja8/4jRnvdreHa3U86iCzi8QUk9M2WnNL6PeZPsv"}]},"_npmUser":{"name":"artman325","email":"subbotin.artem@gmail.com"},"directories":{},"maintainers":[{"name":"artman325","email":"subbotin.artem@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/simple-web3-php_1.0.0_1695699249234_0.31940618904729723"},"_hasShrinkwrap":false}},"time":{"created":"2023-09-26T03:34:09.105Z","1.0.0":"2023-09-26T03:34:09.422Z","modified":"2023-09-26T03:34:09.702Z"},"maintainers":[{"name":"artman325","email":"subbotin.artem@gmail.com"}],"description":"[![Latest Version on Packagist](https://img.shields.io/packagist/v/drlecks/simple-web3-php.svg?style=flat-square)](https://packagist.org/packages/drlecks/simple-web3-php) [![Join the chat at https://gitter.im/drlecks/Simple-Web3-Php](https://img.shields.i","license":"ISC","readme":"# simple-web3-php\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/drlecks/simple-web3-php.svg?style=flat-square)](https://packagist.org/packages/drlecks/simple-web3-php)\n[![Join the chat at https://gitter.im/drlecks/Simple-Web3-Php](https://img.shields.io/badge/gitter-join%20chat-brightgreen.svg)](https://gitter.im/Simple-Web3-Php/community)\n[![Licensed under the MIT License](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/drlecks/Simple-Web3-Php/blob/master/LICENSE)\n\n\nA php interface for interacting with the Ethereum blockchain and ecosystem.\n\n\n# Features\n\n- PHP >= 7.4\n- Customizable curl calls\n- Call: get net state\n- Send signed transactions\n- Batch call requests and signed transactions \n- Address & private key creation\n- Message signing\n- Full ABIv2 encode/decode \n- Contract creation\n- Contract interaction (call/send)\n- Contract Events/logs with filters\n- Support for ERC20 contracts with non-nominative decimal values\n- Examples provided interacting with simple types, strings, tuples, arrays, arrays of tuples with arrays, multi-dimension arrays... \n\n\n# Install\n\n### Latest stable release\n```\ncomposer require drlecks/simple-web3-php \"^0.10.0\"\n```\n\nOr you can add this line in composer.json\n\n```\n\"drlecks/simple-web3-php\": \"^0.10.0\"\n```\n\n\n### Development (main branch)\n```\ncomposer require drlecks/simple-web3-php dev-master\n```\n\nOr you can add this line in composer.json\n\n```\n\"drlecks/simple-web3-php\": \"dev-master\"\n```\n\n\n# Usage\n\n### New instance\n```php\nuse SWeb3\\SWeb3;\n//initialize SWeb3 main object\n$sweb3 = new SWeb3('http://ethereum.node.provider:optional.node.port');\n\n//optional if not sending transactions\n$from_address = '0x0000000000000000000000000000000000000000';\n$from_address_private_key = '345346245645435....';\n$sweb3->setPersonalData($from_address, $from_address_private_key);\n```\n\n### Convert values \n- Most calls return hex or BigNumber to represent numbers. \n- Most calls expect numeric parameters represented as BigNumbers.\n\nHex to Big Number: \n```php \nuse SWeb3\\Utils;\n \n$res = $sweb3->call('eth_blockNumber', []);\n$bigNum = Utils::hexToBn($res->result);\n``` \n\nNumber to BigNumber:\n```php \n$bigNum = Utils::ToBn(123);\n``` \n\nGet average-human readable string representation from Big Number:\n```php \n$s_number = $bigNum->toString();\n``` \n\nFormat 1 ether to wei (unit required for ether values in transactions):\n```php \nUtils::toWei('0.001', 'ether');\n``` \n\nGet average-human readable string representation from a value conversion:\n```php  \n$s_val = Utils::fromWeiToString('1001', 'kwei'); // \"1.001\"\n\n$s_val = Utils::toWeiString('1.001', 'kwei'); // \"1001\"\n```  \n\n\n### General ethereum block information call:\n```php \n$res = $sweb3->call('eth_blockNumber', []);\n```\n \n### Refresh gas price \n```php \n$gasPrice = $sweb3->getGasPrice();\n``` \n\n### Estimate  gas price (from send params)\n```php \n$sweb3->chainId = '0x3';//ropsten \n$sendParams = [ \n    'from' =>\t$sweb3->personal->address,  \n    'to' =>     '0x1111111111111111111111111111111111111111', \n    'value' => \tUtils::toWei('0.001', 'ether'),\n    'nonce' => \t$sweb3->personal->getNonce()  \n]; \n$gasEstimateResult = $sweb3->call('eth_estimateGas', [$sendParams]);\n```\n \n### Send 0.001 ether to address\n```php\n//remember to set personal data first with a valid pair of address & private key\n$sendParams = [ \n    'from' =>   \t$sweb3->personal->address,  \n    'to' =>     \t'0x1111111111111111111111111111111111111111', \n    'gasLimit' => \t210000,\n    'value' => \t\tUtils::toWei('0.001', 'ether'),\n    'nonce' => \t\t$sweb3->personal->getNonce()\n];    \n$result = $sweb3->send($sendParams); \n```\n \n### Batch calls & transactions\n```php \n//enable batching\n$sweb3->batch(true);\n\n$sweb3->call('eth_blockNumber', []); \n$sweb3->call('eth_getBalance', [$sweb3->personal->address], 'latest');\n\n//execute all batched calls in one request\n$res = $sweb3->executeBatch();\n\n//batching has to be manually disabled\n$sweb3->batch(false); \n```\n\n### Account\n```php \nuse SWeb3\\Accounts; \nuse SWeb3\\Account;\n\n//create new account privateKey/address (returns Account)\n$account = Accounts::create();\n\n//retrieve account (address) from private key \n$account2 = Accounts::privateKeyToAccount('...private_key...');\n\n//sign message with account\n$res = $account2->sign('Some data'); \n\n```\n\n \n### Contract interaction\n\n```php\nuse SWeb3\\SWeb3_Contract;\n\n$contract = new SWeb3_contract($sweb3, '0x2222222222222222222222222222222222222222', '[ABI...]'); //'0x2222...' is contract address\n  \n// call contract function\n$res = $contract->call('autoinc_tuple_a');\n\n// change function state\n//remember to set the sign values and chain id first: $sweb3->setPersonalData() & $sweb3->chainId\n$extra_data = ['nonce' => $sweb3->personal->getNonce()]; \n$result = $contract->send('Set_public_uint', 123,  $extra_data);\n```\n\n### Contract events (logs)\n\n```php\n//optional parameters fromBlock, toBlock, topics\n//default values -> '0x0', 'latest', null (all events/logs from this contract) \n$res = $contract->getLogs();\n``` \n\n### Contract creation (deployment)\n\n```php\nuse SWeb3\\SWeb3_Contract;\n \n$creation_abi = '[abi...]';\n$contract = new SWeb3_contract($sweb3, '', $creation_abi);\n\n//set contract bytecode data\n$contract_bytecode = '123456789....';\n$contract->setBytecode($contract_bytecode);\n\n//remember to set the sign values and chain id first: $sweb3->setPersonalData() & $sweb3->chainId\n$extra_params = ['nonce' => $sweb3->personal->getNonce()];  \n$result = $contract->deployContract( [123123],  $extra_params); \n```\n\n\n### Usual required includes\n\n```php \nuse SWeb3\\SWeb3;                            //always needed, to create the Web3 object\nuse SWeb3\\Utils;                            //sweb3 helper classes (for example, hex conversion operations)\nuse SWeb3\\SWeb3_Contract;                   //contract creation and interaction\nuse SWeb3\\Accounts;                   \t\t//account creation\nuse SWeb3\\Account;                   \t\t//single account management (signing)\nuse phpseclib\\Math\\BigInteger as BigNumber; //BigInt handling\nuse stdClass;                               //for object interaction \n```\n\n# Provided Examples\n\nIn the folder Examples/ there are some extended examples with call & send examples:\n\n- example.call.php\n- example.send.php\n- example.batch.php\n- example.account.php\n- example.contract_creation.php\n- example.erc20.php\n\n ### Example configuration\n\n To execute the examples you will need to add some data to the configuration file (example.config.php).\n\nThe example is pre-configured to work with an infura endpoint:\n\n```php\ndefine('INFURA_PROJECT_ID', 'XXXXXXXXX');\ndefine('INFURA_PROJECT_SECRET', 'XXXXXXXXX');\ndefine('ETHEREUM_NET_NAME', 'ropsten'); //ropsten , mainnet\n\ndefine('ETHEREUM_NET_ENDPOINT', 'https://'.ETHEREUM_NET_NAME.'.infura.io/v3/'.INFURA_PROJECT_ID); \n```\nJust add your infura project keys. If you have not configured the api secret key requisite, just ignore it.\n\nIf you are using a private endpoint, just ignore all the infura definitions:\n\n```php \ndefine('ETHEREUM_NET_ENDPOINT', 'https://123.123.40.123:1234'); \n```\n\nTo enable contract interaction, set the contract data (address & ABI). The file is preconfigured to work with our example contract, already deployed on ropsten.\n```php\n//swp_contract.sol is available on ropsten test net, address: 0x706986eEe8da42d9d85997b343834B38e34dc000\ndefine('SWP_Contract_Address','0x706986eEe8da42d9d85997b343834B38e34dc000'); \n$SWP_Contract_ABI = '[...]';\ndefine('SWP_Contract_ABI', $SWP_Contract_ABI);\n```\n\nTo enable transaction sending & signing, enter a valid pair of address and private key. Please take this advises before continuing:\n- The address must be active on the ropsten network and have some ether available to send the transactions.\n- Double check that you are using a test endpoint, otherwise you will be spending real eth to send the transactions\n- Be sure to keep your private key secret! \n\n```php\n//SIGNING\ndefine('SWP_ADDRESS', 'XXXXXXXXXX');\ndefine('SWP_PRIVATE_KEY', 'XXXXXXXXXX');\n```\n\n### Example contract\n\nThe solidity contract used in this example is available too in the same folder: swp_contract.sol\n\n### Example disclaimer\n\nDon'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.\n\n\n# Modules\n\n- Utils library forked & extended from web3p/web3.php\n- Transaction signing: kornrunner/ethereum-offline-raw-tx\n- sha3 encoding: from kornrunner/keccak\n- BigNumber interaction: phpseclib\\Math  \n- Asymetric key handling: simplito/elliptic-php\n\n\n# TODO\n \n- Node accounts creation / interaction\n\n\n# License\nMIT\n \n\n# DONATIONS (ETH)\n \n``` \n0x4a890A7AFB7B1a4d49550FA81D5cdca09DC8606b\n```\n","readmeFilename":"README.md"}