Skip to content

Commit

Permalink
Removed Blueprint all around
Browse files Browse the repository at this point in the history
  • Loading branch information
hrajchert committed Feb 20, 2024
1 parent 32dfa41 commit ca22762
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 111 deletions.
20 changes: 10 additions & 10 deletions packages/marlowe-template/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
* a Marlowe contract template.
*
* We call a contract template to a function that receives a set of parameters and returns a Marlowe contract.
* Manually sharing the contract parameters can be challenging, so this package aims to solve that by {@link Blueprint.encode | encoding}
* and {@link Blueprint.decode | decoding} the parameters as {@link @marlowe.io/runtime-core!index.Metadata}.
* Manually sharing the contract parameters can be challenging, so this package aims to solve that by {@link MarloweTemplate.encode | encoding}
* and {@link MarloweTemplate.decode | decoding} the parameters as {@link @marlowe.io/runtime-core!index.Metadata}.
*
* ```
* import { mkMarloweTemplate, TemplateParametersOf } from "@marlowe.io/blueprint";
* import { mkMarloweTemplate, TemplateParametersOf } from "@marlowe.io/marlowe-template";
* import { addressBech32 } from "@marlowe.io/runtime-core";
*
* const myTemplate = mkMarloweTemplate({
* name: "My template example",
* description: "This is a blueprint for a simple object",
* description: "This defines the parameters of a simple contract",
* params: [
* {
* name: "aString",
Expand Down Expand Up @@ -54,21 +54,21 @@
* @packageDocumentation
*/
export {
Blueprint,
MarloweTemplate,
mkMarloweTemplate,
MkBlueprintOptions,
MkTemplateOptions,
TemplateParametersOf,
Expand,
DecodingBlueprintError,
DecodingTemplateError,
} from "./template.js";
export {
BlueprintParam,
BlueprintType,
TemplateParam,
TemplateType,
StringParam,
ValueParam,
AddressParam,
DateParam,
BlueprintKeys,
TemplateKeys,
TypeOfParam,
TokenParam,
} from "./template-param.js";
57 changes: 31 additions & 26 deletions packages/marlowe-template/src/template-param.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import {
import { Token } from "@marlowe.io/language-core-v1";

/**
* This interface represents a string parameter in a {@link Blueprint}.
* This interface represents a string parameter in a {@link MarloweTemplate}.
*
* Strings are encoded as an array of strings of 64 characters or less to satisfy the ledger's constraints.
* @typeParam Name - The name of the parameter is used by different type functions to infer the Blueprint's ObjectParam.
* @typeParam Name - The name of the parameter is used by different type functions to infer the Template's ObjectParam.
* @category Template parameters
*/
export interface StringParam<Name extends string> {
Expand All @@ -21,10 +21,10 @@ export interface StringParam<Name extends string> {
}

/**
* This interface represents a value parameter in a {@link Blueprint}.
* This interface represents a value parameter in a {@link MarloweTemplate}.
*
* Values are encoded as bigints.
* @typeParam Name - The name of the parameter is used by different type functions to infer the Blueprint's ObjectParam.
* @typeParam Name - The name of the parameter is used by different type functions to infer the Template's ObjectParam.
* @category Template parameters
*/
export interface ValueParam<Name extends string> {
Expand All @@ -34,10 +34,10 @@ export interface ValueParam<Name extends string> {
}

/**
* This interface represents an AddressBech32 parameter in a {@link Blueprint}.
* This interface represents an AddressBech32 parameter in a {@link MarloweTemplate}.
*
* An Address is econded as a {@link StringParam}, and it is also guarded to be a valid {@link @marlowe.io/runtime-core!index.AddressBech32}.
* @typeParam Name - The name of the parameter is used by different type functions to infer the Blueprint's ObjectParam.
* @typeParam Name - The name of the parameter is used by different type functions to infer the Template's ObjectParam.
* @category Template parameters
*/
export interface AddressParam<Name extends string> {
Expand All @@ -47,10 +47,10 @@ export interface AddressParam<Name extends string> {
}

/**
* This interface represents a Date parameter in a {@link Blueprint}.
* This interface represents a Date parameter in a {@link MarloweTemplate}.
*
* Dates are encoded as a bigint representing the milliseconds since the epoch.
* @typeParam Name - The name of the parameter is used by different type functions to infer the Blueprint's ObjectParam.
* @typeParam Name - The name of the parameter is used by different type functions to infer the Template's ObjectParam.
* @category Template parameters
*/
export interface DateParam<Name extends string> {
Expand All @@ -60,10 +60,10 @@ export interface DateParam<Name extends string> {
}

/**
* This interface represents a token parameter in a {@link Blueprint}.
* This interface represents a token parameter in a {@link MarloweTemplate}.
*
* Tokens are encoded as a tuple of 2 {@link StringParam}. The first string is the policy id and the second one is the token name.
* @typeParam Name - The name of the parameter is used by different type functions to infer the Blueprint's ObjectParam.
* @typeParam Name - The name of the parameter is used by different type functions to infer the Template's ObjectParam.
* @category Template parameters
*/
export interface TokenParam<Name extends string> {
Expand All @@ -73,24 +73,25 @@ export interface TokenParam<Name extends string> {
}

/**
* This type represents one of the possible parameters in a {@link Blueprint}.
* This type represents one of the possible parameters in a {@link MarloweTemplate}.
*
* @See The documentation on each parameter to see how is encoded and decoded.
* @typeParam Name - The name of the parameter is used by different type functions to infer the Blueprint's ObjectParam.
* @typeParam Name - The name of the parameter is used by different type functions to infer the Template's ObjectParam.
* @category Template parameters
*/
export type BlueprintParam<Name extends string> =
export type TemplateParam<Name extends string> =
| StringParam<Name>
| ValueParam<Name>
| AddressParam<Name>
| DateParam<Name>
| TokenParam<Name>;

/**
* This type function assigns a type to each {@link TemplateParam}.
* @internal
* @category Type functions
*/
export type TypeOfParam<Param extends BlueprintParam<any>> =
export type TypeOfParam<Param extends TemplateParam<any>> =
Param extends StringParam<infer Name>
? string
: Param extends ValueParam<infer Name>
Expand All @@ -104,22 +105,26 @@ export type TypeOfParam<Param extends BlueprintParam<any>> =
: never;

/**
* This type function receives a list of {@link TemplateParam} and returns
* the union of the names of the parameters.
* @internal
* @category Type functions
*/
export type BlueprintKeys<T extends readonly BlueprintParam<any>[]> = {
[K in keyof T]: T[K] extends BlueprintParam<infer Name> ? Name : never;
export type TemplateKeys<T extends readonly TemplateParam<any>[]> = {
[K in keyof T]: T[K] extends TemplateParam<infer Name> ? Name : never;
}[number];

/**
* This type function receives a list of {@link TemplateParam} and returns
* an object whose keys are the param name and value is the {@link TypeOfParam}.
* @internal
* @category Type functions
*/
export type BlueprintType<T extends readonly BlueprintParam<any>[]> = {
[K in BlueprintKeys<T>]: TypeOfParam<Extract<T[number], { name: K }>>;
export type TemplateType<T extends readonly TemplateParam<any>[]> = {
[K in TemplateKeys<T>]: TypeOfParam<Extract<T[number], { name: K }>>;
};

function blueprintParamCodec<Param extends BlueprintParam<any>>(
function templateParamCodec<Param extends TemplateParam<any>>(
param: Param
): t.Mixed {
switch (param.type) {
Expand All @@ -141,21 +146,21 @@ function blueprintParamCodec<Param extends BlueprintParam<any>>(
/**
* @hidden
*/
export function blueprintParamsCodec<T extends readonly BlueprintParam<any>[]>(
blueprint: T
export function templateParamsCodec<T extends readonly TemplateParam<any>[]>(
template: T
): t.Mixed {
return t.tuple(blueprint.map(blueprintParamCodec) as any);
return t.tuple(template.map(templateParamCodec) as any);
}

/**
* @hidden
*/
export function blueprintParamsObjectGuard<
T extends readonly BlueprintParam<any>[],
>(blueprint: T): t.Mixed {
export function templateParamsObjectGuard<
T extends readonly TemplateParam<any>[],
>(template: T): t.Mixed {
return t.type(
Object.fromEntries(
blueprint.map((param) => [param.name, blueprintParamCodec(param)])
template.map((param) => [param.name, templateParamCodec(param)])
) as any
);
}
Loading

0 comments on commit ca22762

Please sign in to comment.