# AssemblyScript API（一）

\
AssemblyScript API

本节讲解在编写子图映射关系时可以使用哪些内置API。

开箱即用提供的API有两种:

TheGraph TypeScript库(graph-ts)和

graph codegen 命令从子图形文件生成的代码。

如果其他库与AssemblyScript兼容，也可以将其他库添加为依赖项。由于这编写的语言映射，因此可以在 AssemblyScript Wiki上找到语言和标准库功能的更多帮助和资源。因此，Web3 相关的库暂时不能在AssemblyScript 中使用。

安装

```
yarn install # Yarn
npm install  # NPM
```

使用graph init 创建的子图带有预先配置好的依赖项。安装这些依赖项所需的全部就是运行以下命令之一:

```
yarn add --dev @graphprotocol/graph-ts         # Yarn
npm install --save-dev @graphprotocol/graph-ts # NPM
```

如果子图是从头开始创建的，则以下两个命令之一将安装TheGraph TypeScript库作为依赖项:

API参考

@ graphprotocol / graph-ts 库提供以下API:

一个以太坊API，用于处理以太坊智能合约、事件、区块、交易和以太坊链上的各类值。

一个存储API，用于从HyperGraph节点存储中加载实体以及将实体保存到节点存储中。

一个日志API，用于将消息记录到HyperGraph节点输出和控制台浏览器上。

一个IPFS API，用于从IPFS加载文件。

一个用于解析JSON数据的JSON API。

一个使用加密功能的加密API。

基本类型可以在不同类型系统(例如以太坊，JSON，GraphQL和AssemblyScript)之间转换。

内置类型

可以在AssemblyScript Wiki中找到关于AssemblyScript内置的类型的文档。

为了实现对区块链数据的解析、存储等，@graphprotocol / graph-ts 提供了以下附加类型。

ByteArray 字节数组

```
import { ByteArray } from '@graphprotocol/graph-ts'
```

ByteArray代表u8(8位无符号整数)类型的数组。

构造:

fromI32(x:i32):ByteArray —— 将x转换为字节数组类型

fromHexString(hex:string):ByteArray——十六进制字符串转换为字节数组，输入长度必须为偶数。带0x前缀是可选的。

类型转换

toHexString():string——将字节数组类型转换为以0x为前缀的十六进制字符串。

toString():string——将字节数组转换为UTF-8字符串。

toBase58():string——将字节数组转换为base58字符串。

toU32():u32——将字节转换为小尾数u32(32位无符号整数)。在溢出的情况下抛出异常。

toI32():i32——将字节数组转换为小端 i32(32位有符号整数)。在溢出的情况下抛出异常。

操作符

equals(y:ByteArray):bool —— 可以写成x == y。

BigDecimal

```
import { BigDecimal } from '@graphprotocol/graph-ts'
```

BigDecimal 用于表示任意精度的小数。

构造

constructor(bigInt:BigInt) —— 从 BigInt 创建一个BigDecimal。

static fromString(s:string):BigDecimal——从十进制字符串解析成 BigDecimal

类型转换

toString():string—— 打印为十进制字符串。

Math

plus(y:​​BigDecimal):BigDecimal——可以写成x + y。

minus(y:​​BigDecimal):BigDecimal——可以写成x-y。

times(y:​​BigDecimal):BigDecimal——可以写成x \* y。

splitBy(y:​​BigDecimal):BigDecimal——可以写为x / y。

equals(y:​​BigDecimal):bool——可以写成x == y。

notEqual(y:​​BigDecimal):bool——可以写成x!= y。

lt(y:​​BigDecimal):bool——可以写成x \<y。

le(y:​​BigDecimal):bool——可以写成x <= y。

gt(y:​​BigDecimal):bool——可以写成x> y。

ge(y:​​BigDecimal):bool——可以写成x> = y。

neg():BigDecimal——可以写为-x。

BigInt

```
import { BigInt } from '@graphprotocol/graph-ts'
```

BigInt用于表示大整数。这包括 uint32 到 uint256 以及 int64 到 int256的以太坊值。少于 uint32以下的所有值，例如int32，uint24或int8都表示为i32。

BigInt类具有以下API:

构造

BigInt.fromI32(x:i32):BigInt ——从i32创建一个BigInt。

BigInt.fromString(s:string):BigInt ——从字符串中解析一个BigInt。

BigInt.fromUnsignedBytes(x:Bytes):BigInt——将字节解释为无符号，小尾数的整数。如果您输入的是大端整数，请首先调用.reverse()。

BigInt.fromSignedBytes(x:Bytes):BigInt——将字节解释为有符号的小端整数。如果您输入的是大端整数，请首先调用.reverse()。

类型转换

x.toHex():字符串——将BigInt转换为十六进制字符的字符串。

x.toString():字符串——将BigInt转换为十进制数字字符串。

x.toI32():i32——将BigInt作为i32返回；如果该值不是i32（有符号32位整数），则失败。建议首先检查x.isI32()。

x.toBigDecimal():BigDecimal——转换为无小数位数的小数。

数学运算

x.plus(y:BigInt):BigInt——可以写成x + y。

x.minus(y:BigInt):BigInt ——可以写成x-y。

x.times(y:BigInt):BigInt——可以写成x \* y。

x.dividedBy(y:BigInt):BigInt——可以写成x / y。

x.mod(y:BigInt):BigInt ——可以写为x％y。

x.equals(y:BigInt):bool ——可以写成x == y。

x.notEqual(y:BigInt):bool——可以写成x！= y。

x.lt(y:BigInt):bool——可以写成x \<y。

x.le(y:BigInt):bool——可以写成x <= y。

x.gt(y:BigInt):bool——可以写成x> y。

x.ge(y:BigInt):bool——可以写成x> = y。

x.neg():BigInt ——可以写为-x。

x.divDecimal(y:BigDecimal):BigDecimal——除以小数，得到小数类型的结果。

x.isZero():bool——检查数字是否为零的便捷方法。

x.isI32():bool——检查数字是否是i32。

x.abs():BigInt——绝对值

x.pow(exp:u8):BigInt——求幂

bitOr(x:BigInt,y:BigInt):BigInt ——可以写为x | y。

bitAnd(x:BigInt,y:BigInt):BigInt——可以写为x＆y。

leftShift(x:BigInt,位:u8):BigInt——可以写成x << y。

rightShift(x:BigInt,位:u8):BigInt ——可以写成x >> y。

TypedMap

```
import { TypedMap } from '@graphprotocol/graph-ts'
```

TypedMap可用于存储键值对。请参阅本示例。 TypedMap类具有以下API： new TypedMap ()——创建一个空映射，其键类型为K，值类型为T map.set(key:K,value:V):void——将key的值设置为value map.getEntry(key:K):TypedMapEntry |null——返回键的键/值对；如果映射中不存在键，则返回null map.get(key:K):V | null——返回键的值；如果映射中不存在键，则返回null map.isSet(key:K):bool——如果映射中存在键值，则返回true;否则返回false

Bytes 字节类型

```
import { Bytes } from '@graphprotocol/graph-ts'
```

Bytes 字节类型用于表示任意长度的字节数组。这包括类型为 byte，bytes32 等的以太坊值。 Bytes 类扩展了 AssemblyScript 的Uint8Array，它支持所有Uint8Array功能，以及以下新方法：

b.toHex()—— 返回一个十六进制字符串，表示数组中的字节 b.toString()—— 将数组中的字节转换为一串 unicode 字符 b.toBase58()—— 将以太坊字节值转换为base58编码（用于IPFS哈希）

Address

```
import { Address } from '@graphprotocol/graph-ts'
```

Address 类型扩展 Bytes 以表示以太坊地址值。 它在 Bytes API之上添加了以下方法： Address.fromString(s:string):Address——从一个十六进制字符串创建一个Address


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.hg.network/product-chan-pin-bang-zhu/cheng-xu-kai-fa/graphql-api.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
