Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: referral api & get all wallet api #2

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"description": "",
"dependencies": {
"body-parser": "^1.20.2",
"crypto": "^1.0.1",
"dotenv": "^16.4.5",
"ethereum-address": "^0.0.4",
"express": "^4.19.2",
Expand Down
13 changes: 12 additions & 1 deletion src/controller/wallet.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { WalletModel } = require("../schema/wallet");
const { generateReferralCode } = require("../utils/utils");

exports.getWallet = async function (req, res) {
try {
Expand All @@ -11,6 +12,15 @@ exports.getWallet = async function (req, res) {
}
};

exports.getAllWallets = async function (req, res) {
try {
const result = await WalletModel.find().select("-_id -__v").sort('-points');
return res.status(200).json(result);
} catch (error) {
return res.status(500).json({ error: error.message });
}
};

exports.getWalletRank = async function (req, res) {
try {
const address = req.params.address;
Expand Down Expand Up @@ -57,9 +67,10 @@ exports.create = async function (req, res) {
if (!requestBody.address) {
return res.status(400).json({ error: "Invalid request params" });
}
const refCode = generateReferralCode(requestBody.address);
await WalletModel.findOneAndUpdate(
{ address: requestBody.address },
{ $setOnInsert: { points: 0 } },
{ $setOnInsert: { points: 0, refCode } },
{ upsert: true }
);

Expand Down
3 changes: 2 additions & 1 deletion src/router/WalletRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ const express = require("express");
const router = express.Router();
const WalletController = require("../controller/wallet");

router.get("/:address", WalletController.getWallet);
router.get("/getAll", WalletController.getAllWallets);
router.get("/rank/:address", WalletController.getWalletRank);
router.get("/:address", WalletController.getWallet);
router.post("/create", WalletController.create);
router.post("/addPoints", WalletController.addPoints);

Expand Down
1 change: 1 addition & 0 deletions src/schema/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const mongoose = require("mongoose");
const walletSchema = new mongoose.Schema({
address: { type: String, unique: true },
points: Number,
refCode: String,
});

const WalletModel = mongoose.model("Wallet", walletSchema);
Expand Down
14 changes: 5 additions & 9 deletions src/utils/utils.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
const stripCosmosSystemValues = (obj) => {
const systemKeys = ["_id", "__v"];
const crypto = require("crypto");

const strippedObj = { ...obj };
const generateReferralCode = (walletAddress) => {
const hash = crypto.createHash("sha1").update(walletAddress).digest("hex");

for (const key of systemKeys) {
delete strippedObj[key];
}

return strippedObj;
return hash.substring(0, 8);
};

module.exports = { stripCosmosSystemValues };
module.exports = { generateReferralCode };
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ crypto-js@^3.1.6:
resolved "https://registry.yarnpkg.com/crypto-js/-/crypto-js-3.3.0.tgz#846dd1cce2f68aacfa156c8578f926a609b7976b"
integrity sha512-DIT51nX0dCfKltpRiXV+/TVZq+Qq2NgF4644+K7Ttnla7zEzqc+kjJyiB96BHNyUTBxyjzRcZYpUdZa+QAqi6Q==

crypto@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/crypto/-/crypto-1.0.1.tgz#2af1b7cad8175d24c8a1b0778255794a21803037"
integrity sha512-VxBKmeNcqQdiUQUW2Tzq0t377b54N2bMtXO/qiLa+6eRRmmC4qT3D4OnTGoT/U6O9aklQ/jTwbOtRMTTY8G0Ig==

[email protected]:
version "2.6.9"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
Expand Down
Loading