-
Notifications
You must be signed in to change notification settings - Fork 0
/
cip68.ts
127 lines (108 loc) · 4.7 KB
/
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import {
Address,
Blockfrost,
Constr,
Data,
Lucid,
SpendingValidator,
TxHash,
fromHex,
fromText,
toHex,
} from "https://deno.land/x/[email protected]/mod.ts";
// test passed: these code has been ran well
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);
//===============Đọc mã CBOR của SC ============================
// async function readValidator(): Promise<SpendingValidator> {
// const validator = JSON.parse(await Deno.readTextFile("plutus.json")).validators[0];
// return {
// type: "PlutusV2",
// script: toHex(cbor.encode(fromHex(validator.compiledCode))),
// };
// }
// const validator = await readValidator();
// const policyId = lucid.utils.mintingPolicyToId(validator);
// const avalidator_Address: Address = lucid.utils.validatorToAddress(validator,);
const mintingPolicy: SpendingValidator = {
type: "PlutusV2",
script: "49480100002221200101",
};
const policyId = lucid.utils.mintingPolicyToId(mintingPolicy);
const avalidator_Address: Address = lucid.utils.validatorToAddress(mintingPolicy,);
const ownerPublicKeyHash = lucid.utils.getAddressDetails(await lucid.wallet.address()).paymentCredential.hash;
type Metadata = {
name: string;
image: string;
owner: string;
// version: string;
};
const Datum = {
name: "INDIA-FLAG",
image:"ipfs://QmbneFHiFLDnBcnA24VzDB7opGxLnSipEBygjWMD2xHqv5",
owner:ownerPublicKeyHash,
// version:fromText("1"),
}
console.log(Datum)
const Redeemer = () => Data.void();
export async function mintNFT(
assetName: string,
metadata: MyDatum,
): 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]));
console.log(metadata)
const tx = await lucid
.newTx()
.mintAssets({ [refNft]: 1n, [userToken]: 1n },Redeemer() )// mint refNft and user token pair
.payToContract(avalidator_Address, 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(avalidator_Address, refNft); // tim ra refUTXO
const datumMetadata = Data.to(new Constr(0, [Data.fromJson(metadata), 2n]));
const tx = await lucid.newTx()
.collectFrom([refUtxo],Redeemer())
.payToContract(avalidator_Address, 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("IND",Datum);
console.log(`Giao dịch thành công với TX-ID: ${txHash}`)
// const txHash=await updateNFT("SHOES68",Datum);
// console.log(`Giao dịch update metadata 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;
}