如何解码Tron全节点API的原始数据?

问题描述 投票:0回答:2

比如我想批量获取10个区块中的所有trc20交易,我可以调用APIhttps://developers.tron.network/reference/getblockbylimitnext,示例python代码:

import requests, json
startNum=48221000
payload = {'startNum': startNum, 'endNum': startNum+100 }
res=requests.post(url, json=payload)
blocks=json.loads(res.text)['block']

选择一笔 USDT 转账交易,例如:blocks[0]['transactions'][4],则 raw_data 和 raw_data_hex 为:

blocks[0]['transactions'][4]['raw_data']
{'contract': [{'parameter': {'value': {'data': '23b872dd000000000000000000000041ad17c064d7f5868e8a18a7cfdda9f2dbb9391a91000000000000000000000041d482b7eb71da62083b389a494dc5c77d13fcbd1f0000000000000000000000000000000000000000000000000000000000000000', 'owner_address': '41e4802a30928257f4c34173e2c68b10472fff6029', 'contract_address': '41a614f803b6fd780986a42c78ec9c7f77e6ded13c'}, 'type_url': 'type.googleapis.com/protocol.TriggerSmartContract'}, 'type': 'TriggerSmartContract'}], 'ref_block_bytes': 'd3aa', 'ref_block_hash': 'de1122c81e7942d1', 'expiration': 1675270551000, 'fee_limit': 25000000, 'timestamp': 1675270493081}
blocks[0]['transactions'][4]['raw_data_hex']
'0a02d3aa2208de1122c81e7942d140d8eb9fefe0305acf01081f12ca010a31747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e54726967676572536d617274436f6e74726163741294010a1541e4802a30928257f4c34173e2c68b10472fff6029121541a614f803b6fd780986a42c78ec9c7f77e6ded13c226423b872dd000000000000000000000041ad17c064d7f5868e8a18a7cfdda9f2dbb9391a91000000000000000000000041d482b7eb71da62083b389a494dc5c77d13fcbd1f00000000000000000000000000000000000000000000000000000000000000007099a79cefe0309001c0f0f50b'

如何解码这个十六进制数字?好像是protobuf序列化数据。我试图找出哪些交易正在进行转账或其他操作。谢谢。

protocol-buffers tron
2个回答
0
投票

根据官方文档,您应该使用以太币来解码raw_data_hex:

var ethers = require('ethers')

const AbiCoder = ethers.utils.AbiCoder;
const ADDRESS_PREFIX_REGEX = /^(41)/;
const ADDRESS_PREFIX = "41";

//types:Parameter type list, if the function has multiple return values, the order  of the types in the list should conform to the defined order
//output: Data before decoding
//ignoreMethodHash:Decode the function return value, fill falseMethodHash with false, if decode the data field in the gettransactionbyid result, fill ignoreMethodHash with true

async function decodeParams(types, output, ignoreMethodHash) {

if (!output || typeof output === 'boolean') {
    ignoreMethodHash = output;
    output = types;
}

if (ignoreMethodHash && output.replace(/^0x/, '').length % 64 === 8)
    output = '0x' + output.replace(/^0x/, '').substring(8);

const abiCoder = new AbiCoder();

if (output.replace(/^0x/, '').length % 64)
    throw new Error('The encoded string is not valid. Its length must be a multiple of 64.');
return abiCoder.decode(types, output).reduce((obj, arg, index) => {
    if (types[index] == 'address')
        arg = ADDRESS_PREFIX + arg.substr(2).toLowerCase();
    obj.push(arg);
    return obj;
}, []);
}


async function main() {

  let data = '0xa9059cbb0000000000000000000000004f53238d40e1a3cb8752a2be81f053e266d9ecab000000000000000000000000000000000000000000000000000000024dba7580'

  result = await decodeParams(['address', 'uint256'], data, true)
  console.log(result)
}

输出:

  [ '414f53238d40e1a3cb8752a2be81f053e266d9ecab', BigNumber { _hex: '0x024dba7580' } ]

**** 对于较新版本的 ethers.js,请使用 AbiCoder,如下所示:

const AbiCoder = ethers.AbiCoder;

否则你会得到例外。


0
投票

它是protobuf序列化数据,你找到解码raw_data_hex的方法了吗,或者如何创建它?我刚刚看到了创建它的API:https://developers.tron.network/reference/createtransaction

© www.soinside.com 2019 - 2024. All rights reserved.