Skip to content

Commit

Permalink
API-Edits (#781)
Browse files Browse the repository at this point in the history
* Update website/pages/en/developing/graph-ts/api.mdx

Co-authored-by: Benoît Rouleau <[email protected]>
  • Loading branch information
idalithb and benface authored Sep 20, 2024
1 parent 56d0d0c commit a27ae49
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions website/pages/en/developing/graph-ts/api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
title: AssemblyScript API
---

> Note: if you created a subgraph prior to `graph-cli`/`graph-ts` version `0.22.0`, you're using an older version of AssemblyScript, we recommend taking a look at the [`Migration Guide`](/release-notes/assemblyscript-migration-guide)
> 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`](/release-notes/assemblyscript-migration-guide).
This page documents what built-in APIs can be used when writing subgraph mappings. Two kinds of APIs are available out of the box:
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](https://github.com/graphprotocol/graph-tooling/tree/main/packages/ts) (`graph-ts`) and
- code generated from subgraph files by `graph codegen`.
- The [Graph TypeScript library](https://github.com/graphprotocol/graph-tooling/tree/main/packages/ts) (`graph-ts`)
- Code generated from subgraph files by `graph codegen`

It is also possible to add other libraries as dependencies, as long as they are compatible with [AssemblyScript](https://github.com/AssemblyScript/assemblyscript). Since this is the language mappings are written in, the [AssemblyScript wiki](https://github.com/AssemblyScript/assemblyscript/wiki) is a good source for language and standard library features.
You can also add other libraries as dependencies, as long as they are compatible with [AssemblyScript](https://github.com/AssemblyScript/assemblyscript).

Since language mappings are written in AssemblyScript, it is useful to review the language and standard library features from the [AssemblyScript wiki](https://github.com/AssemblyScript/assemblyscript/wiki).

## API Reference

Expand Down Expand Up @@ -246,7 +248,9 @@ export function handleTransfer(event: TransferEvent): void {

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.
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

Expand All @@ -262,15 +266,18 @@ if (transfer == null) {
// Use the Transfer entity as before
```

As the entity may not exist in the store yet, the `load` method returns a value of type `Transfer | null`. It may thus be necessary to check for the `null` case before using the value.
As 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.
> 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 withing 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 on-chain 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 just 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 loadInBlock avoids this database roundtrip. For some subgraphs, these missed lookups can contribute significantly to the indexing time.
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 on-chain 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 `loadInBlock` avoids this database roundtrip.
- For some subgraphs, these missed lookups can contribute significantly to the indexing time.

```typescript
let id = event.transaction.hash // or however the ID is constructed
Expand Down Expand Up @@ -497,7 +504,9 @@ Any other contract that is part of the subgraph can be imported from the generat

#### 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 `gravatarToOwner` method. This code would be able to handle a revert in that method:
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 `gravatarToOwner` method. This code would be able to handle a revert in that method:

```typescript
let gravity = Gravity.bind(event.address)
Expand All @@ -509,7 +518,7 @@ if (callResult.reverted) {
}
```

Note that 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.
> 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

Expand Down

0 comments on commit a27ae49

Please sign in to comment.