123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- <?php
- namespace App\Console\Commands;
- use App\Models\Meta;
- use BlockMatrix\EosRpc\ChainFactory;
- use BlockMatrix\EosRpc\EosRpc;
- use BlockMatrix\EosRpc\WalletFactory;
- use Carbon\Carbon;
- use GuzzleHttp\Client;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Storage;
- use Org\Multilinguals\Apollo\Client\ApolloClient;
- use ZanySoft\Zip\Zip;
- class Trace extends Command
- {
-
- protected $signature = 'trace';
-
- protected $description = '自动打zip包,并进行上链操作';
- protected $save_dir = '';
-
- public function __construct()
- {
- parent::__construct();
- $this->save_dir = '';
- }
-
- public function handle()
- {
- $this->line('-----------traceing---------');
- $metas = Meta::where('status', 0)->get();
- foreach ($metas as $meta) {
- $md5 = md5(time());
- $this->line('------开始上链:' . $md5);
- $row = $this->transcation($md5);
- $this->line('------上链结果:' . var_export($row, true));
- if (isset($row['transaction_id']) && $row['transaction_id']) {
- $data['transaction_id'] = $row['transaction_id'];
- $data['block_height'] = $row['block_num'];
- $data['block_time'] = Carbon::parse($row['block_time'])->toDateTimeString();
- $data['status'] = 1;
- } else {
- $data['status'] = 2;
- }
- Meta::where('patch_num', $meta['patch_num'])->update($data);
- }
- $this->line('-----------traceing-end--------');
- }
-
- protected function downloadImg($path, $img, $name = '')
- {
- $client = new Client(['verify' => false]);
- if ($name) {
- $path = $path . '/' . $name . '.jpg';
- } else {
- $path = $path . '/' . md5(time()) . '.jpg';
- }
- $response = $client->get($img, ['save_to' => $path]);
- if ($response->getStatusCode() == 200) {
- return $path;
- }
- $this->line('下载' . $img . '失败');
- return false;
- }
- protected function transcation($md5)
- {
- try{
- $api = (new ChainFactory())->api(base_path());
- $walapi = (new WalletFactory())->api(base_path());
- $eos = (new EosRpc($api, $walapi));
- $accountA = "chxqsuyuanaa";
- $accountB = "chxqsuyuanbb";
- $coinCode = "EOS";
- $coin = "1.0000";
- $walletPassword = "PW5JbENyMrmYGc5LBMKGf2E6QaFoXSQZ6CDWMET7a6o6cXvy35qH1";
- $walletName = "caihongxingqiu";
- $eos->setWalletInfo($walletName, $walletPassword);
- $aliceBalance = json_decode($api->getCurrencyBalance('eosio.token', $accountA, $coinCode));
- $bobBalance = json_decode($api->getCurrencyBalance('eosio.token', $accountB, $coinCode));
- $balaceA = 0;
- $balaceB = 0;
- if (isset($aliceBalance[0])) {
- $balaceA = $aliceBalance[0];
- }
- if (isset($bobBalance[0])) {
- $balaceB = $bobBalance[0];
- }
- if ($balaceA == 0 && $balaceB == 0) {
- $this->line("两个账号都没钱了,快充钱");
- return false;
- }
- $this->line("准备打开钱包");
- $walapi->open($walletName);
- $this->line("钱包已打开");
- $this->line("准备解锁钱包");
- $walapi->unlock([$walletName,$walletPassword]);
- $this->line("钱包已解锁");
- $this->line("准备发起交易");
- if ($balaceA >= $coin * 2) {
- $trans = $eos->transfer($accountA, $accountB, $coin . " " . $coinCode, $md5, 'eosio.token', false);
- }elseif ($balaceB >= $coin * 2) {
- $trans = $eos->transfer($accountB, $accountA, $coin . " " . $coinCode, $md5, 'eosio.token', false);
- }else{
- $this->line('账号余额不足');
- return false;
- }
- Log::debug('transinfo:'.\GuzzleHttp\json_encode($trans));
- return [
- 'transaction_id' => $trans['transaction_id'] ?? '',
- 'block_num' => $trans['processed']['block_num'] ?? 0,
- 'block_time' => $trans['processed']['block_time'] ?? ''
- ];
- }catch (\Exception $exception){
- $this->line('上链失败:'.$exception->getMessage());
- return false;
- }
- }
- }
|