Extracting Blockchain Game Data Part 1 - Alchemy API

Alchemy API
Cover Image for Extracting Blockchain Game Data Part 1 - Alchemy API

When utilizing blockchain data, Alchemy and Infura are commonly used.

For example, using Alchemy's getNFTs function,

curl --request GET \
     --url 'https://eth-mainnet.g.alchemy.com/nft/v2/docs-demo/getNFTs?owner=0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045&withMetadata=true&pageSize=100' \
     --header 'accept: application/json'

This allows for the extraction of data like the following.

{
  "ownedNfts": [
    {
      "contract": {
        "address": "0x000386e3f7559d9b6a2f5c46b4ad1a9587d59dc3"
      },
      "id": {
        "tokenId": "0x0000000000000000000000000000000000000000000000000000000000000001",
        "tokenMetadata": {
          "tokenType": "ERC721"
        }
      },
      "balance": "26",
      "title": "",
      "description": "",
      "tokenUri": {
        "gateway": "http://api.nikeapenft.xyz/ipfs/1",
        "raw": "http://api.nikeapenft.xyz/ipfs/1"
      },
      "media": [
        {
          "gateway": "",
          "raw": ""
        }
      ],
      "metadata": {},
      "timeLastUpdated": "2024-01-15T18:08:38.483Z",
      "contractMetadata": {
        "name": "Bored Ape Nike Club",
        "symbol": "BANC",
        "tokenType": "ERC721",
        "contractDeployer": "0x51d7d428041e23ef51422e110dfeff906e821cfe",
        "deployedBlockNumber": 14276343,
        "openSea": {
          "floorPrice": 0,
          "collectionName": "BoredApeNikeClub",
          "collectionSlug": "bored-ape-nike-club-v2",
          "safelistRequestStatus": "not_requested",
          "imageUrl": "https://lh3.googleusercontent.com/yJ9DgXqjRwgdCkrQmHj7krCbixM8fPVAyYJWJ5NHXap1L0c3QL5MPvrNT0QDINIStGOK857lOvab8MpNQS9X4pkHPktmhVmN82qoVw=s120",
          "description": "COUNTDOWN OVER. MINTING LIVE.\n\n[Mint on the website.](https://nikemetaverse.xyz)\n",
          "bannerImageUrl": "https://lh3.googleusercontent.com/i84LsC2dtbF5I3YiuaXzzfvSijlBI-ZJ8UEta04Ukl4V57Uoj0ZGw8tNyuPdwrF7N5pclyzdqSJjxHZ65z4G5jQrVRK_DHUMVrzTYQ=s2500",
          "lastIngestedAt": "2024-01-08T20:49:13.000Z"
        }


...

Using Alchemy API like above, one can retrieve NFT information from addresses such as 0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045.

Let's examine more practical data.

curl --request POST \
     --url https://eth-mainnet.g.alchemy.com/v2/docs-demo \
     --header 'accept: application/json' \
     --header 'content-type: application/json' \
     --data '
{
  "id": 1,
  "jsonrpc": "2.0",
  "method": "alchemy_getAssetTransfers",
  "params": [
    {
      "fromBlock": "0x0",
      "toBlock": "latest",
      "toAddress": "0x5c43B1eD97e52d009611D89b74fA829FE4ac56b1",
      "withMetadata": false,
      "excludeZeroValue": true,
      "maxCount": "0x3e8"
    }
  ]
}
'

Using Alchemy API's getAssetTransfers as above, you can retrieve all results of token transfers.

In the case of the BigTime blockchain game, consider tokens sent from the address '0x46950ba8946d7be4594399bcf203fb53e1fd7d37' within the '0x64Bc2cA1Be492bE7185FAA2c8835d9b824c8a194' token contract as 'rewards'.

This data can be used to fetch BigTime game's minting information using Alchemy.

Translating the above assumption into an API would look like the following.

curl --request POST \
     --url https://eth-mainnet.g.alchemy.com/v2/docs-demo \
     --header 'accept: application/json' \
     --header 'content-type: application/json' \
     --data '
{
  "id": 1,
  "jsonrpc": "2.0",
  "method": "alchemy_getAssetTransfers",
  "params": [
    {
      "fromBlock": "0x0",
      "toBlock": "latest",
      "withMetadata": false,
      "excludeZeroValue": true,
      "maxCount": "0x3e8",
      "fromAddress": "0x46950ba8946d7be4594399bcf203fb53e1fd7d37",
      "contractAddresses": [
        "0x64Bc2cA1Be492bE7185FAA2c8835d9b824c8a194"
      ],
      "category": [
        "erc20"
      ]
    }
  ]
}
'

The results would be as follows.

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "transfers": [
      {
        "blockNum": "0x1171ff3",
        "uniqueId": "0x2dcc08e6ea34af507b3403f3c72309ec1431ff4e207214a97c12cf11c38be3c3:log:76",
        "hash": "0x2dcc08e6ea34af507b3403f3c72309ec1431ff4e207214a97c12cf11c38be3c3",
        "from": "0x46950ba8946d7be4594399bcf203fb53e1fd7d37",
        "to": "0xe358f4aff5995197d676819678c40ba9e2c05953",
        "value": 5,
        "erc721TokenId": null,
        "erc1155Metadata": null,
        "tokenId": null,
        "asset": "BIGTIME",
        "category": "erc20",
        "rawContract": {
          "value": "0x4563918244f40000",
          "address": "0x64bc2ca1be492be7185faa2c8835d9b824c8a194",
          "decimal": "0x12"
        }
      },
      {
        "blockNum": "0x11720ae",
        "uniqueId": "0x48aa54206488cffe9bcbdeb42b28005ba05365ad160338d7956ae392656e19c5:log:255",
        "hash": "0x48aa54206488cffe9bcbdeb42b28005ba05365ad160338d7956ae392656e19c5",
        "from": "0x46950ba8946d7be4594399bcf203fb53e1fd7d37",
        "to": "0xe358f4aff5995197d676819678c40ba9e2c05953",
        "value": 5,
        "erc721TokenId": null,
        "erc1155Metadata": null,
        "tokenId": null,
        "asset": "BIGTIME",
        "category": "erc20",
        "rawContract": {
          "value": "0x4563918244f40000",
          "address": "0x64bc2ca1be492be7185faa2c8835d9b824c8a194",
          "decimal": "0x12"
        }
      },


Examining the above curl API, 'fromBlock' with 0x0 represents the initial state. If you input a block time from one hour ago, it extracts data from within that hour. 'toBlock' determines how far to retrieve block data, which can be sourced from Etherscan. Since the maximum 'maxCount' is 1000, the default value of 0x3e8 is used. 'fromAddress' is the parameter for the transfer origin, and 0x64Bc2cA1Be492bE7185FAA2c8835d9b824c8a194 is the token smart contract address.

However, running the above curl can result in an error, as it's not done with an API Key. Simply obtaining an API Key from the Alchemy platform and replacing it in the URL will work. Using Alchemy, simple data on how much each blockchain game mints can be created.

Alchemy also offers a variety of APIs, including those for token balances, metadata, and transfer data, as previously mentioned. Most of its offerings are related to NFTs.

It provides various APIs, such as those retrieving NFTs from a single address or all owners of an NFT. Typically, Alchemy can be used for free up to a certain point, after which payment is required for over twenty million API calls. Hence, it suffices for personal or hobbyist use.

The downside of Alchemy is the cost associated with increased usage and its slow nature. As seen in the API call, 'https://eth-mainnet.g.alchemy.com/v2/docs-demo' is specific to the Ethereum mainnet. Different calls are needed for each chain, and if the 'fromAddress' varies, additional calls are necessary. For example, checking ten different addresses across three chains requires thirty API calls, taking over 15 seconds if each call takes 0.5 seconds.

To solve these issues, subgraphs can be utilized, and Alchemy has recently started supporting them. However, using The Graph for subgraphs is more convenient than Alchemy's. In the next part, I'll explore using The Graph to create subgraphs and extract data from BigTime as described above.

Image source: Generated through OpenAI's DALL·E

More Articles

Cover Image for The World EigenLayer is Trying to Create
EigenLayer

The World EigenLayer is Trying to Create

EigenLayer aims to be the AWS of Web3, offering verifiable and decentralized cloud services. Web3 apps operate securely within this ecosyste...

Cover Image for  The significance of Blast in the Layer 2 space
Blast

The significance of Blast in the Layer 2 space

Unlike other L2s that focus on reducing gas fees, Blast introduces automatic rebalancing for Ethereum and stablecoins, yielding up to 4% int...