Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

Carlos Koyoc / jobicoin

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Settings
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Find file
Normal viewHistoryPermalink
Switch branch/tag
  • jobicoin
  • app
  • Blockchain
  • Accesscontract.php
Accesscontract.php 4.58 KB
carlos kf's avatar
ejemplo de rawtransaction
57c462b5
 
carlos kf committed 3 years ago
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
<?php

namespace App\Blockchain;

use think\Db;

use Web3\Contract;
use Web3\Web3;
use EthTool\Callback;
use Exception;
use Web3\Utils;


class Accesscontract
{
    /**
     *  Access token contracts 
     * @param $web3
     * @param $artifact    currency 
     * @return Contract
     */
    function loadContract($web3,$artifact){
        $dir = \config('contractBuild');
        $abi = file_get_contents($dir . $artifact . '.abi');
        $addr = file_get_contents($dir . $artifact . '.addr');
        // dd($addr);
        $contract = new Contract($web3->provider,$abi);
        $contract->at($addr);

        return $contract;
    }
    /**
     * [checkEvent description]  Get the event log 
     * @param  [type] $contract [description]
     * @param  string $name     [description]   Event name 
     * @param  string $hash     [description]
     * @return [type]           [description]
     */
    function checkEvent($contract,$name='',$hash=''){

        if(!$contract|| !$name || !$hash )return false;

        try {
            $abi = $contract->abi;

            if(!$abi) return false;

            $event_arr = [];
//         Splicing events 
            foreach($abi as $k => $v){
                if($v['type'] == 'event' && $name == $v['name'] ){

                    $v['checkHs'] = $v['name'].'(';
                    $arr = [];
                    foreach ($v['inputs'] as $key => $value) {
                        $arr[] = $value['type'];
                    }
                    $v['checkHs'] .= implode(',',$arr).')';
                    $event_arr = $v;
                    break;

                }

            }

            if(!$event_arr) return false;

            $ethabi = $contract->ethabi;
            $mytopic = $ethabi->encodeEventSignature($event_arr['checkHs']); // Get the event hash

            $cb = new Callback;
            $contract->eth->getTransactionReceipt($hash,$cb); // Get the bill 
            $is_hs = $cb->result;

            if(!$is_hs)return false;

            $logs =$is_hs->logs;
            foreach ($logs as $key => $value) {

                if($value->topics){

                    if($value->topics['0'] == $mytopic){
                        $str = 	$value->data;
                        $len = mb_strlen($str);

                        if($len <= 2) return false;

                        $div = ($len-2)%64;

                        if($div) return false;
                        // dump($str);    
                        $str = mb_substr($str,2,$len);     // Remove 0x
                        $data_arr = str_split($str,64);
                        $re_arr = [];
                        // dump($data_arr);
                        // dd($event_arr['inputs']);
                        foreach ($event_arr['inputs'] as $i_k => $i_v) {
                            $tmp = $ethabi->decodeParameter($i_v['type'],$data_arr[$i_k]);  // Decrypt 
                            if(isset($tmp->value)){
                                $re_arr[$i_v['name']] = gmp_strval($tmp->value);
                            }else{
                                $re_arr[$i_v['name']] = $tmp;
                            }
                        }
                        return 	$re_arr;
                    }
                }
            }

        } catch (Exception $e) {
            return false;
        }
        return false;
    }

    /**
     *  Whether the deployment is successful 
     * @param $eth
     * @param $txhash
     * @param int $timeout
     * @param int $interval
     * @return mixed
     */
    function waitForReceipt($eth,$txhash,$timeout=60,$interval=1){
        // echo 'waiting for tx receipt...' . PHP_EOL;
        $cb = new Callback;
        $t0 = time();
        while(true){
            $eth->getTransactionReceipt($txhash,$cb);
            if($cb->result) break;
            $t1 = time();
            if(($t1 - $t0) > $timeout) break;
            sleep($interval);
        }
        return $cb->result;
    }




    /**
     *  Transfer accounts 
     * @param $contract
     * @param $initiator
     * @param $to
     * @param $value
     * @return mixed
     */
    function transfer($contract,$initiator,$to,$value){
        $cb = new Callback;
        $opts = [
            'from' => $initiator,
            'gas' => Utils::toHex(100000,true)
        ];
        $contract->send('transfer',$to,$value,$opts,$cb);
        $txhash = $cb->result;

        $receipt = $this -> waitForReceipt($contract->eth,$txhash);

        return $receipt;
    }


    function CheckData($contract,$name){

        $cb = new Callback;
        $contract->call($name,$cb);
        return $cb->result;
    }


}