阅读 652

关于波场TRC20转账事件监听from地址和to地址带0x加密而无法判断归属的问题

在Tron区块链开发项目中,根据官方API TRONGRID V1 API -> Events -> Get events by contract address developers.tron.network/reference#e…

这个API的介绍获取某token的转账事件,发现返回的值不能跟数据库的地址做轮询匹配 返回的值如下数据格式显示

[
    "data":[
        [{
          "block_number":27836680
          "block_timestamp":1613898027000
          "caller_contract_address":"TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t"
          "contract_address":"TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t"
          "event_index":0
          "event_name":"Transfer"
          "result":{
              "from":"0x1e970666b8145c5a1e047d66f5eaf0021db5876f",
              "to":"0x133ffc1df450e88d882216dcd65e4d02553ba787",
              "value":"100000000",
          }
          "result_type":{
              "from":"address",
              "to":"address",
              "value":"uint256",
          }
          "transaction_id":"13e9a3149ee1758e5d8f88458abec605ee410d27268be237bee019438fb945f6"
          "_unconfirmed":true
        }],
        [...]
    ],
    "success":true,
    "meta":{"at":1613898030415,"fingerprint":"9mPzvhwtq8uTG3KkJa6hTeawdSUn7rFe4HYN6jr6Ahms98yHK7vrsVhEbok6Am8E55Tjf4P8yjtbr5Ru6ZcCPnoJTcxcYWWtw","links":{...},"page_size":20,}
]
复制代码

而我们后端开发一般都是获取到data的数据,然后for遍历数据,查询数据库是否有这个地址TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t,这样的地址格式存放在数据库中,并绑定这个用户,如果存在就是这个用户的转入行为,给与上分操作。

问题关键是data->result->from和data->result->to的地址是 0x1e970666b8145c5a1e047d66f5eaf0021db5876f 这种格式。 我们无法用TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t0x1e970666b8145c5a1e047d66f5eaf0021db5876f 来判断数据库是否存在这个地址,也就无法知道这笔记录是否是该用户充值。

而官方文档也没有给出如何将这个地址直接转换成tron链的地址,在摸索中查询文档和各种工具的使用都没办法转换为真实的地址。

好了,问题介绍完毕,下面开始介绍SDK包的使用方法

PHP SDK版本使用hexString2Address把地址的0x替换为41即可转换

<?php

        try {
            $tron = new \IEXBase\TronAPI\Tron();
            //获取USDT转账事件数据
            $result = $tron->getEventResult("TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t",0,"Transfer");

            if (empty($result)) return false;

            $data = [];

            foreach ($result as $key =>$item){
                $data[$key]['caller_contract_address'] = $item['caller_contract_address'];
                $data[$key]['result'] = [
                    //注意这里使用hexString2Address方法把地址ox替换为41即可得到原地址
                    "from" => $tron->hexString2Address(str_replace("0x","41",$item['result']['from'])),
                    "to" => $tron->hexString2Address(str_replace("0x","41",$item['result']['to'])),
                    //直接把value转账金额转换为实际精度余额
                    "value" => $tron->fromTron($item['result']['value'])
                ];
                $data[$key]['transaction_id'] = $item['transaction_id'];
                $data[$key]['result_type'] = $item['result_type'];
                $data[$key]['_unconfirmed'] = $item['_unconfirmed'];
                $data[$key]['block_timestamp'] = $item['block_timestamp'];
                $data[$key]['block_number'] = $item['block_number'];
                $data[$key]['event_name'] = $item['event_name'];
                $data[$key]['contract_address'] = $item['contract_address'];
                $data[$key]['event_index'] = $item['event_index'];
            }

            return $data;

        }catch (\Exception $exception){
            return false;
        }
复制代码

Python SDK使用

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import json

from tronapi import Tron
from tronapi import HttpProvider
from trx_utils import remove_0x_prefix

full_node = HttpProvider('https://api.trongrid.io')
solidity_node = HttpProvider('https://api.trongrid.io')
event_server = HttpProvider('https://api.trongrid.io')
tron = Tron(full_node=full_node, solidity_node=solidity_node, event_server=event_server)

tron.private_key = "1fa38830634f258d469468581de973ce21babaa27b65231d8b83b17647bf49d5"
tron.default_address = "TYpPJskU8BNChMgqbaw7BMQqH6wRTYuYT4"

#获取代币转账事件
event_result = tron.get_event_result(contract_address="TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t", event_name="Transfer")
for item in event_result:
    data = tron.toJSON(item)
    data = json.loads(data)
    # 类似于 0x8de284e277c0230323c8b79129be3b50bcea5c6b 格式
    # 删除 0x 开头字符
    toAddress = remove_0x_prefix(data['result']['to'])
    # 加41开头得到真实地址
    toAddress = tron.address.from_hex("41"+toAddress)
    print(toAddress)
复制代码

node tronWeb SDK 的使用,这个官网有转换示例,至于去掉开头0x字符和添加41开头可以使用JavaScript公共方法,这里就不多说了 developers.tron.network/reference#a…

tronWeb.address.fromHex("418840E6C55B9ADA326D211D818C34A994AECED808")
> "TNPeeaaFB7K9cmo4uQpcU32zGK8G1NYqeL"
复制代码
文章分类
后端
文章标签