-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
275 lines (240 loc) · 9.33 KB
/
main.js
File metadata and controls
275 lines (240 loc) · 9.33 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//Modify these fields before running your code
//Number of NFTokens (NFTs) you want to mint
const numberNFTs = 5;
//Select your network "Testnet" or "Mainnet"
const net = "Testnet";
//Secret of your wallet (Don't share it!) Testnet address generator: https://xrpl.org/xrp-testnet-faucet.html Fake example: "sn5XTrWNGNysp4o1JYEFp7wSbN6Gz"
const seed = "sEd79DNQKP7cS1RFEgqhGrF94WQQvVw";
//Price of every NFT in XRP. Example: for 1 XRP price per NFT, put 1
const xrp = 10;
//CID from tour ipfs files without 'ipfs://' part. Fake example: 'bafybeigyy2u2sbgtxxr2tdc6snxgefdo52bx2qy2nd3vjrjzaieg4yr3ce'
const ipfs_cid = "bafqbeigrsqxvodv424n5imp22wjurbo3kdsugn56kgkmr4bj4ubywphirq";
//NFT flags, this is a sum of properties, sum all you want.
const nft_flags = 8;
//Royalties fee,Example: for a 5% of royalties, put 5, for a 10%, put 10. Attention: 50% is the maximun.
const royalties_fee = 5;
//Taxon, the number that represents your collection in your wallet. For your first collection use 0, next one the 1, etc... up to you.
const taxon=3;
//End modify variables
//Don't touch anything after this line
const xrpl = require("xrpl");
const { XrplAccountLib } = require("xrpl");
async function main() {
function wait(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
const price = xrp * 1000000;
let network = "wss://s.altnet.rippletest.net:51233/";
if (net === "Mainnet") {
network = "wss://xrplcluster.com/";
}
const royalties = royalties_fee*1000;
const client = new xrpl.Client(network);
await client.connect();
const my_wallet = xrpl.Wallet.fromSeed(seed);
console.log(`Your public address is: ${my_wallet.address}`);
const response = await client.request({
command: "account_info",
account: my_wallet.address,
ledger_index: "validated",
});
const total_balance = response.result.account_data.Balance / 1000000;
const reserves = response.result.account_data.OwnerCount * 2 + 10;
console.log(
`Your total balance (available+reserves) is: ${total_balance} XRP`
);
console.log(`Your reserves is: ${reserves} xrp`);
const balance = total_balance - reserves;
console.log(`Your available balance is: ${balance} xrp`);
//The reserve per object is 2 but I prefer to be safe
const TicketTotalCost = numberNFTs * 2.1;
console.log(
`To create the tickets needed, you need to have at least this balance: ${TicketTotalCost} XRP`
);
if (balance <= TicketTotalCost) {
console.log(
`To create the tickets needed, you need to have at least this balance: ${TicketTotalCost} XRP. I recommend have a bit more.`
);
client.disconnect();
console.log(`Connection closed`);
} else {
//We check how many tickets you had before running the code
let response = await client.request({
command: "account_objects",
account: my_wallet.address,
type: "ticket",
});
let numberTickets = 0;
if (
Array.isArray(response.result.account_objects) &&
response.result.account_objects.length > 0
) {
numberTickets = response.result.account_objects.length;
console.log(`This account has ${numberTickets} tickets already`);
} else {
console.log("This account has no tickets already");
}
const account_info = await client.request({
command: "account_info",
account: my_wallet.address,
});
numberTickets = numberNFTs - numberTickets;
console.log(`${numberTickets} tickets will be created`);
if (numberTickets > 0) {
let current_sequence = account_info.result.account_data.Sequence;
console.log("Actual Sequence", current_sequence);
//Generate tickets:
const prepared = await client.autofill({
TransactionType: "TicketCreate",
Account: my_wallet.address,
TicketCount: numberTickets,
Sequence: current_sequence,
});
const signed = my_wallet.sign(prepared);
console.log(`Getting ready tx TicketCreate ${signed.hash}`);
// Submit TicketCreate
const tx = await client.submitAndWait(signed.tx_blob);
console.log("Info tx ", tx);
const jsonDataString = JSON.stringify(tx);
console.log(jsonDataString);
//finished
await wait(10000);
} else {
console.log(
`New tickets are not created. You have enough created already.`
);
}
const response2 = await client.request({
command: "account_objects",
account: my_wallet.address,
type: "ticket",
});
console.log(
"Checking the tickets created are enough for your bulk minting, wait 10 seconds..."
);
let tickets = [];
await wait(10000);
for (let i = 0; i < numberNFTs; i++) {
y = i + 1;
tickets[i] = response2.result.account_objects[i].TicketSequence;
console.log("Generated tickets nº ", y, tickets[i]);
}
console.log("Ticket generation finished");
if (numberNFTs > response2.result.account_objects.length) {
console.log(`Tickets needed ${numberNFTs}`);
console.log(`Tickets created ${response2.result.account_objects.length}`);
console.log(
`You need more tickets to start the mint, re-execute this code.`
);
} else {
console.log(`Tickets needed ${numberNFTs}`);
console.log(`Tickets created ${response2.result.account_objects.length}`);
//Let's get our tickets
let accObjRequest = {
command: "account_objects",
account: my_wallet.address,
type: "ticket",
ledger_index: "validated",
};
let accObjResponse = await client.request(accObjRequest);
let ticketObjects = [];
if (accObjResponse?.result?.account_objects) {
ticketObjects = accObjResponse?.result?.account_objects;
let marker = accObjResponse.result.marker;
while (marker) {
accObjRequest.marker = marker;
accObjRequest.ledger_index = accObjResponse.result.ledger_index;
accObjResponse = await client.request(accObjRequest);
marker = accObjResponse?.result?.marker;
if (accObjResponse?.result?.account_objects) {
ticketObjects = ticketObjects.concat(
accObjResponse.result.account_objects
);
} else {
marker = null;
}
}
}
let tickets = [];
for (let i = 0; i < numberNFTs; i++) {
tickets[i] = ticketObjects[i].TicketSequence;
console.log("ticket", i, tickets[i]);
}
console.log(tickets);
console.log(`Let's mint!`);
for (let i=0; i < numberNFTs; i++) {
y=i+1
const transactionBlob = {
"TransactionType": "NFTokenMint",
"Account": my_wallet.address,
"URI": xrpl.convertStringToHex(`ipfs://${ipfs_cid}/${y}.json`),
"Flags": nft_flags,
"TransferFee": royalties,
"Sequence": 0,
"TicketSequence": tickets[i],
"LastLedgerSequence": null,
"NFTokenTaxon": taxon
}
console.log();
console.log(JSON.stringify(transactionBlob));
const tx = await client.submit(transactionBlob, { wallet: my_wallet} )
console.log('TX Json');
console.log("Asking summiting tx ",y)
console.log(tx.result);
}
console.log("Mint is over. Wait 10 seconds until continue with offers...");
//Start create offers
await wait(10000);
console.log('Created account_info')
const nfts = await client.request({
method: "account_nfts",
account: my_wallet.classicAddress
})
console.log('Creating allNfts')
let allNfts = nfts.result.account_nfts
console.log('allNFTs created')
let marker = nfts.result.marker
console.log('Maker created')
console.log('marker',marker)
while(marker) {
nfts.result.marker=marker
console.log('nfts.ledger_index',nfts.ledger_index)
nfts.ledger_index = nfts.result.ledger_current_index
console.log('nfts.result.ledger_current_index',nfts.result.ledger_current_index)
console.log('nfts.ledger_index',nfts.ledger_index)
const nfts2 = await client.request({
method: "account_nfts",
account: my_wallet.classicAddress,
ledger_index: nfts.ledger_index,
marker: nfts.result.marker
})
marker = nfts2.result.marker
console.log('nfts2.result.marker',nfts2.result.marker)
console.log('marker',marker)
console.log('nfts2.result.ledger_current_index',nfts2.result.ledger_current_index)
if(nfts2.result.account_nfts){
allNfts= allNfts.concat(nfts2.result.account_nfts)
} else {
marker=null
}
}
for (let i=0; i < allNfts.length; i++) {
if(allNfts[i].NFTokenTaxon==taxon){
const transactionBlob = {
"TransactionType": "NFTokenCreateOffer",
"Account": my_wallet.classicAddress,
"NFTokenID": allNfts[i].NFTokenID,
"Amount": `${price}`,
"Flags": 1
}
const tx = await client.submit(transactionBlob, { wallet: my_wallet} )
console.log('tx',i,tx)
console.log('tx.result',i,tx.result)
console.log("Asking submit for the NFT offer: ",i,allNfts[i].NFTokenID)
}
}
console.log('Create offers ends here');
}
}
}
main();