-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_emulation.ts
180 lines (152 loc) · 4.8 KB
/
run_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
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
// $ deno run -A run_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 masterKey = blueprint.validators.find((v) =>
v.title === "mint.master_key"
);
const redeem = blueprint.validators.find((v) => v.title === "mint.redeem");
const tokenName = "MASTER-KEY";
let policyId, masterKeyMintingPolicy, assetName, redeemValidator, lockAddress;
const applyParamsToContract = (outputReference: OutRef) => {
masterKeyMintingPolicy = applyParamsToScript(
masterKey?.compiledCode,
[
fromText(tokenName),
outputReference,
],
);
policyId = lucid.utils.validatorToScriptHash({
type: "PlutusV2",
script: masterKeyMintingPolicy,
});
assetName = `${policyId}${fromText(tokenName)}`;
redeemValidator = applyParamsToScript(redeem?.compiledCode, [
fromText(tokenName),
policyId,
]);
lockAddress = lucid.utils.validatorToAddress({
type: "PlutusV2",
script: redeemValidator,
});
};
const mintMasterKey = async (minterPrivateKey: PrivateKey) => {
const owner: Lucid = lucid.selectWalletFromPrivateKey(minterPrivateKey);
const [utxo] = await owner.wallet.getUtxos();
// console.log({ utxo });
const outRef: OutRef = new Constr(0, [
new Constr(0, [utxo.txHash]),
BigInt(utxo.outputIndex),
]);
applyParamsToContract(outRef);
const mintRedeemer = Data.to(new Constr(0, []));
const txHash = await owner.newTx()
.collectFrom([utxo])
// use the master_key validator
.attachMintingPolicy(
{
type: "PlutusV2",
script: applyDoubleCborEncoding(masterKeyMintingPolicy),
},
)
// mint 1 of the asset
.mintAssets(
{ [assetName]: BigInt(1) },
// this redeemer is the first argument to the master_key validator
mintRedeemer,
)
.payToContract(
lockAddress,
{
// On unlock this gets passed to the redeem
// validator as datum. Our redeem validator
// doesn't use it so we can just pass in anything.
inline: Data.void(),
},
{ lovelace: BigInt(50_000_000) },
)
.complete()
.then((tx) => tx.sign().complete())
.then((tx) => tx.submit());
console.log(txHash);
};
const redeemAssets = async (minterPrivateKey: PrivateKey) => {
const owner: Lucid = lucid.selectWalletFromPrivateKey(minterPrivateKey);
const utxos = await lucid.utxosAt(lockAddress);
// console.log({ utxos });
const burnRedeemer = Data.to(new Constr(1, []));
const txHash = await owner.newTx()
.collectFrom(
utxos,
// This is the second argument to the redeem validator
// and we also don't do anything with it similar to the
// inline datum. It's fine to pass in anything in this case.
Data.void(),
)
// use the master_key validator again
.attachMintingPolicy(
{
type: "PlutusV2",
script: applyDoubleCborEncoding(masterKeyMintingPolicy),
},
)
// use the redeem validator
.attachSpendingValidator({
type: "PlutusV2",
script: applyDoubleCborEncoding(redeemValidator),
})
.mintAssets(
// notice the negative one here
{ [assetName]: BigInt(-1) },
// this redeemer is the first argument to the master_key validator
burnRedeemer,
)
.complete()
.then((tx) => tx.sign().complete())
.then((tx) => tx.submit());
console.log({ txHash });
};
try {
console.log("Alice before minting Master Key");
console.log(await getBalanceAtAddress(addressAlice));
console.log("Alice minting Master Key and paying to contract");
await mintMasterKey(privateKeyAlice);
emulator.awaitBlock(4);
console.log("Alice after minting Master Key");
console.log(await getBalanceAtAddress(addressAlice));
console.log("Contract after receiving payment");
console.log(await getBalanceAtAddress(lockAddress));
console.log("Alice redeeming Master Key");
await redeemAssets(privateKeyAlice);
emulator.awaitBlock(4);
console.log("Alice after redeeming Master Key");
console.log(await getBalanceAtAddress(addressAlice));
} catch (e) {
console.log("error " + e);
}
function getBalanceAtAddress(address: Address): Promise<UTxO> {
return lucid.utxosAt(address);
}