TronGridJS
介绍
TronGridJS 是一个 Javascript 库,用于利用 TronGrid API 从波场网络中检索区块链数据。
兼容性
- 为 Node.js v6 及以上版本构建的版本。
- 为市场份额超过 0.25% 的浏览器构建的版本。
TronGridJS 还与前端框架兼容,如:
- Angular
- React
- Vue
TronGridJS也可以通过Chrome扩展程序上线。
安装
TronGridJS - NPM 软件包
NPM
> npm install trongridYarn
> yarn add trongrid构建步骤
如果您想在本地下载和构建,请按照以下步骤操作:
git clone https://github.com/TRON-US/trongrid-js.git
cd trongrid-js
yarn install
yarn dev
yarn build
yarn test支持的API
可以轻松访问由TronGrid提供的新 v1 API:
tronGrid.account.get(accountAddress, options)描述 | 替代 |
|---|---|
在 | /wallet/getaccount |
参数 | 描述 | 值 |
|---|---|---|
onlyConfirmed | 仅显示最近确认的块的情况。 | true | false(默认为 false) |
tronGrid.account.getTransactions(accountAddress, options)描述 | 替代 |
|---|---|
返回与 | /walletextension/gettransactionfromthis |
参数 | 描述 | 值 |
|---|---|---|
only_confirmed | 显示仅确认。 | true | false(默认为false) |
only_unconfirmed | 显示只有未经证实。 | true | false(默认为false) |
only_to | 只有交易才能解决。 | true | false(默认为false) |
only_from | 只来自地址的交易。 | true | false(默认为false) |
limit | 请求的每页交易次数。 | 默认为 20。最多 200。 |
fingerprint | 上一页返回的最后一笔交易的指纹。 | |
order_by | 对查询结果进行排序。 order_by=timestamp, desc | |
min_timestamp | 最小事务时间戳。 | 默认为 0 |
max_timestamp | 最大事务时间戳。 | 默认为 now |
tronGrid.asset.getAll(options)描述 | 替代 |
|---|---|
返回TRON平台上的所有资产。 | N/A |
参数 | 描述 | 值 |
|---|---|---|
order_by | 对结果进行排序。 | 接受的字段: |
tronGrid.asset.get(assetIdentifier, options)描述 | 替代 |
|---|---|
返回由所有者地址或其自己的ID标识的资产。 | /wallet/getassetissuebyaccount /wallet/getassetissuebyid |
tronGrid.asset.getList(assetName, options)描述 | 替代 |
|---|---|
返回名为 | /wallet/getassetissuelistbyname |
参数 | 描述 | 值 |
|---|---|---|
limit | 请求的每页资产数量。 | 默认为 20。最多 200。 |
fingerprint | 上一页返回的最后一个资产的指纹。有分页时,最小限制设置为20。 | |
order_by | 对查询结果进行排序。 | 接受的字段: 例: order_by = start_time,desc (从最近的 ICO 开始) order_by = id, asc (从最古老的开始) |
only_confirmed | 仅显示最近确认的块的情况。 | true | false(默认为 false) |
tronGrid.block.getEvents(identifier, options)描述 | 替代 |
|---|---|
返回特定块的所有事件。标识符可以是 “latest” 或块号。 | N/A |
tronGrid.contract.getEvents(contractAddress, options)描述 | 替代 |
|---|---|
返回智能合约发出的所有事件。 | N/A |
参数 | 描述 | 值 |
|---|---|---|
only_confirmed | 显示仅确认。 | |
only_unconfirmed | 显示只有未经证实。 | |
event_name | 事件的名称。 | |
block_number | 需要事件的块编号。 | |
min_timestamp | 最小块时间戳。 | |
max_timestamp | 最大块时间戳。 | |
order_by | 对事件进行排序。 | 可接受的值: |
limit | 为了分页。 | 默认为20 |
fingerprint | 页面中检索到的最后一个事件的指纹。 |
tronGrid.transaction.getEvents(id, options)描述 | 替代 |
|---|---|
返回由 | N/A |
回应 & 分页
任何API都将返回带有success属性、数据数组和元对象的响应。 例如,await tronGrid.asset.getAll()将返回如下内容:
{
"success": true,
"data": [
{
"confirmed": true,
"id": "1002225",
"abbr": "DbDsgVP3GRh",
"description": "KEYS unlock Cryptocurrency. Keys are a digital asset designed to work as medium of exchange.",
"frozen_supply": [
{
"frozen_days": 730,
"frozen_amount": 75926666666
}
],
"name": "KEYS",
"num": 22778,
"precision": 0,
"total_supply": 227780000000,
"trx_num": 22778,
"url": "www.KEYS.crypto.org",
"vote_score": 0,
"owner_address": "4149b3dad5ef9dbab6a059fc95159efcecd5db910e",
"start_time": 1553538720706,
"end_time": 1553538960706
},
...
],
"meta": {
"total": 2,
"at": 1553548776704,
"fingerprint": "8xuwf4jd2dpoSms5KzLhxY9fmCm9oJA5164Qd7T2SexRSHYCwvRAr2zJGtwJceEcGWz",
...
}
}如您所见,在元字段中,您必须将指纹作为选项传递给下一个请求,以获取下一页。
用法
正常使用
安装TronWeb
npm install tronweb初始化TronWeb并创建TronGridJS实例
const TronGrid = require('trongrid');
const TronWeb = require('tronweb');
const tronWeb = new TronWeb({
fullHost: 'https://api.trongrid.io'
});
const tronGrid = new TronGrid(tronWeb);例
const TronGrid = require('trongrid');
const TronWeb = require('tronweb');
const tronWeb = new TronWeb({
fullHost: 'https://api.trongrid.io'
});
const tronGrid = new TronGrid(tronWeb);
tronGrid.setExperimental('your experimental key');
async function getAccount() {
const address = 'TPL66VK2gCXNCD7EJg9pgJRfqcRazjhUZY';
const options = {
Show_assets: true,
only_confirmed: true,
};
// awaiting
const account = await tronGrid.account.get(address, options);
console.log({account});
// promise
tronGrid.account.get(address, options).then(account => {
console.log({account});
}).catch(err => console.error(err));
// callback
tronGrid.account.get(address, options, (err, account) => {
if (err)
return console.error(err);
console.log({account});
});
}
async function getTransactions() {
const address = 'TPL66VK2gCXNCD7EJg9pgJRfqcRazjhUZY';
const options = {
only_to: true,
only_confirmed: true,
limit: 100,
order_by: 'timestamp,asc',
min_timestamp: Date.now() - 60000 // from a minute ago to go on
};
// awaiting
const transactions = await tronGrid.account.getTransactions(address, options);
console.log({transactions});
// promise
tronGrid.account.getTransactions(address, options).then(transactions => {
console.log({transactions});
}).catch(err => console.error(err));
// callback
tronGrid.account.getTransactions(address, options, (err, transactions) => {
if (err)
return console.error(err);
console.log({transactions});
});
}
async function getAssets() {
const address = 'TXk39yyhzpfbqtU1BATUzpcfQ37L8Tc4Ht';
const options = {};
// awaiting
const assets = await tronGrid.asset.get(address);
console.log({assets});
// promise
tronGrid.asset.get(address, options).then(assets => {
console.log({assets});
}).catch(err => console.error(err));
// callback
tronGrid.asset.get(address, options, (err, assets) => {
if (err)
return console.error(err);
console.log({assets});
});
}
getAccount();
getTransactions();
getAssets();插入
你可以在TronWeb中插入TronGrid,用一个内部调用tronGrid.account.getTransactions(...)的方法替换标准方法tronWeb.trx.getTransactionRelated(...)。
要使用它,用户必须将TronGrid注册为插件:
tronWeb.plugin.register(TronGrid);有时可能需要将一些选项传递给插件界面。查看 test/unit/plugin.test.js 中的测试,您可以将插件注册为:
let result = tronWeb.plugin.register(GetNowBlock, {
someParameter: 'some value'
})对于MVP,实验性访问代码可以作为选项传递给插件:
tronWeb.plugin.register(TronGrid, {experimental: ... });注册插件后,请致电:
await tronWeb.trx.getTransactionsFromAddress(address);和
await tronWeb.trx.getTransactionsToAddress(address);将从引擎盖下的TronGrid返回结果。
Updated 9 months ago