-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
249 lines (207 loc) · 7.35 KB
/
script.js
File metadata and controls
249 lines (207 loc) · 7.35 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
var updatedEntityJson;
var updatedAssetJson;
// Function to run on startup
async function onStartup() {
try {
updatedEntityJson = await fetchEntityData();
updatedAssetJson = await fetchAssetData();
const copyAssetJsonBtn = document.getElementById("copyEntityJsonBtn");
copyAssetJsonBtn.disabled = true;
} catch (error) {
console.error("Error during startup:", error);
}
}
// Fetch JSON data from the provided URL
async function fetchAssetData() {
const timestamp = new Date().getTime(); // Generate a timestamp
const url = `https://raw.githubusercontent.com/coinhall/yacar/main/injective/asset.json?timestamp=${timestamp}`;
try {
const response = await fetch(url);
const data = await response.json();
return data;
} catch (error) {
console.error("Error fetching assetData:", error);
throw error; // Rethrow the error for the calling function to handle
}
}
function clearAssetForm() {
const formContainer = document.getElementById("assetForm");
for (const input of formContainer.getElementsByTagName("input")) {
input.value = "";
}
// Hide the form container
formContainer.style.display = "none";
const copyAssetJsonBtn = document.getElementById("copyAssetJsonBtn");
copyAssetJsonBtn.disabled = false;
}
// Display asset information in the form
function displayAssetForm(asset) {
const formContainer = document.getElementById("assetForm");
formContainer.innerHTML = "";
const assetFields = [
"id",
"entity",
"name",
"symbol",
"decimals",
"type",
"verification_tx",
"circ_supply",
"total_supply",
"circ_supply_api",
"total_supply_api",
"icon",
"coinmarketcap",
"coingecko",
];
for (const field of assetFields) {
const label = document.createElement("label");
label.innerHTML = `${field}:`;
const input = document.createElement("input");
input.type = "text";
input.value = asset[field] || ""; // Use an empty string if the field is undefined
input.id = field;
formContainer.appendChild(label);
formContainer.appendChild(input);
}
}
async function searchAsset() {
const assetId = document.getElementById("assetId").value;
const assetData = updatedAssetJson;
const foundAsset = assetData.find((asset) => asset.id === assetId);
const formContainer = document.getElementById("assetForm");
if (foundAsset) {
displayAssetForm(foundAsset);
// Show the form container
formContainer.style.display = "block";
} else {
// Asset not found, pre-fill the ID and display an empty form
displayAssetForm({ id: assetId, name: "", symbol: "", decimals: "", type: "" });
// Show the form container
formContainer.style.display = "block";
}
const copyAssetJsonBtn = document.getElementById("copyAssetJsonBtn");
copyAssetJsonBtn.disabled = true;
}
async function updateAsset() {
const formContainer = document.getElementById("assetForm");
const updatedAsset = {};
// Validate required fields
const requiredFields = ["name", "symbol", "decimals", "id", "type"];
for (const field of requiredFields) {
const input = document.getElementById(field);
if (!input.value.trim()) {
alert(`${field} is required`);
return;
}
updatedAsset[field] = input.value;
}
for (const input of formContainer.getElementsByTagName("input")) {
updatedAsset[input.id] = input.value;
}
try {
// Fetch the entire assetData array
let assetData = updatedAssetJson;
// Find the index of the asset to update
const assetIndex = assetData.findIndex((asset) => asset.id === updatedAsset.id);
if (assetIndex !== -1) {
// Update the specific entry in the assetData array
assetData[assetIndex] = { ...assetData[assetIndex], ...updatedAsset };
updatedAssetJson = assetData;
// Show a popup notification
alert("Asset Updated!");
} else {
alert("Asset not found in the array.");
}
} catch (error) {
console.error("Error fetching or updating assetData:", error);
}
clearAssetForm();
}
async function fetchEntityData() {
const response = await fetch(
"https://raw.githubusercontent.com/coinhall/yacar/main/injective/entity.json"
);
const data = await response.json();
return data;
}
// Display entity information in the form
function displayEntityForm(entity) {
const formContainer = document.getElementById("assetForm");
formContainer.innerHTML = "";
const entityFields = ["name", "website", "telegram", "twitter", "discord"];
for (const field of entityFields) {
const label = document.createElement("label");
label.innerHTML = `${field}:`;
const input = document.createElement("input");
input.type = "text";
input.value = entity[field] || "";
input.id = field;
formContainer.appendChild(label);
formContainer.appendChild(input);
}
}
async function updateEntityAndAssets() {
const entityName = document.getElementById("entityName").value;
const entityData = updatedEntityJson;
const assetData = updatedAssetJson;
const updatedEntity = {
name: entityName,
website: document.getElementById("entityWebsite").value || undefined,
telegram: document.getElementById("entityTelegram").value || undefined,
twitter: document.getElementById("entityTwitter").value || undefined,
discord: document.getElementById("entityDiscord").value || undefined,
};
// Update the entity data
const existingEntityIndex = entityData.findIndex(
(entity) => entity.name === entityName
);
if (existingEntityIndex !== -1) {
entityData[existingEntityIndex] = updatedEntity;
} else {
entityData.push(updatedEntity);
}
// Update corresponding assets if asset IDs are provided
const assetIdsInput = document.getElementById("assetIds").value;
const assetIdsArray = assetIdsInput ? assetIdsInput.split(",") : [];
for (const assetId of assetIdsArray) {
const existingAssetIndex = assetData.findIndex(
(asset) => asset.id === assetId
);
if (existingAssetIndex !== -1) {
assetData[existingAssetIndex].entity = entityName;
}
}
updatedEntityJson = entityData;
updatedAssetJson = assetData;
navigator.clipboard
.writeText(updatedEntityJson)
.then(() =>
alert(
"Entity added and entity.json and asset.json are updated! Please copy both of them and do a github pr with the updated info!"
)
)
.catch((err) => console.error("Error copying to clipboard:", err));
const copyAssetJsonBtn = document.getElementById("copyEntityJsonBtn");
copyAssetJsonBtn.disabled = false;
}
// Copy entity.json to clipboard
async function copyEntityJson() {
// Copy to clipboard
const updatedJson = /* Update logic here */ JSON.stringify(updatedEntityJson, null, 2);
// Copy to clipboard
navigator.clipboard
.writeText(updatedJson)
.then(() => alert("entity.json copied to clipboard!"))
.catch((err) => console.error("Error copying to clipboard:", err));
}
// Copy asset.json to clipboard
async function copyAssetJson() {
// Copy to clipboard
const updatedJson = /* Update logic here */ JSON.stringify(updatedAssetJson, null, 2);
navigator.clipboard
.writeText(updatedJson)
.then(() => alert("asset.json copied to clipboard!"))
.catch((err) => console.error("Error copying to clipboard:", err));
}
onStartup();