@ixily/activ
Version:
Alpha Capture Trade Idea Verification. Blockchain ownership proven trade ideas and strategies.
533 lines • 19.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ViewsSourceMessModule = void 0;
const __1 = require("../..");
const config_module_1 = require("./config.module");
const state = {
servers: undefined,
};
const startLocalServer = async (details) => {
if (state.servers === undefined) {
state.servers = [];
state.servers = await __1.Mess.serveContractsDb([
{
blockchainNetwork: 'amoy',
contractName: 'v4',
details,
},
{
blockchainNetwork: 'polygon',
contractName: 'v4',
details,
},
]);
state.servers.forEach((server) => {
server.error$.subscribe((error) => {
if (error !== undefined) {
console.error(error);
throw error;
}
});
});
}
else {
state.servers.forEach((server) => {
server.resume();
});
}
};
config_module_1.ConfigModule.getConfigUpdatedStream().subscribe((config) => {
if (config !== undefined) {
if (config.dataSource !== undefined) {
if (config.dataSource.publicCache.source === 'local') {
if (config.dataSource.publicCache.autoServe !== false) {
startLocalServer(config.web3AuthorizationOptions);
}
}
}
}
});
const pauseLocalServer = () => {
if (state.servers !== undefined) {
state.servers.forEach((server) => {
server.pause();
});
}
};
const listContracts = async () => {
return __1.Mess.contractsInMemoryDbView.listAllContracts();
};
const getCreator = async (contract, creatorWallet) => {
return __1.Mess.contractsInMemoryDbView.getCreator(contract, creatorWallet);
};
const getIdeaByUniqueId = async (uniqueIdeaId) => {
return __1.Mess.contractsInMemoryDbView.getIdeaData(uniqueIdeaId);
};
const listCreatorStrategies = async (contract, creatorWallet, page = 1, limit = 5) => {
try {
const preCut = await __1.Mess.contractsInMemoryDbView.listContractCreatorStrategies(contract, creatorWallet);
preCut.reverse();
const preInflate = await (0, __1.getDataWithPaging)({
data: preCut,
paging: {
page: page || 1,
limit: limit || 5,
},
});
const result = {
...preInflate,
data: await Promise.all(preInflate.data.map((strategyKey) => __1.Mess.contractsInMemoryDbView.getStrategyWithCreator(strategyKey))),
};
return result;
}
catch (err) {
if (typeof err.message === 'string') {
if (err.message.indexOf('No creator found for creatorWallet') !== -1) {
return (0, __1.voidPaging)(page, limit);
}
else {
throw err;
}
}
else {
throw err;
}
}
};
const listStrategiesWithAccessibleIdeasBy = async (contract, accessorWallet, page = 1, limit = 5) => {
try {
const preCut = await __1.Mess.contractsInMemoryDbView.listContractUserWithAccessibleIdeasStrategies(contract, accessorWallet);
const preInflate = await (0, __1.getDataWithPaging)({
data: preCut,
paging: {
page: page || 1,
limit: limit || 5,
},
});
const result = {
...preInflate,
data: await Promise.all(preCut.map((strategyKey) => __1.Mess.contractsInMemoryDbView.getStrategyWithCreator(strategyKey))),
};
return result;
}
catch (err) {
if (typeof err.message === 'string') {
if (err.message.indexOf('No contractDb found for uniqueContractKey:') !== -1) {
return (0, __1.voidPaging)(page, limit);
}
else {
throw err;
}
}
else {
throw err;
}
}
};
const listStrategiesSubscribedToBy = async (contract, accessorWallet, page = 1, limit = 5) => {
try {
const preCut = await __1.Mess.contractsInMemoryDbView.listContractUserSubscribedToIdeasStrategies(contract, accessorWallet);
const preInflate = await (0, __1.getDataWithPaging)({
data: preCut,
paging: {
page: page || 1,
limit: limit || 5,
},
});
const result = {
...preInflate,
data: await Promise.all(preCut.map((strategyKey) => __1.Mess.contractsInMemoryDbView.getStrategyWithCreator(strategyKey))),
};
return result;
}
catch (err) {
if (typeof err.message === 'string') {
if (err.message.indexOf('No contractDb found for uniqueContractKey:') !== -1) {
return (0, __1.voidPaging)(page, limit);
}
else {
throw err;
}
}
else {
throw err;
}
}
};
const listStrategiesPublic = async (contract, page = 1, limit = 5) => {
let contracts = [];
if (contract !== undefined) {
if (typeof contract === 'string') {
if (contract.length > 1) {
const keyContracts = await __1.Mess.contractsInMemoryDbView.listAllContracts();
if (keyContracts.find((one) => one === contract) !== undefined) {
contracts.push(contract);
}
else {
// not fount contract key error
return (0, __1.voidPaging)(page, limit);
}
}
else {
// not fount contract key error
return (0, __1.voidPaging)(page, limit);
}
}
else {
// not fount contract key error
return (0, __1.voidPaging)(page, limit);
}
}
// console.log('&&&&&&&&')
// console.log('contracts')
// console.log(contracts)
// console.log('&&&&&&&&')
if (contracts.length === 0) {
contracts = [...(await __1.Mess.contractsInMemoryDbView.listAllContracts())];
}
const allData = await Promise.all(contracts.map((contract) => (async () => {
// console.log('contract')
// console.log(contract)
const data = (await __1.Mess.contractsInMemoryDbView.listContractWithPublicIdeasStrategies(contract)).reverse();
// console.log('data')
// console.log(data)
return data;
})()));
// console.log('allData')
// console.log(allData)
const preCut = __1.CONTRACT_TOOLS.deterministicEvenlyMixedArray(allData);
const preInflate = await (0, __1.getDataWithPaging)({
data: preCut,
paging: {
page: page || 1,
limit: limit || 5,
},
});
// contract here is unique contract key. we need to translate it into right strategy chain
const result = {
...preInflate,
data: await Promise.all(preInflate.data.map((strategyKey) => __1.Mess.contractsInMemoryDbView.getStrategyWithCreator(strategyKey))),
};
return result;
};
const getPublicStrategyWithCreator = async (strategyUniqueKey) => {
return __1.Mess.contractsInMemoryDbView.getStrategyWithCreator(strategyUniqueKey);
};
const listIdeasUniqueIndexesByLatest = async (contract, page = 1, limit = 5) => {
const preCut = await __1.Mess.contractsInMemoryDbView.listAllIdeasIndexesByContract(contract);
const posReverse = preCut.reverse();
const preInflate = await (0, __1.getDataWithPaging)({
data: posReverse,
paging: {
page: page || 1,
limit: limit || 5,
},
});
return preInflate;
};
const listPublicIdeasUniqueIndexesByLatest = async (contract, page = 1, limit = 5) => {
const preReverse = await __1.Mess.contractsInMemoryDbView.listAllPublicIdeasIndexesByContract(contract);
const preCut = preReverse.reverse();
const preInflate = await (0, __1.getDataWithPaging)({
data: preCut,
paging: {
page: page || 1,
limit: limit || 5,
},
});
return preInflate;
};
/*
const listIdeasIndexes = async (
contract: string,
page: number = 1,
limit: number = 5,
): Promise<IPaging<MyIdeaKeys>> => {
const indexes = await listIdeasUniqueIndexesByLatest(contract, page, limit)
const dataKeys = await Promise.all(
indexes.data.map((ideaKey) =>
Mess.contractsInMemoryDbView.getIdeaKeys(ideaKey),
),
)
const result: IPaging<MyIdeaKeys> = {
...indexes,
data: dataKeys,
}
return result
}
*/
const listLatestIdeas = async (contract, page = 1, limit = 5) => {
const keys = await listIdeasUniqueIndexesByLatest(contract, page, limit);
const result = {
...keys,
data: await Promise.all(keys.data.map((ideaKey) => __1.Mess.contractsInMemoryDbView.getIdeaData(ideaKey))),
};
return result;
};
const listLatestPublicIdeas = async (contract, page = 1, limit = 5) => {
let contracts = [];
if (contract !== undefined) {
if (typeof contract === 'string') {
if (contract.length > 1) {
const keyContracts = await __1.Mess.contractsInMemoryDbView.listAllContracts();
if (keyContracts.find((one) => one === contract) !== undefined) {
contracts.push(contract);
}
else {
// not fount contract key error
return (0, __1.voidPaging)(page, limit);
}
}
else {
// not fount contract key error
return (0, __1.voidPaging)(page, limit);
}
}
else {
// not fount contract key error
return (0, __1.voidPaging)(page, limit);
}
}
if (contracts.length === 0) {
contracts = [...(await __1.Mess.contractsInMemoryDbView.listAllContracts())];
}
const allData = await Promise.all(contracts.map((contract) => (async () => {
// console.log('contract')
// console.log(contract)
const data = (await __1.Mess.contractsInMemoryDbView.listAllPublicIdeasIndexesByContract(contract)).reverse();
// console.log('data')
// console.log(data)
return data;
})()));
const preCut = __1.CONTRACT_TOOLS.deterministicEvenlyMixedArray(allData);
const preInflate = await (0, __1.getDataWithPaging)({
data: preCut,
paging: {
page: page || 1,
limit: limit || 5,
},
});
const result = {
...preInflate,
data: await Promise.all(preInflate.data.map((ideaKey) => __1.Mess.contractsInMemoryDbView.getIdeaData(ideaKey))),
};
return result;
};
const listCreatorIdeas = async (contract, creatorWallet, page = 1, limit = 5, filterType = 'bypass', filterIncludeEncrypted = false, bypassPaginationAndGetAll = false) => {
const preReverse = await __1.Mess.contractsInMemoryDbView.listCreatedByIdeasIndexesByContract(contract, creatorWallet);
const preCut = preReverse.reverse();
const postInflate = await Promise.all(preCut.map((ideaKey) => __1.Mess.contractsInMemoryDbView.getIdeaData(ideaKey)));
const postPageFiltered = (0, __1.postQueryIdeasFilterAndPagination)(postInflate, page, limit, filterType, filterIncludeEncrypted, bypassPaginationAndGetAll);
/*
const preInflate = await getDataWithPaging<string>({
data: preCut as string[],
paging: {
page: page || 1,
limit: limit || 5,
},
})
const result: IPaging<CONTRACT_INTERFACES.ITradeIdea> = {
...preInflate,
data: await Promise.all(
preInflate.data.map((ideaKey) =>
Mess.contractsInMemoryDbView.getIdeaData(ideaKey),
),
),
}
*/
return postPageFiltered;
};
const listOwnedIdeas = async (contract, creatorWallet, page = 1, limit = 5, filterType = 'bypass', filterIncludeEncrypted = false, bypassPaginationAndGetAll = false) => {
const preCut = await __1.Mess.contractsInMemoryDbView.listOwnedByIdeasKeysByContract(contract, creatorWallet);
const onlyLastStagesOfEachIdeaIndexes = getOnlyLastStagesOfEachIdeaIndexes(preCut);
onlyLastStagesOfEachIdeaIndexes.reverse();
const postInflate = await Promise.all(onlyLastStagesOfEachIdeaIndexes.map((ideaKey) => __1.Mess.contractsInMemoryDbView.getIdeaData(ideaKey)));
const postPageFiltered = (0, __1.postQueryIdeasFilterAndPagination)(postInflate, page, limit, filterType, filterIncludeEncrypted, bypassPaginationAndGetAll);
/*
const preInflate = await getDataWithPaging<string>({
data: preCut as string[],
paging: {
page: page || 1,
limit: limit || 5,
},
})
const result: IPaging<CONTRACT_INTERFACES.ITradeIdea> = {
...preInflate,
data: await Promise.all(
preInflate.data.map((ideaKey) =>
Mess.contractsInMemoryDbView.getIdeaData(ideaKey),
),
),
}
*/
return postPageFiltered;
};
const getOnlyLastStagesOfEachIdeaIndexes = (ideas) => {
const alreadyKey = new Map();
const ideaKeysOrdered = [];
for (const idKey of ideas) {
if (!alreadyKey.has(idKey.ideaKey)) {
ideaKeysOrdered.push(idKey.ideaKey);
}
alreadyKey.set(idKey.ideaKey, idKey.ideaIndex);
}
// console.log('ideaKeysOrdered')
// console.log(ideaKeysOrdered)
const onlyLatestStagesOrderedIndexes = ideaKeysOrdered.map((each) => alreadyKey.get(each));
return onlyLatestStagesOrderedIndexes;
};
const listStrategyIdeas = async (strategyUniqueKey, page = 1, limit = 5, filterType = 'bypass', filterIncludeEncrypted = true, bypassPaginationAndGetAll = false) => {
const preCut = await __1.Mess.contractsInMemoryDbView.listAllIdeasKeysByStrategy(strategyUniqueKey);
// console.log('preCut')
// console.log(preCut)
const onlyLatestStagesOrderedIndexes = getOnlyLastStagesOfEachIdeaIndexes(preCut);
// console.log('onlyLatestStagesOrderedIndexes')
// console.log(onlyLatestStagesOrderedIndexes)
onlyLatestStagesOrderedIndexes.reverse();
const postInflate = await Promise.all(onlyLatestStagesOrderedIndexes.map((ideaKey) => __1.Mess.contractsInMemoryDbView.getIdeaData(ideaKey)));
// console.log('postInflate')
// console.log(postInflate)
const postPageFiltered = (0, __1.postQueryIdeasFilterAndPagination)(postInflate, page, limit, filterType, filterIncludeEncrypted, bypassPaginationAndGetAll);
return postPageFiltered;
};
const getStrategyPortfolio = async (strategyUniqueKey) => {
const portfolio = await __1.Mess.contractsInMemoryDbView.getStrategyPortfolio(strategyUniqueKey);
return portfolio;
};
const listStrategyRebalances = async (strategyUniqueKey, page = 1, limit = 5) => {
const preCut = await __1.Mess.contractsInMemoryDbView.getStrategyRebalances(strategyUniqueKey);
// console.log('preCut')
// console.log(preCut)
preCut.reverse();
const preInflate = await (0, __1.getDataWithPaging)({
data: preCut,
paging: {
page: page || 1,
limit: limit || 5,
},
});
const result = {
...preInflate,
data: await Promise.all(preInflate.data.map(async (rebalance) => {
let assets = await Promise.all(rebalance.ideaIndexes.map(async (ideaIndex) => {
const lastIdea = await __1.Mess.contractsInMemoryDbView.getIdeaData(ideaIndex);
const result = {
lastIdea,
};
return result;
}));
assets = assets.filter((asset) => {
let kindClose = false;
if (typeof asset.lastIdea.idea !== 'string') {
if (asset.lastIdea
.idea.kind === 'close') {
kindClose = true;
}
}
return !kindClose;
});
const result = {
strategyKey: strategyUniqueKey,
reference: rebalance.reference,
totalAssets: assets.length,
assets,
};
return result;
})),
};
return result;
};
const getStrategyRebalance = async (strategyUniqueKey, rebalanceReference) => {
const rebalance = await __1.Mess.contractsInMemoryDbView.getStrategyRebalance(strategyUniqueKey, rebalanceReference);
let assets = await Promise.all(rebalance.ideaIndexes.map(async (ideaIndex) => {
const lastIdea = await __1.Mess.contractsInMemoryDbView.getIdeaData(ideaIndex);
const result = {
lastIdea,
};
return result;
}));
assets = assets.filter((asset) => {
let kindClose = false;
if (typeof asset.lastIdea.idea !== 'string') {
if (asset.lastIdea.idea
.kind === 'close') {
kindClose = true;
}
}
return !kindClose;
});
const result = {
strategyKey: strategyUniqueKey,
reference: rebalance.reference,
totalAssets: assets.length,
assets,
};
return result;
};
const listStrategyRebalancesInPeriod = async (strategyUniqueKey, start, end, page = 1, limit = 5) => {
const preCut = await __1.Mess.contractsInMemoryDbView.getStrategyRebalancesInPeriod(strategyUniqueKey, start, end);
const preInflate = await (0, __1.getDataWithPaging)({
data: preCut,
paging: {
page: page || 1,
limit: limit || 5,
},
});
const result = {
...preInflate,
data: await Promise.all(preInflate.data.map(async (rebalance) => {
let assets = await Promise.all(rebalance.ideaIndexes.map(async (ideaIndex) => {
const lastIdea = await __1.Mess.contractsInMemoryDbView.getIdeaData(ideaIndex);
const result = {
lastIdea,
};
return result;
}));
assets = assets.filter((asset) => {
let kindClose = false;
if (typeof asset.lastIdea.idea !== 'string') {
if (asset.lastIdea
.idea.kind === 'close') {
kindClose = true;
}
}
return !kindClose;
});
const result = {
strategyKey: strategyUniqueKey,
reference: rebalance.reference,
totalAssets: assets.length,
assets,
};
return result;
})),
};
return result;
};
exports.ViewsSourceMessModule = {
startLocalServer,
pauseLocalServer,
//
listContracts,
getCreator,
listCreatorStrategies,
listStrategiesWithAccessibleIdeasBy,
listStrategiesSubscribedToBy,
listStrategiesPublic,
listIdeasUniqueIndexesByLatest,
listLatestIdeas,
listLatestPublicIdeas,
listCreatorIdeas,
listOwnedIdeas,
listStrategyIdeas,
getIdeaByUniqueId,
getPublicStrategyWithCreator,
getStrategyPortfolio,
listStrategyRebalances,
getStrategyRebalance,
listStrategyRebalancesInPeriod,
// listIdeaStagesIndexes,
// getIdeaByNftId,
// listIdeaNftsByStrategy,
};
//# sourceMappingURL=views-source-mess.module.js.map