<?php
/**
 * Created By Sublime Text 3
 *
 * @date    2018-09-18 10:28:23
 * @authors rlgy <rlgyzhcn@qq.com>
 */

namespace common\service\chain33;

use Yii;
use common\helpers\Curl;

/**
 * chain33 区块链接口
 */
class Chain33Service
{
    public static function urlBuild()
    {
        $config = Yii::$app->params['chain33'];
        $scheme = $config['scheme'] ?? 'http';
        $host   = $config['host'] ?? '127.0.0.1';
        $port   = (string)$config['port'] ?? '';
        if ($port) {
            return $scheme . '://' . $host . ':' . $port;
        } else {
            return $scheme . '://' . $host;
        }
    }

    public static function jsonRpcBuild($params = [], $method = 'Chain33.Query')
    {
        $data = [
            'jsonrpc' => '2.0',
            'id'      => 0,
            'method'  => $method,
            'params'  => [$params],
        ];
        return json_encode($data);
    }

    public function send($params = [], $method = 'Chain33.Query')
    {
        $ch      = new Curl();
        $jsonrpc = self::jsonRpcBuild($params, $method);
        echo $jsonrpc;
        $ch->setHeader('Content-Type', 'application/json');
        $ch->setRawPostData($jsonrpc);
        $result = $ch->post(self::urlBuild(), false);
        if (!$result) {
            return ['code' => -1, 'msg' => '请求失败'];
        }
        if (0 == $result['id'] && empty($result['error'])) {
            $result['code'] = $result['id'];
            unset($result['id']);
            return $result;
        } else {
            return ['code' => -1, 'msg' => $result['error']];
        }
    }

    /**
     * 获取地址下的所有token资产
     *
     * @param  string $address
     * @param  string $symbol
     * @return array
     */
    public function getAccountTokenAssets($address, $symbol)
    {
        $params = [
            "execer"   => "token",
            "funcName" => "GetAccountTokenAssets",
            "payload"  => [
                "address" => $address,
                "execer"  => "token",
            ],
        ];
        return $this->send($params);
    }

    /**
     * 提币
     *
     * @param        $from
     * @param        $to
     * @param        $amount
     * @param string $note
     * @param bool   $isToken
     * @param string $tokenSymbol
     * @return array
     */
    public function extractToken($from, $to, $amount, $note = '', $isToken = true, $tokenSymbol = '')
    {
        $params = [
            "from"        => $from,
            "to"          => $to,
            "amount"      => -$amount,
            "note"        => $note,
            "isToken"     => $isToken ? true : false,
            "tokenSymbol" => strtoupper($tokenSymbol)
        ];
        return $this->send($params, 'Chain33.SendToAddress');
    }

    /**
     *  token 转账
     *
     * @param        $from
     * @param        $to
     * @param        $amount
     * @param string $note
     * @param bool   $isToken
     * @param string $tokenSymbol
     * @return array
     */
    public function transToken($from, $to, $amount, $note = '', $isToken = true, $tokenSymbol = '')
    {
        $params = [
            "from"        => $from,
            "to"          => $to,
            "amount"      => $amount,
            "note"        => $note,
            "isToken"     => $isToken ? true : false,
            "tokenSymbol" => strtoupper($tokenSymbol)
        ];
        return $this->send($params, 'Chain33.SendToAddress');
    }
}