Skip to content

Commit

Permalink
wip apply merkleized inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
hrajchert committed Dec 15, 2023
1 parent e53bc88 commit b05e8eb
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 115 deletions.
103 changes: 0 additions & 103 deletions examples/nodejs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion examples/nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
},
"dependencies": {
"@inquirer/prompts": "^3.3.0",
"@marlowe.io/language-core-v1": "^0.2.0-alpha-22",
"arg": "^5.0.2"
}
}
45 changes: 34 additions & 11 deletions examples/nodejs/src/marlowe-object-flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import { mkLucidWallet, WalletAPI } from "@marlowe.io/wallet";
import { mkRuntimeLifecycle } from "@marlowe.io/runtime-lifecycle";
import { Lucid, Blockfrost, C } from "lucid-cardano";
import { readConfig } from "./config.js";
import { datetoTimeout } from "@marlowe.io/language-core-v1";
import { datetoTimeout, getNextTimeout, Timeout } from "@marlowe.io/language-core-v1";
import {
addressBech32,
contractId,
ContractId,
contractIdToTxId,
Tags,
transactionWitnessSetTextEnvelope,
TxId,
unAddressBech32,
} from "@marlowe.io/runtime-core";
import { Address } from "@marlowe.io/language-core-v1";
import { Bundle, Label, lovelace } from "@marlowe.io/marlowe-object";
Expand Down Expand Up @@ -108,7 +108,7 @@ async function createContractMenu(lifecycle: RuntimeLifecycle) {
);

const [contractId, txId] = await createContract(lifecycle, {
payFrom: { address: unAddressBech32(walletAddress) },
payFrom: { address: walletAddress },
payTo: { address: payee },
amount,
depositDeadline,
Expand All @@ -118,20 +118,43 @@ async function createContractMenu(lifecycle: RuntimeLifecycle) {
console.log(`Contract created with id ${contractId}`);
await waitIndicator(lifecycle.wallet, txId);

await contractMenu();
await contractMenu(lifecycle, contractId);
}

async function loadContractMenu() {
const answer = await input({
async function loadContractMenu(lifecycle: RuntimeLifecycle) {
const cid = await input({
message: "Enter the contractId",
});
console.log(answer);
await contractMenu();
await contractMenu(lifecycle, contractId(cid));
}

// async function contractMenu(contractId: ContractId) {
async function contractMenu() {
async function contractMenu(
lifecycle: RuntimeLifecycle,
contractId: ContractId
) {
console.log("TODO: print contract state");
const contractDetails = await lifecycle.restClient.getContractById(
contractId
);
const now = datetoTimeout(new Date());

if (contractDetails.currentContract._tag === "None") {
console.log("DEBUG: current contract none");
}

const currentContract =
contractDetails.currentContract._tag === "None"
? contractDetails.initialContract
: contractDetails.currentContract.value;
const nextTimeout = getNextTimeout(currentContract, now);
const oneDayFrom = (time: Timeout) => time + 24n * 60n * 60n * 1000n; // in milliseconds
const applicableInputs = await lifecycle.contracts.getApplicableInputs(
contractId,
{ timeInterval: { from: now, to: nextTimeout ?? oneDayFrom(now)} }
);
console.log("applicable inputs");
console.log(applicableInputs);

const answer = await select({
message: "Contract menu",
choices: [
Expand Down Expand Up @@ -159,7 +182,7 @@ async function mainLoop(lifecycle: RuntimeLifecycle) {
await createContractMenu(lifecycle);
break;
case "load":
await loadContractMenu();
await loadContractMenu(lifecycle);
break;
case "exit":
process.exit(0);
Expand Down
32 changes: 32 additions & 0 deletions packages/language/core/v1/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,35 @@ export function matchContract<T>(matcher: Partial<ContractMatcher<T>>) {
}
};
}
// Copied from semantic module, maybe we want to move this to a common place? An adaptor bigint maybe?
const minBigint = (a: bigint, b: bigint): bigint => (a < b ? a : b);

/**
* This function calculates the next timeout of a contract after a given minTime.
* @param minTime Normally the current time, but it represents any time for which you want to see what is the next timeout after that.
* @param contract The contract to analyze
* @returns The next timeout after minTime, or undefined if there is no timeout after minTime.
* @category Introspection
*/
export function getNextTimeout(contract: Contract, minTime: Timeout): Timeout | undefined {
return matchContract<Timeout | undefined>({
close: () => undefined,
pay: (pay) => getNextTimeout(pay.then, minTime),
if: (ifContract) => {
const thenTimeout = getNextTimeout(ifContract.then, minTime);
const elseTimeout = getNextTimeout(ifContract.else, minTime);
return thenTimeout && elseTimeout
? minBigint(thenTimeout, elseTimeout)
: thenTimeout || elseTimeout;
},
when: (whenContract) => {
if (minTime > whenContract.timeout) {
return getNextTimeout(whenContract.timeout_continuation, minTime);
} else {
return whenContract.timeout;
}
},
let: (letContract) => getNextTimeout(letContract.then, minTime),
assert: (assertContract) => getNextTimeout(assertContract.then, minTime)
})(contract)
}
1 change: 1 addition & 0 deletions packages/language/core/v1/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export {
datetoTimeout,
timeoutToDate,
Timeout,
getNextTimeout
} from "./contract.js";
export { Environment, mkEnvironment, TimeInterval } from "./environment.js";

Expand Down

0 comments on commit b05e8eb

Please sign in to comment.