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

Install

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:

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

ByteArray represents an array of type u8 (8-bit unsigned integer).

structure:

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.

Operator

equals(y:ByteArray):bool â€”â€” can be written as x == y.

BigDecimal

BigDecimal is used to represent arbitrary precision decimals.

Constructor

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.

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.

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.

BigInt

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:

Construct

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

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

x.lt(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-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.

TypedMap

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

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)

Address

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

Last updated