-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtask.ts
263 lines (232 loc) · 12.5 KB
/
task.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import {
Constr,
Credential,
Data,
fromText,
getAddressDetails,
Lucid,
MintingPolicy,
OutRef,
Script,
SpendingValidator,
UTxO,
} from "https://deno.land/x/lucid@0.10.7/mod.ts";
import { shuffleArray } from "https://deno.land/x/shuffle_array@v1.0.7/mod.ts";
import { UNIQUE_ID } from "../../common/offchain/setup_lucid.ts";
import {
failTest,
failTests,
passAllTests,
passTest,
submitSolutionRecord,
} from "../../common/offchain/test_utils.ts";
import {
awaitTxConfirms,
decodeBase64,
filterUTXOsByTxHash,
getFormattedTxDetails,
getWalletBalanceLovelace,
setupMintingPolicy,
setupValidator,
} from "../../common/offchain/utils.ts";
import { createPurchaseOfferDatumSchema } from "./types.ts";
import blueprint from "../plutus.json" with { type: "json" };
export type Validators = {
nftPolicy: MintingPolicy;
nftPolicyId: string;
purchaseOfferValidator: SpendingValidator;
};
export type AppliedValidators = {
mintNFT: MintingPolicy;
policyId: string;
};
export type GameData = {
scriptValidator: Script;
scriptUtxos: UTxO[];
assetPolicyId: string;
assetTokenName: string;
balanceAfterSetup: bigint;
};
export type TestData = void;
type Prescription = {
owner: string;
desiredAssetPolicyId: string;
desiredAssetTokenName: string | null;
setProtocolStakingCredential: boolean;
};
const offerredPrice = 10000000n; // 10 ADA
function readValidators(
lucid: Lucid,
outputReference: OutRef,
tokenName: string,
): Validators {
const offer = setupValidator(
lucid,
blueprint,
"purchase_offer.purchase_offer",
);
const outRef = new Constr(0, [
new Constr(0, [outputReference.txHash]),
BigInt(outputReference.outputIndex),
]);
const mintNFT = setupMintingPolicy(lucid, blueprint, "nft.unique_nft", [
fromText(tokenName),
outRef,
]);
return {
nftPolicy: mintNFT.policy,
nftPolicyId: mintNFT.policyId,
purchaseOfferValidator: offer.validator,
};
}
export async function setup(lucid: Lucid): Promise<GameData> {
console.log(`=== SETUP IN PROGRESS ===`);
const originalBalance = await getWalletBalanceLovelace(lucid);
console.log(`Your wallet's balance at the beginning is ${originalBalance}`);
const assetTokenName = `${UNIQUE_ID} -- NFT`;
const utxosAtBeginning = await lucid.wallet.getUtxos();
const nftOriginUtxo = utxosAtBeginning[0];
const outputReference = {
txHash: nftOriginUtxo.txHash,
outputIndex: nftOriginUtxo.outputIndex,
};
// Compile and setup the validators
const validators = readValidators(lucid, outputReference, assetTokenName);
const asset = `${validators.nftPolicyId}${fromText(assetTokenName)}`;
const protocolStakingCredential: Credential = {
type: "Key",
hash: "84848484848484848484848484848484848484848484848484848484",
};
// Based on these encoded "prescriptions", purchase offer UTxOs are created.
// Note that the resulting array is randomly shuffled -- resulting in a different order every time you run it.
const encodedPrescriptions =
"W3sib3duZXIiOiJhZGRyX3Rlc3QxcXpwcXJjMnh2ZXZuM2pkbnh2aDB6am0yZm5ta3lqcmVudGZrZDZjZndsbnpqcnV6cThzNXZlamU4cnlteHZldzc5OWs1bjhodmZ5OG54a252bTRzamFseDl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Bc3NldFBvbGljeUlkIjoiUE9MSUNZMCIsImRlc2lyZWRBc3NldFRva2VuTmFtZSI6IlRPS0VOMCIsInNldFByb3RvY29sU3Rha2luZ0NyZWRlbnRpYWwiOmZhbHNlfV0=";
const tokenNameMapping = {
TOKEN0: fromText(assetTokenName),
TOKEN1: fromText(assetTokenName.slice(1)),
TOKEN2: fromText(assetTokenName.slice(2)),
};
const policyMapping = {
POLICY0: validators.nftPolicyId,
POLICY1: (validators.nftPolicyId[0] != "f" ? "f" : "d") +
validators.nftPolicyId.slice(1),
};
const decodeAssets = (prescription: Prescription): Prescription => ({
...prescription,
desiredAssetPolicyId: policyMapping[
prescription.desiredAssetPolicyId as keyof typeof policyMapping
],
desiredAssetTokenName: prescription.desiredAssetTokenName != null
? tokenNameMapping[
prescription.desiredAssetTokenName as keyof typeof tokenNameMapping
]
: null,
});
const offerPrescriptions = shuffleArray(
JSON.parse(decodeBase64(encodedPrescriptions)) as Prescription[],
).map(decodeAssets);
console.log(
"Minting a precious NFT and creating multiple purchase offers...",
);
const tx = lucid
.newTx()
.collectFrom([nftOriginUtxo])
.attachMintingPolicy(validators.nftPolicy)
.mintAssets(
{ [asset]: BigInt(1) },
Data.void(),
);
const enhancedTx = offerPrescriptions.reduce(
(
accTx,
{
owner,
desiredAssetPolicyId,
desiredAssetTokenName,
setProtocolStakingCredential,
},
) => {
const validatorAddress = lucid.utils.validatorToAddress(
validators!.purchaseOfferValidator,
setProtocolStakingCredential
? protocolStakingCredential
: getAddressDetails(owner)!.stakeCredential,
);
return accTx.payToContract(validatorAddress, {
inline: createPurchaseOfferDatumSchema(
owner,
desiredAssetPolicyId,
desiredAssetTokenName,
),
}, { lovelace: offerredPrice });
},
tx,
);
const completedTx = await enhancedTx.complete();
const signedTx = await completedTx.sign().complete();
const txHash = await signedTx.submit();
console.log(
"Setup transaction was submitted to testnet, awaiting confirmations!",
);
await awaitTxConfirms(lucid, txHash);
console.log(
`NFT was minted and put to your balance. Purchase offers were also created${
getFormattedTxDetails(txHash, lucid)
}`,
);
const scriptUtxos = filterUTXOsByTxHash(
await lucid.utxosAt(
lucid.utils.scriptHashToCredential(
lucid.utils.validatorToScriptHash(validators!.purchaseOfferValidator),
),
),
txHash,
);
console.log(`=== SETUP WAS SUCCESSFUL ===`);
const balanceAfterSetup = await getWalletBalanceLovelace(lucid);
console.log(`Your wallet's balance after setup is ${balanceAfterSetup}`);
return {
scriptValidator: validators!.purchaseOfferValidator,
scriptUtxos,
assetPolicyId: validators.nftPolicyId,
assetTokenName,
balanceAfterSetup,
};
}
export async function test(
lucid: Lucid,
gameData: GameData,
_testData: TestData,
): Promise<boolean> {
console.log("================TESTS==================");
let passed = true;
const endBalance = await getWalletBalanceLovelace(lucid);
console.log(`Your wallet's balance at the end is ${endBalance}`);
// 1. Test that the user earned more than 50 ADA on top of his initial balance
const balanceDiff = endBalance - gameData.balanceAfterSetup;
if (balanceDiff > offerredPrice) {
passTest(
`TEST 1 PASSED - you earned more than the offerred price`,
lucid,
);
} else {
failTest(`TEST 1 FAILED - you did not earn enough ADA!`);
passed = false;
}
if (passed) {
await submitSolutionRecord(lucid, 5n);
const encodedBlogURL =
"aHR0cHM6Ly9tZWRpdW0uY29tL0B2YWN1dW1sYWJzX2F1ZGl0aW5nL2NhcmRhbm8tY3RmLWhpbnRzLWFuZC1zb2x1dGlvbnMtZTM5OTFjZTZhOTQ0";
passAllTests(
"\nCongratulations on the successful completion of the Level 05: Purchase Offer\n" +
`You can compare your solution with ours by reading this blog post: ${
decodeBase64(encodedBlogURL)
}` + "\nGood luck with the next level.",
lucid,
);
return true;
} else {
failTests();
return false;
}
}