Last active
October 13, 2022 14:43
-
-
Save cgcardona/fdb102a263a7b568d55fc784aa94587a to your computer and use it in GitHub Desktop.
This script first fetches the hex of an X-Chain transaction. From that hex it creates a `Buffer` & then it creates an empty X-Chain `Tx` & populates it via passing in the `Buffer` to `tx.fromBuffer`. Next it calls `JSON.stringify` on the `Tx` to encode it as a JSON string. Lastly it takes the JSON string & convert it to an actual JSON object.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Avalanche, Buffer } from "avalanche" | |
import { AVMAPI, Tx } from "avalanche/dist/apis/avm" | |
const ip: string = "api.avax.network" | |
const port: number = 443 | |
const protocol: string = "https" | |
const networkID: number = 1 | |
const avalanche: Avalanche = new Avalanche(ip, port, protocol, networkID) | |
const xchain: AVMAPI = avalanche.XChain() | |
const main = async (): Promise<any> => { | |
const txID: string = "2fJer7o3HpPYxqyHXo23G4HoPvfEqcUXYojMULi2mbBEoBFqoM" | |
const hex = await xchain.getTx(txID) as string | |
const buf: Buffer = new Buffer(hex.slice(2), "hex") | |
const tx: Tx = new Tx() | |
tx.fromBuffer(buf) | |
const jsonStr: string = JSON.stringify(tx) | |
console.log(jsonStr) | |
const jsn: Object = JSON.parse(jsonStr) | |
console.log(jsn) | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment