UNPKG

@kiroboio/fct-core

Version:

Kirobo.io FCT Core library

112 lines 5.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getFctMaxGasPrice = exports.getL1BlockNumber = exports.getRandomNumber = exports.getFlowHash = exports.getChainID = exports.getEngineAddress = exports.getActivatorAddress = exports.getInvestorAddress = exports.getOriginAddress = exports.getMinerAddress = exports.getGasPrice = exports.getBlockTimestamp = exports.getBlockNumber = exports.ComputedOperators = void 0; exports.getOutputVariable = getOutputVariable; exports.getExternalVariable = getExternalVariable; exports.getComputedVariable = getComputedVariable; exports.getGlobalVariable = getGlobalVariable; const ethers_1 = require("ethers"); const constants_1 = require("../constants"); const globalVariables_1 = require("./globalVariables"); var computedConstants_1 = require("../batchMultiSigCall/classes/Variables/computedConstants"); Object.defineProperty(exports, "ComputedOperators", { enumerable: true, get: function () { return computedConstants_1.ComputedOperators; } }); // Global Variables const getBlockNumber = () => ({ type: "global", id: "blockNumber" }); exports.getBlockNumber = getBlockNumber; const getBlockTimestamp = () => ({ type: "global", id: "blockTimestamp" }); exports.getBlockTimestamp = getBlockTimestamp; const getGasPrice = () => ({ type: "global", id: "gasPrice" }); exports.getGasPrice = getGasPrice; const getMinerAddress = () => ({ type: "global", id: "minerAddress" }); exports.getMinerAddress = getMinerAddress; const getOriginAddress = () => ({ type: "global", id: "originAddress" }); exports.getOriginAddress = getOriginAddress; const getInvestorAddress = () => ({ type: "global", id: "investorAddress" }); exports.getInvestorAddress = getInvestorAddress; const getActivatorAddress = () => ({ type: "global", id: "activatorAddress" }); exports.getActivatorAddress = getActivatorAddress; const getEngineAddress = () => ({ type: "global", id: "engineAddress" }); exports.getEngineAddress = getEngineAddress; const getChainID = () => ({ type: "global", id: "chainId" }); exports.getChainID = getChainID; const getFlowHash = () => ({ type: "global", id: "flowHash" }); exports.getFlowHash = getFlowHash; const getRandomNumber = () => ({ type: "global", id: "randomNumber" }); exports.getRandomNumber = getRandomNumber; const getL1BlockNumber = () => ({ type: "global", id: "l1BlockNumber" }); exports.getL1BlockNumber = getL1BlockNumber; const getFctMaxGasPrice = () => ({ type: "global", id: "fctMaxGasPrice" }); exports.getFctMaxGasPrice = getFctMaxGasPrice; /** * Generates the output variable based on the provided call index and inner index. * @param {number} index - The index of the call. * @param {number} innerIndex - The index of the slot of the result. * @param {string} type - The type of the output variable. Defaults to "uint256". * @returns {string} - The output variable. */ function getOutputVariable({ index, offset, type = "uint256", }) { const outputIndexHex = (index + 1).toString(16).padStart(4, "0"); let base; let offsetHex; if (offset < 0) { offset = offset + 32 > 0 ? 0 : offset + 32; offsetHex = (offset * -1).toString(16).padStart(5, "0"); base = type === "address" ? constants_1.BackOutputVariableBaseAddress : constants_1.BackOutputVariableBaseBytes32; } else { offsetHex = (offset + 32).toString(16).padStart(4, "0"); base = type === "address" ? constants_1.OutputVariableBaseAddress : constants_1.OutputVariableBaseBytes32; } const value = (offsetHex + outputIndexHex).padStart(base.length, base); if (type !== "address") return value; const address = ethers_1.utils.getAddress(value.toLowerCase()); return address; } /** * Retrieves the external variable based on the provided index and type. * @param {number} index - The index of the external variable. * @param {string} type - The type of the parameter. * @returns {string} - The external variable. */ function getExternalVariable({ index, type }) { const outputIndexHex = (index + 1).toString(16).padStart(4, "0"); const base = type === "address" ? constants_1.ExternalVariableBaseAddress : constants_1.ExternalVariableBaseBytes32; return outputIndexHex.padStart(base.length, base); } /** * Retrieves the computed variable based on the provided index and type. * @param {number} index - The index of the computed variable. * @param {string} type - The type of the parameter. * @returns {string} - The computed variable. */ function getComputedVariable({ index, type }) { const outputIndexHex = (index + 1).toString(16).padStart(4, "0"); const base = type === "address" ? constants_1.ComputedBaseAddress : constants_1.ComputedBaseBytes32; return outputIndexHex.padStart(base.length, base); } /** * Retrieves the value of a global variable based on its ID and type. * @param {string} id - The ID of the global variable. * @param {string} type - The type of the parameter. * @returns {string} The value of the global variable. * @throws If the global variable does not exist. */ function getGlobalVariable({ id, type }) { if (type.includes("bytes")) { if (!(id in globalVariables_1.globalVariablesBytes)) { throw new Error(`Global variable ${id} does not support bytes`); } return globalVariables_1.globalVariablesBytes[id]; } const globalVariable = globalVariables_1.globalVariables[id]; if (!globalVariable) { throw new Error("Global variable not found"); } console.log({ globalvartype: type }); if (type !== "address") return globalVariable; const address = ethers_1.utils.getAddress(globalVariable.toLowerCase()); return address; } //# sourceMappingURL=variables.js.map