Skip to content
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

implement V3 types and their instances #37

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions src/Lib/Ratio.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/**
* TypeScript implementation for {@link https://github.com/IntersectMBO/plutus/blob/1.36.0.0/plutus-tx/src/PlutusTx/Ratio.hs}
*
* @module plutus-ledger-api/Ratio.js
*/
import {
caseFieldWithValue,
type Eq,
eqInteger,
type Integer,
type Json,
jsonInteger,
} from "prelude";
import type { IsPlutusData } from "./PlutusData.js";
import { isPlutusDataInteger, isPlutusDataPairWithTag } from "./V1.js";

// TODO(chfanghr): Maintain the invariants

/**
* {@link Rational} represents an arbitrary-precision ratio.
*
* @see {@link https://github.com/IntersectMBO/plutus/blob/1.36.0.0/plutus-tx/src/PlutusTx/Ratio.hs#L69}
*/
export type Rational = {
numerator: Integer;
denominator: Integer;
};

/**
* {@link Eq} instance for {@link Rational}
*/
export const eqRational: Eq<Rational> = {
eq: (l, r) => {
return (
eqInteger.eq(l.numerator, r.numerator) &&
eqInteger.eq(l.denominator, r.denominator)
);
},

neq: (l, r) => {
return (
eqInteger.neq(l.numerator, r.numerator) ||
eqInteger.neq(l.denominator, r.denominator)
);
},
};

/**
* {@link Json} instance for {@link Rational}
*/
export const jsonRational: Json<Rational> = {
toJson: (rational) => {
return {
numerator: jsonInteger.toJson(rational.numerator),
denominator: jsonInteger.toJson(rational.denominator),
};
},
fromJson: (value) => {
const numerator = caseFieldWithValue(
"numerator",
jsonInteger.fromJson,
value,
);
const denominator = caseFieldWithValue(
"denominator",
jsonInteger.fromJson,
value,
);

return {
numerator,
denominator,
};
},
};

/**
* {@link IsPlutusData} instance for {@link Rational}
*/
export const isPlutusDataRational: IsPlutusData<Rational> = {
toData: (rational) => {
return isPlutusDataPairWithTag(
isPlutusDataInteger,
isPlutusDataInteger,
).toData([rational.numerator, rational.denominator]);
},
fromData: (value) => {
const [numerator, denominator] = isPlutusDataPairWithTag(
isPlutusDataInteger,
isPlutusDataInteger,
).fromData(value);

return {
numerator,
denominator,
};
},
};
5 changes: 5 additions & 0 deletions src/Lib/V1/Value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,8 @@ export const adaSymbol: CurrencySymbol = Uint8Array.from([]) as CurrencySymbol;
* @see {@link https://github.com/input-output-hk/plutus/blob/1.16.0.0/plutus-ledger-api/src/PlutusLedgerApi/V1/Value.hs#L152-L155}
*/
export const adaToken: TokenName = Uint8Array.from([]) as TokenName;

/**
* @see {@link https://github.com/IntersectMBO/plutus/blob/1.36.0.0/plutus-ledger-api/src/PlutusLedgerApi/V1/Value.hs#L598}
*/
export type Lovelace = Integer;
15 changes: 15 additions & 0 deletions src/Lib/V3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* TypeScript types for {@link https://github.com/IntersectMBO/plutus/blob/1.36.0.0/plutus-ledger-api/src/PlutusLedgerApi/V3/}
*
* @example
* ```ts
* import * as PlaV3 from 'plutus-ledger-api/V3.js'
*
* // ...
* ```
*
* @module plutus-ledger-api/V3.js
*/

export * from "./V3/Contexts.js";
export * from "./Prelude/Instances.js";
Loading