目的:用于设计合理的数据库字段存储特定地址的历史
获取最近num个区块的方式:api.trongrid.io/wallet/getblockbylatestnum?num=num
以下例子基于区块ID为32378435的数据
TRX
示例:
"ret": [
{
"contractRet": "SUCCESS"
}
],
"signature": [
"910f1ba80fc530124c909a1d046251a356fa743c0e91fc6c77f3ec2e8eef1fc30be292ba42b1dc3bb6bb7572ac234a096fe9d37c2599ffc83b3cca64fd35723a01"
],
"txID": "196d9cb5072d05dd6c5ffe89b0cde71789800abfda4ce3adfb6bd7f1cfeb7aea",
"raw_data": {
"data": "e4b893e6b3a8e4ba8ee58cbae59d97e993bee9a1b9e79baee58c85e8a385e7ad96e58892e8bf90e890a5e4b880e7ab99e5bc8fe69c8de58aa1efbc8ce4b880e994aee58f91e5b881efbc8c44415050e5bc80e58f91efbc8ce689b9e9878fe8bdace8b4a6efbc8ce6b5b7e58685e5a496e8b4a2e7bb8fe5aa92e4bd93e5aea3e58f91efbc8ce6b3a2e59cbae58aa9e6898b68747470733a2f2f74726f6e68656c702e696fe5aea2e69c8de5beaee4bfa174726f6e617373697374616e742f74726f6e61696465",
"contract": [
{
"parameter": {
"value": {
"amount": 1,
"owner_address": "410ac5e852004d831a3ba6e6c84f290bd39e189b5e",
"to_address": "410986687edcd493d062fd05c326de17a64eebd89a"
},
"type_url": "type.googleapis.com/protocol.TransferContract"
},
"type": "TransferContract"
}
],
"ref_block_bytes": "0e2f",
"ref_block_hash": "443029efb0bf917c",
"expiration": 1627611975000,
"timestamp": 1627611917575
},
"raw_data_hex": "0a020e2f2208443029efb0bf917c40d89aefa9af2f52c601e4b893e6b3a8e4ba8ee58cbae59d97e993bee9a1b9e79baee58c85e8a385e7ad96e58892e8bf90e890a5e4b880e7ab99e5bc8fe69c8de58aa1efbc8ce4b880e994aee58f91e5b881efbc8c44415050e5bc80e58f91efbc8ce689b9e9878fe8bdace8b4a6efbc8ce6b5b7e58685e5a496e8b4a2e7bb8fe5aa92e4bd93e5aea3e58f91efbc8ce6b3a2e59cbae58aa9e6898b68747470733a2f2f74726f6e68656c702e696fe5aea2e69c8de5beaee4bfa174726f6e617373697374616e742f74726f6e616964655a65080112610a2d747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e5472616e73666572436f6e747261637412300a15410ac5e852004d831a3ba6e6c84f290bd39e189b5e1215410986687edcd493d062fd05c326de17a64eebd89a18017087daeba9af2f"
字段含义:
- "type": "TransferContract"表示这是一个TRX Transfer
- "contractRet": "SUCCESS"表示成功
- "amount": 1为发送数额,需要除以10^精度 才是真实值,TRX精度是6
- owner_address为发送者地址的HEX格式
- to_address为接收者地址的HEX格式
- data是备注,转为Utf8即可看到备注文本,如果Transfer没备注则没这个字段
TRC10
示例
"ret": [
{
"contractRet": "SUCCESS"
}
],
"signature": [
"6be003e9ae8bc24b7c320ebf716d65bbf27d77339fa2223b5e87055fc20a6d49612e2e8d481cadb8941f3bc1ed14638ef374c2fdbe338723938a5fef9412d1a900"
],
"txID": "18f7ada1eac34435357a38b12baa188fae1914d15d24c7b129663fcee0838663",
"raw_data": {
"data": "53707265616420756e636f6e646974696f6e616c206c6f76652c206d616b65206120686170707920776f726c64",
"contract": [
{
"parameter": {
"value": {
"amount": 7618406,
"asset_name": "31303034303331",
"owner_address": "41ef3ebcf4eeb3f16e511d7c94dced8f318d6625ec",
"to_address": "4119db9504e434508bfc5f29be11a69d387780d694"
},
"type_url": "type.googleapis.com/protocol.TransferAssetContract"
},
"type": "TransferAssetContract"
}
],
"ref_block_bytes": "0e2f",
"ref_block_hash": "443029efb0bf917c",
"expiration": 1627611927306,
"timestamp": 1627611867306
},
"raw_data_hex": "0a020e2f2208443029efb0bf917c408aa6eca9af2f522d53707265616420756e636f6e646974696f6e616c206c6f76652c206d616b65206120686170707920776f726c645a76080212720a32747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e5472616e736665724173736574436f6e7472616374123c0a0731303034303331121541ef3ebcf4eeb3f16e511d7c94dced8f318d6625ec1a154119db9504e434508bfc5f29be11a69d387780d69420e6fed00370aad1e8a9af2f"
字段含义
- "type": "TransferAssetContract"表示这是一个TRC10 Transfer
- "contractRet": "SUCCESS"表示成功,除了SUCCESS之外还有OUT_OF_ENERGY,表示能量不足。
- "asset_name": "31303034303331"为TRC10通证ID的HEX格式,将31303034303331转为ASCII或者Utf8字符串可得到1004031,1004031就是通证ID
- "amount": 7618406为Transfer数额,需要除以通证的10^精度 才是真实值
- owner_address为发送者地址的HEX格式
- to_address为接收者地址的HEX格式
- data是备注,转为Utf8即可看到备注文本,如果转账没备注则没这个字段
- 遗留问题:通证的精度从哪里解析?目前只能根据通证ID再去查一遍
TRC20
示例
"ret": [
{
"contractRet": "SUCCESS"
}
],
"signature": [
"cce3f2ae7f1ed71f8221a9f1c1e17e0d6e5522acf6d9ee1a3e6947548e9dc000665eb09ede7c2918552a69ec7b995fe8c4d0c913e4c07eadce1f3fb4691a0da800"
],
"txID": "24190ffd0ef9aeac092cdbc1cb72608aa6878100b60df376e44d41772edc8b56",
"raw_data": {
"data": "6d656d6f",
"contract": [
{
"parameter": {
"value": {
"data": "a9059cbb00000000000000000000000045c1098b64c646b2766ba322a704d12e709d7ba800000000000000000000000000000000000000000000001ba5abf9e779380000",
"owner_address": "41568b3b208d915cce7d0d785b4437e177cd2181c5",
"contract_address": "411fc796d0601456996b9a9c85a2fcc09a91c51065"
},
"type_url": "type.googleapis.com/protocol.TriggerSmartContract"
},
"type": "TriggerSmartContract"
}
],
"ref_block_bytes": "0e42",
"ref_block_hash": "0d97e93a1faed6fa",
"expiration": 1627647918000,
"scripts": "73637269707473",
"fee_limit": 10000000,
"timestamp": 1627611918000
},
"raw_data_hex": "0a020e4222080d97e93a1faed6fa40b0ff80bbaf2f52046d656d6f5aae01081f12a9010a31747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e54726967676572536d617274436f6e747261637412740a1541568b3b208d915cce7d0d785b4437e177cd2181c51215411fc796d0601456996b9a9c85a2fcc09a91c510652244a9059cbb00000000000000000000000045c1098b64c646b2766ba322a704d12e709d7ba800000000000000000000000000000000000000000000001ba5abf9e77938000062077363726970747370b0ddeba9af2f900180ade204"
字段含义:
- raw_data_hex包含2244a9059cbb为transfer方法转账,不包含则可能是其它function的智能合约触发
- "type": "TriggerSmartContract"表示当前为一个TRC20 Transfer
- "contractRet": "SUCCESS"表示成功,除了SUCCESS之外还有OUT_OF_ENERGY,表示能量不足。
- 第一个data是备注,转为Utf8即可看到备注文本,如果转账没备注则没这个字段
- owner_address为发送者地址的HEX格式
- contract_address为TRC20发行合约的HEX格式
- 第二个data参数,从第33位开始连续40位抽取出来(45c1098b64c646b2766ba322a704d12e709d7ba8),前面增加“41”即为收款人的地址HEX(4145c1098b64c646b2766ba322a704d12e709d7ba8)
- 第二个data参数,倒数64位为Transfer数量的16进制,转10进制除以该合约的10^精度 可得到真实数量
其它注意事项
- 有些TRC20 Transfer没有timestamp字段,例如:
703f7451e23293a629f8a5ddd62305bb2173d5efece482aef5d58d1f641752cb
这个时候可以用expiration这个时间戳 - 有些TRC20转账多了个call_value字段,这种不是常规的TRC20转账,第二个data字段也无法解析出相应信息,例如:
b00b60c2c25ec540b7a65456c4001881ceb36ee36992e57cd56da104c0ac8e5e
这个时候就放弃该条历史
如何获取所有区块上的接收者地址?
- 遍历,如果是TRX或TRC10,则直接把to_address转base58;如果是TRC20,判断不存在call_value后,第二个data参数,从第33位开始连续40位抽取出来,前面增加“41”再转base58即为接收者的地址
- 得到所有base58地址和对应的txID后,去跟数据库的地址取交集即需要记录历史信息的地址