-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_ppp_emulation.ts
109 lines (86 loc) · 2.63 KB
/
run_ppp_emulation.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
// $ deno run -A run_ppp_emulation.ts
import {
Address,
applyDoubleCborEncoding,
applyParamsToScript,
Constr,
Data,
Emulator,
fromText,
generatePrivateKey,
Lucid,
OutRef,
PrivateKey,
UTxO,
} from "https://deno.land/x/lucid/mod.ts";
// Imports blueprint generated from `aiken build`
// Blueprints CIP: https://cips.cardano.org/cips/cip57/
import blueprint from "./plutus.json" assert { type: "json" };
// Define wallets, balances and Custom network
const privateKeyAlice = generatePrivateKey();
const addressAlice = await (await Lucid.new(undefined, "Custom"))
.selectWalletFromPrivateKey(privateKeyAlice)
.wallet.address();
const emulator = new Emulator([
{
address: addressAlice,
assets: { lovelace: 100_000_000n },
},
]);
const lucid = await Lucid.new(emulator);
const nft = blueprint.validators.find((v) => v.title === "ppp_mint.nft_policy");
const tokenName = "JUNGLE-GIFT-CARD";
let policyId: string;
let nftMintingPolicy: string;
let assetName: string;
const applyParamsToContract = (outputReference: OutRef) => {
nftMintingPolicy = applyParamsToScript(nft?.compiledCode, [
outputReference,
fromText(tokenName),
]);
policyId = lucid.utils.validatorToScriptHash({
type: "PlutusV2",
script: nftMintingPolicy,
});
assetName = `${policyId}${fromText(tokenName)}`;
};
const mintNFT = async (
minterPrivateKey: PrivateKey,
receiverAddress: Address,
) => {
const owner: Lucid = lucid.selectWalletFromPrivateKey(minterPrivateKey);
const [utxo] = await owner.wallet.getUtxos();
const outRef: OutRef = new Constr(0, [
new Constr(0, [utxo.txHash]),
BigInt(utxo.outputIndex),
]);
applyParamsToContract(outRef);
const mintingPolicy = {
type: "PlutusV2",
script: applyDoubleCborEncoding(nftMintingPolicy),
};
// Change this to > 1n and script will error
const mintAmount = 1n;
const txHash = await owner
.newTx()
.collectFrom([utxo])
// use the nft validator
.attachMintingPolicy(mintingPolicy)
// mint 1 of the asset
.mintAssets({ [assetName]: mintAmount }, Data.void())
.payToAddress(receiverAddress, { [assetName]: mintAmount })
.complete()
.then((tx) => tx.sign().complete())
.then((tx) => tx.submit())
.catch((e) => console.log(e));
console.log(txHash);
};
console.log("Alice's balance before mint");
console.log(await getBalanceAtAddress(addressAlice));
await mintNFT(privateKeyAlice, addressAlice);
emulator.awaitBlock(4);
console.log("Alice's balance after mint");
console.log(await getBalanceAtAddress(addressAlice));
function getBalanceAtAddress(address: Address): Promise<UTxO> {
return lucid.utxosAt(address);
}