Skip to content

Commit

Permalink
Merge pull request #407 from zikriya/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
zikriya authored May 21, 2024
2 parents e7bdaaf + 26e070a commit 7c8d179
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 0 deletions.
33 changes: 33 additions & 0 deletions app/controllers/api/v1/randomOrdinalsPassportKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {
attachAddressWithRandomKey,
findRandomKeyByTag,
} from "../../../helpers/multiSwapHelpers/randomOrdinalsPassportKeyHelper";
let asyncMiddleware = require("../../../lib/response/asyncMiddleware");

module.exports = function (router: any) {
router.post(
"/attach/address",
asyncMiddleware(async (req: any, res: any) => {
const { address } = req.body;
if (!address) {
return res.http400("address is required.");
}
return res.http200({
data: await attachAddressWithRandomKey(address),
});
})
);

router.get(
"/:tag",
asyncMiddleware(async (req: any, res: any) => {
const { tag } = req.params;
if (!tag) {
return res.http400("tag is required.");
}
return res.http200({
data: await findRandomKeyByTag(tag),
});
})
);
};
22 changes: 22 additions & 0 deletions app/controllers/api/v1/super-admin/randomOrdinalsPassportKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { isValidObjectId } from "mongoose";
import { saveRandomKeys } from "../../../../helpers/multiSwapHelpers/randomOrdinalsPassportKeyHelper";
let stringHelper = (global as any).stringHelper;

module.exports = function (router: any) {
router.post("/import", async (req: any, res: any) => {
if (!req.body.data || req.body.data.length == 0) {
return res.http400("data is required.");
}
await saveRandomKeys(req.body.data);
return res.http200({
message: stringHelper.strSuccess,
});
});

router.delete("/:id", async (req: any, res: any) => {
await db.RandomOrdinalsPassportKeys.remove({ _id: req.params.id });
return res.http200({
message: stringHelper.strSuccess,
});
});
};
69 changes: 69 additions & 0 deletions app/helpers/multiSwapHelpers/randomOrdinalsPassportKeyHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
let stringHelper = (global as any).stringHelper;
const TAGS = [
"BadKarma",
"ShadowPulse",
"NeoBlade",
"YellowSnowman",
"BlueSnowman",
"HeartTicker",
"Crazy-Cat-Lady",
"unfriendme",
"Babushka",
"SaintBroseph",
"iNeed2p",
"FrostedCupcake",
];

export const saveRandomKeys = async (data: any) => {
let count = await db.RandomOrdinalsPassportKeys.countDocuments({});
for (let item of data) {
if (item && (await isNewRandomKey(item))) {
count = count + 1;
let body = {
tag: getTag(count),
key: item,
createdAt: new Date(),
updatedAt: new Date(),
};
await db.RandomOrdinalsPassportKeys.create(body);
}
}
};

export const attachAddressWithRandomKey = async (address: string) => {
let keyItem = await db.RandomOrdinalsPassportKeys.findOne({
address: address,
});
if (keyItem) {
return keyItem;
}
let data = await db.RandomOrdinalsPassportKeys.findOne({ address: "" });
if (!data) {
throw stringHelper.strErrorNoRandmonKeyAvailable;
}
data = await db.RandomOrdinalsPassportKeys.findOneAndUpdate(
{ _id: data._id },
{ address: address },
{ new: true }
);
return data;
};

export const findRandomKeyByTag = async (tag: string) => {
let data = await db.RandomOrdinalsPassportKeys.findOne({ tag: tag });
return data;
};

const isNewRandomKey = async (key: any): Promise<boolean> => {
let count = await db.RandomOrdinalsPassportKeys.countDocuments({ key: key });
return count > 0 ? false : true;
};

const getTag = (count: number): string => {
let index = 0;
try {
index = Math.floor(Math.random() * TAGS.length);
index = index < TAGS.length ? index : 0;
} catch (e) {}
return `${TAGS[index]}-${count}`;
};
2 changes: 2 additions & 0 deletions app/helpers/stringHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ let invalidHashMessage = "Backend_Invalid_Hash_Message";
let swapFailedMessage = "Backend_Swap_Failed_Message";
var strErrorCabnAlreadyExist = "Backend_Error_Cabn_Already_Exist";
var strErrorNodePairAlreadyExist = "This pair already exist.";
var strErrorNoRandmonKeyAvailable = "No randmon key available";

// success messages
let strSuccessResetPasswordLink = "Backend_Success_Reset_Password_Link";
Expand Down Expand Up @@ -164,4 +165,5 @@ module.exports = {
strErrorNodePairAlreadyExist,
chainIdNotSupported,
strErrorCabnAlreadyExist,
strErrorNoRandmonKeyAvailable,
};
21 changes: 21 additions & 0 deletions app/models/randomOrdinalsPassportKeys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"use strict";
var mongoose = require("mongoose");

var schema = mongoose.Schema(
{
key: { type: String, default: "" },
address: { type: String, default: "" },
tag: { type: String, default: "" },
isActive: { type: Boolean, default: true },
isNonEVM: { type: Boolean, default: false },
createdAt: { type: Date, default: new Date() },
updatedAt: { type: Date, default: new Date() },
},
{ collection: "randomOrdinalsPassportKeys" }
);

var randomOrdinalsPassportKeys = mongoose.model(
"randomOrdinalsPassportKeys",
schema
);
module.exports = randomOrdinalsPassportKeys;
1 change: 1 addition & 0 deletions app/models/swapAndWithdrawTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ var schema = mongoose.Schema(
messageHash: { type: String, default: "" },
},
distributedFee: { type: String, default: "" },
referralCode: { type: String, default: "" },
},
{ collection: "swapAndWithdrawTransactions" }
);
Expand Down

0 comments on commit 7c8d179

Please sign in to comment.