deserialize program data in log notification
deserialize program data in log notification
Loading saved threads...
Michael Dee · External communityPost link
External question — Solana Stack Exchange
Author: Michael Dee
Original post: https://solana.stackexchange.com/questions/18378
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
I am trying to decode program data in a log notification with the following code:
import { deserialize } from "borsh";
const LOG_PROGRAMDATA_SCHEMA = {
struct: { name: "string" ,symbol: "string", uri: "string" } //from solnafm
};
const program_data = 'G3KpTd7rY3YNAAAAQlVORExFRCBQRU5HVQYAAABCVU5ETEVDAAAAaHR0cHM6Ly9pcGZzLmlvL2lwZnMvUW1ZdzY1WkxQMUhUdXFOdzNrQllpdXhkRGNQOW5xWTJlSmpZdGpTd1lZS05ZQwQRBm8MluSSPn07JLVylyl9fEhruZBBGubiqpRNzNif9xQ6WBKoNWbAilWVgC2UlsFyeY5hOr4G4Yj5QaW+bQZtjdsv57C0K2nWYY2eQ5dY4P5I0CDudhEcxFNJN62RZw=='
const buffer = Buffer.from(program_data, "base64");
const len = buffer.length;
// console.log("Buffer:", buffer.toString());
try {
const decoded = deserialize(LOG_PROGRAMDATA_SCHEMA, buffer, false);
console.log("Decoded:", decoded);
} catch (e) {
console.log("Error:", e);
}
but got this error:
Error: Error: Error in schema, the buffer is smaller than expected
at DecodeBuffer.assert_enough_buffer (file:///mnt/4T/yolo/data/helius/node_modules/.pnpm/borsh@2.0.0/node_modules/borsh/lib/esm/buffer.js:46:19)
at DecodeBuffer.consume_bytes (file:///mnt/4T/yolo/data/helius/node_modules/.pnpm/borsh@2.0.0/node_modules/borsh/lib/esm/buffer.js:59:14)
at BorshDeserializer.decode_string (file:///mnt/4T/yolo/data/helius/node_modules/.pnpm/borsh@2.0.0/node_modules/borsh/lib/esm/deserialize.js:54:49)
at BorshDeserializer.decode_value (file:///mnt/4T/yolo/data/helius/node_modules/.pnpm/borsh@2.0.0/node_modules/borsh/lib/esm/deserialize.js:15:29)
at BorshDeserializer.decode_struct (file:///mnt/4T/yolo/data/helius/node_modules/.pnpm/borsh@2.0.0/node_modules/borsh/lib/esm/deserialize.js:129:32)
at BorshDeserializer.decode_value (file:///mnt/4T/yolo/data/helius/node_modules/.pnpm/borsh@2.0.0/node_modules/borsh/lib/esm/deserialize.js:31:29)
at BorshDeserializer.decode (file:///mnt/4T/yolo/data/helius/node_modules/.pnpm/borsh@2.0.0/node_modules/borsh/lib/esm/deserialize.js:8:21)
at deserialize (file:///mnt/4T/yolo/data/helius/node_modules/.pnpm/borsh@2.0.0/node_modules/borsh/lib/esm/index.js:16:25)
at <anonymous> (/mnt/4T/yolo/data/helius/testDeserialize.ts:12:19)
I can the original buffer is string is large enough, and print out the buffer, I can see the name and symbols and uri. Why did I get this error?
Quote
Report
mumutik · External communityPost link
External answer — Solana Stack Exchange
Author: mumutik
Original post: https://solana.stackexchange.com/a/18379
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
The error you're encountering with the
borsh
library in your deserialization code is due to an improperly defined schema. The
LOG_PROGRAMDATA_SCHEMA
does not match the expected structure needed for deserialization with Borsh.
Here's what you need to fix:
Define a proper schema:
Ensure that the schema correctly defines the structure of your serialized data.
Deserialization function
: Utilize the correct functionality of Borsh's deserialize method.
Updated Code Example
First, correct the schema definition. It should follow the structure expected by Borsh.
import { deserialize, Schema, BinaryReader, BinaryWriter } from 'borsh';
import base58 from 'bs58';
class LogProgramData {
constructor(properties) {
Object.keys(properties).forEach((key) => {
this[key] = properties[key];
});
}
}
const schema = new Map([
[LogProgramData, {
kind: 'struct',
fields: [
['name', 'string'],
['symbol', 'string'],
['uri', 'string'],
]
}]
]);
const program_data_base64 = 'G3KpTd7rY3YNAAAAQlVORExFRCBQRU5HVQYAAABCVU5ETEVDAAAAaHR0cHM6Ly9pcGZzLmlvL2lwZnMvUW1ZdzY1WkxQMUhUdXFOdzNrQllpdXhkRGNQOW5xWTJlSmpZdGpTd1lZS05ZQwQRBm8MluSSPn07JLVylyl9fEhruZBBGubiqpRNzNif9xQ6WBKoNWbAilWVgC2UlsFyeY5hOr4G4Yj5QaW+bQZtjdsv57C0K2nWYY2eQ5dY4P5I0CDudhEcxFNJN62RZw==';
const buffer = Buffer.from(program_data_base64, 'base64');
try {
const decoded = deserialize(schema, LogProgramData, buffer);
console.log('Decoded:', decoded);
} catch (e) {
console.error('Error:', e);
}
Explanation
:
Define the Class:
Create a class
LogProgramData
that matches the structure of your data.
Create Schema:
Use
borsh
to create a schema describing the structure of your data.
Deserialize:
Pass the schema, the class, and the buffer into the
deserialize
method.
This should resolve the Error in schema, the buffer is smaller than expected issue and let you successfully deserialize the program data.
Quote
Report
Post Reply
Quoted from Forex.com.bd-Editorial External question — Solana Stack Exchange Author: Michael Dee Source score (net votes, not local likes): 0 Original post: https://solana.stackexchange.com/questions/18378 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. I am trying to decode program data in a log notification with the following code: import { deserialize } from "borsh"; const LOG_PROGRAMDATA_SCHEMA = { struct: { name: "string" ,symbol: "string", uri: "string" } //from solnafm }; const program_data = 'G3KpTd7rY3YNAAAAQlVORExFRCBQRU5HVQYAAABCVU5ETEVDAAAAaHR0cHM6Ly9pcGZzLmlvL2lwZnMvUW1ZdzY1WkxQMUhUdXFOdzNrQllpdXhkRGNQOW5xWTJlSmpZdGpTd1lZS05ZQwQRBm8MluSSPn07JLVylyl9fEhruZBBGubiqpRNzNif9xQ6WBKoNWbAilWVgC2UlsFyeY5hOr4G4Yj5QaW+bQZtjdsv57C0K2nWYY2eQ5dY4P5I0CDudhEcxFNJN62RZw==' const buffer = Buffer.from(program_data, "base64"); const len = buffer.length; // console.log("Buffer:", buffer.toString()); try { const decoded = deserialize(LOG_PROGRAMDATA_SCHEMA, buffer, false); console.log("Decoded:", decoded); } catch (e) { console.log("Error:", e); } but got this error: Error: Error: Error in schema, the buffer is smaller than expected at DecodeBuffer.assert_enough_buffer (file:///mnt/4T/yolo/data/helius/node_modules/.pnpm/borsh@2.0.0/node_modules/borsh/lib/esm/buffer.js:46:19) at DecodeBuffer.consume_bytes (file:///mnt/4T/yolo/data/helius/node_modules/.pnpm/borsh@2.0.0/node_modules/borsh/lib/esm/buffer.js:59:14) at BorshDeserializer.decode_string (file:///mnt/4T/yolo/data/helius/node_modules/.pnpm/borsh@2.0.0/node_modules/borsh/lib/esm/deserialize.js:54:49) at BorshDeserializer.decode_value (file:///mnt/4T/yolo/data/helius/node_modules/.pnpm/borsh@2.0.0/node_modules/borsh/lib/esm/deserialize.js:15:29) at BorshDeserializer.decode_struct (file:///mnt/4T/yolo/data/helius/node_modules/.pnpm/borsh@2.0.0/node_modules/borsh/lib/esm/deserialize.js:129:32) at BorshDeserializer.decode_value (file:///mnt/4T/yolo/data/helius/node_modules/.pnpm/borsh@2.0.0/node_modules/borsh/lib/esm/deserialize.js:31:29) at BorshDeserializer.decode (file:///mnt/4T/yolo/data/helius/node_modules/.pnpm/borsh@2.0.0/node_modules/borsh/lib/esm/deserialize.js:8:21) at deserialize (file:///mnt/4T/yolo/data/helius/node_modules/.pnpm/borsh@2.0.0/node_modules/borsh/lib/esm/index.js:16:25) at <anonymous> (/mnt/4T/yolo/data/helius/testDeserialize.ts:12:19) I can the original buffer is string is large enough, and print out the buffer, I can see the name and symbols and uri. Why did I get this error?
Checking account access…