Skip to content

Commit 7929b33

Browse files
author
jared
committed
Replaced ToData/FromData with IsPlutusData
1 parent cd5e959 commit 7929b33

34 files changed

+578
-819
lines changed

runtimes/typescript/lbr-plutus/src/LambdaBuffers/Runtime/AssocMap.ts

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import * as LbPrelude from "lbr-prelude";
22
import * as LbPreludeInstances from "./Prelude/Instances.js";
33
import type { Bool, Eq, Json, List, Maybe } from "lbr-prelude";
4-
import { FromDataError } from "./PlutusData.js";
5-
import type { FromData, PlutusData, ToData } from "./PlutusData.js";
4+
import { IsPlutusDataError } from "./PlutusData.js";
5+
import type { IsPlutusData, PlutusData } from "./PlutusData.js";
66

77
/**
88
* {@link Map | `Map<K,V>`} is an mapping from keys `K` to values `V` where `K` only needs an
@@ -60,54 +60,50 @@ export function jsonMap<K, V>(dictK: Json<K>, dictV: Json<V>): Json<Map<K, V>> {
6060
}
6161

6262
/**
63-
* {@link ToData} instance for {@link Map}
63+
* {@link IsPlutusData} instance for {@link Map}
64+
*
65+
* @remarks
66+
* The `fromData` copies the Haskell definition which uses {@link fromList} that does
67+
* _not_ verify uniqueness of the keys.
6468
*/
65-
export function toDataMap<K, V>(
66-
dictK: ToData<K>,
67-
dictV: ToData<V>,
68-
): ToData<Map<K, V>> {
69+
export function isPlutusDataMap<K, V>(
70+
dictK: IsPlutusData<K>,
71+
dictV: IsPlutusData<V>,
72+
): IsPlutusData<Map<K, V>> {
6973
return {
7074
toData: (arg) => {
7175
return {
7276
name: "Map",
7377
fields: arg.map((kv) => {
74-
const kvData = LbPreludeInstances.toDataPairWithoutTag(dictK, dictV)
78+
const kvData = LbPreludeInstances.isPlutusDataPairWithoutTag(
79+
dictK,
80+
dictV,
81+
)
7582
.toData(kv);
7683
if (kvData.name === "List") {
7784
return kvData.fields as [PlutusData, PlutusData];
7885
} else {
7986
throw new Error(
80-
"Internal error: toDataPairWithoutTag didn't return a list",
87+
"Internal error: isPlutusDataPairWithoutTag didn't return a list",
8188
);
8289
}
8390
}),
8491
};
8592
},
86-
};
87-
}
8893

89-
/**
90-
* {@link FromData} instance for {@link Map}
91-
*
92-
* @remarks
93-
* This copies the Haskell definition which uses {@link fromList} that does
94-
* _not_ verify uniqueness of the keys.
95-
*/
96-
export function fromDataMap<K, V>(
97-
dictK: FromData<K>,
98-
dictV: FromData<V>,
99-
): FromData<Map<K, V>> {
100-
return {
10194
fromData: (plutusData) => {
10295
switch (plutusData.name) {
10396
case "Map":
10497
return fromList(plutusData.fields.map((kvData) => {
105-
const kv = LbPreludeInstances.fromDataPairWithoutTag(dictK, dictV)
98+
const kv = LbPreludeInstances.isPlutusDataPairWithoutTag(
99+
dictK,
100+
dictV,
101+
)
106102
.fromData({ name: "List", fields: kvData });
107103
return kv;
108104
}));
109105
default:
110-
throw new FromDataError("Expected Map but got " + plutusData);
106+
throw new IsPlutusDataError("Expected Map but got " + plutusData);
111107
}
112108
},
113109
};

runtimes/typescript/lbr-plutus/src/LambdaBuffers/Runtime/PlutusData.ts

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,19 @@ export type PlutusData =
1616
| { name: "Integer"; fields: Integer };
1717

1818
/**
19-
* {@link FromDataError} is thrown when `fromData` fails in the type class {@link FromData}
19+
* {@link IsPlutusDataError} is thrown when `fromData` fails in the type class {@link IsPlutusData}
2020
*/
21-
export class FromDataError extends Error {
21+
export class IsPlutusDataError extends Error {
2222
constructor(message: string) {
2323
super(message);
2424
}
2525
}
2626

2727
/**
28-
* {@link ToData} is a type class to translate to {@link PlutusData}
28+
* {@link IsPlutusData} is a type class to translate to {@link PlutusData}
2929
*/
30-
export interface ToData<A> {
30+
export interface IsPlutusData<A> {
3131
readonly toData: (arg: Readonly<A>) => PlutusData;
32-
}
33-
34-
/**
35-
* {@link FromData} is a type class to translate from {@link PlutusData}
36-
*/
37-
export interface FromData<A> {
3832
readonly fromData: (arg: Readonly<PlutusData>) => A;
3933
}
4034

@@ -87,16 +81,10 @@ export const eqPlutusData: Eq<PlutusData> = {
8781
};
8882

8983
/**
90-
* {@link ToData} instance for {@link PlutusData}
84+
* {@link IsPlutusData} instance for {@link PlutusData}
9185
*/
92-
export const toDataPlutusData: ToData<PlutusData> = {
86+
export const isPlutusDataPlutusData: IsPlutusData<PlutusData> = {
9387
toData: (arg) => arg,
94-
};
95-
96-
/**
97-
* {@link FromData} instance for {@link PlutusData}
98-
*/
99-
export const fromDataPlutusData: FromData<PlutusData> = {
10088
fromData: (arg) => arg,
10189
};
10290

runtimes/typescript/lbr-plutus/src/LambdaBuffers/Runtime/PlutusLedgerApi/V1/Address.ts

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { FromDataError } from "../../PlutusData.js";
2-
import type { FromData, ToData } from "../../PlutusData.js";
1+
import { IsPlutusDataError } from "../../PlutusData.js";
2+
import type { IsPlutusData } from "../../PlutusData.js";
33
import type { Eq, Json, Maybe } from "lbr-prelude";
44
import * as LbPrelude from "lbr-prelude";
55
import * as LbPreludeInstances from "../../Prelude/Instances.js";
@@ -78,36 +78,31 @@ export const jsonAddress: Json<Address> = {
7878
};
7979

8080
/**
81-
* {@link ToData} instance for {@link Address}
81+
* {@link IsPlutusData} instance for {@link Address}
8282
*/
83-
export const toDataAddress: ToData<Address> = {
83+
export const isPlutusDataAddress: IsPlutusData<Address> = {
8484
toData: (address) => {
8585
return {
8686
name: "Constr",
8787
fields: [0n, [
88-
LbCredential.toDataCredential.toData(address.addressCredential),
88+
LbCredential.isPlutusDataCredential.toData(address.addressCredential),
8989
LbPreludeInstances
90-
.toDataMaybe(LbCredential.toDataStakingCredential)
90+
.isPlutusDataMaybe(LbCredential.isPlutusDataStakingCredential)
9191
.toData(address.addressStakingCredential),
9292
]],
9393
};
9494
},
95-
};
9695

97-
/**
98-
* {@link FromData} instance for {@link Address}
99-
*/
100-
export const fromDataAddress: FromData<Address> = {
10196
fromData: (plutusData) => {
10297
switch (plutusData.name) {
10398
case "Constr": {
10499
if (plutusData.fields[0] === 0n && plutusData.fields[1].length === 2) {
105100
return {
106-
addressCredential: LbCredential.fromDataCredential.fromData(
101+
addressCredential: LbCredential.isPlutusDataCredential.fromData(
107102
plutusData.fields[1][0]!,
108103
),
109-
addressStakingCredential: LbPreludeInstances.fromDataMaybe(
110-
LbCredential.fromDataStakingCredential,
104+
addressStakingCredential: LbPreludeInstances.isPlutusDataMaybe(
105+
LbCredential.isPlutusDataStakingCredential,
111106
).fromData(plutusData.fields[1][1]!),
112107
};
113108
} else {
@@ -117,6 +112,6 @@ export const fromDataAddress: FromData<Address> = {
117112
default:
118113
break;
119114
}
120-
throw new FromDataError("Unexpected data");
115+
throw new IsPlutusDataError("Unexpected data");
121116
},
122117
};

runtimes/typescript/lbr-plutus/src/LambdaBuffers/Runtime/PlutusLedgerApi/V1/Bytes.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import * as LbPrelude from "lbr-prelude";
22
import { JsonError } from "lbr-prelude";
3-
import { FromDataError } from "../../PlutusData.js";
3+
import { IsPlutusDataError } from "../../PlutusData.js";
44
import * as LbHex from "../../Hex.js";
5-
import type { FromData, ToData } from "../../PlutusData.js";
5+
import type { IsPlutusData } from "../../PlutusData.js";
66
import type { Bytes, Json } from "lbr-prelude";
77

88
// https://github.com/input-output-hk/plutus/blob/1.16.0.0/plutus-ledger-api/src/PlutusLedgerApi/V1/Bytes.hs
@@ -37,24 +37,18 @@ export const jsonLedgerBytes: Json<LedgerBytes> = {
3737
};
3838

3939
/**
40-
* {@link ToData} instance for {@link LedgerBytes}
40+
* {@link IsPlutusData} instance for {@link LedgerBytes}
4141
*/
42-
export const toDataLedgerBytes: ToData<LedgerBytes> = {
42+
export const isPlutusDataLedgerBytes: IsPlutusData<LedgerBytes> = {
4343
toData: (bytes) => {
4444
return { name: "Bytes", fields: bytes };
4545
},
46-
};
47-
48-
/**
49-
* {@link FromData} instance for {@link LedgerBytes}
50-
*/
51-
export const fromDataLedgerBytes: FromData<LedgerBytes> = {
5246
fromData: (plutusData) => {
5347
switch (plutusData.name) {
5448
case "Bytes":
5549
return plutusData.fields;
5650
default:
57-
throw new FromDataError("Expected bytes but got " + plutusData);
51+
throw new IsPlutusDataError("Expected bytes but got " + plutusData);
5852
}
5953
},
6054
};

runtimes/typescript/lbr-plutus/src/LambdaBuffers/Runtime/PlutusLedgerApi/V1/Credential.ts

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { FromDataError } from "../../PlutusData.js";
2-
import type { FromData, ToData } from "../../PlutusData.js";
1+
import { IsPlutusDataError } from "../../PlutusData.js";
2+
import type { IsPlutusData } from "../../PlutusData.js";
33
import type { Integer } from "lbr-prelude";
44
import type { PubKeyHash } from "./Crypto.js";
55
import type { ScriptHash } from "./Scripts.js";
@@ -117,60 +117,57 @@ export const jsonStakingCredential: Json<StakingCredential> = {
117117
};
118118

119119
/**
120-
* {@link ToData} instance for {@link StakingCredential}
120+
* {@link IsPlutusData} instance for {@link StakingCredential}
121121
*/
122-
export const toDataStakingCredential: ToData<StakingCredential> = {
122+
export const isPlutusDataStakingCredential: IsPlutusData<StakingCredential> = {
123123
toData: (stakingCredential) => {
124124
switch (stakingCredential.name) {
125125
case "StakingHash":
126126
return {
127127
name: "Constr",
128-
fields: [0n, [toDataCredential.toData(stakingCredential.fields)]],
128+
fields: [0n, [
129+
isPlutusDataCredential.toData(stakingCredential.fields),
130+
]],
129131
};
130132
case "StakingPtr":
131133
return {
132134
name: "Constr",
133135
fields: [1n, [
134-
LbPreludeInstances.toDataInteger.toData(
136+
LbPreludeInstances.isPlutusDataInteger.toData(
135137
stakingCredential.fields[0],
136138
),
137-
LbPreludeInstances.toDataInteger.toData(
139+
LbPreludeInstances.isPlutusDataInteger.toData(
138140
stakingCredential.fields[1],
139141
),
140-
LbPreludeInstances.toDataInteger.toData(
142+
LbPreludeInstances.isPlutusDataInteger.toData(
141143
stakingCredential.fields[2],
142144
),
143145
]],
144146
};
145147
}
146148
},
147-
};
148149

149-
/**
150-
* {@link FromData} instance for {@link StakingCredential}
151-
*/
152-
export const fromDataStakingCredential: FromData<StakingCredential> = {
153150
fromData: (plutusData) => {
154151
switch (plutusData.name) {
155152
case "Constr": {
156153
if (plutusData.fields[0] === 0n && plutusData.fields[1].length === 1) {
157154
return {
158155
name: "StakingHash",
159-
fields: fromDataCredential.fromData(plutusData.fields[1][0]!),
156+
fields: isPlutusDataCredential.fromData(plutusData.fields[1][0]!),
160157
};
161158
} else if (
162159
plutusData.fields[0] === 1n && plutusData.fields[1].length === 3
163160
) {
164161
return {
165162
name: "StakingPtr",
166163
fields: [
167-
LbPreludeInstances.fromDataInteger.fromData(
164+
LbPreludeInstances.isPlutusDataInteger.fromData(
168165
plutusData.fields[1][0]!,
169166
),
170-
LbPreludeInstances.fromDataInteger.fromData(
167+
LbPreludeInstances.isPlutusDataInteger.fromData(
171168
plutusData.fields[1][1]!,
172169
),
173-
LbPreludeInstances.fromDataInteger.fromData(
170+
LbPreludeInstances.isPlutusDataInteger.fromData(
174171
plutusData.fields[1][2]!,
175172
),
176173
],
@@ -182,7 +179,7 @@ export const fromDataStakingCredential: FromData<StakingCredential> = {
182179
default:
183180
break;
184181
}
185-
throw new FromDataError("Unexpected data");
182+
throw new IsPlutusDataError("Unexpected data");
186183
},
187184
};
188185

@@ -260,36 +257,35 @@ export const jsonCredential: Json<Credential> = {
260257
};
261258

262259
/**
263-
* {@link ToData} instance for {@link Credential}
260+
* {@link IsPlutusData} instance for {@link Credential}
264261
*/
265-
export const toDataCredential: ToData<Credential> = {
262+
export const isPlutusDataCredential: IsPlutusData<Credential> = {
266263
toData: (credential) => {
267264
switch (credential.name) {
268265
case "PubKeyCredential":
269266
return {
270267
name: "Constr",
271-
fields: [0n, [LbCrypto.toDataPubKeyHash.toData(credential.fields)]],
268+
fields: [0n, [
269+
LbCrypto.isPlutusDataPubKeyHash.toData(credential.fields),
270+
]],
272271
};
273272
case "ScriptCredential":
274273
return {
275274
name: "Constr",
276-
fields: [1n, [LbScript.toDataScriptHash.toData(credential.fields)]],
275+
fields: [1n, [
276+
LbScript.isPlutusDataScriptHash.toData(credential.fields),
277+
]],
277278
};
278279
}
279280
},
280-
};
281281

282-
/**
283-
* {@link FromData} instance for {@link Credential}
284-
*/
285-
export const fromDataCredential: FromData<Credential> = {
286282
fromData: (plutusData) => {
287283
switch (plutusData.name) {
288284
case "Constr": {
289285
if (plutusData.fields[0] === 0n && plutusData.fields[1].length === 1) {
290286
return {
291287
name: "PubKeyCredential",
292-
fields: LbCrypto.fromDataPubKeyHash.fromData(
288+
fields: LbCrypto.isPlutusDataPubKeyHash.fromData(
293289
plutusData.fields[1][0]!,
294290
),
295291
};
@@ -298,7 +294,7 @@ export const fromDataCredential: FromData<Credential> = {
298294
) {
299295
return {
300296
name: "ScriptCredential",
301-
fields: LbScript.fromDataScriptHash.fromData(
297+
fields: LbScript.isPlutusDataScriptHash.fromData(
302298
plutusData.fields[1][0]!,
303299
),
304300
};
@@ -309,6 +305,6 @@ export const fromDataCredential: FromData<Credential> = {
309305
default:
310306
break;
311307
}
312-
throw new FromDataError("Unexpected data");
308+
throw new IsPlutusDataError("Unexpected data");
313309
},
314310
};

0 commit comments

Comments
 (0)