123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?php
- namespace qiniu\services\blockchain;
- use qiniu\services\blockchain\bsc\Bep20;
- use qiniu\services\blockchain\bsc\Bep721;
- use qiniu\services\blockchain\bsc\Callback;
- use qiniu\services\blockchain\bsc\Credential;
- use qiniu\services\blockchain\bsc\Kit;
- use qiniu\services\blockchain\bsc\NodeClient;
- use qiniu\services\blockchain\Throwable\Web3Exception;
- use Web3p\EthereumUtil\Util;
- define('EVENTSIG_TRANSFER', '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef');
- class Web3Service
- {
- /** @var Bep20|Bep721 $token */
- private static $token;
- private static $decimal;
- private static $address;
- private static $kit;
- public function __construct($contact, $decimal, $type, $private_key)
- {
- $node = NodeClient::create('mainNet');
- $credential = Credential::fromKey($private_key);
- self::$kit = new Kit($node, $credential);
- self::$address = $credential->getAddress();
- self::$token = self::$kit->$type($contact);
- self::$decimal = $decimal;
- }
- /**
- * @param $token
- * @return Web3Service
- * @throws Web3Exception
- */
- public static function instance($token): Web3Service
- {
- if (!get_token_info($token, 'address')) {
- throw new Web3Exception('未定义Token');
- }
- return new self(get_token_info($token, 'address'), get_token_info($token, 'decimal'), get_token_info($token, 'type'), get_token_info($token, 'private_key'));
- }
- /**
- * 签名转地址
- * @param $str
- * @param $sign
- * @return string
- * @throws Web3Exception
- */
- public static function signToAddress($str, $sign)
- {
- $util = new Util();
- if (strlen($sign) != 132) {
- throw new Web3Exception('签名格式错误,类型:001');
- }
- if (!$util->isHex($sign)) {
- throw new Web3Exception('签名格式错误,类型:002');
- }
- $sign_str = $util->stripZero($sign);
- $r = substr($sign_str, 0, 64);
- $s = substr($sign_str, 64, 64);
- $v = substr($sign_str, 128, 2);
- $message = $util->hashPersonalMessage($str);
- $public_key = $util->recoverPublicKey($message, $r, $s, hexdec($v) - 27);
- return strtolower($util->publicKeyToAddress($public_key));
- }
- public static function getAddress()
- {
- return self::$address;
- }
- public static function getBalance($address)
- {
- return bcdiv(self::$token->balanceOf($address)->toString(), bcpow(10, self::$decimal), self::$decimal);
- }
- public static function getLastTransfer($num = 0)
- {
- return self::$token->getTransferEvents([], [], $num ? bcsub(self::$token->getBlockNumber(), $num) : 'latest');
- }
- public static function getBlockTransfer($block, $transferHash)
- {
- $res = self::$token->getTransferEvents([], [], $block, $block);
- $ress = [];
- foreach ($res as $v) {
- if ($v->transactionHash == $transferHash) {
- $ress[] = $v;
- }
- }
- return $ress;
- }
- public static function getTransferInfo($tx_hash)
- {
- return self::$token->getTransactionReceipt($tx_hash);
- }
- public static function transfer($to, $amount)
- {
- return self::$token->transfer($to, bcmul($amount, bcpow(10, self::$decimal)));
- }
- public static function getBNBTransferInfo($tx_hash)
- {
- return self::$token->getTransactionReceipt($tx_hash);
- }
- public static function estimateTransferGas($to, $amount)
- {
- $cb = new Callback();
- self::$token->estimateGas("transfer", self::$address, $to, bcmul($amount, bcpow(10, self::$decimal)), $cb);
- return $cb->result->toString();
- }
- public static function getData($name, ...$args)
- {
- $data = self::$token->getData($name, ...$args);
- $tx = [
- 'data' => '0x' . $data,
- 'to' => self::$token->getToAddress()
- ];
- return $tx;
- }
- public static function totalSupply()
- {
- $data = self::$token->totalSupply();
- $value = $data->toString();
- return bcdiv($value, bcpow(10, self::$decimal), self::$decimal);
- }
- }
|