-
Notifications
You must be signed in to change notification settings - Fork 11
/
mint.js
139 lines (121 loc) · 3.65 KB
/
mint.js
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
const { cardanocliJs } = require("./cardanocli");
const fetch = require("node-fetch");
const metadata = require("./metadata.json");
const createTransaction = (tx) => {
let raw = cardanocliJs.transactionBuildRaw(tx);
let fee = cardanocliJs.transactionCalculateMinFee({
...tx,
txBody: raw,
});
tx.txOut[0].amount.lovelace -= fee;
return cardanocliJs.transactionBuildRaw({ ...tx, fee });
};
const signTransaction = (wallet, tx, script) => {
return cardanocliJs.transactionSign({
signingKeys: [wallet.payment.skey, wallet.payment.skey],
scriptFile: script,
txBody: tx,
});
};
const signTransactionRefund = (wallet, tx) => {
return cardanocliJs.transactionSign({
signingKeys: [wallet.payment.skey],
txBody: tx,
});
};
exports.initateMint = async (_utxo, id) => {
const wallet = cardanocliJs.wallet("Test");
const mintScript = {
type: "all",
scripts: [
{ slot: 21560175 + 632000, type: "before" },
{
keyHash: cardanocliJs.addressKeyHash(wallet.name),
type: "sig",
},
],
};
const utxo = JSON.parse(JSON.stringify(_utxo));
const oldTxHash = utxo.txHash;
const receiver = await fetch(
`https://cardano-testnet.blockfrost.io/api/v0/txs/${oldTxHash}/utxos`,
{ headers: { project_id: "rbkrp5hOr3khPAWNo3x47t6CP7qKFyA5" } }
)
.then((res) => res.json())
.then((res) => res.inputs[0].address);
const policy = cardanocliJs.transactionPolicyid(mintScript);
const name = `SpaceBud${id}`;
const SPACEBUD = policy + `.${name}`;
const balance = utxo.amount;
balance.lovelace -= cardanocliJs.toLovelace(1.5);
const [ziegAmount, alesAmount] = [
balance.lovelace - Math.floor(balance.lovelace / 2),
Math.floor(balance.lovelace / 2),
];
const tx = {
txIn: [utxo],
txOut: [
{
address:
"addr_test1qzuyhmjlv2qcg5rrk9hxp9hen595m7h8zs4h5yfz743k86gjx0wny5qwycfv0qp94vml3dqp3590v4f4xv2xym02eypqjc2430",
amount: { lovelace: alesAmount },
},
{
address:
"addr_test1qrez968pdxa5p55jpq4apn7uxv0msdeq8fa8x5su54s9k4gjx0wny5qwycfv0qp94vml3dqp3590v4f4xv2xym02eypqwleec8",
amount: { lovelace: ziegAmount },
},
{
address: receiver,
amount: {
...balance,
[SPACEBUD]: 1,
lovelace: cardanocliJs.toLovelace(1.5),
},
},
],
mint: [{ action: "mint", amount: 1, token: SPACEBUD }],
metadata: { 721: { [policy]: { [name]: metadata[id] } } },
witnessCount: 2,
};
const raw = createTransaction(tx);
const signed = signTransaction(wallet, raw, mintScript);
const txHash = cardanocliJs.transactionSubmit(signed);
return txHash;
};
exports.initateRefund = async (_utxo) => {
const wallet = cardanocliJs.wallet("Test");
const mintScript = {
keyHash: cardanocliJs.addressKeyHash(wallet.name),
type: "sig",
};
const utxo = JSON.parse(JSON.stringify(_utxo));
const oldTxHash = utxo.txHash;
const receiver = await fetch(
`https://cardano-testnet.blockfrost.io/api/v0/txs/${oldTxHash}/utxos`,
{ headers: { project_id: "rbkrp5hOr3khPAWNo3x47t6CP7qKFyA5" } }
)
.then((res) => res.json())
.then((res) => res.inputs[0].address);
const balance = utxo.amount;
const tx = {
txIn: [utxo],
txOut: [
{
address: receiver,
amount: balance,
},
],
witnessCount: 1,
metadata: {
0: {
message:
"Refund. Amount matches no SpaceBud. Please send the exact amount",
},
},
};
const raw = createTransaction(tx);
const signed = signTransactionRefund(wallet, raw, mintScript);
const txHash = cardanocliJs.transactionSubmit(signed);
return txHash;
};