Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin' into pairs-toggle-3
Browse files Browse the repository at this point in the history
  • Loading branch information
saidam90 committed Aug 4, 2024
2 parents 50d8597 + ed45039 commit cee0da1
Show file tree
Hide file tree
Showing 40 changed files with 1,241 additions and 158 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@ next-env.d.ts

localhost:3000/
.env

# Script outputs
*/.scriptOutputs/
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
# instead of duplicating the .gitignore file,
# but duplicating way is more IDE friendly

# fetchUsers json output
scripts/.scriptOutputs/

# dependencies
/node_modules
/.pnp
Expand Down
226 changes: 226 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

73 changes: 59 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"test": "vitest",
"test-integration": "playwright test",
"test-all": "npm run lint && npm run test && npm run test-integration",
"copy-pr": "node ./scripts/copy-pr.js"
"copy-pr": "node ./scripts/copy-pr.js",
"fetchUsers": "node ./scripts/fetchUsers.js"
},
"dependencies": {
"@mdx-js/loader": "^3.0.0",
Expand All @@ -33,13 +34,16 @@
"js-cookie": "^3.0.5",
"lightweight-charts": "^4.1.3",
"next": "^14.1.1",
"papaparse": "^5.4.1",
"postcss": "^8.4.32",
"react": "^18.2.0",
"react-countup": "^6.5.3",
"react-dom": "18.2.0",
"react-hot-toast": "^2.4.1",
"react-icons": "^4.11.0",
"react-redux": "^8.1.2",
"resize-observer-polyfill": "^1.5.1",
"tailwind-merge": "^2.4.0",
"tailwindcss": "3.3.2",
"typescript": "5.1.6"
},
Expand All @@ -49,11 +53,12 @@
"@testing-library/react": "^14.2.2",
"@types/big.js": "^6.2.2",
"@types/js-cookie": "^3.0.6",
"@types/papaparse": "^5.3.14",
"@typescript-eslint/eslint-plugin": "^5.60.1",
"@typescript-eslint/parser": "^5.60.1",
"@vitejs/plugin-react": "^4.2.1",
"cross-env": "^7.0.3",
"daisyui": "^3.1.7",
"daisyui": "^4.12.10",
"dotenv": "^16.4.5",
"eslint": "^8.44.0",
"eslint-config-prettier": "^8.8.0",
Expand Down
1 change: 1 addition & 0 deletions public/wallet.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
147 changes: 147 additions & 0 deletions scripts/fetchUsers.js
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);
})();
Loading

0 comments on commit cee0da1

Please sign in to comment.