{"id":13543995,"url":"https://github.com/protofire/subgraph-toolkit","last_synced_at":"2025-04-02T13:31:25.810Z","repository":{"id":42446996,"uuid":"267588650","full_name":"protofire/subgraph-toolkit","owner":"protofire","description":"A collection of utilities and helpers to support the development of subgraphs","archived":false,"fork":false,"pushed_at":"2022-11-10T07:18:43.000Z","size":102,"stargazers_count":51,"open_issues_count":6,"forks_count":7,"subscribers_count":10,"default_branch":"main","last_synced_at":"2025-03-22T21:39:21.687Z","etag":null,"topics":["ethereum","subgraphs","thegraphprotocol"],"latest_commit_sha":null,"homepage":"https://thegraph.com/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/protofire.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-05-28T12:49:35.000Z","updated_at":"2025-03-10T15:48:36.000Z","dependencies_parsed_at":"2022-08-31T00:22:26.877Z","dependency_job_id":null,"html_url":"https://github.com/protofire/subgraph-toolkit","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/protofire%2Fsubgraph-toolkit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/protofire%2Fsubgraph-toolkit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/protofire%2Fsubgraph-toolkit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/protofire%2Fsubgraph-toolkit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/protofire","download_url":"https://codeload.github.com/protofire/subgraph-toolkit/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246823736,"owners_count":20839772,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ethereum","subgraphs","thegraphprotocol"],"created_at":"2024-08-01T11:00:40.311Z","updated_at":"2025-04-02T13:31:20.792Z","avatar_url":"https://github.com/protofire.png","language":"TypeScript","readme":"# Subgraph Toolkit\nA Swiss Army knife for building subgraphs on [The Graph Protocol](https://thegraph.com)\n\n[![NPM Package](https://img.shields.io/npm/v/@protofire/subgraph-toolkit)](https://www.npmjs.com/package/@protofire/subgraph-toolkit)\n[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)\n[![Code Style: prettier](https://img.shields.io/badge/Code_Style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)\n\n---\n\n**Content**\n\n* [Features](#features-)\n* [Install](#install-)\n* [Usage](#usage-)\n* [Documentation](#documentation-)\n* [Examples](#example-)\n* [Contributing](#contributing-)\n\n\n## Features ✨\n\nThis library complements and extends the official library `@graphprotocol/graph-ts` with the following functionality: \n- [x] Helper functions to convert between different types not available in the standard library\n    - [x] Convert Bytes to Address\n    - [x] Convert Bytes to signed and unsigned integer\n    - [x] Convert BigDecimal to BigInt with a given precision\n    - [x] Convert BigInt to Bytes\n    - [x] Convert BigInt to BigDecimal with a given precision\n- [x] Useful constants ready to use in the subgraph mappings (including numeric constants, addresses, hashes and more)\n- [x] Calculate maximum and minimum of BigInt and BigDecimal values\n- [x] Hex Strings helpers\n- [x] Counters\n- [x] Accumulators\n- [ ] Helper functions to handle aggregated data [WIP]\n- [ ] Utilities to manipulate tokens (ERC20/ERC721/ERC1115)\n\n\n## Install 🐙\n\n```bash\n  $ yarn add @protofire/subgraph-toolkit --exact\n```\n\n\u003e\n\u003e**NOTE**\n\u003e\n\u003eIt's recommended installing a specific version of this library since the API in early versions is subject to change without previous notice.\n\u003e\n\n\n## Usage 💡\n\n\n```typescript\nimport { integer, decimal, DEFAULT_DECIMALS, ZERO_ADDRESS } from '@protofire/subgraph-toolkit'\n\n// ...\n\nexport function handleTransfer(event: Transfer): void {\n  // ...\n  \n  if (event.params._to.toHexString == ZERO_ADDRESS) {\n    let amount = decimal.max(\n      decimal.ZERO,\n      decimal.fromBigInt(event.params._value, DEFAULT_DECIMALS)\n    )\n\n    totalBurned += amount\n\n    burnCount = integer.increment(burnCount)\n  }\n  \n  // ...\n}\n\n// ...\n\n```\n\n### Counters and accumulators\n\nTo use metrics module you need to append the content of [schema.graphql](./schema.graphql) to the subgraph schema.\n\n\n### Prettier configuration\n\nThis package also provides an opinionated Prettier configuration file. To apply this configuration:\n\n```javascript\n// prettier.config.js or .prettierrc.js\n\nmodule.exports = require('@protofire/subgraph-toolkit/prettier.config.js')\n```\n\nTo override and/or extend the configuration:\n\n```javascript\n// prettier.config.js or .prettierrc.js\n\nmodule.exports = {\n  ...require('@protofire/subgraph-toolkit/prettier.config.js'),\n\n  printWidth: 120,\n\n  overrides: [\n    {\n      files: '*.json',\n      options: {\n        printWidth: 80,\n      },\n    },\n  ],\n}\n```\n\n\n## Documentation 📄\n\n### Constants\n\u003cdl\u003e\n\u003cdt\u003eADDRESS_ZERO\u003c/dt\u003e\n\u003cdd\u003eThe address zero, which is 20 bytes (40 nibbles) of zero. Aliases: GENESIS_ADDRESS, ZERO_ADDRESS\u003c/dd\u003e\n\n\u003cdt\u003eHASH_ZERO\u003c/dt\u003e\n\u003cdd\u003eThe hash zero, which is 32 bytes (64 nibbles) of zero. Alias: ZERO_HASH\u003c/dd\u003e\n\n\u003cdt\u003eMAX_UINT\u003c/dt\u003e\n\u003cdd\u003eThe hex string representing the maximum `uint256` value. Alias: MAX_UINT_256\u003c/dd\u003e\n\u003c/dl\u003e\n\n#### Integers\n\u003cdl\u003e\n\u003cdt\u003einteger. NEGATIVE_ONE\u003c/dt\u003e\n\u003cdd\u003eThe BigInt representing -1\u003c/dd\u003e\n\n\u003cdt\u003einteger. ZERO\u003c/dt\u003e\n\u003cdd\u003eThe BigInt representing 0\u003c/dd\u003e\n\n\u003cdt\u003einteger. ONE\u003c/dt\u003e\n\u003cdd\u003eThe BigInt representing 1\u003c/dd\u003e\n\n\u003cdt\u003einteger. TWO\u003c/dt\u003e\n\u003cdd\u003eThe BigInt representing 2\u003c/dd\u003e\n\n\u003cdt\u003einteger. ONE_THOUSAND\u003c/dt\u003e\n\u003cdd\u003eThe BigInt representing 1000\u003c/dd\u003e\n\n\u003cdt\u003einteger. WEI_PER_ETHER\u003c/dt\u003e\n\u003cdd\u003eThe BigInt representing 10\u003csup\u003e18\u003c/sup\u003e\u003c/dd\u003e\n\u003c/dl\u003e\n\n#### Decimals\n\u003cdl\u003e\n\u003cdt\u003edecimal. NEGATIVE_ONE\u003c/dt\u003e\n\u003cdd\u003eThe BigDecimal representing -1\u003c/dd\u003e\n\n\u003cdt\u003edecimal. ZERO\u003c/dt\u003e\n\u003cdd\u003eThe BigDecimal representing 0\u003c/dd\u003e\n\n\u003cdt\u003edecimal. ONE\u003c/dt\u003e\n\u003cdd\u003eThe BigDecimal representing 1\u003c/dd\u003e\n\n\u003cdt\u003edecimal. TWO\u003c/dt\u003e\n\u003cdd\u003eThe BigDecimal representing 2\u003c/dd\u003e\n\u003c/dl\u003e\n\n#### Units\n\n##### MakerDAO\n\u003cdl\u003e\n\u003cdt\u003eunits. WAD\u003c/dt\u003e\n\u003cdd\u003eThe BigDecimal representing 10\u003csup\u003e18\u003c/sup\u003e\u003c/dd\u003e\n\n\u003cdt\u003eunits. RAY\u003c/dt\u003e\n\u003cdd\u003eThe BigDecimal representing 10\u003csup\u003e27\u003c/sup\u003e\u003c/dd\u003e\n\n\u003cdt\u003eunits. RAD\u003c/dt\u003e\n\u003cdd\u003eThe BigDecimal representing 10\u003csup\u003e45\u003c/sup\u003e\u003c/dd\u003e\n\u003c/dl\u003e\n\n### Helper functions\n\n#### Addresses\n\u003cdl\u003e\n\u003cdt\u003eaddress. isZeroAddress(address: Address) → Boolean\u003c/dt\u003e\n\u003cdd\u003eChecks if a given address is the zero address\u003c/dd\u003e\n\u003c/dl\u003e\n\n#### Byte Manipulation\n\u003cdl\u003e\n\u003cdt\u003ebytes. toAddress(address: Bytes) → Address\u003c/dt\u003e\n\u003cdd\u003eChecks if a given address is the zero address (0x0)\u003c/dd\u003e\n\n\u003cdt\u003ebytes. toSignedInt(value: Bytes, bigEndian: Boolean = true) → BigInt\u003c/dt\u003e\n\u003cdd\u003eConverts Bytes to a signed BigInt using a given endianness\u003c/dd\u003e\n\n\u003cdt\u003ebytes. toUnsignedInt(value: Bytes, bigEndian: Boolean = true) → BigInt\u003c/dt\u003e\n\u003cdd\u003eConverts Bytes to a unsigned BigInt using a given endianness\u003c/dd\u003e\n\n\u003cdt\u003ebytes. hexZeroPad(value: Bytes, length: i32 → String\u003c/dt\u003e\n\u003cdd\u003eReturns a hex string padded with zeros (on the left) to length bytes (each byte is two nibbles)\u003c/dd\u003e\n\u003c/dl\u003e\n\n\n#### Integer\n\u003cdl\u003e\n\u003cdt\u003einteger. fromBigInt(value: BigInt, decimals: i32 = 18) → BigDecimal\u003c/dt\u003e\n\u003cdd\u003eConverts a BigInt to a BigDecimal including a given number of decimals (by default, 18 decimals)\u003c/dd\u003e\n\n##### Factory methods\n\u003cdt\u003einteger. fromNumber(value: i32) → BigInt\u003c/dt\u003e\n\u003cdd\u003eConverts a integer number to a BigInt\u003c/dd\u003e\n\n\u003cdt\u003einteger. fromString(value: String) → BigInt\u003c/dt\u003e\n\u003cdd\u003eParses a string as a BigInt\u003c/dd\u003e\n\u003c/dl\u003e\n\n##### Converters\n\u003cdl\u003e\n\u003cdt\u003einteger. toBytes(value: BigInt) → Bytes\u003c/dt\u003e\n\u003cdd\u003eConverts a BigInt to a raw Bytes\u003c/dd\u003e\n\u003c/dl\u003e\n\n##### Other helpers\n\u003cdl\u003e\n\u003cdt\u003einteger. decrement(value: BigInt, amount: BigInt = 1) → BigInt\u003c/dt\u003e\n\u003cdd\u003eDecrements a BigInt by a given amount and returns the new value (by default, decrements by 1)\u003c/dd\u003e\n\n\u003cdt\u003einteger. increment(value: BigInt, amount: BigInt = 1) → BigInt\u003c/dt\u003e\n\u003cdd\u003eIncrements a BigInt by a given amount and returns the new value (by default, increments by 1)\u003c/dd\u003e\n\n\u003cdt\u003einteger. min(a: BigInt, b: BigInt) → BigInt\u003c/dt\u003e\n\u003cdd\u003eReturns the smallest of the given numbers\u003c/dd\u003e\n\n\u003cdt\u003einteger. max(a: BigInt, b: BigInt) → BigInt\u003c/dt\u003e\n\u003cdd\u003eReturns the largest of the given numbers\u003c/dd\u003e\n\u003c/dl\u003e\n\n\n#### Decimal\n\u003cdl\u003e\n\u003cdt\u003edecimal. fromBigInt(value: BigInt, decimals: i32 = 18) → BigDecimal\u003c/dt\u003e\n\u003cdd\u003eConverts a BigInt to a BigDecimal including a given number of decimals (by default, 18 decimals)\u003c/dd\u003e\n\n##### Factory methods\n\u003cdt\u003edecimal. fromNumber(value: f64) → BigDecimal\u003c/dt\u003e\n\u003cdd\u003eConverts a float number to a BigDecimal\u003c/dd\u003e\n\n\u003cdt\u003edecimal. fromString(value: String) → BigDecimal\u003c/dt\u003e\n\u003cdd\u003eParses a string as a BigDecimal\u003c/dd\u003e\n\u003c/dl\u003e\n\n##### Converters\n\u003cdl\u003e\n\u003cdt\u003edecimal. toBigInt(value: BigDecimal, decimals: u8 = 18) → BigInt\u003c/dt\u003e\n\u003cdd\u003eConverts a BigDecimal to a BigInt using a given number of decimals (by default, 18 decimals)\u003c/dd\u003e\n\u003c/dl\u003e\n\n##### Other helpers\n\u003cdl\u003e\n\u003cdt\u003edecimal. getPrecision(decimals: u8 = 18) → BigInt\u003c/dt\u003e\n\u003cdd\u003eReturns a BigInt representing a unit of a fixed point decimal with a given number of decimals (by default, 18 decimals)\u003c/dd\u003e\n\n\u003cdt\u003edecimal. min(a: BigDecimal, b: BigDecimal) → BigDecimal\u003c/dt\u003e\n\u003cdd\u003eReturns the smallest of the given numbers\u003c/dd\u003e\n\n\u003cdt\u003edecimal. max(a: BigDecimal, b: BigDecimal) → BigDecimal\u003c/dt\u003e\n\u003cdd\u003eReturns the largest of the given numbers\u003c/dd\u003e\n\u003c/dl\u003e\n\n\n#### Units\n\n##### MakerDAO\n\u003cdl\u003e\n\u003cdt\u003eunits. fromRad(value: BigInt) → BigDecimal\u003c/dt\u003e\n\u003cdd\u003eReads a BigInt as a fixed point decimal with 18 decimals (used for basic quantities, e.g. balances)\u003c/dd\u003e\n\n\u003cdt\u003eunits. fromRay(value: BigInt) → BigDecimal\u003c/dt\u003e\n\u003cdd\u003eReads a BigInt as a fixed point decimal with 27 decimals (for precise quantites, e.g. ratios)\u003c/dd\u003e\n\n\u003cdt\u003eunits. fromWad(value: BigInt) → BigDecimal\u003c/dt\u003e\n\u003cdd\u003eReads a BigInt as a fixed point decimal with 45 decimals (result of integer multiplication with a `wad` and a `ray)\u003c/dd\u003e\n\n\u003cdt\u003eunits. toRad(value: BigDecimal) → BigInt\u003c/dt\u003e\n\u003cdd\u003eConverts a BigDecimal to a BigInt representing a fixed point decimal with 18 decimals (used for basic quantities, e.g. balances)\u003c/dd\u003e\n\n\u003cdt\u003eunits. toRay(value: BigDecimal) → BigInt\u003c/dt\u003e\n\u003cdd\u003eConverts a BigDecimal to a BigInt representing a fixed point decimal with 27 decimals (for precise quantites, e.g. ratios)\u003c/dd\u003e\n\n\u003cdt\u003eunits. toWad(value: BigDecimal) → BigInt\u003c/dt\u003e\n\u003cdd\u003eConverts a BigDecimal to a BigInt representing a fixed point decimal with 45 decimals (result of integer multiplication with a \u003ccode\u003eWAD\u003c/code\u003e and a \u003ccode\u003eRAY\u003c/code\u003e)\u003c/dd\u003e\n\u003c/dl\u003e\n\n\n## Examples 🖍\n\nThe following subgraphs are using this library:\n* [Maker Protocol](https://thegraph.com/explorer/subgraph/protofire/maker-protocol)\n* [Curve](https://thegraph.com/explorer/subgraph/protofire/curve)\n* [Nexus Mutual](https://thegraph.com/explorer/subgraph/protofire/nexus-mutual)\n* [SuperRare](https://thegraph.com/explorer/subgraph/protofire/superrare)\n\n\n## Contributing 🍰\n\nPlease make sure to read the [Contributing Guide]() before making a pull request.\n\nThank you to all the people who already contributed to this project!\n\n\n## License ⚖️\n\nCopyright © 2020 Protofire.io and contributors\n\nThis program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3.\n\nThis program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.\n\nYou should have received a copy of the GNU General Public License along with this program. If not, see \u003chttps://www.gnu.org/licenses\u003e.\n","funding_links":[],"categories":["SubGraph","Uncategorized"],"sub_categories":["Tools","Uncategorized"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprotofire%2Fsubgraph-toolkit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprotofire%2Fsubgraph-toolkit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprotofire%2Fsubgraph-toolkit/lists"}