@signumjs/contracts
Version:
Smart Contracts package for Signum Network
30 lines • 1.29 kB
JavaScript
;
/**
* Copyright (c) 2019 Burst Apps Team
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getContractDatablock = getContractDatablock;
const util_1 = require("@signumjs/util");
/**
* Extracts a variables value as hexadecimal string from a contract's machine data
*
* This is a generic function to extract arbitrary data from a contract. It's recommended to use the {@link ContractDataView} class instead
*
* @param contract The contract
* @param position The variables position
* @param length The length of data to be extracted
* @return The value as hexadecimal string (already considering endianness)
*
*/
function getContractDatablock(contract, position, length = 16) {
const startIndex = position * 16;
const requiredSize = startIndex + length;
if (requiredSize > contract.machineData.length) {
throw new Error(`Insufficient length for variable at position: ${startIndex} (and given length: ${length})`);
}
if (requiredSize % 2 !== 0) {
throw new Error(`Invalid position: ${startIndex} (or given length: ${length}) - must be at least multiple of 2`);
}
return (0, util_1.convertHexEndianess)(contract.machineData.substr(startIndex, length));
}
//# sourceMappingURL=getContractDatablock.js.map