kornrunner / ethereum-token
PHP Ethereum Token Utils
Installs: 12 178
Dependents: 3
Suggesters: 0
Security: 0
Stars: 11
Watchers: 2
Forks: 4
Open Issues: 1
pkg:composer/kornrunner/ethereum-token
Requires
- php: >=8.1
- ext-bcmath: *
Requires (Dev)
- phpunit/phpunit: ^9
README
PHP Ethereum Token Utils
Installation
$ composer require kornrunner/ethereum-token
Usage
To prepare a offline transaction, using kornrunner/ethereum-offline-raw-tx
Transfer
use kornrunner\Ethereum\Token; use kornrunner\Ethereum\Transaction; $nonce = '04'; $gasPrice = '03f5476a00'; $gasLimit = '027f4b'; $to = '1a8c8adfbe1c59e8b58cc0d515f07b7225f51c72'; $privateKey = 'b2f2698dd7343fa5afc96626dee139cb92e58e5d04e855f4c712727bf198e898'; $token = new Token; $usdt = new Token\USDT; $amount = 20; $hexAmount = $token->hexAmount($usdt, $amount); // 0x1312d00 $data = $token->getTransferData($to, $hexAmount); // 0xa9059cbb0000000000000000000000001a8c8adfbe1c59e8b58cc0d515f07b7225f51c720000000000000000000000000000000000000000000000000000000001312d00 $transaction = new Transaction($nonce, $gasPrice, $gasLimit, $usdt::ADDRESS, '', $data); $transaction->getRaw($privateKey); // f8a9048503f5476a0083027f4b94dac17f958d2ee523a2206206994597c13d831ec7b844a9059cbb0000000000000000000000001a8c8adfbe1c59e8b58cc0d515f07b7225f51c720000000000000000000000000000000000000000000000000000000001312d00801ba03e141ea4233ec00bb3a80d7fea5f774b736772851b7bad18453d0f3c6097c42e9fa6eb47b6bead6a76d7db12809e2c916df999d7b99b613fcaa135abd8a0078e
Approve
Approve another address to spend tokens on your behalf:
use kornrunner\Ethereum\Token; use kornrunner\Ethereum\Transaction; $token = new Token; $usdt = new Token\USDT; $spender = '1a8c8adfbe1c59e8b58cc0d515f07b7225f51c72'; $amount = 100; $hexAmount = $token->hexAmount($usdt, $amount); $data = $token->getApproveData($spender, $hexAmount); // use $data with Transaction as shown above
Burn
Burn (destroy) tokens:
use kornrunner\Ethereum\Token; use kornrunner\Ethereum\Transaction; $token = new Token; $usdt = new Token\USDT; $amount = 50; $hexAmount = $token->hexAmount($usdt, $amount); $data = $token->getBurnData($hexAmount); // use $data with Transaction as shown above
Mint
Mint (create) new tokens to an address:
use kornrunner\Ethereum\Token; use kornrunner\Ethereum\Transaction; $token = new Token; $usdt = new Token\USDT; $recipient = '1a8c8adfbe1c59e8b58cc0d515f07b7225f51c72'; $amount = 1000; $hexAmount = $token->hexAmount($usdt, $amount); $data = $token->getMintData($recipient, $hexAmount); // use $data with Transaction as shown above
TransferFrom
Transfer tokens from one address to another using allowance:
use kornrunner\Ethereum\Token; use kornrunner\Ethereum\Transaction; $token = new Token; $usdt = new Token\USDT; $from = 'd9040b8a1f12a40511ac1f7b994a21dd08ceac20'; $to = '1a8c8adfbe1c59e8b58cc0d515f07b7225f51c72'; $amount = 50; $hexAmount = $token->hexAmount($usdt, $amount); $data = $token->getTransferFromData($from, $to, $hexAmount); // use $data with Transaction as shown above
Read-Only Functions
For querying token information (generates ABI-encoded call data):
BalanceOf
Get the token balance of an address:
use kornrunner\Ethereum\Token; $token = new Token; $address = '1a8c8adfbe1c59e8b58cc0d515f07b7225f51c72'; $data = $token->getBalanceOfData($address); // 0x70a082310000000000000000000000001a8c8adfbe1c59e8b58cc0d515f07b7225f51c72
Allowance
Check how many tokens a spender is allowed to use:
use kornrunner\Ethereum\Token; $token = new Token; $owner = '1a8c8adfbe1c59e8b58cc0d515f07b7225f51c72'; $spender = 'd9040b8a1f12a40511ac1f7b994a21dd08ceac20'; $data = $token->getAllowanceData($owner, $spender); // 0x...
TotalSupply
Get the total supply of tokens:
use kornrunner\Ethereum\Token; $token = new Token; $data = $token->getTotalSupplyData(); // 0x18160ddd
0x9c7b7a00972121fb843af7af74526d7eb585b171