@btc-vision/transaction
Version:
OPNet transaction library allows you to create and sign transactions for the OPNet network.
52 lines • 1.81 kB
JavaScript
import { ABIDataTypes } from './ABIDataTypes.js';
import { AbiTypeToStr } from './AbiTypes.js';
/**
* Type guard: returns true if the ABI type is a tuple (ordered array of types).
*/
export function isAbiTuple(type) {
return Array.isArray(type);
}
/**
* Type guard: returns true if the ABI type is a struct (named fields).
*/
export function isAbiStruct(type) {
return typeof type === 'object' && type !== null && !Array.isArray(type);
}
/**
* Type guard: returns true if the ABI type is a simple ABIDataTypes enum value.
*/
export function isSimpleAbiType(type) {
return typeof type === 'string';
}
/**
* Converts a structured AbiType into a canonical selector string.
* - Simple: ABIDataTypes.ADDRESS → "address"
* - Struct: { a: ADDRESS, b: UINT256 } → "tuple(address,uint256)" (inline, no [])
* - Single-element tuple: [UINT256] → "uint256[]"
* - Multi-element tuple: [ADDRESS, UINT256] → "tuple(address,uint256)[]"
*/
export function abiTypeToSelectorString(type) {
if (isSimpleAbiType(type)) {
const str = AbiTypeToStr[type];
if (!str) {
throw new Error(`Unknown ABI type: ${type}`);
}
return str;
}
// Struct: inline tuple (no [] suffix)
if (isAbiStruct(type)) {
const inner = Object.values(type)
.map((t) => abiTypeToSelectorString(t))
.join(',');
return `tuple(${inner})`;
}
// Single-element tuple: unwrap to "type[]"
const firstType = type[0];
if (type.length === 1 && firstType !== undefined) {
return `${abiTypeToSelectorString(firstType)}[]`;
}
// Multi-element tuple: "tuple(types...)[]"
const inner = type.map((t) => abiTypeToSelectorString(t)).join(',');
return `tuple(${inner})[]`;
}
//# sourceMappingURL=TupleUtils.js.map