-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
159 lines (137 loc) · 3.43 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import {
applyDoubleCborEncoding,
applyParamsToScript,
Constr,
fromText,
Lucid,
MintingPolicy,
OutRef,
SpendingValidator,
} from "lucid-cardano";
import { Blueprint } from "./blueprint.ts";
import blueprint from "./plutus.json" assert { type: "json" };
export type Validators = {
platformNFT: MintingPolicy;
propertyFunds: SpendingValidator;
propertyToken: MintingPolicy;
};
export function readValidators(): Validators {
const platformNFT = (blueprint as Blueprint).validators.find((v) =>
v.title === "platform_nft.platform_nft"
);
if (!platformNFT) {
throw new Error("Platform NFT validator not found");
}
const propertyToken = (blueprint as Blueprint).validators.find((v) =>
v.title === "property_funds.property_token"
);
if (!propertyToken) {
throw new Error("Property Token validator not found");
}
const propertyFunds = (blueprint as Blueprint).validators.find((v) =>
v.title === "property_funds.property_funds"
);
if (!propertyFunds) {
throw new Error("Property funds validator not found");
}
return {
platformNFT: {
type: "PlutusV2",
script: platformNFT.compiledCode,
},
propertyFunds: {
type: "PlutusV2",
script: propertyFunds.compiledCode,
},
propertyToken: {
type: "PlutusV2",
script: propertyToken.compiledCode,
},
};
}
export type AppliedValidators = {
platformNFT: MintingPolicy;
policyId: string;
};
export function applyParams(
tokenName: string,
outputReference: OutRef,
validators: Validators,
lucid: Lucid,
): AppliedValidators {
const outRef = new Constr(0, [
new Constr(0, [outputReference.txHash]),
BigInt(outputReference.outputIndex),
]);
const platformNFT = applyParamsToScript(validators.platformNFT.script, [
fromText(tokenName),
outRef,
]);
const policyId = lucid.utils.validatorToScriptHash({
type: "PlutusV2",
script: platformNFT,
});
return {
platformNFT: {
type: "PlutusV2",
script: applyDoubleCborEncoding(platformNFT),
},
policyId,
};
}
// PROPERTY LOGIC
export type AppliedValidatorsProperty = {
propertyToken: MintingPolicy;
propertyFunds: SpendingValidator;
propertyPolicyId: string;
propertyScriptAddress: string;
};
export function applyParamsProperty(
manager: ByteArray,
lockUntil: BigInt,
price: BigInt,
size: BigInt,
address: string,
outputReference: OutRef,
validators: Validators,
lucid: Lucid,
): AppliedValidators {
const outRef = new Constr(0, [
new Constr(0, [outputReference.txHash]),
BigInt(outputReference.outputIndex),
]);
const propertyToken = applyParamsToScript(validators.propertyToken.script, [
manager,
size,
fromText(address.substring(0, 7)),
outRef,
]);
const propertyPolicyId = lucid.utils.validatorToScriptHash({
type: "PlutusV2",
script: propertyToken,
});
const propertyFunds = applyParamsToScript(validators.propertyFunds.script, [
manager,
lockUntil,
price,
size,
fromText(address.substring(0, 7)),
propertyPolicyId,
]);
const propertyScriptAddress = lucid.utils.validatorToAddress({
type: "PlutusV2",
script: propertyFunds,
});
return {
propertyToken: {
type: "PlutusV2",
script: applyDoubleCborEncoding(propertyToken),
},
propertyFunds: {
type: "PlutusV2",
script: applyDoubleCborEncoding(propertyFunds),
},
propertyPolicyId,
propertyScriptAddress,
};
}