-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple-cip68.ts
109 lines (94 loc) · 4.25 KB
/
simple-cip68.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
import {
Address,
Blockfrost,
Constr,
Data,
Lucid,
SpendingValidator,
TxHash,
} from "https://deno.land/x/[email protected]/mod.ts";
import {utf8ToHex,} from "https://deno.land/x/[email protected]/mod.ts";
const lucid = await Lucid.new(new Blockfrost( "https://cardano-preview.blockfrost.io/api/v0", "preview4QbiejGzlyhUgfvyXZNf8lDEIFjCHjP0",),"Preview",);
//===============Active Bob Wallet ==============================
const Bob_mnonic = "lottery novel civil green oppose whip offer correct mushroom cricket awkward vague shine another tree boss there perfect asset side release song wedding captain";
lucid.selectWalletFromSeed(Bob_mnonic);
/*
AlwaysSucceeds Example
Lock a UTxO with some ADA
UTxO can be unlocked by anyone
Showcasing PlutusV2
Contract:
validate :: () -> () -> ScriptContext -> Bool
validate _ _ _ = True
*/
const mintingPolicy: SpendingValidator = {
type: "PlutusV2",
script: "49480100002221200101",
};
const policyId = lucid.utils.mintingPolicyToId(mintingPolicy);
const alwaysSucceedAddress: Address = lucid.utils.validatorToAddress(mintingPolicy,);
type Metadata = {
name: string;
image: string;
};
const Redeemer = () => Data.void();
export async function mintNFT(
assetName: string,
metadata: Metadata,
): Promise<TxHash> {
const refNft = policyId + '000643b0' + utf8ToHex(assetName); // tính ra tên refNFT
const userToken = policyId + '000de140' + utf8ToHex(assetName);
const datumMetadata = Data.to(new Constr(0, [Data.fromJson(metadata), 1n]));
const tx = await lucid
.newTx()
// .payToContract(alwaysSucceedAddress, { inline: Datum}, { lovelace })
.mintAssets({ [refNft]: 1n, [userToken]: 1n },Redeemer() )// mint refNft and user token pair
.payToContract(alwaysSucceedAddress, datumMetadata, { [refNft]: 1n }) // send userToken to user wallet address
.payToAddress(await lucid.wallet.address(), { [userToken]: 1n })
.attachMintingPolicy(mintingPolicy)
.complete();
const signedTx = await tx.sign().complete();
const txHash = await signedTx.submit();
return txHash;
}
export async function updateNFT(assetName: string, metadata: Metadata,): Promise<TxHash> {
const refNft = policyId + '000643b0' + utf8ToHex(assetName); // tính ra tên refNFT
// const userToken = policyId + '000de140' + utf8ToHex(assetName);
const [refUtxo] = await lucid.utxosAtWithUnit(alwaysSucceedAddress, refNft); // tim ra refUTXO
const datumMetadata = Data.to(new Constr(0, [Data.fromJson(metadata), 2n]));
const tx = await lucid.newTx()
.collectFrom([refUtxo],Redeemer())
.payToContract(alwaysSucceedAddress, datumMetadata, { [refNft]: 1n }) // send userToken to user wallet address
.attachSpendingValidator(mintingPolicy)
.complete();
const signedTx = await tx.sign().complete();
const txHash = await signedTx.submit();
return txHash;
}
const txHash=await mintNFT("MK", { name: "MONKEY", image: "ipfs://Qme49fCRJ1CNcUCWEhG75b53vZ8tpBLyvmEhNwPYyCXpn5" });
console.log(`Giao dịch thành công với TX-ID: ${txHash}`)
// const txHash=await mintNFT("HCM-CWG", { name: "Warm-up", image: "ipfs://QmXdny7WMQrHsydwbynJi2icTP9Rhh9D8SWdMtXZxfFsdH" });
// console.log(`Giao dịch thành công với TX-ID: ${txHash}`)
// const txHash=await redeemUtxo();
// console.log(`Giao dịch thành công với TX-ID: ${txHash}`)
export async function redeemUtxo(): Promise<TxHash> {
const referenceScriptUtxo = (await lucid.utxosAt(alwaysSucceedAddress)).find(
(utxo) => Boolean(utxo.scriptRef),
);
if (!referenceScriptUtxo) throw new Error("Reference script not found");
const utxo = (await lucid.utxosAt(alwaysSucceedAddress)).find((utxo) =>
utxo.datum === Datum
//&& !utxo.scriptRef
);
if (!utxo) throw new Error("Spending script utxo not found");
console.log(utxo);
const tx = await lucid
.newTx()
// .readFrom([referenceScriptUtxo]) // spending utxo by reading plutusV2 from reference utxo
.collectFrom([utxo], Redeemer())
.attachSpendingValidator(alwaysSucceedScript)
.complete();
const signedTx = await tx.sign().complete();
const txHash = await signedTx.submit();
return txHash;
}