Home

GraphProtocol: Encoding & Decoding data and hashes in subgraph

abi.decode

Consider the following solidity snippet

abi.decode(data, (uint, uint, uint, uint, uint8, bytes32, bytes32))

If you want to do the same in subgraph, do the following in your mapping.ts file

import { ethereum } from '@graphprotocol/graph-ts';

const decoded = ethereum.decode(
  '(uint,uint,uint,uint,uint8,bytes32,bytes32)',
  dataInBytes
);

toUtf8String

Consider the following code

const {toUtf8CodePoints,toUtf8String} = require("@ethersproject/strings");

const message = "hello"
const messageBytes = '0x' + toUtf8CodePoints(message).map(x=>x.toString(16)).join('')
const messageStr = toUtf8String(messageBytes)

console.log(messageStr == message)

To convert bytes hex string into UTF-8 string in subgraph, do the following in your mapping.ts file

import { ByteArray } from "@graphprotocol/graph-ts";

const messageBytes = message.toHexString()

const messageStr = ByteArray.fromHexString(messageBytes).toString()


Last Updated on

Next Post: GraphProtocol: TS2322 null assignment in Subgraph →

Comments