forked from spacebudz/lucid
-
Notifications
You must be signed in to change notification settings - Fork 4
/
emulate_something.ts
63 lines (49 loc) · 1.38 KB
/
emulate_something.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
import {
Emulator,
fromText,
generatePrivateKey,
getAddressDetails,
Lucid,
toUnit,
TxHash,
} from "../../mod.ts";
const privateKey = generatePrivateKey();
const address = await (await Lucid.new(undefined, "Custom"))
.selectWalletFromPrivateKey(privateKey).wallet.address();
const { paymentCredential } = getAddressDetails(address);
const emulator = new Emulator([{ address, assets: { lovelace: 3000000000n } }]);
const lucid = await Lucid.new(emulator);
lucid.selectWalletFromPrivateKey(privateKey);
const mintingPolicy = lucid.utils.nativeScriptFromJson({
type: "all",
scripts: [
{
type: "before",
slot: lucid.utils.unixTimeToSlot(emulator.now() + 60000),
},
{ type: "sig", keyHash: paymentCredential?.hash! },
],
});
const policyId = lucid.utils.mintingPolicyToId(mintingPolicy);
async function mint(): Promise<TxHash> {
const tx = await lucid.newTx()
.mintAssets({
[toUnit(policyId, fromText("Wow"))]: 123n,
})
.validTo(emulator.now() + 30000)
.attachMintingPolicy(mintingPolicy)
.complete();
const signedTx = await tx.sign().complete();
return signedTx.submit();
}
await mint();
emulator.awaitBlock(4);
console.log(await lucid.wallet.getUtxos());
// This should fail
try {
await mint();
} catch (_e) {
console.error(
"Error: Second transaction failed because upper bound exceeded in mint script.",
);
}