@ixily/activ
Version:
Alpha Capture Trade Idea Verification. Blockchain ownership proven trade ideas and strategies.
310 lines (309 loc) • 11.3 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.copyObj = exports.isDecryptableIdea = exports.deserializeDataTool = exports.serializeDataTool = exports.getBasicNFTsInflatableByMyIdeaKeys = exports.getBasicNFTInflatableByMyIdeaKeys = exports.placeholderError = exports.retryFunctionHelper = exports.loop = exports.getBoolean = exports.isNullOrWhiteSpace = exports.isNullOrUndefined = exports.isUrl = exports.generateUUID = exports.wait = exports.rest = exports.randomNumberBetween = exports.skipExpirationInSeconds = void 0;
const serialize_javascript_1 = __importDefault(require("serialize-javascript"));
// in this case when the idea is closed we don't need cache then we update the expiration time to 100 years :)
exports.skipExpirationInSeconds = 60 * 60 * 24 * 365 * 100;
const randomNumberBetween = (minimum, limit) => {
return Math.floor(Math.random() * (limit - minimum) + minimum);
};
exports.randomNumberBetween = randomNumberBetween;
const rest = async (delay) => {
await new Promise((resolve) => setTimeout(resolve, delay));
};
exports.rest = rest;
const wait = (time = 1000) => {
return new Promise((resolve, reject) => {
try {
const interval = setInterval(() => {
clearInterval(interval);
resolve();
}, time);
}
catch (err) {
reject();
}
});
};
exports.wait = wait;
const generateUUID = () => {
let d = new Date().getTime();
let d2 = (typeof performance !== 'undefined' &&
performance.now &&
performance.now() * 1000) ||
0;
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
let r = Math.random() * 16;
if (d > 0) {
r = (d + r) % 16 | 0;
d = Math.floor(d / 16);
}
else {
r = (d2 + r) % 16 | 0;
d2 = Math.floor(d2 / 16);
}
return (c === 'x' ? r : (r & 0x3) | 0x8).toString(16);
});
};
exports.generateUUID = generateUUID;
const isUrl = (url) => {
return /^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:[/?#]\S*)?$/i.test(url);
};
exports.isUrl = isUrl;
const isNullOrUndefined = (value) => {
const checkValue = ['undefined', 'null', undefined, null]?.includes(value)
? undefined
: value;
const check = checkValue === undefined || null ? true : false;
return check;
};
exports.isNullOrUndefined = isNullOrUndefined;
const isNullOrWhiteSpace = (value) => {
return (0, exports.isNullOrUndefined)(value) || value?.trim()?.length === 0;
};
exports.isNullOrWhiteSpace = isNullOrWhiteSpace;
const getBoolean = (value, defaultValue = false) => {
const data = {
true: true,
false: false,
1: true,
0: false,
undefined: false,
null: false,
};
const response = data[value] || defaultValue;
return response;
};
exports.getBoolean = getBoolean;
const loop = (next, validator, settings, errorCallback) => new Promise(async (resolve, reject) => {
try {
// default settings
if (!settings?.loopTimeInMs || settings?.loopTimeInMs <= 0) {
//@ts-ignore
settings.loopTimeInMs = 5000;
}
if (!settings?.limitTimeSecond || settings?.limitTimeSecond <= 0) {
//@ts-ignore
settings.limitTimeSecond = 60;
}
const loopTimeInMs = settings?.loopTimeInMs;
const limitTimeSecond = settings?.limitTimeSecond;
//@ts-ignore
const loopTimeInMsToSecond = Math.floor((loopTimeInMs / 1000) % 60);
// check
//@ts-ignore
if (loopTimeInMsToSecond >= limitTimeSecond) {
throw new Error('The loop can not be greater than limit.');
}
const startDate = new Date();
let interval = null;
interval = setInterval(async () => {
try {
const status = await validator();
const endDate = new Date();
const seconds = (endDate.getTime() - startDate.getTime()) / 1000;
// If we exceed the standby limit, we cut the process
if (seconds >= limitTimeSecond && !status) {
clearInterval(interval);
if (errorCallback) {
await errorCallback('Time limit exceeded');
}
throw new Error('Time limit exceeded');
}
// if validator is completed!
if (status) {
clearInterval(interval);
interval = null;
await next();
resolve();
}
}
catch (err) {
reject(err);
}
}, loopTimeInMs);
}
catch (err) {
if (errorCallback) {
await errorCallback(err?.message);
}
reject(err);
}
});
exports.loop = loop;
const retryFunctionHelper = async (payload) => {
let retryCount = 1;
// this is the function that will be called to notify the caller of the error
// using slack or email or whatever instead of throwing an error
const notify = async (errMsg, retryCount) => {
try {
if (payload?.notificationCallback) {
await payload.notificationCallback(errMsg, retryCount);
}
}
catch (err) {
console.log('retryFunctionHelper [Error notifying]', `${err?.message}. Retry #${retryCount}`);
}
};
try {
const { maxRetries, retryCallback } = payload;
while (retryCount <= maxRetries) {
try {
const data = await retryCallback(retryCount);
if (data) {
return data;
}
else {
await notify('No data returned', retryCount);
}
}
catch (err) {
await notify(err?.message || 'Unknown error', retryCount);
// optionally reject on max retries
if (payload?.rejectOnMaxRetries &&
retryCount === payload.maxRetries) {
throw new Error(err?.message || 'Unknown error');
}
}
if (retryCount < maxRetries) {
// await 5 second before retrying
await (0, exports.wait)(5000);
}
retryCount++;
}
return null;
}
catch (err) {
await notify(err?.message || 'Unknown error', retryCount);
// optionally reject on max retries
if (payload?.rejectOnMaxRetries && retryCount === payload.maxRetries) {
throw new Error(err?.message || 'Unknown error');
}
}
};
exports.retryFunctionHelper = retryFunctionHelper;
const placeholderError = (errorMessage, placeholderReturned) => {
throw new Error(errorMessage);
return placeholderReturned;
};
exports.placeholderError = placeholderError;
// export const IdeaStatusByKind: {
// [key in CONTRACT_INTERFACES.ITradeIdeaIdeaKind]: IdeaStatus
// } = {
// open: 1,
// adjust: 2,
// close: 3,
// }
// export const IdeaKindByStatus: {
// [key in number]: CONTRACT_INTERFACES.ITradeIdeaIdeaKind
// } = {
// 1: 'open',
// 2: 'adjust',
// 3: 'close',
// }
// export const kindTypeToIdeaStatus = (
// kindType: CONTRACT_INTERFACES.ITradeIdeaIdeaKind[],
// ): IdeaStatus[] => {
// return kindType.map((kind) => IdeaStatusByKind[kind])
// }
// export const IdeaStatusToKindType = (
// status: IdeaStatus[],
// ): CONTRACT_INTERFACES.ITradeIdeaIdeaKind[] => {
// return status.map((s) => IdeaKindByStatus[s])
// }
// export const getBasicNFTByNftId = (res: number) => {
// if(typeof res !== 'number') {
// throw new Error('Check contract return format here')
// }
// let result: IBasicNFT = {
// id: res,
// strategyReference: res[1],
// status: res[2].toNumber(),
// isPublic: res[3],
// }
// return result
// }
const getBasicNFTInflatableByMyIdeaKeys = (idKeys) => {
let result = {
id: idKeys.nftId,
strategyReference: idKeys.strategyKey,
strategyUniqueReference: idKeys.strategyUniqueKey,
};
return result;
};
exports.getBasicNFTInflatableByMyIdeaKeys = getBasicNFTInflatableByMyIdeaKeys;
// export const getBasicNFTsByNftIds = (data: number[]) => {
// let result: IBasicNFT[] = data.map((res: number) => {
// return getBasicNFTByNftId(res) as IBasicNFT
// })
// return result
// }
const getBasicNFTsInflatableByMyIdeaKeys = (data) => {
let result = data.map((idKeys) => {
return (0, exports.getBasicNFTInflatableByMyIdeaKeys)(idKeys);
});
return result;
};
exports.getBasicNFTsInflatableByMyIdeaKeys = getBasicNFTsInflatableByMyIdeaKeys;
const serializeDataTool = (obj) => {
return (0, serialize_javascript_1.default)(obj);
};
exports.serializeDataTool = serializeDataTool;
const deserializeDataTool = (serializedJavascript) => {
return eval('(' + serializedJavascript + ')');
};
exports.deserializeDataTool = deserializeDataTool;
const isDecryptableIdea = (idea, myWallet) => {
if (typeof idea.idea === 'string') {
if (idea.creator.walletAddress === myWallet) {
return true;
}
if (idea.access !== undefined) {
if (idea.access.wallets.includes(myWallet)) {
return true;
}
}
}
return false;
};
exports.isDecryptableIdea = isDecryptableIdea;
const copyObj = (obj) => {
return (0, exports.deserializeDataTool)((0, exports.serializeDataTool)(obj));
};
exports.copyObj = copyObj;
/*
export const floodProtectiveWrapper = async <T>(
_f: () => Promise<T>,
_fName: string,
times: number = 10,
): Promise<T> => {
let attempts = 0
let value: T | 'null' | undefined = 'null'
const randomBetweenOneAnd20 = Math.floor(Math.random() * 20) + 1
await new Promise((resolve) => setTimeout(resolve, randomBetweenOneAnd20))
while (value === 'null') {
try {
value = await _f()
} catch (err) {
if (attempts >= times) {
const errorParsed =
(err as unknown as any).message || JSON.stringify(err)
console.error(errorParsed)
throw new Error(
`Error in ${_fName} ` + times + ` times as logged.`,
)
}
const randomBetweenOneAnd20 = Math.floor(Math.random() * 20) + 1
await new Promise((resolve) =>
setTimeout(resolve, randomBetweenOneAnd20 * 10),
)
}
attempts++
}
return value
}
*/
//# sourceMappingURL=helpers.tool.js.map