-
Notifications
You must be signed in to change notification settings - Fork 1
/
initialization.ts
89 lines (77 loc) · 2.02 KB
/
initialization.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
import { Contract } from "ethers";
import { Character } from "./types";
import { getUnicodeData } from "./unicode-data";
// roughly the number of characters we can set within the gas limit
const BATCH_SIZE = 125;
const charToParameter = (c: Character) =>
Object.values({
name: c.name,
numeric: [c.numeric.numerator, c.numeric.denominator],
decompositonMapping: c.decomposition || [],
category: c.category,
combining: c.combining,
bidirectional: c.bidirectional,
decompositionType: c.decompositionType,
decimal: c.decimal,
digit: c.digit,
mirrored: c.mirrored,
uppercase: c.uppercase,
lowercase: c.lowercase,
titlecase: c.titlecase,
});
export const initializeUnicodeData = async (contract: Contract) => {
// get charactesr
const characters = await getUnicodeData();
const total = characters.length;
const numBatches = total / BATCH_SIZE;
let count = 0;
let failed = false;
let batch = {
codes: [],
data: [],
};
for (let char of characters) {
// keep track of progress
count++;
process.stdout.clearLine(1);
process.stdout.write(
`${count} of ${total}\t${((count / total) * 100).toFixed(2)}% \t ${
char.name
}\r`
);
// set each character
const data = charToParameter(char);
// @ts-ignore
batch.codes = [...batch.codes, char.code];
// @ts-ignore
batch.data = [...batch.data, data];
if (batch.codes.length < BATCH_SIZE) {
continue;
}
try {
await contract.setBatch(batch.codes, batch.data);
// reset
batch = {
codes: [],
data: [],
};
} catch (err) {
console.log(err);
console.log("failed to set batch:", batch);
failed = true;
break;
}
}
// set final batch if it exists
if (!batch.codes.length || failed) return;
try {
await contract.setBatch(batch.codes, batch.data);
// reset
batch = {
codes: [],
data: [],
};
} catch (err) {
console.log("failed to set final batch:", batch);
}
};