Ethereum Quick Start - Ethscriptions
Ethereum Quick Start - Ethscriptions
This guide serves as a starting point for the Ethscriptions SubQuery indexer. Ethscriptions, serving as an alternative to smart contracts, operates as a protocol on Ethereum L1, enabling users to share data and execute computations at significantly reduced expenses.
Important
We suggest starting with the Ethereum Gravatar example. The Ethereum Uniswap project is a lot more complicated and introduces some more advanced concepts
In the earlier Quickstart section , you should have taken note of three crucial files. To initiate the setup of a project from scratch, you can proceed to follow the steps outlined in the initialisation description.
As a prerequisite, you will need to generate types from the ABI files of each smart contract. Additionally, you can kickstart your project by using the EVM Scaffolding approach (detailed here). You'll find all the relevant events to be scaffolded in the documentation for each type of smart contract.
Note
The final code of this project can be found here.
Your Project Manifest File
The Project Manifest file is an entry point to your project. It defines most of the details on how SubQuery will index and transform the chain data.
For EVM chains, there are three types of mapping handlers (and you can have more than one in each project):
- BlockHanders: On each and every block, run a mapping function
- TransactionHandlers: On each and every transaction that matches optional filter criteria, run a mapping function
- LogHanders: On each and every log that matches optional filter criteria, run a mapping function
Ethscriptions approach involves circumventing smart contract storage and execution processes, relying on deterministic protocol rules applied to basic Ethereum calldata to derive the state. The primary aim of Ethscriptions is to empower everyday users with the capability to conduct decentralized computations affordably. Update dataSources
object in your manifest file to look like this:
dataSources: [
{
kind: EthereumDatasourceKind.Runtime,
startBlock: 18130011,
mapping: {
file: "./dist/index.js",
handlers: [
{
kind: EthereumHandlerKind.Call,
handler: "handleTransaction",
filter: {
function: "0x64617461",
},
},
],
},
},
],
As observed, our sole handler at this point is the transaction handler. Given that all inscriptions essentially comprise Base 64-encoded data URIs, the initial sub-string is consistently data:
, equivalent to 0x64617461
in hex format.
Note
Check out our Manifest File documentation to get more information about the Project Manifest (project.ts
) file.
Update Your GraphQL Schema File
The schema.graphql
file determines the shape of your data from SubQuery due to the mechanism of the GraphQL query language. Hence, updating the GraphQL Schema file is the perfect place to start. It allows you to define your end goal right at the start.
In our case, the GraphQL schema is quite straightforward, comprising only one entity called Inscriptions
, structured as follows:
type Inscription @entity {
id: ID!
block: Int!
creator: String!
data: String!
created: Date!
}
Note
Importantly, these relationships can not only establish one-to-many connections but also extend to include many-to-many associations. To delve deeper into entity relationships, you can refer to this section. If you prefer a more example-based approach, our dedicated Hero Course Module can provide further insights.
SubQuery simplifies and ensures type-safety when working with GraphQL entities, smart contracts, events, transactions, and logs. The SubQuery CLI will generate types based on your project's GraphQL schema and any contract ABIs included in the data sources.
yarn codegen
npm run-script codegen
This action will generate a new directory (or update the existing one) named src/types
. Inside this directory, you will find automatically generated entity classes corresponding to each type defined in your schema.graphql
. These classes facilitate type-safe operations for loading, reading, and writing entity fields. You can learn more about this process in the GraphQL Schema section.
It will also generate a class for every contract event, offering convenient access to event parameters, as well as information about the block and transaction from which the event originated. You can find detailed information on how this is achieved in the EVM Codegen from ABIs section. All of these types are stored in the src/types/abi-interfaces
and src/types/contracts
directories.
You can conveniently import all these types:
import { Inscription } from "../types";
import { EthereumTransaction } from "@subql/types-ethereum";
Check out the GraphQL Schema documentation to get in-depth information on schema.graphql
file.
Now that you have made essential changes to the GraphQL Schema file, let’s proceed ahead with the Mapping Function’s configuration.
Add a Mapping Function
Mapping functions define how blockchain data is transformed into the optimised GraphQL entities that we previously defined in the schema.graphql
file.
In this scenario, there's a mapping file containing a pre-established handler. Additionally, there's a utils.ts
file housing the helper functions responsible for Ethereum hex data processing during execution.
mappingHandlings.ts
export async function handleTransaction(
tx: EthereumTransaction,
): Promise<void> {
if (tx.to && tx.input) {
let inscription: Inscription;
const decodedData = hexToUTF8(tx.input);
if (isValidDataUri(decodedData)) {
inscription = Inscription.create({
id: tx.hash,
data: decodedData,
block: tx.blockNumber,
creator: tx.from,
created: new Date(Number(tx.blockTimestamp) * 1000),
});
await inscription.save();
}
}
}
utils.ts
export function hexToUTF8(hexString: string): string {
if (hexString.indexOf("0x") === 0) {
hexString = hexString.slice(2);
}
const bytes = new Uint8Array(hexString.length / 2);
for (let index = 0; index < bytes.length; index++) {
const start = index * 2;
const hexByte = hexString.slice(start, start + 2);
const byte = Number.parseInt(hexByte, 16);
if (Number.isNaN(byte) || byte < 0)
throw new Error(
`Invalid byte sequence ("${hexByte}" in "${hexString}").`,
);
bytes[index] = byte;
}
let result = String.fromCharCode.apply(null, Array.from(bytes));
return result.replace(/\0/g, "");
}
export function isValidDataUri(uri: string): boolean {
const regexp =
/data:(?<mediatype>(?<mimetype>.+?\/.+?)?(?<parameters>(?:;.+?=.*?)*))?(?<extension>;base64)?,(?<data>.*)/;
const match = regexp.exec(uri);
if (!match || !match.groups) {
return false;
}
const { data, extension } = match.groups;
return validBase64Content(data, extension);
}
export function validBase64Content(data: string, extension?: string): boolean {
if (extension) {
try {
atob(data);
return true;
} catch (error) {
return false;
}
} else {
return true;
}
}
The handleTransaction
function in mappingHandlings.ts
processes Ethereum transactions. It decodes the input data of a transaction from hexadecimal format to UTF-8, checks if the decoded data is a valid data URI, and if so, creates an Inscription
entity with specific attributes derived from the transaction information (like hash, data, block number, creator, and creation date). Finally, it saves this Inscription
entity. During the processing, it utilises functions sourced from utils.ts
.
The hexToUTF8
function in utils.ts
converts a given hexadecimal string to its corresponding UTF-8 string representation. The isValidDataUri
function checks if a given string is a valid data URI by parsing and matching its components according to a specific regular expression pattern. It verifies if the provided data is valid base64 content, considering if it is in base64 format when the URI has the 'base64' extension. The validBase64Content
function determines if a given string is valid base64 content, particularly focusing on checking this when the content is expected to be in base64 format.
Note
For more information on mapping functions, please refer to our Mappings documentation.
Build Your Project
Next, build your work to run your new SubQuery project. Run the build command from the project's root directory as given here:
yarn build
npm run-script build
Important
Whenever you make changes to your mapping functions, you must rebuild your project.
Now, you are ready to run your first SubQuery project. Let’s check out the process of running your project in detail.
Whenever you create a new SubQuery Project, first, you must run it locally on your computer and test it and using Docker is the easiest and quickiest way to do this.
Run Your Project Locally with Docker
The docker-compose.yml
file defines all the configurations that control how a SubQuery node runs. For a new project, which you have just initialised, you won't need to change anything.
However, visit the Running SubQuery Locally to get more information on the file and the settings.
Run the following command under the project directory:
yarn start:docker
npm run-script start:docker
Note
It may take a few minutes to download the required images and start the various nodes and Postgres databases.
Query your Project
Next, let's query our project. Follow these three simple steps to query your SubQuery project:
Open your browser and head to
http://localhost:3000
.You will see a GraphQL playground in the browser and the schemas which are ready to query.
Find the Docs tab on the right side of the playground which should open a documentation drawer. This documentation is automatically generated and it helps you find what entities and methods you can query.
Try the following queries to understand how it works for your new SubQuery starter project. Don’t forget to learn more about the GraphQL Query language.
query {
inscriptions(first: 2) {
nodes {
id
data
creator
block
created
}
}
}
{
"data": {
"inscriptions": {
"nodes": [
{
"id": "0x464612703fbc884e68a8b53cff271d7dca9af868c4e7638f5052abc764e05251",
"data": "data:,{\"p\":\"erc-cash\",\"op\":\"mint\",\"tick\":\"ESH\",\"id\":\"10348\",\"amt\":\"1000\"}",
"creator": "0x0d283dF942DA60E67AA41b3393d0420EBf65c8d1",
"block": 18130571,
"created": "2023-09-13T23:11:59"
},
{
"id": "0xe1a62ebfa91493d6b2d3651e72a27b2ccb227b38c5cd68c697832987b659d501",
"data": "data:,{\"p\":\"erc-20\",\"op\":\"mint\",\"tick\":\"defi\",\"id\":\"3938\",\"amt\":\"1000\"}",
"creator": "0xBe3C8c48845c42484d896EF0A5a7fDa3A3ceCE7F",
"block": 18130661,
"created": "2023-09-13T23:30:23"
}
]
}
}
}
Note
The final code of this project can be found here.
What's next?
Congratulations! You have now a locally running SubQuery project that accepts GraphQL API requests for transferring data.
Tip
Find out how to build a performant SubQuery project and avoid common mistakes in Project Optimisation.
Click here to learn what should be your next step in your SubQuery journey.