UNPKG

@harmoniclabs/plu-ts-onchain

Version:

An embedded DSL for Cardano smart contracts creation coupled with a library for Cardano transactions, all in Typescript

52 lines (51 loc) 3.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.pPair = void 0; // !!! IMPORTANT !!! // `isWellFormedType` is used both here and by `addPPairMethods` // DO NOT change the order of the two imports var type_system_1 = require("../../../type_system/index.js"); var TermPair_1 = require("../UtilityTerms/TermPair.js"); var Term_1 = require("../../../Term/index.js"); var toData_minimal_1 = require("../data/conversion/toData_minimal.js"); var punsafeConvertType_1 = require("../../punsafeConvertType/index.js"); var ppairData_1 = require("../../builtins/ppairData.js"); var pappArg_1 = require("../../pappArg.js"); var assert_1 = require("../../../../utils/assert.js"); var IRConst_1 = require("../../../../IR/IRNodes/IRConst.js"); var plutus_machine_1 = require("@harmoniclabs/plutus-machine"); var clearAsData_1 = require("../../../type_system/tyArgs/clearAsData.js"); function pPair(fstT, sndT) { (0, assert_1.assert)((0, type_system_1.isWellFormedType)(fstT), "plutus only supports pairs of types that can be converted to constants"); (0, assert_1.assert)((0, type_system_1.isWellFormedType)(sndT), "plutus only supports pairs of types that can be converted to constants"); return function (_fst, _snd) { if (!(_fst instanceof Term_1.Term)) _fst = (0, pappArg_1.pappArgToTerm)(_fst, fstT); if (!(_snd instanceof Term_1.Term)) _snd = (0, pappArg_1.pappArgToTerm)(_snd, sndT); var fst = _fst; var snd = _snd; (0, assert_1.assert)(fst instanceof Term_1.Term && ((0, type_system_1.typeExtends)(fst.type, fstT) || (0, type_system_1.typeExtends)(fst.type, (0, clearAsData_1.clearAsData)(fstT)) || (0, type_system_1.typeExtends)(fst.type, type_system_1.data)), "first element of a constant pair was not a constant"); (0, assert_1.assert)(snd instanceof Term_1.Term && ((0, type_system_1.typeExtends)(snd.type, sndT) || (0, type_system_1.typeExtends)(snd.type, (0, clearAsData_1.clearAsData)(sndT)) || (0, type_system_1.typeExtends)(snd.type, type_system_1.data)), "second element of a constant pair was not a constant"); var _fst_ = (0, toData_minimal_1._toData)((0, clearAsData_1.clearAsData)(fstT))(fst); var _snd_ = (0, toData_minimal_1._toData)((0, clearAsData_1.clearAsData)(sndT))(snd); if (!fst.isConstant || !snd.isConstant) { return (0, punsafeConvertType_1.punsafeConvertType)(ppairData_1.ppairData .$(_fst_) .$(_snd_), (0, type_system_1.pair)((0, type_system_1.asData)(fstT), (0, type_system_1.asData)(sndT))); } return (0, TermPair_1.addPPairMethods)( // al pairs MUST have `asData` elements // causes problem in uplc generation otherwhise new Term_1.Term((0, type_system_1.pair)((0, type_system_1.asData)(fstT), (0, type_system_1.asData)(sndT)), function (_dbn) { return IRConst_1.IRConst.pairOf(type_system_1.data, type_system_1.data)(plutus_machine_1.Machine.evalSimple(_fst_).value, plutus_machine_1.Machine.evalSimple(_snd_).value); }, true // isConstant )); }; } exports.pPair = pPair;