-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Experimental feature: Add @marlowe.io/marlowe-template package #184
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
86385ee
Fix metadata typing
hrajchert c70ef58
wip initial blueprint work
hrajchert 52c2067
wip: initial Blueprint class
hrajchert b959041
Added basic unit testing to blueprints
hrajchert f021508
Add first encode to Blueprint
hrajchert 24d8406
First version of blueprint decode
hrajchert f543af1
Improved blueprint typing, added name, description and tests
hrajchert c09a83e
Added AddressBech32 guard in Blueprint
hrajchert d22ed40
Modify marlowe-object-flow to use Blueprint
hrajchert 91fdb6b
Remove old experimenta-feature/metadata
hrajchert e49f93c
Fix build
hrajchert e4bc120
wip small refactor and documentation
hrajchert 5618a40
Small refactor and documentation regarding blueprints
hrajchert dcb4cf8
Add TokenParam to Blueprint
hrajchert a49629e
Add scriv entry
hrajchert c7bf939
Fix missing export
hrajchert 783f96d
Added preservedBrand io-ts helper
hrajchert 712adfc
Renamed package blueprint to marlowe-template
hrajchert bae26a5
Renamed TemplateParametersOf and mkMarloweTemplate
hrajchert 7725df3
Small codec refactor
hrajchert 32dfa41
Renamed some files from blueprint to template
hrajchert ca22762
Removed Blueprint all around
hrajchert ee4bd09
Renamed toMetadata and fromMetadata
hrajchert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
|
||
|
||
### General | ||
|
||
- Feat: Created a new experimental package `@marlowe.io/marlowe-template` that helps to share the parameters used in the creation of a Marlowe contract. ([PR-184](https://github.com/input-output-hk/marlowe-ts-sdk/pull/184)) | ||
|
||
|
||
### @marlowe.io/runtime-core | ||
|
||
- Feat: Added AddressBech32 validation using the lucid library ([PR-184](https://github.com/input-output-hk/marlowe-ts-sdk/pull/184)) | ||
- Fix: Added proper type guards to Metadata ([PR-184](https://github.com/input-output-hk/marlowe-ts-sdk/pull/184)) | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,14 +15,13 @@ import { Lucid, Blockfrost, C } from "lucid-cardano"; | |
import { readConfig } from "./config.js"; | ||
import { datetoTimeout, When } from "@marlowe.io/language-core-v1"; | ||
import { | ||
addressBech32, | ||
contractId, | ||
ContractId, | ||
stakeAddressBech32, | ||
StakeAddressBech32, | ||
Tags, | ||
TxId, | ||
} from "@marlowe.io/runtime-core"; | ||
import { Address } from "@marlowe.io/language-core-v1"; | ||
import { ContractBundleMap, lovelace, close } from "@marlowe.io/marlowe-object"; | ||
import { input, select } from "@inquirer/prompts"; | ||
import { RuntimeLifecycle } from "@marlowe.io/runtime-lifecycle/api"; | ||
|
@@ -32,12 +31,15 @@ import { | |
mkApplicableActionsFilter, | ||
} from "./experimental-features/applicable-inputs.js"; | ||
import arg from "arg"; | ||
import { splitAddress } from "./experimental-features/metadata.js"; | ||
import * as t from "io-ts/lib/index.js"; | ||
import { mkSourceMap, SourceMap } from "./experimental-features/source-map.js"; | ||
import { POSIXTime, posixTimeToIso8601 } from "@marlowe.io/adapter/time"; | ||
import { SingleInputTx } from "@marlowe.io/language-core-v1/semantics"; | ||
import * as ObjG from "@marlowe.io/marlowe-object/guards"; | ||
import { | ||
TemplateParametersOf, | ||
mkMarloweTemplate, | ||
} from "@marlowe.io/marlowe-template"; | ||
|
||
// When this script is called, start with main. | ||
main(); | ||
|
@@ -139,10 +141,12 @@ async function createContractMenu( | |
lifecycle: RuntimeLifecycle, | ||
rewardAddress?: StakeAddressBech32 | ||
) { | ||
const payee = await input({ | ||
message: "Enter the payee address", | ||
validate: bech32Validator, | ||
}); | ||
const payee = addressBech32( | ||
await input({ | ||
message: "Enter the payee address", | ||
validate: bech32Validator, | ||
}) | ||
); | ||
const amountStr = await input({ | ||
message: "Enter the payment amount in lovelaces", | ||
validate: positiveBigIntValidator, | ||
|
@@ -176,17 +180,18 @@ async function createContractMenu( | |
} | ||
|
||
const scheme = { | ||
payFrom: { address: walletAddress }, | ||
payTo: { address: payee }, | ||
payer: walletAddress, | ||
payee, | ||
amount, | ||
depositDeadline, | ||
releaseDeadline, | ||
}; | ||
const tags = mkDelayPaymentTags(scheme); | ||
const metadata = delayPaymentTemplate.toMetadata(scheme); | ||
const sourceMap = await mkSourceMap(lifecycle, mkDelayPayment(scheme)); | ||
const [contractId, txId] = await sourceMap.createContract({ | ||
stakeAddress: rewardAddress, | ||
tags, | ||
tags: { DELAY_PAYMENT_VERSION: "2" }, | ||
metadata, | ||
}); | ||
|
||
console.log(`Contract created with id ${contractId}`); | ||
|
@@ -209,7 +214,7 @@ async function loadContractMenu(lifecycle: RuntimeLifecycle) { | |
const cid = contractId(cidStr); | ||
// Then we make sure that contract id is an instance of our delayed payment contract | ||
const validationResult = await validateExistingContract(lifecycle, cid); | ||
if (validationResult === "InvalidTags") { | ||
if (validationResult === "InvalidMarloweTemplate") { | ||
console.log("Invalid contract, it does not have the expected tags"); | ||
return; | ||
} | ||
|
@@ -222,8 +227,8 @@ async function loadContractMenu(lifecycle: RuntimeLifecycle) { | |
|
||
// If it is, we print the contract details and go to the contract menu | ||
console.log("Contract details:"); | ||
console.log(` * Pay from: ${validationResult.scheme.payFrom.address}`); | ||
console.log(` * Pay to: ${validationResult.scheme.payTo.address}`); | ||
console.log(` * Pay from: ${validationResult.scheme.payer}`); | ||
console.log(` * Pay to: ${validationResult.scheme.payee}`); | ||
console.log(` * Amount: ${validationResult.scheme.amount} lovelaces`); | ||
console.log( | ||
` * Deposit deadline: ${validationResult.scheme.depositDeadline}` | ||
|
@@ -245,7 +250,7 @@ async function loadContractMenu(lifecycle: RuntimeLifecycle) { | |
*/ | ||
async function contractMenu( | ||
lifecycle: RuntimeLifecycle, | ||
scheme: DelayPaymentScheme, | ||
scheme: DelayPaymentParameters, | ||
sourceMap: SourceMap<DelayPaymentAnnotations>, | ||
contractId: ContractId | ||
): Promise<void> { | ||
|
@@ -374,32 +379,45 @@ async function mainLoop( | |
// #endregion | ||
|
||
// #region Delay Payment Contract | ||
const delayPaymentTemplate = mkMarloweTemplate({ | ||
name: "Delayed payment", | ||
description: | ||
"In a delay payment, a `payer` transfer an `amount` of ADA to the `payee` which can be redeemed after a `releaseDeadline`. While the payment is held by the contract, it can be staked to the payer, to generate pasive income while the payee has the guarantees that the money will be released.", | ||
params: [ | ||
{ | ||
name: "payer", | ||
description: "Who is making the payment", | ||
type: "address", | ||
}, | ||
{ | ||
name: "payee", | ||
description: "Who is receiving the payment", | ||
type: "address", | ||
}, | ||
{ | ||
name: "amount", | ||
description: "The amount of lovelaces to be paid", | ||
type: "value", | ||
}, | ||
{ | ||
name: "depositDeadline", | ||
description: | ||
"The deadline for the payment to be made. If the payment is not made by this date, the contract can be closed", | ||
type: "date", | ||
}, | ||
{ | ||
name: "releaseDeadline", | ||
description: | ||
"A date after the payment can be released to the receiver. NOTE: An empty transaction must be done to close the contract", | ||
type: "date", | ||
}, | ||
] as const, | ||
}); | ||
|
||
/** | ||
* These are the parameters of the contract | ||
*/ | ||
interface DelayPaymentScheme { | ||
/** | ||
* Who is making the delayed payment | ||
*/ | ||
payFrom: Address; | ||
/** | ||
* Who is receiving the payment | ||
*/ | ||
payTo: Address; | ||
/** | ||
* The amount of lovelaces to be paid | ||
*/ | ||
amount: bigint; | ||
/** | ||
* The deadline for the payment to be made. If the payment is not made by this date, the contract can be closed | ||
*/ | ||
depositDeadline: Date; | ||
/** | ||
* A date after the payment can be released to the receiver. | ||
* NOTE: An empty transaction must be done to close the contract | ||
*/ | ||
releaseDeadline: Date; | ||
} | ||
type DelayPaymentParameters = TemplateParametersOf<typeof delayPaymentTemplate>; | ||
|
||
type DelayPaymentAnnotations = | ||
| "initialDeposit" | ||
|
@@ -415,7 +433,7 @@ const DelayPaymentAnnotationsGuard = t.union([ | |
]); | ||
|
||
function mkDelayPayment( | ||
scheme: DelayPaymentScheme | ||
scheme: DelayPaymentParameters | ||
): ContractBundleMap<DelayPaymentAnnotations> { | ||
return { | ||
main: "initial-deposit", | ||
|
@@ -436,10 +454,10 @@ function mkDelayPayment( | |
when: [ | ||
{ | ||
case: { | ||
party: scheme.payFrom, | ||
deposits: scheme.amount, | ||
party: { address: scheme.payer }, | ||
deposits: BigInt(scheme.amount), | ||
of_token: lovelace, | ||
into_account: scheme.payTo, | ||
into_account: { address: scheme.payee }, | ||
}, | ||
then: { | ||
ref: "release-funds", | ||
|
@@ -500,12 +518,10 @@ type Closed = { | |
result: "Missed deposit" | "Payment released"; | ||
}; | ||
|
||
function printState(state: DelayPaymentState, scheme: DelayPaymentScheme) { | ||
function printState(state: DelayPaymentState, scheme: DelayPaymentParameters) { | ||
switch (state.type) { | ||
case "InitialState": | ||
console.log( | ||
`Waiting ${scheme.payFrom.address} to deposit ${scheme.amount}` | ||
); | ||
console.log(`Waiting ${scheme.payer} to deposit ${scheme.amount}`); | ||
break; | ||
case "PaymentDeposited": | ||
console.log( | ||
|
@@ -562,55 +578,11 @@ function getState( | |
|
||
// #endregion | ||
|
||
const mkDelayPaymentTags = (schema: DelayPaymentScheme) => { | ||
const tag = "DELAY_PYMNT-1"; | ||
const tags = {} as Tags; | ||
|
||
tags[`${tag}-from-0`] = splitAddress(schema.payFrom)[0]; | ||
tags[`${tag}-from-1`] = splitAddress(schema.payFrom)[1]; | ||
tags[`${tag}-to-0`] = splitAddress(schema.payTo)[0]; | ||
tags[`${tag}-to-1`] = splitAddress(schema.payTo)[1]; | ||
tags[`${tag}-amount`] = schema.amount; | ||
tags[`${tag}-deposit`] = schema.depositDeadline; | ||
tags[`${tag}-release`] = schema.releaseDeadline; | ||
return tags; | ||
}; | ||
|
||
const extractSchemeFromTags = ( | ||
tags: unknown | ||
): DelayPaymentScheme | undefined => { | ||
const tagsGuard = t.type({ | ||
"DELAY_PYMNT-1-from-0": t.string, | ||
"DELAY_PYMNT-1-from-1": t.string, | ||
"DELAY_PYMNT-1-to-0": t.string, | ||
"DELAY_PYMNT-1-to-1": t.string, | ||
"DELAY_PYMNT-1-amount": t.bigint, | ||
"DELAY_PYMNT-1-deposit": t.string, | ||
"DELAY_PYMNT-1-release": t.string, | ||
}); | ||
|
||
if (!tagsGuard.is(tags)) { | ||
return; | ||
} | ||
|
||
return { | ||
payFrom: { | ||
address: `${tags["DELAY_PYMNT-1-from-0"]}${tags["DELAY_PYMNT-1-from-1"]}`, | ||
}, | ||
payTo: { | ||
address: `${tags["DELAY_PYMNT-1-to-0"]}${tags["DELAY_PYMNT-1-to-1"]}`, | ||
}, | ||
amount: tags["DELAY_PYMNT-1-amount"], | ||
depositDeadline: new Date(tags["DELAY_PYMNT-1-deposit"]), | ||
releaseDeadline: new Date(tags["DELAY_PYMNT-1-release"]), | ||
}; | ||
}; | ||
Comment on lines
-565
to
-607
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these functions that manually encoded and decoded the parameters as tags are now provided by the |
||
|
||
type ValidationResults = | ||
| "InvalidTags" | ||
| "InvalidMarloweTemplate" | ||
| "InvalidContract" | ||
| { | ||
scheme: DelayPaymentScheme; | ||
scheme: DelayPaymentParameters; | ||
sourceMap: SourceMap<DelayPaymentAnnotations>; | ||
}; | ||
|
||
|
@@ -629,10 +601,10 @@ async function validateExistingContract( | |
contractId, | ||
}); | ||
|
||
const scheme = extractSchemeFromTags(contractDetails.tags); | ||
const scheme = delayPaymentTemplate.fromMetadata(contractDetails.metadata); | ||
|
||
if (!scheme) { | ||
return "InvalidTags"; | ||
return "InvalidMarloweTemplate"; | ||
} | ||
|
||
// If the contract seems to be an instance of the contract we want (meanin, we were able | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before we manually defined the DelayPaymentScheme, now we manually define the
delayPaymentTemplate
and theScheme
is inferred (and renamed as DelayPaymentParameters)