Skip to content

Commit

Permalink
Move max and min bigint to adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
hrajchert committed Jan 8, 2024
1 parent 99ffcbd commit c90e37d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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[] = [];
Expand All @@ -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};
Expand Down
2 changes: 2 additions & 0 deletions jsdelivr-npm-importmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
6 changes: 6 additions & 0 deletions packages/adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 6 additions & 0 deletions packages/adapter/src/bigint.ts
Original file line number Diff line number Diff line change
@@ -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);
9 changes: 3 additions & 6 deletions packages/language/core/v1/src/semantics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit c90e37d

Please sign in to comment.