Skip to content

Instantly share code, notes, and snippets.

@kwrooijen
Created March 25, 2022 13:33
Show Gist options
  • Save kwrooijen/2c01ef62d74c99d17f6762cafa8c2623 to your computer and use it in GitHub Desktop.
Save kwrooijen/2c01ef62d74c99d17f6762cafa8c2623 to your computer and use it in GitHub Desktop.
Elrond serialization
// Import from erdjs SDK
import { AbiRegistry, BinaryCodec } from "@elrondnetwork/erdjs";
// Our base64 response buffer that was queries
let buffer = Buffer.from("AAAACFNvbWUgaGF0AAAAClNvbWUgc2hpcnQAACMp", "base64");
// Load the main.abi.json into our program
let registry = await AbiRegistry.load({ files: ["main.abi.json"] });
// Get MyNFT struct from the registry
let type = registry.getStruct("MyNFT");
// Decode the base64 response using the MyNFT type
let result = (new BinaryCodec).decodeTopLevel(buffer, type).valueOf();
// Print the result
console.log(result);
#![no_std]
elrond_wasm::imports!();
elrond_wasm::derive_imports!();
#[derive(TopEncode, TopDecode, TypeAbi)]
pub struct MyNFT<M: ManagedTypeApi> {
hat: ManagedBuffer<M>,
shirt: ManagedBuffer<M>,
rarity: i32
}
#[elrond_wasm::contract]
pub trait App {
#[init]
fn init(&self) {}
#[view(my_nft)]
fn my_nft(&self) -> MyNFT<Self::Api>
{
MyNFT {
hat: ManagedBuffer::from(b"Some hat"),
shirt: ManagedBuffer::from(b"Some shirt"),
rarity: 9001,
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment