AssemblyScript API Part 1

AssemblyScript API
This section explains which built-in APIs can be used when writing sub-graph mapping relationships.
There are two APIs provided out of the box:
TheGraph TypeScript library (graph-ts) and The code generated by the graph codegen command from the sub-graph file.
If other libraries are compatible with AssemblyScript, you can also add other libraries as dependencies. Because of the language mapping written here, you can find more help and resources for the language and standard library functions on the AssemblyScript Wiki. Therefore, Web3 related libraries cannot be used in AssemblyScript for the time being.
yarn install # Yarn
npm install # NPM
The subgraph created with graph init comes with pre-configured dependencies. All you need to install these dependencies is to run one of the following commands:
yarn add --dev @graphprotocol/graph-ts # Yarn
npm install --save-dev @graphprotocol/graph-ts # NPM
If the subgraph is created from scratch, one of the following two commands will install TheGraph TypeScript library as a dependency:
API reference
The @graphprotocol/graph-ts library provides the following APIs:
An Ethereum API for processing Ethereum smart contracts, events, blocks, transactions, and various values on the Ethereum chain.
A storage API used to load entities from the HyperGraph node storage and save entities to the node storage.
A logging API for logging messages to the HyperGraph node output and console browser.
An IPFS API for loading files from IPFS.
A JSON API for parsing JSON data.
An encryption API that uses encryption functions.
Basic types can be converted between different type systems (such as Ethereum, JSON, GraphQL and AssemblyScript).
Built-in type
You can find documentation on the types built into AssemblyScript in the AssemblyScript Wiki.
In order to realize the analysis and storage of blockchain data, @graphprotocol / graph-ts provides the following additional types.
ByteArray byte array
import { ByteArray } from '@graphprotocol/graph-ts'
ByteArray represents an array of type u8 (8-bit unsigned integer).
fromI32(x:i32):ByteArray-convert x to byte array type
fromHexString(hex:string):ByteArray——Convert a hexadecimal string to a byte array, and the input length must be an even number. The 0x prefix is optional.
Type conversion
toHexString():string-Convert the byte array type to a hexadecimal string prefixed with 0x.
toString():string-Convert the byte array to a UTF-8 string.
toBase58():string-Convert the byte array to a base58 string.
toU32():u32——Convert bytes to little-endian u32 (32-bit unsigned integer). Throw an exception in case of overflow.
toI32():i32——Convert the byte array to little-endian i32 (32-bit signed integer). Throw an exception in case of overflow.
equals(y:ByteArray):bool —— can be written as x == y.
import { BigDecimal } from '@graphprotocol/graph-ts'
BigDecimal is used to represent arbitrary precision decimals.
constructor(bigInt:BigInt)-Create a BigDecimal from BigInt.
static fromString(s:string): BigDecimal-Parse from decimal string to BigDecimal
Type conversion
toString():string—— Print as a decimal string.
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.
splitBy(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. ge(y:​​BigDecimal):
bool-can be written as x>= y. neg(): BigDecimal-can be written as -x.
import { BigInt } from '@graphprotocol/graph-ts'
BigInt is used to represent large integers. This includes Ethereum values from uint32 to uint256 and int64 to int256. All values less than uint32, such as int32, uint24 or int8 are represented as i32.
The BigInt class has the following API:
BigInt.fromI32(x:i32): BigInt-Create a BigInt from i32.
BigInt.fromString(s:string): BigInt-Parse a BigInt from a string.
BigInt.fromUnsignedBytes(x:Bytes): BigInt-Interpret bytes as unsigned, little-endian integers. If you enter a big-endian integer, call .reverse() first.
BigInt.fromSignedBytes(x:Bytes): BigInt-Interpret bytes as signed little-endian integers. If you enter a big-endian integer, call .reverse() first.
Type conversion
x.toHex(): String-Convert BigInt to a string of hexadecimal characters.
x.toString(): String-Convert BigInt to decimal number string.
x.toI32():i32——return BigInt as i32; if the value is not i32 (signed 32-bit integer), it fails. It is recommended to check x.isI32() first.
x.toBigDecimal():BigDecimal——Convert to a decimal with no decimal places.
computation 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.dividedBy(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. bool-can be written as x y. bool-can be written as x>= y.
x.neg(): BigInt-can be written as -x.
x.divDecimal(y:BigDecimal):BigDecimal-Divide by a decimal to get the result of the decimal type.
x.isZero():bool-a convenient way to check if the number is zero.
x.isI32(): bool-check whether the number is 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, bit: u8): BigInt-can be written as x << y. rightShift(x: BigInt, bit: u8): BigInt-can be written as x >> y.
import { TypedMap } from '@graphprotocol/graph-ts'
TypedMap can be used to store key-value pairs. See this example. The TypedMap class has the following APIs: new TypedMap ()——Create an empty map whose key type is K and value type is T map.set(key:K,value:V):void——Set the value of key to value map.getEntry(key:K):TypedMapEntry |null——returns the key/value pair of the key; if there is no key in the map, it returns null map.get(key:K):V | null——returns the value of the key ; If there is no key in the map, it returns null map.isSet(key:K):bool——If there is a key in the map, it returns true; otherwise it returns false
Bytes byte type
import { Bytes } from '@graphprotocol/graph-ts'
The Bytes type is used to represent byte arrays of arbitrary length. This includes Ethereum values of type byte, bytes32, etc. The Bytes class extends AssemblyScript's Uint8Array, which supports all Uint8Array functions, as well as the following new methods:
b.toHex()—— returns a hexadecimal string representing the bytes in the array b.toString()—— converts the bytes in the array into a string of unicode characters b.toBase58()—— converts the ether Convert byte value to base58 encoding (used for IPFS hashing)
import { Address } from '@graphprotocol/graph-ts'
The Address type extends Bytes to represent the Ethereum address value. It adds the following methods to the Bytes API: Address.fromString(s:string):Address——Create an Address from a hexadecimal string