-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfiles.js
More file actions
63 lines (50 loc) · 1.4 KB
/
files.js
File metadata and controls
63 lines (50 loc) · 1.4 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
import fs from "fs";
export function writeToFile(collectionId, ids) {
const defaultPrice = process.env.PRICE ?? 1; // Price without decimals part
const defaultCurrency = process.env.CURRENCY ?? 0; // Currency from .env or UNQ (0)
const out = ids
.sort((a, b) => a - b)
.map((id) => `${id},${defaultPrice},${defaultCurrency}`)
.join("\n");
const filename = `collection_${collectionId}.csv`;
fs.writeFileSync(
filename,
`token id,price,currency
${out}`
);
console.log(`saved to file: ${filename}`);
}
export function loadFromFile(collectionId) {
const filename = `collection_${collectionId}.csv`;
if (!fs.existsSync(filename)) {
throw new Error(`File ${filename} not found`);
}
return fs
.readFileSync(filename)
.toString()
.trim()
.split("\n")
.slice(1)
.map((line) => {
const data = line.split(",");
const tokenId = +data[0];
if (!tokenId) {
throw new Error("Invalid tokenId in csv file");
}
const price = +data[1];
if (!price) {
throw new Error("Invalid price in csv file");
}
const currency = +data[2];
if (!currency) {
throw new Error("Invalid currency in csv file");
}
// TODO: move to params
if (currency !== 0 && currency !== 437) throw Error("Wrong currency");
return {
tokenId,
price,
currency,
};
});
}