You've already forked qlg.tsgz.moe
Init Repo
This commit is contained in:
115
hyhproject/app/model/Chain3base.php
Executable file
115
hyhproject/app/model/Chain3base.php
Executable file
@ -0,0 +1,115 @@
|
||||
<?php
|
||||
namespace wstmart\app\model;
|
||||
use think\Model;
|
||||
use think\Db;
|
||||
Vendor('web3.vendor.autoload');
|
||||
use Web3\Web3;
|
||||
/**
|
||||
* ============================================================================
|
||||
* 基础模型器
|
||||
*/
|
||||
class Chain3base extends Model{
|
||||
|
||||
/**
|
||||
* web3
|
||||
*
|
||||
* @var \Web3\Web3
|
||||
*/
|
||||
protected $web3;
|
||||
|
||||
protected $wei = 18;
|
||||
|
||||
/**
|
||||
* testHost
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $testHost = 'http://localhost:8545';
|
||||
|
||||
/**
|
||||
* coinbase
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $coinbase=0;
|
||||
|
||||
public function __construct(){
|
||||
parent::__construct();
|
||||
$web3 = new Web3($this->testHost);
|
||||
$this->web3 = $web3;
|
||||
}
|
||||
public function getCoinbase(){
|
||||
$this->web3->eth->coinbase(function ($err, $coinbase) {
|
||||
if ($err !== null) {
|
||||
|
||||
//return $this->fail($err->getMessage());
|
||||
}else{
|
||||
$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
|
||||
*/
|
||||
public function createAccount($pass){
|
||||
$web3->personal->newAccount($pass, function ($err, $account) use (&$newAccount) {
|
||||
if ($err !== null) {
|
||||
return WSTReturn($err->getMessage());
|
||||
// echo 'Error: ' . $err->getMessage();
|
||||
//return;
|
||||
}else{
|
||||
$data['account'] = $account;
|
||||
return WSTReturn('',1,$data);
|
||||
}
|
||||
});
|
||||
}
|
||||
public function getBalance($address){
|
||||
$msg='';
|
||||
$status=-1;
|
||||
$data['balance']=0;
|
||||
$this->web3->eth->getBalance($address , function ($err, $balance) use (&$msg,&$status,&$data) {
|
||||
if ($err !== null) {
|
||||
$msg=$err->getMessage();
|
||||
//return WSTReturn($err->getMessage());
|
||||
//echo 'Error: ' . $err->getMessage();
|
||||
//return;
|
||||
}else{
|
||||
$my_balance = $balance->toString();
|
||||
$my_balance = $my_balance/pow(10,$this->wei);
|
||||
$status=1;
|
||||
$data['balance'] = $my_balance;
|
||||
}
|
||||
});
|
||||
return WSTReturn($msg,$status,$data);
|
||||
}
|
||||
//账号解锁
|
||||
public 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;
|
||||
return WSTReturn('',1,$data);
|
||||
} else {
|
||||
$data['unlock'] = 0;
|
||||
return WSTReturn('',1,$data);
|
||||
//echo 'New account isn\'t unlocked' . PHP_EOL;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user