新浪博客

波场TronGridAPIKey使用详解

2024-09-22 13:30阅读:
文章目录
一、什么是tron的API Key?
二、请求速率限制
2.1 如何判断是否受到速率限制?
2.2 如果速率受限怎么办?
2.3 限制请求的因素
三、使用Trongrid的API Key
3.1 创建TronGrid API Key
3.2 使用Trongrid的API Key
3.3 API Key安全设置
3.4 申领Trongrid 50万请求量
四、Tronscan、Trongrid、自建节点的区别
4.1 Tronscan
4.2 Trongrid
4.3 自建节点
一、什么是tron的API Key?
TronGrid提供TRON网络所有的全节点HTTP API和扩展 API,为了保证请求资源的合理分配,所有的请求API都需要带参数API Key,没有API Key的请求将不响应。
目前Tron Grid 为波场提供了以下三个网络的URL
网络
URL
tron 主网 https://api.trongrid.io
Shasta测试网 https://api.shasta.trongrid.io
Nile测试网 https://nile.trongrid.io
注:目前使用Trongrid请求Shasta/Nile测试网不需要设置API Key。
注:shata测试网跟nile有什么区别?
shasta 更贴近于主网 ; nile 负责测试新功能
二、请求速率限制
目前Trongrid的速率限制有两个方面:
当用户使用量超过一天的总量时,每秒的频率会限制在一个很低的值,例如5qps,超过这个限制就会拒绝访问。
当用户使用量没有超过一天的总量,则限制在15qps,超过这个限制,就会拒绝访问,并且有一个惩罚机制,30s内不响应服务。
2.1 如何判断是否受到速率限制?
如果受到速率限制,那么您的请求返回中将具有HTTP状态代码4xx,并包含错误信息,您可以根据相应的错误信息进行调整。
v1接口示例
{
“success”: false,
“error”: “The key exceeds the frequency limit(15), and the query server is suspended for 30s”,
“statusCode”: 403
}
1
2
3
4
5
Fullnode接口
{
“Error”: “The key exceeds the frequency limit(30), and the query server is suspended for 30s”
}
1
2
3
2.2 如果速率受限怎么办?
如果受到速率限制,可以考虑以下一些解决方法:
确保在接口请求中使用API Key。没有API Key的请求将受到严格的速率限制,甚至被完全拒绝。
在服务启动时限制请求次数。
不要时时请求Trongrid,因为TRON网络3s左右出一个块,所以一直请求新数据通常是没有意义的。
2.3 限制请求的因素
目前TronGrid对所有TronGrid请求有IP频率限制,对于使用API Key的TronGrid HTTP请求, IP频率限制为15QPS
不使用API Key的TronGrid HTTP请求,目前IP频率限制为10QPS并且会定期逐渐下降。使用TronGrid GRPC服务目前IP频率限制为15QPS,超出频率限制后将返回503错误,如有503报错,请降低TronGrid请求频率,及建议尽快添加API Key。
参考tron官方文档:
https://cn.developers.tron.network/reference/api-key
https://cn.developers.tron.network/docs/faq#10如何解决trongrid-503-service-temporarily-unavailable报错?
https://cn.developers.tron.network/reference/rate-limits
三、使用Trongrid的API Key
3.1 创建TronGrid API Key
创建TronGrid账号
https://www.trongrid.io/login
注:需要使用邮箱注册,并使用邮箱激活
创建 API Key
点击 “Create API Key” 来创建一个 API Key
输入一个API Key的名称,即可获得一个新的API Key
以下请注意:
每个账号可以生成三个 API Key,总请求量是10万请求/天,三个API Key
同一账号中的请求速率是:15QPS,同一个服务器,使用两个Key,QPS依然是15。因为策略限制的是:账号和IP地址,如果需要更多的QPS,可以尝试多个账号,多台服务器请求。
3.2 使用Trongrid的API Key
curl
curl -X POST \
\
\-H 'Content-Type: application/json' \
\-H 'TRON-PRO-API-KEY: 25f66928-0b70-48cd-9ac6-da6f8247c663' \
\-d '{
'to\_address': '41e9d79cc47518930bc322d9bf7cddd260a0260a8d',
'owner\_address': '41D1E7A6BC354106CB410E65FF8B181C600FF14292',
'amount': 1000
}'
1
2
3
4
5
6
7
8
9
golang
// httpRequest http请求
func (c \*Client) HttpRequest(methed, params string) (resBody \[]byte, err error) {
// 初始化请求
body := strings.NewReader(params)
req, err := http.NewRequest('POST', c.url+methed, body)
if err != nil {
return nil, errors.Wrap(err, 'Http NewRequest')
}
// 执行请求
req.Header.Add('Content-Type', 'application/json')
req.Header.Add('Accept', 'application/json')
req.Header.Add('Accept', 'application/json')
req.Header.Add('TRON-PRO-API-KEY','c1c9a2da-8fd0-40d7-9599-d55129f43c2d')
res, err := c.client.Do(req)
if err != nil {
return nil, errors.Wrap(err, 'Client Do')
}
defer res.Body.Close()
// 接收返回结果
resBody, err = ioutil.ReadAll(res.Body)
if err != nil {
return nil, errors.Wrap(err, 'ioutil.ReadAll')
}
return resBody, nil
}
* python
import requests
url = ''
payload = '{ 'to\_address': '41e9d79cc47518930bc322d9bf7cddd260a0260a8d', 'owner\_address': '41D1E7A6BC354106CB410E65FF8B181C600FF14292', 'amount': 1000}'
headers = {
'Content-Type': 'application/json',
'TRON-PRO-API-KEY': '25f66928-0b70-48cd-9ac6-da6f8247c663'
}
response = requests.request('POST', url, data=payload, headers=headers)
print(response.text)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
JavaScript
var request = require('request');
var options = { method: 'POST',
url: '',
headers: {\
'TRON-PRO-API-KEY': '25f66928-0b70-48cd-9ac6-da6f8247c663',
'Content-Type': 'application/json'
},
body: {
to\_address: '41e9d79cc47518930bc322d9bf7cddd260a0260a8d',
owner\_address: '41D1E7A6BC354106CB410E65FF8B181C600FF14292',
amount: 1000
},
json: true
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
参考:
https://cn.developers.tron.network/reference/api-key
3.3 API Key安全设置
JWT
JWT required - 要求请求中是否包含JWT
JWT public keys - 用于验证随请求发送的JWT的公共密钥
AllowList: 对允许的请求类型设置限制
User-Agents(允许的用户代理)
HTTP Origin (允许请求来源)
Contract Addresses (允许的合约地址)
仅查询来自特定智能合约或地址来源的数据,请将这些地址添加到您的合约地址允许列表。
将地址添加到允许列表后,所有查询地址不在允许列表中的部分API请求都会被拒绝。
Method (允许的请求方法)
可使用API Key将请求限制为特定的调用方法。 如果列表不为空,则列表中未指定的任何方法调用都将被拒绝。
参考文档:
https://cn.developers.tron.network/reference/security-settings
3.4 申领Trongrid 50万请求量
https://www.trongrid.io/price
登录需要扩容至50万请求量的 Tron Grid账号
链接波场的TronLink钱包
TronLink 钱包同意链接请求并签名
扩容完毕
四、Tronscan、Trongrid、自建节点的区别
4.1 Tronscan
Tronscan接口与以上限制策略是一样的
但是tronscan的API 不用设置API key ,但因为没有API key ,所以无法进行权限管理
tronscan的单个API调用是5分钟2000次,频率也是 单个IP地址 15次每秒,超过了就会报错。
Tronscan接口文档:https://github.com/tronscan/tronscan-frontend/blob/dev2019/document/api.md
4.2 Trongrid
Trongrid 有请求速率限制
Trongrid 拥有特有的v1接口,而自建节点节点是没有v1接口的,其它接口都是一样的。
v1接口官方文档:https://cn.developers.tron.network/reference/background
4.3 自建节点
自建节点没有请求速率限制,且Trongrid目前没有使用收费提高限制的服务,如果项目请求量较大,建议使用自建节点。
自建节点配置最低需求:
cpu:16核
内存:36GB
磁盘:3T ssd硬盘
带宽:100M
1
2
3
4
参考文档:
官方节点部署文档:https://cn.developers.tron.network/docs/deploy-the-fullnode-or-supernode
官方快照文档:https://cn.developers.tron.network/docs/main-net-database-snapshots
————————————————

我的更多文章

下载客户端阅读体验更佳

APP专享