-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtask.ts
113 lines (93 loc) · 2.74 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
import {
Data,
Lucid,
SpendingValidator,
TxHash,
UTxO,
} from "https://deno.land/x/[email protected]/mod.ts";
import {
awaitTxConfirms,
filterUTXOsByTxHash,
getFormattedTxDetails,
getWalletBalanceLovelace,
setupValidator,
} from "../../common/offchain/utils.ts";
import blueprint from "../plutus.json" with { type: "json" };
import {
failTest,
failTests,
passAllTests,
passTest,
submitSolutionRecord,
} from "../../common/offchain/test_utils.ts";
export type GameData = {
scriptValidator: SpendingValidator;
scriptUtxo: UTxO;
originalBalance: bigint;
};
export type TestData = void;
function readValidator(lucid: Lucid): SpendingValidator {
const hello = setupValidator(lucid, blueprint, "hello_world.hello_world");
return hello.validator;
}
export async function lock(
lovelace: bigint,
{ into }: { into: SpendingValidator },
lucid: Lucid,
): Promise<TxHash> {
const contractAddress = lucid.utils.validatorToAddress(into);
const tx = await lucid
.newTx()
.payToContract(contractAddress, { inline: Data.void() }, { lovelace })
.complete();
const signedTx = await tx.sign().complete();
return signedTx.submit();
}
export async function setup(lucid: Lucid) {
console.log(`=== SETUP IN PROGRESS ===`);
const validator = readValidator(lucid);
const _publicKeyHash = lucid.utils.getAddressDetails(
await lucid.wallet.address(),
).paymentCredential?.hash;
console.log(`Creating an UTxO at the smart contract script address...`);
const txHash = await lock(10000000n, { into: validator }, lucid);
await awaitTxConfirms(lucid, txHash);
console.log(
`10 ADA locked into the contract${getFormattedTxDetails(txHash, lucid)}`,
);
const contractAddress = lucid.utils.validatorToAddress(validator);
const originalBalance = await getWalletBalanceLovelace(lucid);
console.log(`=== SETUP WAS SUCCESSFUL ===`);
return {
scriptValidator: validator,
scriptUtxo:
filterUTXOsByTxHash(await lucid.utxosAt(contractAddress), txHash)[0],
originalBalance: originalBalance,
};
}
export async function test(
lucid: Lucid,
gameData: GameData,
_testData: TestData,
): Promise<boolean> {
let passed = true;
console.log("================TESTS==================");
const endBalance = await getWalletBalanceLovelace(lucid);
if (gameData.originalBalance - endBalance > 4000000n) {
failTest("TEST 1 FAILED - you spent too much ADA");
passed = false;
} else {
passTest("TEST 1 PASSED", lucid);
}
if (passed) {
await submitSolutionRecord(lucid, 0n);
passAllTests(
"\nCongratulations on the successful completion of the Level 00: Hello World!\nGood luck with the next level.",
lucid,
);
return true;
} else {
failTests();
return false;
}
}