@gorbchain-xyz/chaindecode
Version:
GorbchainSDK V1.3+ - Complete Solana development toolkit with advanced cryptography, messaging, and collaboration features. Build secure applications with blockchain, DeFi, and end-to-end encryption.
112 lines (111 loc) • 5.15 kB
JavaScript
// Default decoder registry factory
import { DecoderRegistry } from './registry.js';
import { decodeSPLTokenInstruction } from './splToken.js';
import { decodeToken2022Instruction } from './token2022.js';
import { decodeNFTInstruction } from './nft.js';
import { decodeSystemInstruction } from './system.js';
import { decodeATAInstruction } from './ata.js';
import { getGorbchainConfig } from '../utils/gorbchainConfig.js';
// Wrapper functions to convert RawInstruction to specific instruction types
function wrapSPLTokenDecoder(instruction) {
var _a, _b;
const convertedInstruction = {
programId: instruction.programId,
data: (_a = instruction.data) !== null && _a !== void 0 ? _a : new Uint8Array(0),
accounts: (_b = instruction.accounts) !== null && _b !== void 0 ? _b : []
};
return decodeSPLTokenInstruction(convertedInstruction);
}
function wrapToken2022Decoder(instruction) {
var _a, _b;
const convertedInstruction = {
programId: instruction.programId,
data: (_a = instruction.data) !== null && _a !== void 0 ? _a : new Uint8Array(0),
accounts: (_b = instruction.accounts) !== null && _b !== void 0 ? _b : []
};
return decodeToken2022Instruction(convertedInstruction);
}
function wrapNFTDecoder(instruction) {
var _a, _b;
const convertedInstruction = {
programId: instruction.programId,
data: (_a = instruction.data) !== null && _a !== void 0 ? _a : new Uint8Array(0),
accounts: (_b = instruction.accounts) !== null && _b !== void 0 ? _b : []
};
return decodeNFTInstruction(convertedInstruction);
}
function wrapSystemDecoder(instruction) {
var _a, _b, _c, _d;
const data = (_a = instruction.data) !== null && _a !== void 0 ? _a : new Uint8Array(0);
// Handle malformed data - if it's not Uint8Array or valid array, return unknown
let uint8Data;
try {
if (data instanceof Uint8Array) {
uint8Data = data;
}
else if (Array.isArray(data)) {
uint8Data = new Uint8Array(data);
}
else {
// Invalid data type - return unknown instead of system-unknown
return {
type: 'unknown',
programId: instruction.programId,
data: { error: 'Invalid data type for system instruction' },
accounts: (_b = instruction.accounts) !== null && _b !== void 0 ? _b : [],
raw: instruction
};
}
}
catch (error) {
return {
type: 'unknown',
programId: instruction.programId,
data: { error: 'Failed to process system instruction data' },
accounts: (_c = instruction.accounts) !== null && _c !== void 0 ? _c : [],
raw: instruction
};
}
const systemResult = decodeSystemInstruction(uint8Data);
return {
type: systemResult.type,
programId: instruction.programId,
data: systemResult,
accounts: (_d = instruction.accounts) !== null && _d !== void 0 ? _d : [],
raw: instruction
};
}
function wrapATADecoder(instruction) {
var _a, _b;
const convertedInstruction = {
programId: instruction.programId,
data: (_a = instruction.data) !== null && _a !== void 0 ? _a : new Uint8Array(0),
accounts: (_b = instruction.accounts) !== null && _b !== void 0 ? _b : []
};
return decodeATAInstruction(convertedInstruction);
}
/**
* Create a pre-configured decoder registry with common decoders
*/
export function createDefaultDecoderRegistry() {
var _a, _b, _c, _d, _e, _f, _g, _h;
const registry = new DecoderRegistry();
// Get gorbchain config to access program IDs
const config = getGorbchainConfig();
// Register SPL Token decoder
const splTokenProgramId = (_b = (_a = config.programIds) === null || _a === void 0 ? void 0 : _a.splToken) !== null && _b !== void 0 ? _b : 'Gorbj8Dp27NkXMQUkeHBSmpf6iQ3yT4b2uVe8kM4s6br';
registry.register('spl-token', splTokenProgramId, wrapSPLTokenDecoder);
// Register Token-2022 decoder
const token2022ProgramId = (_d = (_c = config.programIds) === null || _c === void 0 ? void 0 : _c.token2022) !== null && _d !== void 0 ? _d : 'G22oYgZ6LnVcy7v8eSNi2xpNk1NcZiPD8CVKSTut7oZ6';
registry.register('token-2022', token2022ProgramId, wrapToken2022Decoder);
// Register NFT/Metaplex decoder
const metaplexProgramId = (_f = (_e = config.programIds) === null || _e === void 0 ? void 0 : _e.metaplex) !== null && _f !== void 0 ? _f : 'GMTAp1moCdGh4TEwFTcCJKeKL3UMEDB6vKpo2uxM9h4s';
registry.register('nft', metaplexProgramId, wrapNFTDecoder);
// Register System program decoder
const systemProgramId = '11111111111111111111111111111111';
registry.register('system', systemProgramId, wrapSystemDecoder);
// Register ATA decoder
const ataProgramId = (_h = (_g = config.programIds) === null || _g === void 0 ? void 0 : _g.ata) !== null && _h !== void 0 ? _h : 'GoATGVNeSXerFerPqTJ8hcED1msPWHHLxao2vwBYqowm';
registry.register('ata', ataProgramId, wrapATADecoder);
return registry;
}