sendTransaction example
This commit is contained in:
parent
4d589b7870
commit
809ffc4387
@ -1,10 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
require('../vendor/autoload.php');
|
require('./exampleBase.php');
|
||||||
|
|
||||||
use Web3\Web3;
|
|
||||||
|
|
||||||
$web3 = new Web3('http://192.168.99.100:8545');
|
|
||||||
$eth = $web3->eth;
|
$eth = $web3->eth;
|
||||||
|
|
||||||
echo 'Eth Get Account and Balance' . PHP_EOL;
|
echo 'Eth Get Account and Balance' . PHP_EOL;
|
||||||
|
7
examples/exampleBase.php
Normal file
7
examples/exampleBase.php
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require('../vendor/autoload.php');
|
||||||
|
|
||||||
|
use Web3\Web3;
|
||||||
|
|
||||||
|
$web3 = new Web3('http://192.168.99.100:8545');
|
60
examples/sendTransaction.php
Normal file
60
examples/sendTransaction.php
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require('./exampleBase.php');
|
||||||
|
|
||||||
|
$eth = $web3->eth;
|
||||||
|
|
||||||
|
echo 'Eth Send Transaction' . PHP_EOL;
|
||||||
|
$eth->accounts(function ($err, $accounts) use ($eth) {
|
||||||
|
if ($err !== null) {
|
||||||
|
echo 'Error: ' . $err->getMessage();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$fromAccount = $accounts->result[0];
|
||||||
|
$toAccount = $accounts->result[1];
|
||||||
|
|
||||||
|
// get balance
|
||||||
|
$eth->getBalance($fromAccount, function ($err, $balance) use($fromAccount) {
|
||||||
|
if ($err !== null) {
|
||||||
|
echo 'Error: ' . $err->getMessage();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
echo $fromAccount . ' Balance: ' . $balance->result . PHP_EOL;
|
||||||
|
});
|
||||||
|
$eth->getBalance($toAccount, function ($err, $balance) use($toAccount) {
|
||||||
|
if ($err !== null) {
|
||||||
|
echo 'Error: ' . $err->getMessage();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
echo $toAccount . ' Balance: ' . $balance->result . PHP_EOL;
|
||||||
|
});
|
||||||
|
|
||||||
|
// send transaction
|
||||||
|
$eth->sendTransaction([
|
||||||
|
'from' => $fromAccount,
|
||||||
|
'to' => $toAccount,
|
||||||
|
'value' => '0x11'
|
||||||
|
], function ($err, $transaction) use ($eth, $fromAccount, $toAccount) {
|
||||||
|
if ($err !== null) {
|
||||||
|
echo 'Error: ' . $err->getMessage();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
echo 'Tx hash: ' . $transaction->result . PHP_EOL;
|
||||||
|
|
||||||
|
// get balance
|
||||||
|
$eth->getBalance($fromAccount, function ($err, $balance) use($fromAccount) {
|
||||||
|
if ($err !== null) {
|
||||||
|
echo 'Error: ' . $err->getMessage();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
echo $fromAccount . ' Balance: ' . $balance->result . PHP_EOL;
|
||||||
|
});
|
||||||
|
$eth->getBalance($toAccount, function ($err, $balance) use($toAccount) {
|
||||||
|
if ($err !== null) {
|
||||||
|
echo 'Error: ' . $err->getMessage();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
echo $toAccount . ' Balance: ' . $balance->result . PHP_EOL;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user