21 minutes
AssemblyScript API
Note: If you created a Subgraph prior to graph-cli/graph-ts version 0.22.0, then you’re using an older version of AssemblyScript. It is recommended to review the Migration Guide.
Learn what built-in APIs can be used when writing Subgraph mappings. There are two kinds of APIs available out of the box:
- The Graph TypeScript library (graph-ts)
- Code generated from Subgraph files by graph codegen
You can also add other libraries as dependencies, as long as they are compatible with AssemblyScript.
Since language mappings are written in AssemblyScript, it is useful to review the language and standard library features from the AssemblyScript wiki.
API Reference
The @graphprotocol/graph-ts library provides the following APIs:
- An ethereumAPI for working with Ethereum smart contracts, events, blocks, transactions, and Ethereum values.
- A storeAPI to load and save entities from and to the Graph Node store.
- A logAPI to log messages to the Graph Node output and Graph Explorer.
- An ipfsAPI to load files from IPFS.
- A jsonAPI to parse JSON data.
- A cryptoAPI to use cryptographic functions.
- Low-level primitives to translate between different type systems such as Ethereum, JSON, GraphQL and AssemblyScript.
Versions
The apiVersion in the Subgraph manifest specifies the mapping API version which is run by Graph Node for a given Subgraph.
| Version | Release notes | 
|---|---|
| 0.0.9 | Adds new host functions eth_get_balance&hasCode | 
| 0.0.8 | Adds validation for existence of fields in the schema when saving an entity. | 
| 0.0.7 | Added TransactionReceiptandLogclasses to the Ethereum typesAdded receiptfield to the Ethereum Event object | 
| 0.0.6 | Added noncefield to the Ethereum Transaction objectAdded baseFeePerGasto the Ethereum Block object | 
| 0.0.5 | AssemblyScript upgraded to version 0.19.10 (this includes breaking changes, please see the Migration Guide)ethereum.transaction.gasUsedrenamed toethereum.transaction.gasLimit | 
| 0.0.4 | Added functionSignaturefield to the Ethereum SmartContractCall object | 
| 0.0.3 | Added fromfield to the Ethereum Call objectethereum.call.addressrenamed toethereum.call.to | 
| 0.0.2 | Added inputfield to the Ethereum Transaction object | 
Built-in Types
Documentation on the base types built into AssemblyScript can be found in the AssemblyScript wiki.
The following additional types are provided by @graphprotocol/graph-ts.
ByteArray
1import { ByteArray } from '@graphprotocol/graph-ts'ByteArray represents an array of u8.
Construction
- fromI32(x: i32): ByteArray- Decomposes- xinto bytes.
- fromHexString(hex: string): ByteArray- Input length must be even. Prefixing with- 0xis optional.
Type conversions
- toHexString(): string- Converts to a hex string prefixed with- 0x.
- toString(): string- Interprets the bytes as a UTF-8 string.
- toBase58(): string- Encodes the bytes into a base58 string.
- toU32(): u32- Interprets the bytes as a little-endian- u32. Throws in case of overflow.
- toI32(): i32- Interprets the byte array as a little-endian- i32. Throws in case of overflow.
Operators
- equals(y: ByteArray): bool– can be written as- x == y.
- concat(other: ByteArray) : ByteArray- return a new- ByteArrayconsisting of- thisdirectly followed by- other
- concatI32(other: i32) : ByteArray- return a new- ByteArrayconsisting of- thisdirectly followed by the byte representation of- other
BigDecimal
1import { BigDecimal } from '@graphprotocol/graph-ts'BigDecimal is used to represent arbitrary precision decimals.
Note: Internally BigDecimal is stored in IEEE-754 decimal128 floating-point format, which supports 34 decimal digits of significand. This makes BigDecimal unsuitable for representing fixed-point types that can span wider than 34 digits, such as a Solidity ufixed256x18 or equivalent.
Construction
- constructor(bigInt: BigInt)– creates a- BigDecimalfrom an- BigInt.
- static fromString(s: string): BigDecimal– parses from a decimal string.
Type conversions
- toString(): string– prints to a decimal string.
Math
- plus(y: BigDecimal): BigDecimal– can be written as- x + y.
- minus(y: BigDecimal): BigDecimal– can be written as- x - y.
- times(y: BigDecimal): BigDecimal– can be written as- x * y.
- div(y: BigDecimal): BigDecimal– can be written as- x / y.
- equals(y: BigDecimal): bool– can be written as- x == y.
- notEqual(y: BigDecimal): bool– can be written as- x != y.
- lt(y: BigDecimal): bool– can be written as- x < y.
- le(y: BigDecimal): bool– can be written as- x <= y.
- gt(y: BigDecimal): bool– can be written as- x > y.
- ge(y: BigDecimal): bool– can be written as- x >= y.
- neg(): BigDecimal- can be written as- -x.
BigInt
1import { BigInt } from '@graphprotocol/graph-ts'BigInt is used to represent big integers. This includes Ethereum values of type uint32 to uint256 and int64 to int256. Everything below uint32, such as int32, uint24 or int8 is represented as i32.
The BigInt class has the following API:
Construction
- 
BigInt.fromI32(x: i32): BigInt– creates aBigIntfrom ani32.
- 
BigInt.fromString(s: string): BigInt– Parses aBigIntfrom a string.
- 
BigInt.fromUnsignedBytes(x: Bytes): BigInt– Interpretsbytesas an unsigned, little-endian integer. If your input is big-endian, call.reverse()first.
- 
BigInt.fromSignedBytes(x: Bytes): BigInt– Interpretsbytesas a signed, little-endian integer. If your input is big-endian, call.reverse()first.Type conversions 
- 
x.toHex(): string– turnsBigIntinto a string of hexadecimal characters.
- 
x.toString(): string– turnsBigIntinto a decimal number string.
- 
x.toI32(): i32– returns theBigIntas ani32; fails if the value does not fit intoi32. It’s a good idea to first checkx.isI32().
- 
x.toBigDecimal(): BigDecimal- converts into a decimal with no fractional part.
Math
- x.plus(y: BigInt): BigInt– can be written as- x + y.
- x.minus(y: BigInt): BigInt– can be written as- x - y.
- x.times(y: BigInt): BigInt– can be written as- x * y.
- x.div(y: BigInt): BigInt– can be written as- x / y.
- x.mod(y: BigInt): BigInt– can be written as- x % y.
- x.equals(y: BigInt): bool– can be written as- x == y.
- x.notEqual(y: BigInt): bool– can be written as- x != y.
- x.lt(y: BigInt): bool– can be written as- x < y.
- x.le(y: BigInt): bool– can be written as- x <= y.
- x.gt(y: BigInt): bool– can be written as- x > y.
- x.ge(y: BigInt): bool– can be written as- x >= y.
- x.neg(): BigInt– can be written as- -x.
- x.divDecimal(y: BigDecimal): BigDecimal– divides by a decimal, giving a decimal result.
- x.isZero(): bool– Convenience for checking if the number is zero.
- x.isI32(): bool– Check if the number fits in an- i32.
- x.abs(): BigInt– Absolute value.
- x.pow(exp: u8): BigInt– Exponentiation.
- bitOr(x: BigInt, y: BigInt): BigInt– can be written as- x | y.
- bitAnd(x: BigInt, y: BigInt): BigInt– can be written as- x & y.
- leftShift(x: BigInt, bits: u8): BigInt– can be written as- x << y.
- rightShift(x: BigInt, bits: u8): BigInt– can be written as- x >> y.
TypedMap
1import { TypedMap } from '@graphprotocol/graph-ts'TypedMap can be used to store key-value pairs. See this example.
The TypedMap class has the following API:
- new TypedMap<K, V>()– creates an empty map with keys of type- Kand values of type- V
- map.set(key: K, value: V): void– sets the value of- keyto- value
- map.getEntry(key: K): TypedMapEntry<K, V> | null– returns the key-value pair for a- keyor- nullif the- keydoes not exist in the map
- map.get(key: K): V | null– returns the value for a- keyor- nullif the- keydoes not exist in the map
- map.isSet(key: K): bool– returns- trueif the- keyexists in the map and- falseif it does not
Bytes
1import { Bytes } from '@graphprotocol/graph-ts'Bytes is used to represent arbitrary-length arrays of bytes. This includes Ethereum values of type bytes, bytes32, etc.
The Bytes class extends AssemblyScript’s Uint8Array and this supports all the Uint8Array functionality, plus the following new methods:
Construction
- fromHexString(hex: string) : Bytes- Convert the string- hexwhich must consist of an even number of hexadecimal digits to a- ByteArray. The string- hexcan optionally start with- 0x
- fromI32(i: i32) : Bytes- Convert- ito an array of bytes
Type conversions
- b.toHex()– returns a hexadecimal string representing the bytes in the array
- b.toString()– converts the bytes in the array to a string of unicode characters
- b.toBase58()– turns an Ethereum Bytes value to base58 encoding (used for IPFS hashes)
Operators
- b.concat(other: Bytes) : Bytes- - return new- Bytesconsisting of- thisdirectly followed by- other
- b.concatI32(other: i32) : ByteArray- return new- Bytesconsisting of- thisdirectly follow by the byte representation of- other
Address
1import { Address } from '@graphprotocol/graph-ts'Address extends Bytes to represent Ethereum address values.
It adds the following method on top of the Bytes API:
- Address.fromString(s: string): Address– creates an- Addressfrom a hexadecimal string
- Address.fromBytes(b: Bytes): Address– create an- Addressfrom- bwhich must be exactly 20 bytes long. Passing in a value with fewer or more bytes will result in an error
Store API
1import { store } from '@graphprotocol/graph-ts'The store API allows to load, save and remove entities from and to the Graph Node store.
Entities written to the store map one-to-one to the @entity types defined in the Subgraph’s GraphQL schema. To make working with these entities convenient, the graph codegen command provided by the Graph CLI generates entity classes, which are subclasses of the built-in Entity type, with property getters and setters for the fields in the schema as well as methods to load and save these entities.
Creating entities
The following is a common pattern for creating entities from Ethereum events.
1// Import the Transfer event class generated from the ERC20 ABI2import { Transfer as TransferEvent } from '../generated/ERC20/ERC20'34// Import the Transfer entity type generated from the GraphQL schema5import { Transfer } from '../generated/schema'67// Transfer event handler8export function handleTransfer(event: TransferEvent): void {9  // Create a Transfer entity, using the transaction hash as the entity ID10  let id = event.transaction.hash11  let transfer = new Transfer(id)1213  // Set properties on the entity, using the event parameters14  transfer.from = event.params.from15  transfer.to = event.params.to16  transfer.amount = event.params.amount1718  // Save the entity to the store19  transfer.save()20}When a Transfer event is encountered while processing the chain, it is passed to the handleTransfer event handler using the generated Transfer type (aliased to TransferEvent here to avoid a naming conflict with the entity type). This type allows accessing data such as the event’s parent transaction and its parameters.
Each entity must have a unique ID to avoid collisions with other entities. It is fairly common for event parameters to include a unique identifier that can be used.
Note: Using the transaction hash as the ID assumes that no other events in the same transaction create entities with this hash as the ID.
Loading entities from the store
If an entity already exists, it can be loaded from the store with the following:
1let id = event.transaction.hash // or however the ID is constructed2let transfer = Transfer.load(id)3if (transfer == null) {4  transfer = new Transfer(id)5}67// Use the Transfer entity as beforeAs the entity may not exist in the store yet, the load method returns a value of type Transfer | null. It may be necessary to check for the null case before using the value.
Note: Loading entities is only necessary if the changes made in the mapping depend on the previous data of an entity. See the next section for the two ways of updating existing entities.
Looking up entities created within a block
As of graph-node v0.31.0, @graphprotocol/graph-ts v0.30.0 and @graphprotocol/graph-cli v0.49.0 the loadInBlock method is available on all entity types.
The store API facilitates the retrieval of entities that were created or updated in the current block. A typical situation for this is that one handler creates a transaction from some onchain event, and a later handler wants to access this transaction if it exists.
- In the case where the transaction does not exist, the Subgraph will have to go to the database simply to find out that the entity does not exist. If the Subgraph author already knows that the entity must have been created in the same block, using loadInBlockavoids this database roundtrip.
- For some Subgraphs, these missed lookups can contribute significantly to the indexing time.
1let id = event.transaction.hash // or however the ID is constructed2let transfer = Transfer.loadInBlock(id)3if (transfer == null) {4  transfer = new Transfer(id)5}67// Use the Transfer entity as beforeNote: If there is no entity created in the given block, loadInBlock will return null even if there is an entity with the given ID in the store.
Looking up derived entities
As of graph-node v0.31.0, @graphprotocol/graph-ts v0.31.0 and @graphprotocol/graph-cli v0.51.0 the loadRelated method is available.
This enables loading derived entity fields from within an event handler. For example, given the following schema:
1type Token @entity {2  id: ID!3  holder: Holder!4  color: String5}67type Holder @entity {8  id: ID!9  tokens: [Token!]! @derivedFrom(field: "holder")10}The following code will load the Token entity that the Holder entity was derived from:
1let holder = Holder.load('test-id')2// Load the Token entities associated with a given holder3let tokens = holder.tokens.load()Updating existing entities
There are two ways to update an existing entity:
- Load the entity with e.g. Transfer.load(id), set properties on the entity, then.save()it back to the store.
- Simply create the entity with e.g. new Transfer(id), set properties on the entity, then.save()it to the store. If the entity already exists, the changes are merged into it.
Changing properties is straight forward in most cases, thanks to the generated property setters:
1let transfer = new Transfer(id)2transfer.from = ...3transfer.to = ...4transfer.amount = ...It is also possible to unset properties with one of the following two instructions:
1transfer.from.unset()2transfer.from = nullThis only works with optional properties, i.e. properties that are declared without a ! in GraphQL. Two examples would be owner: Bytes or amount: BigInt.
Updating array properties is a little more involved, as the getting an array from an entity creates a copy of that array. This means array properties have to be set again explicitly after changing the array. The following assumes entity has a numbers: [BigInt!]! field.
1// This won't work2entity.numbers.push(BigInt.fromI32(1))3entity.save()45// This will work6let numbers = entity.numbers7numbers.push(BigInt.fromI32(1))8entity.numbers = numbers9entity.save()Removing entities from the store
There is currently no way to remove an entity via the generated types. Instead, removing an entity requires passing the name of the entity type and the entity ID to store.remove:
1import { store } from '@graphprotocol/graph-ts'2...3let id = event.transaction.hash4store.remove('Transfer', id)Ethereum API
The Ethereum API provides access to smart contracts, public state variables, contract functions, events, transactions, blocks and the encoding/decoding Ethereum data.
Ethereum JSON‑RPC Schema Changes & Execution Client Compatibility
Following Ethereum’s transition to Proof of Stake, the Execution Layer JSON-RPC schema was updated to reflect changes in block metadata.
The totalDifficulty field was officially removed from the eth_getBlockByNumber response. Additionally, the difficulty field—previously used in Proof of Work—was retained as optional but is now considered obsolete and typically returned as "0x0" by spec-compliant clients. While some clients continue to include these fields for backward compatibility, developers relying on them should update their subgraphs and tooling to accommodate these schema changes. Full schema details are available in the Ethereum Execution APIs documentation. We’ve added a chart with the current client compatibility with these updates as of July 17, 2025. Please check for specific client updates as needed.
Ethereum JSON‑RPC Schema Changes Post-Merge
| Field | JSON‑RPC Schema Status (Post-Merge) | 
|---|---|
| totalDifficulty | ❌ Removed from eth_getBlockByNumberresponse | 
| difficulty | ⚠️ Optional, expected to be 0x0post-Merge, but many clients still return | 
| size | ⚠️ Optional; may be omitted depending on client | 
Execution Client Compatibility
| Client | totalDifficulty | difficulty | size | Notes | 
|---|---|---|---|---|
| Erigon v3 | Not returned | Non-zero | Usually present | Strict spec compliance, noted here | 
| Geth | Returned | Non-zero | Returned | Maintains legacy behavior, noted here | 
| Nethermind | Returned | Non-zero | Returned | Maintains legacy behavior, noted here | 
| Besu | Returns 0for an uncle block | Non-zero | Returned | Returns deprecated fields with valid values, noted here | 
| Reth | Not Returned | "0x0" | Returned | Highly spec-compliant, aligns with post-Merge schema | 
Support for Ethereum Types
As with entities, graph codegen generates classes for all smart contracts and events used in a Subgraph. For this, the contract ABIs need to be part of the data source in the Subgraph manifest. Typically, the ABI files are stored in an abis/ folder.
With the generated classes, conversions between Ethereum types and the built-in types take place behind the scenes so that Subgraph authors do not have to worry about them.
The following example illustrates this. Given a Subgraph schema like
1type Transfer @entity {2  id: Bytes!3  from: Bytes!4  to: Bytes!5  amount: BigInt!6}and a Transfer(address,address,uint256) event signature on Ethereum, the from, to and amount values of type address, address and uint256 are converted to Address and BigInt, allowing them to be passed on to the Bytes! and BigInt! properties of the Transfer entity:
1let id = event.transaction.hash2let transfer = new Transfer(id)3transfer.from = event.params.from4transfer.to = event.params.to5transfer.amount = event.params.amount6transfer.save()Events and Block/Transaction Data
Ethereum events passed to event handlers, such as the Transfer event in the previous examples, not only provide access to the event parameters but also to their parent transaction and the block they are part of. The following data can be obtained from event instances (these classes are a part of the ethereum module in graph-ts):
1class Event {2  address: Address3  logIndex: BigInt4  transactionLogIndex: BigInt5  logType: string | null6  block: Block7  transaction: Transaction8  parameters: Array<EventParam>9  receipt: TransactionReceipt | null10}1112class Block {13  hash: Bytes14  parentHash: Bytes15  unclesHash: Bytes16  author: Address17  stateRoot: Bytes18  transactionsRoot: Bytes19  receiptsRoot: Bytes20  number: BigInt21  gasUsed: BigInt22  gasLimit: BigInt23  timestamp: BigInt24  difficulty: BigInt25  totalDifficulty: BigInt26  size: BigInt | null27  baseFeePerGas: BigInt | null28}2930class Transaction {31  hash: Bytes32  index: BigInt33  from: Address34  to: Address | null35  value: BigInt36  gasLimit: BigInt37  gasPrice: BigInt38  input: Bytes39  nonce: BigInt40}4142class TransactionReceipt {43  transactionHash: Bytes44  transactionIndex: BigInt45  blockHash: Bytes46  blockNumber: BigInt47  cumulativeGasUsed: BigInt48  gasUsed: BigInt49  contractAddress: Address50  logs: Array<Log>51  status: BigInt52  root: Bytes53  logsBloom: Bytes54}5556class Log {57  address: Address58  topics: Array<Bytes>59  data: Bytes60  blockHash: Bytes61  blockNumber: Bytes62  transactionHash: Bytes63  transactionIndex: BigInt64  logIndex: BigInt65  transactionLogIndex: BigInt66  logType: string67  removed: bool | null68}Access to Smart Contract State
The code generated by graph codegen also includes classes for the smart contracts used in the Subgraph. These can be used to access public state variables and call functions of the contract at the current block.
A common pattern is to access the contract from which an event originates. This is achieved with the following code:
1// Import the generated contract class and generated Transfer event class2import { ERC20Contract, Transfer as TransferEvent } from '../generated/ERC20Contract/ERC20Contract'3// Import the generated entity class4import { Transfer } from '../generated/schema'56export function handleTransfer(event: TransferEvent) {7  // Bind the contract to the address that emitted the event8  let contract = ERC20Contract.bind(event.address)910  // Access state variables and functions by calling them11  let erc20Symbol = contract.symbol()12}Transfer is aliased to TransferEvent here to avoid a naming conflict with the entity type.
As long as the ERC20Contract on Ethereum has a public read-only function called symbol, it can be called with .symbol(). For public state variables a method with the same name is created automatically.
Any other contract that is part of the subgraph can be imported from the generated code and can be bound to a valid address. An example of that is presented within the steps below:
- Extend subgraph.yamlfile with desired ABI file:
1mapping:2  kind: ethereum/events3  apiVersion: 0.0.64  language: wasm/assemblyscript5  entities:6    - Gravatar7  abis:8    - name: Gravity9      file: ./abis/Gravity.json10    - name: OtherContract11      file: ./abis/OtherContract.json- Import and bind to the contract using its address
Handling Reverted Calls
If the read-only methods of your contract may revert, then you should handle that by calling the generated contract method prefixed with try_.
- For example, the Gravity contract exposes the gravatarToOwnermethod. This code would be able to handle a revert in that method:
1let gravity = Gravity.bind(event.address)2let callResult = gravity.try_gravatarToOwner(gravatar)3if (callResult.reverted) {4  log.info('getGravatar reverted', [])5} else {6  let owner = callResult.value7}Note: A Graph node connected to a Geth or Infura client may not detect all reverts. If you rely on this, we recommend using a Graph Node connected to a Parity client.
Encoding/Decoding ABI
Data can be encoded and decoded according to Ethereum’s ABI encoding format using the encode and decode functions in the ethereum module.
1import { Address, BigInt, ethereum } from '@graphprotocol/graph-ts'23let tupleArray: Array<ethereum.Value> = [4  ethereum.Value.fromAddress(Address.fromString('0x0000000000000000000000000000000000000420')),5  ethereum.Value.fromUnsignedBigInt(BigInt.fromI32(62)),6]78let tuple = tupleArray as ethereum.Tuple910let encoded = ethereum.encode(ethereum.Value.fromTuple(tuple))!1112let decoded = ethereum.decode('(address,uint256)', encoded)For more information:
- ABI Spec
- Encoding/decoding Rust library/CLI
- More complex example.
Balance of an Address
The native token balance of an address can be retrieved using the ethereum module. This feature is available from apiVersion: 0.0.9 which is defined subgraph.yaml. The getBalance() retrieves the balance of the specified address as of the end of the block in which the event is triggered.
1import { ethereum } from '@graphprotocol/graph-ts'23let address = Address.fromString('0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045')4let balance = ethereum.getBalance(address) // returns balance in BigIntCheck if an Address is a Contract or EOA
To check whether an address is a smart contract address or an externally owned address (EOA), use the hasCode() function from the ethereum module which will return boolean. This feature is available from apiVersion: 0.0.9 which is defined subgraph.yaml.
1import { ethereum } from '@graphprotocol/graph-ts'23let contractAddr = Address.fromString('0x2E645469f354BB4F5c8a05B3b30A929361cf77eC')4let isContract = ethereum.hasCode(contractAddr).inner // returns true56let eoa = Address.fromString('0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045')7let isContract = ethereum.hasCode(eoa).inner // returns falseLogging API
1import { log } from '@graphprotocol/graph-ts'The log API allows Subgraphs to log information to the Graph Node standard output as well as Graph Explorer. Messages can be logged using different log levels. A basic format string syntax is provided to compose log messages from argument.
The log API includes the following functions:
- log.debug(fmt: string, args: Array<string>): void- logs a debug message.
- log.info(fmt: string, args: Array<string>): void- logs an informational message.
- log.warning(fmt: string, args: Array<string>): void- logs a warning.
- log.error(fmt: string, args: Array<string>): void- logs an error message.
- log.critical(fmt: string, args: Array<string>): void– logs a critical message and terminates the Subgraph.
The log API takes a format string and an array of string values. It then replaces placeholders with the string values from the array. The first {} placeholder gets replaced by the first value in the array, the second {} placeholder gets replaced by the second value and so on.
1log.info('Message to be displayed: {}, {}, {}', [value.toString(), anotherValue.toString(), 'already a string'])Logging one or more values
Logging a single value
In the example below, the string value “A” is passed into an array to become['A'] before being logged:
1let myValue = 'A'23export function handleSomeEvent(event: SomeEvent): void {4  // Displays : "My value is: A"5  log.info('My value is: {}', [myValue])6}Logging a single entry from an existing array
In the example below, only the first value of the argument array is logged, despite the array containing three values.
1let myArray = ['A', 'B', 'C']23export function handleSomeEvent(event: SomeEvent): void {4  // Displays : "My value is: A"  (Even though three values are passed to `log.info`)5  log.info('My value is: {}', myArray)6}Logging multiple entries from an existing array
Each entry in the arguments array requires its own placeholder {} in the log message string. The below example contains three placeholders {} in the log message. Because of this, all three values in myArray are logged.
1let myArray = ['A', 'B', 'C']23export function handleSomeEvent(event: SomeEvent): void {4  // Displays : "My first value is: A, second value is: B, third value is: C"5  log.info('My first value is: {}, second value is: {}, third value is: {}', myArray)6}Logging a specific entry from an existing array
To display a specific value in the array, the indexed value must be provided.
1export function handleSomeEvent(event: SomeEvent): void {2  // Displays : "My third value is C"3  log.info('My third value is: {}', [myArray[2]])4}Logging event information
The example below logs the block number, block hash and transaction hash from an event:
1import { log } from '@graphprotocol/graph-ts'23export function handleSomeEvent(event: SomeEvent): void {4  log.debug('Block number: {}, block hash: {}, transaction hash: {}', [5    event.block.number.toString(), // "47596000"6    event.block.hash.toHexString(), // "0x..."7    event.transaction.hash.toHexString(), // "0x..."8  ])9}IPFS API
1import { ipfs } from '@graphprotocol/graph-ts'Smart contracts occasionally anchor IPFS files onchain. This allows mappings to obtain the IPFS hashes from the contract and read the corresponding files from IPFS. The file data will be returned as Bytes, which usually requires further processing, e.g. with the json API documented later on this page.
Given an IPFS hash or path, reading a file from IPFS is done as follows:
1// Put this inside an event handler in the mapping2let hash = 'QmTkzDwWqPbnAh5YiV5VwcTLnGdwSNsNTn2aDxdXBFca7D'3let data = ipfs.cat(hash)45// Paths like `QmTkzDwWqPbnAh5YiV5VwcTLnGdwSNsNTn2aDxdXBFca7D/Makefile`6// that include files in directories are also supported7let path = 'QmTkzDwWqPbnAh5YiV5VwcTLnGdwSNsNTn2aDxdXBFca7D/Makefile'8let data = ipfs.cat(path)Note: ipfs.cat is not deterministic at the moment. If the file cannot be retrieved over the IPFS network before the request times out, it will return null. Due to this, it’s always worth checking the result for null.
It is also possible to process larger files in a streaming fashion with ipfs.map. The function expects the hash or path for an IPFS file, the name of a callback, and flags to modify its behavior:
1import { JSONValue, Value } from '@graphprotocol/graph-ts'23export function processItem(value: JSONValue, userData: Value): void {4  // See the JSONValue documentation for details on dealing5  // with JSON values6  let obj = value.toObject()7  let id = obj.get('id')8  let title = obj.get('title')910  if (!id || !title) {11    return12  }1314  // Callbacks can also created entities15  let newItem = new Item(id)16  newItem.title = title.toString()17  newitem.parent = userData.toString() // Set parent to "parentId"18  newitem.save()19}2021// Put this inside an event handler in the mapping22ipfs.map('Qm...', 'processItem', Value.fromString('parentId'), ['json'])2324// Alternatively, use `ipfs.mapJSON`25ipfs.mapJSON('Qm...', 'processItem', Value.fromString('parentId'))The only flag currently supported is json, which must be passed to ipfs.map. With the json flag, the IPFS file must consist of a series of JSON values, one value per line. The call to ipfs.map will read each line in the file, deserialize it into a JSONValue and call the callback for each of them. The callback can then use entity operations to store data from the JSONValue. Entity changes are stored only when the handler that called ipfs.map finishes successfully; in the meantime, they are kept in memory, and the size of the file that ipfs.map can process is therefore limited.
On success, ipfs.map returns void. If any invocation of the callback causes an error, the handler that invoked ipfs.map is aborted, and the Subgraph is marked as failed.
Crypto API
1import { crypto } from '@graphprotocol/graph-ts'The crypto API makes a cryptographic functions available for use in mappings. Right now, there is only one:
- crypto.keccak256(input: ByteArray): ByteArray
JSON API
1import { json, JSONValueKind } from '@graphprotocol/graph-ts'JSON data can be parsed using the json API:
- json.fromBytes(data: Bytes): JSONValue– parses JSON data from a- Bytesarray interpreted as a valid UTF-8 sequence
- json.try_fromBytes(data: Bytes): Result<JSONValue, boolean>– safe version of- json.fromBytes, it returns an error variant if the parsing failed
- json.fromString(data: string): JSONValue– parses JSON data from a valid UTF-8- String
- json.try_fromString(data: string): Result<JSONValue, boolean>– safe version of- json.fromString, it returns an error variant if the parsing failed
The JSONValue class provides a way to pull values out of an arbitrary JSON document. Since JSON values can be booleans, numbers, arrays and more, JSONValue comes with a kind property to check the type of a value:
1let value = json.fromBytes(...)2if (value.kind == JSONValueKind.BOOL) {3  ...4}In addition, there is a method to check if the value is null:
- value.isNull(): boolean
When the type of a value is certain, it can be converted to a built-in type using one of the following methods:
- value.toBool(): boolean
- value.toI64(): i64
- value.toF64(): f64
- value.toBigInt(): BigInt
- value.toString(): string
- value.toArray(): Array<JSONValue>- (and then convert- JSONValuewith one of the 5 methods above)
Type Conversions Reference
| Source(s) | Destination | Conversion function | 
|---|---|---|
| Address | Bytes | none | 
| Address | String | s.toHexString() | 
| BigDecimal | String | s.toString() | 
| BigInt | BigDecimal | s.toBigDecimal() | 
| BigInt | String (hexadecimal) | s.toHexString() or s.toHex() | 
| BigInt | String (unicode) | s.toString() | 
| BigInt | Bytes | s.fromBigInt() | 
| BigInt | i32 | s.toI32() | 
| Boolean | Boolean | none | 
| Bytes (signed) | BigInt | BigInt.fromSignedBytes(s) | 
| Bytes (unsigned) | BigInt | BigInt.fromUnsignedBytes(s) | 
| Bytes | String (hexadecimal) | s.toHexString() or s.toHex() | 
| Bytes | String (unicode) | s.toString() | 
| Bytes | String (base58) | s.toBase58() | 
| Bytes | i32 | s.toI32() | 
| Bytes | u32 | s.toU32() | 
| Bytes | JSON | json.fromBytes(s) | 
| int8 | i32 | none | 
| int32 | i32 | none | 
| int32 | BigInt | BigInt.fromI32(s) | 
| uint24 | i32 | none | 
| int64 - int256 | BigInt | none | 
| uint32 - uint256 | BigInt | none | 
| JSON | boolean | s.toBool() | 
| JSON | i64 | s.toI64() | 
| JSON | u64 | s.toU64() | 
| JSON | f64 | s.toF64() | 
| JSON | BigInt | s.toBigInt() | 
| JSON | string | s.toString() | 
| JSON | Array | s.toArray() | 
| JSON | Object | s.toObject() | 
| String | Address | Address.fromString(s) | 
| Bytes | Address | Address.fromBytes(s) | 
| String | BigInt | BigInt.fromString(s) | 
| String | BigDecimal | BigDecimal.fromString(s) | 
| String (hexadecimal) | Bytes | Bytes.fromHexString(s) | 
| String (UTF-8) | Bytes | Bytes.fromUTF8(s) | 
Data Source Metadata
You can inspect the contract address, network and context of the data source that invoked the handler through the dataSource namespace:
- dataSource.address(): Address
- dataSource.network(): string
- dataSource.context(): DataSourceContext
Entity and DataSourceContext
The base Entity class and the child DataSourceContext class have helpers to dynamically set and get fields:
- setString(key: string, value: string): void
- setI32(key: string, value: i32): void
- setBigInt(key: string, value: BigInt): void
- setBytes(key: string, value: Bytes): void
- setBoolean(key: string, value: bool): void
- setBigDecimal(key, value: BigDecimal): void
- getString(key: string): string
- getI32(key: string): i32
- getBigInt(key: string): BigInt
- getBytes(key: string): Bytes
- getBoolean(key: string): boolean
- getBigDecimal(key: string): BigDecimal
DataSourceContext in Manifest
The context section within dataSources allows you to define key-value pairs that are accessible within your Subgraph mappings. The available types are Bool, String, Int, Int8, BigDecimal, Bytes, List, and BigInt.
Here is a YAML example illustrating the usage of various types in the context section:
1dataSources:2  - kind: ethereum/contract3    name: ContractName4    network: mainnet5    context:6      bool_example:7        type: Bool8        data: true9      string_example:10        type: String11        data: 'hello'12      int_example:13        type: Int14        data: 4215      int8_example:16        type: Int817        data: 12718      big_decimal_example:19        type: BigDecimal20        data: '10.99'21      bytes_example:22        type: Bytes23        data: '0x68656c6c6f'24      list_example:25        type: List26        data:27          - type: Int28            data: 129          - type: Int30            data: 231          - type: Int32            data: 333      big_int_example:34        type: BigInt35        data: '1000000000000000000000000'- Bool: Specifies a Boolean value (- trueor- false).
- String: Specifies a String value.
- Int: Specifies a 32-bit integer.
- Int8: Specifies an 8-bit integer.
- BigDecimal: Specifies a decimal number. Must be quoted.
- Bytes: Specifies a hexadecimal string.
- List: Specifies a list of items. Each item needs to specify its type and data.
- BigInt: Specifies a large integer value. Must be quoted due to its large size.
This context is then accessible in your Subgraph mapping files, enabling more dynamic and configurable Subgraphs.