Polygon ScanのAPI調査
August 16th, 2022

はじめに

@yutakikuchi_ です。
BlockChainのTransaction履歴を見れる xxscanは見たことある方多いと思いますが、履歴を見るだけではなく、Transactionのデータを自動で引っ張ってきて処理を行うなど後続の処理を自動化したい場合はAPIを利用する良いと思います。

ETHとPolygonのAPIは下記がDocumentとなっています。ざっとAPI Documentを見た感じ、EndpointのBaseとなるURLは異なるものの、EndpointのPath, Interfaceともに同じ構成になっているようです。

Polygon ScanのAPIで特定ContractのEvent listを取得する

まずはじめに、Polygon ScanのAPIを利用するには、下記の作業を行う必要があります。

APIの無償プランの場合、秒間 5 calls、1日に10万 callsという制限があります。またPro planに入らないとcallできないAPIのendpointがいくつか存在します。

API Plans
API Plans

上の準備が完了すると、APIをcallできます。以下では本番のPolygonを使わずにtestnets(Mumbai)のAPIをcallする内容で記載します。

特定のcontract addressについて、例えばERC-721のトランザクション event一覧を取得した場合は、こちに掲載されたURLに対してAPIのリクエストを行います。下記はこちらのPolygom mumbai scanに掲載されているトランザクションのサンプルのRequest URLになります。

https://api-testnet.polygonscan.com/api?module=account &action=tokennfttx&contractaddress=0xd6f9e7defa52ac67cdfd6a9a352ce6e0a9684b0a66&page=1&offset=5&sort=asc&apikey=YourApiKeyToken

Response Bodyについては一部のみ掲載しますが、下記のようなJSON形式の各種Event情報が配列形式で取得できます。また上記のaccountに関する情報を取得するendpoint以外にも、transaction一覧、log一覧、block一覧を取得するAPIもあるので、興味がある方は色々と試してみると良いと思います。

{
"status": "1",
"message": "OK",
"result": [
{
"blockNumber": "24956800",
"timeStamp": "1644919485",
"hash": "0x42a2e8f4379b09bf74eefab614854fc6a7506ed22bcb95c113c956511291cb5d",
"nonce": "0",
"blockHash": "0xfa59f4483ca2f1fa4536e4f51f529ddf3b67680772f34be55c1bfe1844f9a103",
"from": "0x9ea7783b3e45213e0da1e9fe7bca3b7362f052b1",
"contractAddress": "0xd6f9e7defa52ac67cdfd6a9a352ce6e0a9684b0a",
"to": "0xeede4b338f35e38b8c0a79193e766eb5fc937abd",
"tokenID": "592",
"tokenName": "MoonieNFT",
"tokenSymbol": "MOONIE",
"tokenDecimal": "0",
"transactionIndex": "19",
"gas": "0",
"gasPrice": "0",
"gasUsed": "0",
"cumulativeGasUsed": "0",
"input": "deprecated",
"confirmations": "2686826"
}, 

DuneなどのAnalyticsを利用する方法もある

上では公開されたWeb APIを利用してPolygon scanで出力されるデータを取得しましたが、Dune Analyticsなど、SQLベースでデータを抽出する便利なツールもあるので、そちらは今後のentryにしたいと思います。

Subscribe to Y's note
Receive the latest updates directly to your inbox.
Mint this entry as an NFT to add it to your collection.
Verification
This entry has been permanently stored onchain and signed by its creator.
More from Y's note

Skeleton

Skeleton

Skeleton