@helios-lang/contract-utils
Advanced tools
Comparing version 0.3.10 to 0.3.11
{ | ||
"name": "@helios-lang/contract-utils", | ||
"version": "0.3.10", | ||
"version": "0.3.11", | ||
"description": "Convenience and type-safety utilities for using Helios validators from within Typescript", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -0,3 +1,4 @@ | ||
import { bytesToHex } from "@helios-lang/codec-utils" | ||
import { expectDefined, isRight } from "@helios-lang/type-utils" | ||
import { | ||
genFuncType, | ||
genUserFuncArgsType, | ||
@@ -9,2 +10,3 @@ genUserFuncRetType | ||
/** | ||
* @import { UplcProgram } from "@helios-lang/uplc" | ||
* @import { UserFuncProps } from "../index.js" | ||
@@ -28,3 +30,3 @@ * @import { Artifact } from "./Artifact.js" | ||
artifact.writeProps(props) | ||
artifact.writeEvals(props) | ||
artifact.writeEvals(program, props) | ||
@@ -54,5 +56,6 @@ artifact.save() | ||
/** | ||
* @param {UplcProgram} program | ||
* @param {UserFuncProps} props | ||
*/ | ||
writeEvals(props) { | ||
writeEvals(program, props) { | ||
const safeArgsType = genUserFuncArgsType(props) | ||
@@ -62,2 +65,3 @@ const unsafeArgsType = genUserFuncArgsType(props, true) | ||
const unsafeRetType = genUserFuncRetType(props, true) | ||
const isConst = safeArgsType == "{}" | ||
@@ -70,6 +74,6 @@ this.addImport("UplcLogger", "@helios-lang/uplc", true) | ||
.writeDeclLine( | ||
`export function $eval(args${safeArgsType == "{}" ? "?" : ""}: ${safeArgsType}, logOptions?: UplcLogger | undefined): ${retType}` | ||
`export function $eval(args${isConst ? "?" : ""}: ${safeArgsType}, logOptions?: UplcLogger | undefined): ${retType}` | ||
) | ||
.writeDefLine( | ||
`export function $eval(args${safeArgsType == "{}" ? " = {}" : ""}, logOptions = undefined) { | ||
`export function $eval(args${isConst ? " = {}" : ""}, logOptions = undefined) { | ||
return evalUserFunc($program, $props, args, logOptions) | ||
@@ -80,6 +84,6 @@ }` | ||
.writeDeclLine( | ||
`export function $evalUnsafe(args${unsafeArgsType == "{}" ? "?" : ""}: ${unsafeArgsType}, logOptions?: UplcLogger | undefined): ${unsafeRetType}` | ||
`export function $evalUnsafe(args${isConst ? "?" : ""}: ${unsafeArgsType}, logOptions?: UplcLogger | undefined): ${unsafeRetType}` | ||
) | ||
.writeDefLine( | ||
`export function $evalUnsafe(args${unsafeArgsType == "{}" ? " = {}" : ""}, logOptions = undefined) { | ||
`export function $evalUnsafe(args${isConst ? " = {}" : ""}, logOptions = undefined) { | ||
return evalUserFuncUnsafe($program, $props, args, logOptions) | ||
@@ -90,8 +94,46 @@ }` | ||
.writeDeclLine( | ||
`export function $profile(args${unsafeArgsType == "{}" ? "?" : ""}: ${unsafeArgsType}, logOptions?: UplcLogger | undefined): CekResult` | ||
`export function $profile(args${isConst ? "?" : ""}: ${unsafeArgsType}, logOptions?: UplcLogger | undefined): CekResult` | ||
) | ||
.writeDefLine(`export function $profile(args${unsafeArgsType == "{}" ? " = {}" : ""}, logOptions = undefined) { | ||
.writeDefLine(`export function $profile(args${isConst ? " = {}" : ""}, logOptions = undefined) { | ||
return profileUserFunc($program, $props, args, logOptions) | ||
}`) | ||
if (isConst) { | ||
// attempt to evaluate, and add $data | ||
try { | ||
const result = program.eval([]) | ||
if ( | ||
isRight(result.result) && | ||
typeof result.result.right != "string" && | ||
result.result.right.kind == "data" | ||
) { | ||
const data = result.result.right.value | ||
const uplcDataType = expectDefined( | ||
{ | ||
int: "IntData", | ||
bytes: "ByteArrayData", | ||
constr: "ConstrData", | ||
map: "MapData", | ||
list: "ListData" | ||
}[data.kind] | ||
) | ||
this.addImport(uplcDataType, "@helios-lang/uplc", true) | ||
.writeDeclLine( | ||
`export const $constData: ${uplcDataType}` | ||
) | ||
.addImport("decodeUplcData", "@helios-lang/uplc", false) | ||
.writeDefLine( | ||
`export const $constData = /* @__PURE__ */ decodeUplcData("${bytesToHex(data.toCbor())}")` | ||
) | ||
// TODO: $constValue | ||
} | ||
} catch (_e) { | ||
// don't do anything, simply ignore | ||
} | ||
} | ||
} | ||
} |
/** | ||
* @import { UplcProgram } from "@helios-lang/uplc" | ||
* @import { UserFuncProps } from "../index.js" | ||
@@ -3,0 +4,0 @@ * @import { Artifact } from "./Artifact.js" |
Sorry, the diff of this file is not supported yet
291853
7182