118 lines
3.2 KiB
PHP
Executable File
118 lines
3.2 KiB
PHP
Executable File
<?php
|
|
namespace wstmart\app\controller;
|
|
use think\Controller;
|
|
Vendor('web3.vendor.autoload');
|
|
use Web3\Web3;
|
|
/**
|
|
* ============================================================================
|
|
* 默认控制器
|
|
*/
|
|
class Chain3base extends Controller{
|
|
/**
|
|
* web3
|
|
*
|
|
* @var \Web3\Web3
|
|
*/
|
|
protected $web3;
|
|
|
|
protected $wei = 18;
|
|
/**
|
|
* testRinkebyHost
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $testRinkebyHost = 'http://localhost';
|
|
|
|
/**
|
|
* testHost
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $testHost = 'http://localhost:8545';
|
|
|
|
/**
|
|
* coinbase
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $coinbase;
|
|
|
|
public function __construct(){
|
|
$web3 = new Web3($this->testHost);
|
|
$this->web3 = $web3;
|
|
}
|
|
public function getCoinbase(){
|
|
$this->web3->eth->coinbase(function ($err, $coinbase) {
|
|
if ($err !== null) {
|
|
$this->coinbase = 0;
|
|
//return $this->fail($err->getMessage());
|
|
}
|
|
$this->coinbase = $coinbase;
|
|
});
|
|
return $this->coinbase;
|
|
|
|
////获取本机账号列表
|
|
// $this->web3->eth->accounts(function ($err, $accounts) use ($eth) {
|
|
// if ($err !== null) {
|
|
// echo 'Error: ' . $err->getMessage();
|
|
// return;
|
|
// }
|
|
// dump($accounts);
|
|
// });
|
|
}
|
|
/**
|
|
*创建账号
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function create_account(){
|
|
$pass = trim(input('post.pass'));
|
|
if(strlen($pass) < 6 ){
|
|
exit(jsonReturn('密码最低6位'));
|
|
}
|
|
$web3->personal->newAccount($pass, function ($err, $account) use (&$newAccount) {
|
|
if ($err !== null) {
|
|
exit(jsonReturn($err->getMessage()));
|
|
// echo 'Error: ' . $err->getMessage();
|
|
//return;
|
|
}else{
|
|
$data['account'] = $account;
|
|
exit(jsonReturn('',1,$data));
|
|
}
|
|
});
|
|
}
|
|
protected function get_Balance(){
|
|
$address = trim(input('post.address'));
|
|
$this->web3->eth->getBalance($address , function ($err, $balance) {
|
|
if ($err !== null) {
|
|
exit(jsonReturn($err->getMessage()));
|
|
// echo 'Error: ' . $err->getMessage();
|
|
//return;
|
|
}else{
|
|
$my_balance = $balance->toString();
|
|
$my_balance = $my_balance/pow(10,$this->wei);
|
|
$data['balance'] = $my_balance;
|
|
exit(jsonReturn('',1,$data));
|
|
}
|
|
});
|
|
}
|
|
//账号解锁
|
|
protected function unlock_account($pass,$address){
|
|
$web3->personal->unlockAccount($address, $pass, function ($err, $unlocked) {
|
|
if ($err !== null) {
|
|
exit(jsonReturn($err->getMessage()));
|
|
// echo 'Error: ' . $err->getMessage();
|
|
// return;
|
|
}
|
|
if($unlocked) {
|
|
$data['unlock'] = 1;//$unlocked;
|
|
exit(jsonReturn('',1,$data));
|
|
} else {
|
|
$data['unlock'] = 0;
|
|
exit(jsonReturn('',1,$data));
|
|
//echo 'New account isn\'t unlocked' . PHP_EOL;
|
|
}
|
|
});
|
|
}
|
|
}
|