Skip to content

Commit

Permalink
refined NewContractAPI interface
Browse files Browse the repository at this point in the history
  • Loading branch information
nhenin committed Apr 20, 2024
1 parent dd24739 commit 0837b00
Show file tree
Hide file tree
Showing 13 changed files with 325 additions and 450 deletions.
4 changes: 2 additions & 2 deletions examples/nodejs/src/escrow-flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ async function main(action: "buy" | "sell", otherAddress: string, amount: number
console.log("Mediator: " + Mediator);
console.log("Amount: " + amount);

const contractInstance = await runtime.newContractAPI.createContract({
const contractInstance = await runtime.newContractAPI.create({
contract: escrow,
roles: { Buyer, Seller, Mediator },
});

console.log("Contract ID: " + contractInstance.contractId);
console.log("Contract ID: " + contractInstance.id);

console.log("Waiting for confirmation...");
await contractInstance.waitForConfirmation();
Expand Down
19 changes: 4 additions & 15 deletions examples/nodejs/src/experimental-features/source-map.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
import * as M from "fp-ts/lib/Map.js";

import {
ContractBundleMap,
bundleMapToList,
isAnnotated,
stripAnnotations,
} from "@marlowe.io/marlowe-object";
import {
ContractInstanceAPI,
CreateContractRequestBase,
RuntimeLifecycle,
} from "@marlowe.io/runtime-lifecycle/api";
import { ContractBundleMap, bundleMapToList, isAnnotated, stripAnnotations } from "@marlowe.io/marlowe-object";
import { ContractInstanceAPI, CreateContractRequestBase, RuntimeLifecycle } from "@marlowe.io/runtime-lifecycle/api";

import { ContractClosure, getContractClosure } from "./contract-closure.js";
import * as Core from "@marlowe.io/language-core-v1";
Expand Down Expand Up @@ -163,9 +154,7 @@ export interface SourceMap<T> {
closure: ContractClosure;
annotateHistory(history: SingleInputTx[]): SingleInputTx[];
playHistory(history: SingleInputTx[]): TransactionOutput;
createContract(
options: CreateContractRequestBase
): Promise<ContractInstanceAPI>;
createContract(options: CreateContractRequestBase): Promise<ContractInstanceAPI>;
contractInstanceOf(contractId: ContractId): Promise<boolean>;
}

Expand All @@ -188,7 +177,7 @@ export async function mkSourceMap<T>(
},
createContract: (options: CreateContractRequestBase) => {
const contract = stripAnnotations(closure.contracts.get(closure.main)!);
return lifecycle.newContractAPI.createContract({
return lifecycle.newContractAPI.create({
...options,
contract,
});
Expand Down
30 changes: 7 additions & 23 deletions examples/nodejs/src/marlowe-object-flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,7 @@ async function createContractMenu(lifecycle: RuntimeLifecycle, rewardAddress?: S

console.log(`Contract created with id ${contractId}`);

await waitIndicator(
lifecycle.wallet,
contractIdToTxId(contractInstance.contractId)
);
await waitIndicator(lifecycle.wallet, contractIdToTxId(contractInstance.id));

return contractMenu(lifecycle.wallet, contractInstance, scheme, sourceMap);
}
Expand Down Expand Up @@ -216,19 +213,10 @@ async function loadContractMenu(lifecycle: RuntimeLifecycle) {
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}`
);
console.log(
` * Release deadline: ${validationResult.scheme.releaseDeadline}`
);
const contractInstance = await lifecycle.newContractAPI.loadContract(cid);
return contractMenu(
lifecycle.wallet,
contractInstance,
validationResult.scheme,
validationResult.sourceMap
);
console.log(` * Deposit deadline: ${validationResult.scheme.depositDeadline}`);
console.log(` * Release deadline: ${validationResult.scheme.releaseDeadline}`);
const contractInstance = await lifecycle.newContractAPI.load(cid);
return contractMenu(lifecycle.wallet, contractInstance, validationResult.scheme, validationResult.sourceMap);
}

/**
Expand All @@ -243,16 +231,12 @@ async function contractMenu(
// Get and print the contract logical state.

const inputHistory = await contractInstance.getInputHistory();
const contractState = getState(
datetoTimeout(new Date()),
inputHistory,
sourceMap
);
const contractState = getState(datetoTimeout(new Date()), inputHistory, sourceMap);
if (contractState.type === "Closed") return;

printState(contractState, scheme);
// See what actions are applicable to the current contract state
const applicableActions = await contractInstance.computeApplicableActions();
const applicableActions = await contractInstance.evaluateApplicableActions();

const choices: Array<{
name: string;
Expand Down
8 changes: 6 additions & 2 deletions jsdelivr-npm-importmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,16 @@ const importMap = {
"https://cdn.jsdelivr.net/npm/@marlowe.io/[email protected]/dist/bundled/esm/version.js",
"@marlowe.io/language-examples":
"https://cdn.jsdelivr.net/npm/@marlowe.io/[email protected]/dist/bundled/esm/language-examples.js",
"@marlowe.io/language-examples/atomic-swap":
"https://cdn.jsdelivr.net/npm/@marlowe.io/[email protected]/dist/bundled/esm/atomic-swap.js",
"@marlowe.io/language-specification-client":
"https://cdn.jsdelivr.net/npm/@marlowe.io/[email protected]/dist/bundled/esm/language-specification-client.js",
"@marlowe.io/token-metadata-client":
"https://cdn.jsdelivr.net/npm/@marlowe.io/[email protected]/dist/bundled/esm/token-metadata-client.js",
"@marlowe.io/wallet": "https://cdn.jsdelivr.net/npm/@marlowe.io/[email protected]/dist/bundled/esm/wallet.js",
"@marlowe.io/wallet/api": "https://cdn.jsdelivr.net/npm/@marlowe.io/[email protected]/dist/bundled/esm/api.js",
"@marlowe.io/wallet":
"https://cdn.jsdelivr.net/npm/@marlowe.io/[email protected]/dist/bundled/esm/wallet.js",
"@marlowe.io/wallet/api":
"https://cdn.jsdelivr.net/npm/@marlowe.io/[email protected]/dist/bundled/esm/api.js",
"@marlowe.io/wallet/browser":
"https://cdn.jsdelivr.net/npm/@marlowe.io/[email protected]/dist/bundled/esm/browser.js",
"@marlowe.io/wallet/lucid":
Expand Down
8 changes: 4 additions & 4 deletions packages/language/core/v1/src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const mkEnvironment =
});

/**
* TODO: Comment
* Time interval in which the contract is executed. It is defined by a start and end time. The time is represented as a POSIX time.
* @see Appendix E.16 of the {@link https://github.com/input-output-hk/marlowe/releases/download/v3/Marlowe.pdf | Marlowe specification}
* @category Environment
*/
Expand All @@ -22,7 +22,7 @@ export interface TimeInterval {
}

/**
* TODO: Comment
* Guard for {@link TimeInterval}
* @see Appendix E.16 of the {@link https://github.com/input-output-hk/marlowe/releases/download/v3/Marlowe.pdf | Marlowe specification}
* @category Environment
*/
Expand All @@ -32,7 +32,7 @@ export const TimeIntervalGuard: t.Type<TimeInterval> = t.type({
});

/**
* TODO: Comment
* Time interval in which the contract is executed.
* @see Section 2.1.10 and appendix E.22 of the {@link https://github.com/input-output-hk/marlowe/releases/download/v3/Marlowe.pdf | Marlowe specification}
* @category Environment
*/
Expand All @@ -41,7 +41,7 @@ export interface Environment {
}

/**
* TODO: Comment
* Guard for {@link Environment}
* @see Section 2.1.10 and appendix E.22 of the {@link https://github.com/input-output-hk/marlowe/releases/download/v3/Marlowe.pdf | Marlowe specification}
* @category Environment
*/
Expand Down
Loading

0 comments on commit 0837b00

Please sign in to comment.