@helios-lang/contract-utils
Advanced tools
Comparing version 0.2.62 to 0.2.63
{ | ||
"name": "@helios-lang/contract-utils", | ||
"version": "0.2.62", | ||
"version": "0.2.63", | ||
"description": "Convenience and type-safety utilities for using Helios validators from within Typescript", | ||
@@ -31,5 +31,5 @@ "main": "src/index.js", | ||
"@helios-lang/compiler-utils": "^0.1.25", | ||
"@helios-lang/ledger": "^0.4.12", | ||
"@helios-lang/ledger": "^0.5.1", | ||
"@helios-lang/type-utils": "^0.1.21", | ||
"@helios-lang/uplc": "^0.3.6" | ||
"@helios-lang/uplc": "^0.3.8" | ||
}, | ||
@@ -39,2 +39,3 @@ "scripts": { | ||
"build:types": "tsc -p jsconfig.json --noEmit false --emitDeclarationOnly", | ||
"lockfile:sync": "pnpm install --ignore-workspace", | ||
"prettify": "prettier . --write", | ||
@@ -44,4 +45,6 @@ "test": "pnpm run test:pretty && pnpm run test:types && pnpm run test:suite", | ||
"test:suite": "node --test", | ||
"testing": "node --test --watch", | ||
"testing:debug": "node --inspect-brk --test --watch", | ||
"test:types": "pnpm run build:types" | ||
} | ||
} |
@@ -97,7 +97,16 @@ import { decodeUtf8, encodeUtf8 } from "@helios-lang/codec-utils" | ||
/** | ||
* converts javascript object to UPLC data, according to the schema. | ||
* @remarks | ||
* The optional `dataPath` parameter can provide a contextual cue for the | ||
* data-conversion process, and will be displayed as part of any error messages | ||
* thrown during the data transformation | ||
* | ||
* @param {TPermissive} x | ||
* @param {string} dataPath | ||
* @returns {UplcData} | ||
*/ | ||
toUplcData(x) { | ||
return schemaToUplc(this.schema, x) | ||
toUplcData(x, dataPath = "") { | ||
const t = schemaToUplcWithDataPath(this.schema, x, {}, dataPath) | ||
t.rawData = x | ||
return t | ||
} | ||
@@ -110,11 +119,38 @@ } | ||
* @param {Record<string, TypeSchema>} defs | ||
* @param {string} dataPath | ||
* @returns {UplcData} | ||
*/ | ||
function schemaToUplc(schema, x, defs = {}) { | ||
function schemaToUplcWithDataPath(schema, x, defs = {}, dataPath = "") { | ||
try { | ||
const t = schemaToUplc(schema, x, defs, dataPath) | ||
t.dataPath = dataPath | ||
return t | ||
} catch (e) { | ||
if (!e.uplcDataPath) { | ||
e.message = `${e.message}\n ... at ${dataPath}` | ||
e.uplcDataPath = dataPath | ||
} | ||
debugger | ||
throw e | ||
} | ||
} | ||
/** | ||
* @param {TypeSchema} schema | ||
* @param {any} x | ||
* @param {Record<string, TypeSchema>} defs | ||
* @param {string} dataPath | ||
* @returns {UplcData} | ||
*/ | ||
function schemaToUplc(schema, x, defs = {}, dataPath = "") { | ||
const kind = schema.kind | ||
switch (kind) { | ||
case "reference": { | ||
const def = expectSome(defs[schema.id]) | ||
return schemaToUplc(def, x, defs) | ||
return schemaToUplcWithDataPath( | ||
def, | ||
x, | ||
defs, | ||
`${dataPath}::ref{${schema.id}}` | ||
) | ||
} | ||
@@ -189,3 +225,5 @@ case "internal": { | ||
default: | ||
throw new Error(`not yet implemented for ${name}`) | ||
throw new Error( | ||
`schemaToUplc not yet implemented for ${name}` | ||
) | ||
} | ||
@@ -195,10 +233,32 @@ } | ||
return new ListData( | ||
x.map((x) => schemaToUplc(schema.itemType, x, defs)) | ||
x.map((x, i) => | ||
schemaToUplcWithDataPath( | ||
schema.itemType, | ||
x, | ||
defs, | ||
`${dataPath}.list[${i}]` | ||
) | ||
) | ||
) | ||
case "map": { | ||
const entries = x instanceof Map ? x.entries() : x | ||
const entries = | ||
x instanceof Map | ||
? [...x.entries()] | ||
: Array.isArray(x) | ||
? x | ||
: Object.entries(x) | ||
return new MapData( | ||
entries.map(([k, v]) => [ | ||
schemaToUplc(schema.keyType, k, defs), | ||
schemaToUplc(schema.valueType, v, defs) | ||
schemaToUplcWithDataPath( | ||
schema.keyType, | ||
k, | ||
defs, | ||
`${dataPath}[mapKey '${k}']` | ||
), | ||
schemaToUplcWithDataPath( | ||
schema.valueType, | ||
v, | ||
defs, | ||
`${dataPath}[map].${k}` | ||
) | ||
]) | ||
@@ -209,7 +269,21 @@ ) | ||
return new ListData( | ||
x.map((x, i) => schemaToUplc(schema.itemTypes[i], x, defs)) | ||
x.map((x, i) => | ||
schemaToUplcWithDataPath( | ||
schema.itemTypes[i], | ||
x, | ||
defs, | ||
`${dataPath}[tuple@${i}]` | ||
) | ||
) | ||
) | ||
case "option": | ||
return encodeOptionData( | ||
x ? schemaToUplc(schema.someType, x, defs) : None | ||
x | ||
? schemaToUplcWithDataPath( | ||
schema.someType, | ||
x, | ||
defs, | ||
`${dataPath}::Some` | ||
) | ||
: None | ||
) | ||
@@ -220,6 +294,8 @@ case "struct": { | ||
case "singleton": | ||
return schemaToUplc( | ||
const singleFieldName = schema.fieldTypes[0].name | ||
return schemaToUplcWithDataPath( | ||
schema.fieldTypes[0].type, | ||
x[schema.fieldTypes[0].name], | ||
defs | ||
x[singleFieldName], | ||
defs, | ||
`${dataPath}[sStruct.${singleFieldName}]` | ||
) | ||
@@ -229,3 +305,8 @@ case "list": | ||
schema.fieldTypes.map(({ name, type }) => | ||
schemaToUplc(type, x[name]) | ||
schemaToUplcWithDataPath( | ||
type, | ||
x[name], | ||
{}, | ||
`${dataPath}[fStruct].${name}` | ||
) | ||
) | ||
@@ -253,4 +334,14 @@ ) | ||
if (ft) { | ||
const keyData = new ByteArrayData(encodeUtf8(key)) | ||
const valueData = schemaToUplc(ft.type, value, defs) | ||
debugger | ||
// const rawKey = ft.key || ft.name | ||
const rawKey = ft.name | ||
const keyData = new ByteArrayData( | ||
encodeUtf8(rawKey) | ||
) | ||
const valueData = schemaToUplcWithDataPath( | ||
ft.type, | ||
value, | ||
defs, | ||
`${dataPath}[mStruct][${key}]` | ||
) | ||
@@ -287,3 +378,8 @@ pairs.push([keyData, valueData]) | ||
schema.variantTypes[tag].fieldTypes.map((f) => | ||
schemaToUplc(f.type, variantFields[f.name], defs) | ||
schemaToUplcWithDataPath( | ||
f.type, | ||
variantFields[f.name], | ||
defs, | ||
`${dataPath}[${schema.name}::${variantName}].${f.name}` | ||
) | ||
) | ||
@@ -294,6 +390,12 @@ ) | ||
defs[schema.id] = schema | ||
return new ConstrData( | ||
schema.tag, | ||
schema.fieldTypes.map(({ name, type }) => | ||
schemaToUplc(type, x[name]) | ||
schemaToUplcWithDataPath( | ||
type, | ||
x[name], | ||
{}, | ||
`${dataPath}.${name}` | ||
) | ||
) | ||
@@ -300,0 +402,0 @@ ) |
@@ -5,2 +5,3 @@ import { bytesToHex } from "@helios-lang/codec-utils" | ||
import { Cast } from "./Cast.js" | ||
import { BasicUplcLogger } from "@helios-lang/uplc" | ||
@@ -11,2 +12,3 @@ /** | ||
* @typedef {import("@helios-lang/uplc").UplcData} UplcData | ||
* @typedef {import("@helios-lang/uplc").UplcLoggingI} UplcLoggingI | ||
* @typedef {import("@helios-lang/uplc").UplcProgram} UplcProgram | ||
@@ -75,5 +77,6 @@ * @typedef {import("@helios-lang/uplc").UplcValue} UplcValue | ||
* @param {ArgsT} namedArgs | ||
* @param {UplcLoggingI} [logOptions] | ||
* @returns {RetT} | ||
*/ | ||
eval(namedArgs) { | ||
eval(namedArgs, logOptions) { | ||
/** | ||
@@ -106,3 +109,4 @@ * @type {{[argName: string]: any}} | ||
const result = this.evalUnsafe( | ||
/** @type {UnsafeArgsT<ArgsT>} */ (unsafeNamedArgs) | ||
/** @type {UnsafeArgsT<ArgsT>} */ (unsafeNamedArgs), | ||
logOptions | ||
) | ||
@@ -122,6 +126,7 @@ | ||
* @param {UnsafeArgsT<ArgsT>} namedArgs | ||
* @param {UplcLoggingI} [logOptions] | ||
* @returns {RetT extends void ? void : UplcData} | ||
*/ | ||
evalUnsafe(namedArgs) { | ||
const result = this.profile(namedArgs).result | ||
evalUnsafe(namedArgs, logOptions) { | ||
const result = this.profile(namedArgs, logOptions).result | ||
@@ -141,5 +146,6 @@ if (isLeft(result)) { | ||
* @param {UnsafeArgsT<ArgsT>} namedArgs | ||
* @param {UplcLoggingI} [logOptions] - optional, passed to UplcProgram.eval if provided | ||
* @returns {CekResult} | ||
*/ | ||
profile(namedArgs) { | ||
profile(namedArgs, logOptions) { | ||
const isMain = this.name == "main" | ||
@@ -193,6 +199,8 @@ | ||
const cekResult = this.uplc.eval(argValues) | ||
const cekResult = this.uplc.eval(argValues, { logOptions }) | ||
if (this.uplc.alt?.plutusVersion == "PlutusScriptV2") { | ||
const cekResultUnoptim = this.uplc.alt.eval(argValues) | ||
const cekResultUnoptim = this.uplc.alt.eval(argValues, { | ||
logOptions | ||
}) | ||
const resultUnoptim = cekResultUnoptim.result | ||
@@ -199,0 +207,0 @@ const resultUnoptimStr = evalResultToString(resultUnoptim) |
@@ -55,6 +55,13 @@ /** | ||
/** | ||
* converts javascript object to UPLC data, according to the schema. | ||
* @remarks | ||
* The optional `dataPath` parameter can provide a contextual cue for the | ||
* data-conversion process, and will be displayed as part of any error messages | ||
* thrown during the data transformation | ||
* | ||
* @param {TPermissive} x | ||
* @param {string} dataPath | ||
* @returns {UplcData} | ||
*/ | ||
toUplcData(x: TPermissive): UplcData; | ||
toUplcData(x: TPermissive, dataPath?: string): UplcData; | ||
} | ||
@@ -61,0 +68,0 @@ export type UplcData = import("@helios-lang/uplc").UplcData; |
@@ -5,2 +5,3 @@ /** | ||
* @typedef {import("@helios-lang/uplc").UplcData} UplcData | ||
* @typedef {import("@helios-lang/uplc").UplcLoggingI} UplcLoggingI | ||
* @typedef {import("@helios-lang/uplc").UplcProgram} UplcProgram | ||
@@ -59,15 +60,18 @@ * @typedef {import("@helios-lang/uplc").UplcValue} UplcValue | ||
* @param {ArgsT} namedArgs | ||
* @param {UplcLoggingI} [logOptions] | ||
* @returns {RetT} | ||
*/ | ||
eval(namedArgs: ArgsT): RetT; | ||
eval(namedArgs: ArgsT, logOptions?: import("@helios-lang/uplc/types/logging/UplcLoggingI.js").UplcLoggingI | undefined): RetT; | ||
/** | ||
* @param {UnsafeArgsT<ArgsT>} namedArgs | ||
* @param {UplcLoggingI} [logOptions] | ||
* @returns {RetT extends void ? void : UplcData} | ||
*/ | ||
evalUnsafe(namedArgs: UnsafeArgsT<ArgsT>): RetT extends void ? void : UplcData; | ||
evalUnsafe(namedArgs: UnsafeArgsT<ArgsT>, logOptions?: import("@helios-lang/uplc/types/logging/UplcLoggingI.js").UplcLoggingI | undefined): RetT extends void ? void : UplcData; | ||
/** | ||
* @param {UnsafeArgsT<ArgsT>} namedArgs | ||
* @param {UplcLoggingI} [logOptions] - optional, passed to UplcProgram.eval if provided | ||
* @returns {CekResult} | ||
*/ | ||
profile(namedArgs: UnsafeArgsT<ArgsT>): CekResult; | ||
profile(namedArgs: UnsafeArgsT<ArgsT>, logOptions?: import("@helios-lang/uplc/types/logging/UplcLoggingI.js").UplcLoggingI | undefined): CekResult; | ||
} | ||
@@ -77,2 +81,3 @@ export type TypeSchema = import("@helios-lang/type-utils").TypeSchema; | ||
export type UplcData = import("@helios-lang/uplc").UplcData; | ||
export type UplcLoggingI = import("@helios-lang/uplc").UplcLoggingI; | ||
export type UplcProgram = import("@helios-lang/uplc").UplcProgram; | ||
@@ -79,0 +84,0 @@ export type UplcValue = import("@helios-lang/uplc").UplcValue; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
235581
5784
+ Added@helios-lang/ledger@0.5.13(transitive)
+ Added@helios-lang/ledger-babbage@0.3.14(transitive)
+ Added@helios-lang/ledger-conway@0.2.14(transitive)
- Removed@helios-lang/ledger@0.4.12(transitive)
- Removed@helios-lang/ledger-babbage@0.2.12(transitive)
- Removed@helios-lang/ledger-conway@0.1.12(transitive)
Updated@helios-lang/ledger@^0.5.1
Updated@helios-lang/uplc@^0.3.8