diff --git a/examples/nodejs/src/experimental-features/applicable-inputs.ts b/examples/nodejs/src/experimental-features/applicable-inputs.ts index 92def106..b188d9a1 100644 --- a/examples/nodejs/src/experimental-features/applicable-inputs.ts +++ b/examples/nodejs/src/experimental-features/applicable-inputs.ts @@ -40,6 +40,7 @@ import { import { AddressBech32, ContractId, PolicyId } from "@marlowe.io/runtime-core"; import { RestClient } from "@marlowe.io/runtime-rest-client"; import { WalletAPI } from "@marlowe.io/wallet"; +import * as Big from "@marlowe.io/adapter/bigint"; import { Monoid } from "fp-ts/lib/Monoid.js"; import * as R from "fp-ts/lib/Record.js"; type ActionApplicant = Party | "anybody"; @@ -318,8 +319,6 @@ const accumulatorFromNotify = (action: CanNotify) => { }; }; // TODO: Move to adapter -const minBigint = (a: bigint, b: bigint): bigint => (a < b ? a : b); -const maxBigint = (a: bigint, b: bigint): bigint => (a > b ? a : b); function mergeBounds(bounds: Bound[]): Bound[] { const mergedBounds: Bound[] = []; @@ -333,7 +332,7 @@ function mergeBounds(bounds: Bound[]): Bound[] { currentBound = {...bound}; } else { if (bound.from <= currentBound.to) { - currentBound.to = maxBigint(currentBound.to, bound.to); + currentBound.to = Big.max(currentBound.to, bound.to); } else { mergedBounds.push(currentBound); currentBound = {...bound}; diff --git a/jsdelivr-npm-importmap.js b/jsdelivr-npm-importmap.js index 54b79e36..3ad1d631 100644 --- a/jsdelivr-npm-importmap.js +++ b/jsdelivr-npm-importmap.js @@ -4,6 +4,8 @@ const importMap = { "https://cdn.jsdelivr.net/npm/@marlowe.io/adapter@0.3.0-beta-rc1/dist/bundled/esm/adapter.js", "@marlowe.io/adapter/assoc-map": "https://cdn.jsdelivr.net/npm/@marlowe.io/adapter@0.3.0-beta-rc1/dist/bundled/esm/assoc-map.js", + "@marlowe.io/adapter/bigint": + "https://cdn.jsdelivr.net/npm/@marlowe.io/adapter@0.3.0-beta-rc1/dist/bundled/esm/bigint.js", "@marlowe.io/adapter/codec": "https://cdn.jsdelivr.net/npm/@marlowe.io/adapter@0.3.0-beta-rc1/dist/bundled/esm/codec.js", "@marlowe.io/adapter/deep-equal": diff --git a/packages/adapter/package.json b/packages/adapter/package.json index 95acf1c3..f6eeba52 100644 --- a/packages/adapter/package.json +++ b/packages/adapter/package.json @@ -66,7 +66,13 @@ "import": "./dist/esm/deep-equal.js", "require": "./dist/bundled/cjs/deep-equal.cjs", "types": "./dist/esm/deep-equal.d.ts" + }, + "./bigint": { + "import": "./dist/esm/bigint.js", + "require": "./dist/bundled/cjs/bigint.cjs", + "types": "./dist/esm/bigint.d.ts" } + }, "dependencies": { "date-fns": "2.29.3", diff --git a/packages/adapter/src/bigint.ts b/packages/adapter/src/bigint.ts new file mode 100644 index 00000000..691c2118 --- /dev/null +++ b/packages/adapter/src/bigint.ts @@ -0,0 +1,6 @@ +/** + * Utility functions for bigint. + */ + +export const min = (a: bigint, b: bigint): bigint => (a < b ? a : b); +export const max = (a: bigint, b: bigint): bigint => (a > b ? a : b); diff --git a/packages/language/core/v1/src/semantics.ts b/packages/language/core/v1/src/semantics.ts index ee794299..f9b3c9c3 100644 --- a/packages/language/core/v1/src/semantics.ts +++ b/packages/language/core/v1/src/semantics.ts @@ -87,6 +87,7 @@ import { } from "./value-and-observation.js"; import * as G from "./guards.js"; import { POSIXTime } from "@marlowe.io/adapter/time"; +import * as Big from "@marlowe.io/adapter/bigint"; export { Payment, @@ -383,10 +384,6 @@ function giveMoney( ]; } -// TODO: Move to adapter -const minBigint = (a: bigint, b: bigint): bigint => (a < b ? a : b); -const maxBigint = (a: bigint, b: bigint): bigint => (a > b ? a : b); - /** * @hidden */ @@ -433,7 +430,7 @@ function reduceContractStep( }); } const balance = moneyInAccount(from_account, token, state.accounts); - const paidAmount = minBigint(amountToPay, balance); + const paidAmount = Big.min(amountToPay, balance); const newBalance = balance - paidAmount; const newAccs = updateMoneyInAccount( from_account, @@ -929,7 +926,7 @@ function fixInterval( return invalidInterval(interval.from, interval.to); if (interval.to < state.minTime) return intervalInPastError(interval.from, interval.to, state.minTime); - const newFrom = maxBigint(interval.from, state.minTime); + const newFrom = Big.max(interval.from, state.minTime); const environment = { timeInterval: { from: newFrom, to: interval.to } }; return intervalTrimmed({ environment,