-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin' into pairs-toggle-3
- Loading branch information
Showing
40 changed files
with
1,241 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,3 +42,6 @@ next-env.d.ts | |
|
||
localhost:3000/ | ||
.env | ||
|
||
# Script outputs | ||
*/.scriptOutputs/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
|
||
const fetchUsers = async () => { | ||
// fetch all pairs | ||
const pairsList = await fetchAllPairs(); | ||
|
||
// fetch all orders for all pairs | ||
const allOrdersByPairs = await Promise.all( | ||
pairsList.map((pair) => { | ||
const { address, lastOrderId } = pair; | ||
const orderIds = Array.from({ length: lastOrderId }, (_, i) => i + 1); // [1, ..., lastOrderId] | ||
|
||
return fetchOrdersByPair(address, orderIds); | ||
}) | ||
); | ||
const allOrders = allOrdersByPairs.flat(); | ||
|
||
// Process orders to count wallet addresses | ||
const usersDict = {}; | ||
for (let i = 0; i < allOrders.length; i++) { | ||
const radixWalletAddress = allOrders[i].settlementAccount; // account address for this order | ||
if (!radixWalletAddress) { | ||
continue; | ||
} | ||
usersDict[radixWalletAddress] = usersDict[radixWalletAddress] | ||
? usersDict[radixWalletAddress] + 1 | ||
: 1; | ||
} | ||
|
||
return { | ||
usersDict: usersDict, | ||
totalUsers: Object.keys(usersDict).length, | ||
totalOrders: Object.values(usersDict).reduce((a, b) => a + b, 0), | ||
}; | ||
}; | ||
|
||
//** Helpers */ | ||
// fetch all pairs | ||
const fetchAllPairs = async () => { | ||
try { | ||
const allPairsResponse = await fetch( | ||
"https://api.alphadex.net/v0/alphadex/pairs", | ||
{ | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
} | ||
); | ||
|
||
if (!allPairsResponse.ok) { | ||
throw new Error("Error while fetching pairs"); | ||
} | ||
|
||
const allPairsData = await allPairsResponse.json(); | ||
const pairsList = allPairsData?.pairs || []; | ||
|
||
return pairsList; | ||
} catch (error) { | ||
console.error("fetchAllPairs -> error", error); | ||
return []; | ||
} | ||
}; | ||
|
||
// fetch orders | ||
const fetchOrdersByPair = async (pairAddress, orderIds) => { | ||
const allOrders = []; | ||
const chunks = getChunkArray(orderIds, 99); | ||
|
||
// fetch all orders by batches of 99 orders | ||
for (const chunkOrderIds of chunks) { | ||
try { | ||
// eslint-disable-next-line no-console | ||
console.log("Fetching batch for " + pairAddress); | ||
const response = await fetch("https://api.alphadex.net/v0/pair/orders", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ pairAddress, orderIds: chunkOrderIds }), | ||
}); | ||
|
||
const data = await response.json(); | ||
allOrders.push(...data.orders); | ||
} catch (err) { | ||
// eslint-disable-next-line no-console | ||
console.log(err); | ||
} | ||
} | ||
|
||
return allOrders; | ||
}; | ||
|
||
const getChunkArray = (array, size) => { | ||
const chunkArray = []; | ||
for (let i = 0; i < array.length; i += size) { | ||
chunkArray.push(array.slice(i, i + size)); | ||
} | ||
|
||
return chunkArray; | ||
}; | ||
|
||
const getTimestampedFileName = () => { | ||
// Get the current date and time | ||
const now = new Date(); | ||
const year = now.getFullYear(); | ||
const month = String(now.getMonth() + 1).padStart(2, "0"); | ||
const day = String(now.getDate()).padStart(2, "0"); | ||
const hours = String(now.getHours()).padStart(2, "0"); | ||
const minutes = String(now.getMinutes()).padStart(2, "0"); | ||
const seconds = String(now.getSeconds()).padStart(2, "0"); | ||
// Format the filename | ||
return `${year}-${month}-${day}_${hours}${minutes}${seconds}_fetchUsers-output.json`; | ||
}; | ||
|
||
const getFilePath = () => { | ||
const filename = getTimestampedFileName(); | ||
const directory = path.join(__dirname, ".scriptOutputs"); | ||
// Ensure the directory exists | ||
if (!fs.existsSync(directory)) { | ||
fs.mkdirSync(directory, { recursive: true }); | ||
} | ||
// Return the full path for the file | ||
return path.join(directory, filename); | ||
}; | ||
|
||
const writeObjectToFile = (obj) => { | ||
const jsonString = JSON.stringify(obj, null, 2); | ||
const filePath = getFilePath(); | ||
fs.writeFile(filePath, jsonString, (err) => { | ||
if (err) { | ||
console.error("Error writing file:", err); | ||
} else { | ||
// eslint-disable-next-line no-console | ||
console.log(`Successfully saved output to file: ${filePath}`); | ||
} | ||
}); | ||
}; | ||
|
||
// RUN SCRIPT | ||
(async () => { | ||
const result = await fetchUsers(); | ||
// eslint-disable-next-line no-console | ||
console.log(result); | ||
writeObjectToFile(result); | ||
})(); |
Oops, something went wrong.