Skip to content

Instantly share code, notes, and snippets.

@cgcardona
Last active October 13, 2022 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cgcardona/d931f2fac9d3f7e194f8fd4e8a04dcc5 to your computer and use it in GitHub Desktop.
Save cgcardona/d931f2fac9d3f7e194f8fd4e8a04dcc5 to your computer and use it in GitHub Desktop.
This script first fetches the hex of a P-Chain transaction. From that hex it creates a `Buffer` & then it creates an empty P-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.
import { Avalanche, Buffer } from "avalanche"
import { PlatformVMAPI, Tx } from "avalanche/dist/apis/platformvm"
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 pchain: PlatformVMAPI = avalanche.PChain()
const main = async (): Promise<any> => {
const txID: string = "7mnY7SqR1s8aTJShjvW1Yebe4snCzsjhonFrrXiWBE4L9x9A6"
const hex = await pchain.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