@ixily/activ
Version:
Alpha Capture Trade Idea Verification. Blockchain ownership proven trade ideas and strategies.
499 lines (468 loc) • 11.4 kB
text/typescript
import {
CONTRACT_INTERFACES,
CONTRACT_TOOLS,
IPaging,
IStrategyWithCreator,
ProvableIdeasModule,
decomposeUniqueStrategyReference,
uniqueKeyJoin,
uniqueKeySplit,
IPortfolioView,
IPortfolioRebalanceOpaque,
ISupportedBlockchainNetwork,
} from '../..'
import { ViewsSourceMessModule } from './views-source-mess.module'
import { ViewsSourceServerModule } from './views-source-server.module'
import { ConfigModule } from './config.module'
import { network } from 'hardhat'
const state = {
source: 'server' as 'local' | 'server',
}
ConfigModule.getConfigUpdatedStream().subscribe((config) => {
if (config !== undefined) {
if (config.dataSource !== undefined) {
state.source = config.dataSource.publicCache.source
}
}
})
const listContracts = async (
network: ISupportedBlockchainNetwork = 'polygon',
): Promise<string[]> => {
if (state.source === 'server') {
return ViewsSourceServerModule.listContracts(network)
} else {
return ViewsSourceMessModule.listContracts()
}
}
const getCreator = async (
contract: string,
creatorWallet: string,
): Promise<CONTRACT_INTERFACES.ITradeIdeaCreator> => {
if (state.source === 'server') {
return ViewsSourceServerModule.getCreator(contract, creatorWallet)
} else {
return ViewsSourceMessModule.getCreator(contract, creatorWallet)
}
}
const getIdeaByUniqueContractAndId = async (
contract: string,
ideaNftId: number,
): Promise<CONTRACT_INTERFACES.ITradeIdea> => {
const keySplit = uniqueKeySplit(contract)
keySplit.push('' + ideaNftId)
const uniqueIdeaId = uniqueKeyJoin(keySplit)
return getIdeaByUniqueId(uniqueIdeaId)
}
const getIdeaByUniqueId = async (
uniqueIdeaId: string,
): Promise<CONTRACT_INTERFACES.ITradeIdea> => {
if (state.source === 'server') {
return ViewsSourceServerModule.getIdeaByUniqueId(uniqueIdeaId)
} else {
return ViewsSourceMessModule.getIdeaByUniqueId(uniqueIdeaId)
}
}
const getStrategy = async (
uniqueStrategyReference: string,
): Promise<CONTRACT_INTERFACES.ITradeIdeaStrategy> => {
const { creatorAddress, strategyKey } = decomposeUniqueStrategyReference(
uniqueStrategyReference,
)
const data =
await ProvableIdeasModule.chain.getValidatedStrategyChainWithCache(
strategyKey,
creatorAddress,
undefined,
false,
)
return data.cache.cachedStrategy.strategy
}
const getStrategyWithCreator = async (
uniqueStrategyReference: string,
): Promise<{
strategy: CONTRACT_INTERFACES.ITradeIdeaStrategy
creator: CONTRACT_INTERFACES.ITradeIdeaCreator
}> => {
const { creatorAddress, strategyKey } = decomposeUniqueStrategyReference(
uniqueStrategyReference,
)
// console.log('creatorAddress', creatorAddress)
// console.log('strategyKey', strategyKey)
const data =
await ProvableIdeasModule.chain.getValidatedStrategyChainWithCache(
strategyKey,
creatorAddress,
undefined,
false,
)
if (data.cache?.cachedStrategy === undefined) {
throw new Error('Strategy does not exists')
}
const strategy = data.cache.cachedStrategy.strategy
const creator = CONTRACT_TOOLS.queryChain.getCreatorLatestData(
data.cache.cachedStrategy,
)
return {
strategy,
creator,
}
}
const listCreatorStrategies = async (
contract: string,
creatorWallet: string,
page: number = 1,
limit: number = 5,
): Promise<IPaging<IStrategyWithCreator>> => {
if (state.source === 'server') {
return ViewsSourceServerModule.listCreatorStrategies(
contract,
creatorWallet,
page,
limit,
)
} else {
return ViewsSourceMessModule.listCreatorStrategies(
contract,
creatorWallet,
page,
limit,
)
}
}
const getPublicStrategyWithCreator = async (
strategyUniqueKey: string,
): Promise<IStrategyWithCreator> => {
if (state.source === 'server') {
return ViewsSourceServerModule.getPublicStrategyWithCreator(
strategyUniqueKey,
)
} else {
return ViewsSourceMessModule.getPublicStrategyWithCreator(
strategyUniqueKey,
)
}
}
const listStrategiesWithAccessibleIdeasBy = async (
contract: string,
accessorWallet: string,
page: number = 1,
limit: number = 5,
): Promise<IPaging<IStrategyWithCreator>> => {
if (state.source === 'server') {
return ViewsSourceServerModule.listStrategiesWithAccessibleIdeasBy(
contract,
accessorWallet,
page,
limit,
)
} else {
return ViewsSourceMessModule.listStrategiesWithAccessibleIdeasBy(
contract,
accessorWallet,
page,
limit,
)
}
}
const listStrategiesSubscribedToBy = async (
contract: string,
subscriberWallet: string,
page: number = 1,
limit: number = 5,
): Promise<IPaging<IStrategyWithCreator>> => {
if (state.source === 'server') {
return ViewsSourceServerModule.listStrategiesSubscribedToBy(
contract,
subscriberWallet,
page,
limit,
)
} else {
return ViewsSourceMessModule.listStrategiesSubscribedToBy(
contract,
subscriberWallet,
page,
limit,
)
}
}
const listStrategiesPublic = async (
contract?: string,
page: number = 1,
limit: number = 5,
network?: ISupportedBlockchainNetwork,
): Promise<IPaging<IStrategyWithCreator>> => {
if (state.source === 'server') {
return ViewsSourceServerModule.listStrategiesPublic(
contract,
page,
limit,
network,
)
} else {
return ViewsSourceMessModule.listStrategiesPublic(contract, page, limit)
}
}
const listIdeasUniqueIndexesByLatest = async (
contract: string,
page: number = 1,
limit: number = 5,
): Promise<IPaging<string>> => {
if (state.source === 'server') {
return ViewsSourceServerModule.listIdeasUniqueIndexesByLatest(
contract,
page,
limit,
)
} else {
return ViewsSourceMessModule.listIdeasUniqueIndexesByLatest(
contract,
page,
limit,
)
}
}
const listLatestIdeas = async (
contract: string,
page: number = 1,
limit: number = 5,
): Promise<IPaging<CONTRACT_INTERFACES.ITradeIdea>> => {
if (state.source === 'server') {
return ViewsSourceServerModule.listLatestIdeas(contract, page, limit)
} else {
return ViewsSourceMessModule.listLatestIdeas(contract, page, limit)
}
}
const listLatestPublicIdeas = async (
contract?: string,
page: number = 1,
limit: number = 5,
network: ISupportedBlockchainNetwork = 'polygon',
): Promise<IPaging<CONTRACT_INTERFACES.ITradeIdea>> => {
if (state.source === 'server') {
return ViewsSourceServerModule.listLatestPublicIdeas(
contract,
page,
limit,
network,
)
} else {
return ViewsSourceMessModule.listLatestPublicIdeas(
contract,
page,
limit,
)
}
}
const listCreatorIdeas = async (
contract: string,
creatorWallet: string,
page: number = 1,
limit: number = 5,
filterType: CONTRACT_INTERFACES.ITradeIdeaIdeaKind[] | 'bypass' = 'bypass',
filterIncludeEncrypted: boolean = true,
bypassPaginationAndGetAll: boolean = false,
): Promise<IPaging<CONTRACT_INTERFACES.ITradeIdea>> => {
if (state.source === 'server') {
return ViewsSourceServerModule.listCreatorIdeas(
contract,
creatorWallet,
page,
limit,
filterType,
filterIncludeEncrypted,
bypassPaginationAndGetAll,
)
} else {
return ViewsSourceMessModule.listCreatorIdeas(
contract,
creatorWallet,
page,
limit,
filterType,
filterIncludeEncrypted,
bypassPaginationAndGetAll,
)
}
}
const listOwnedIdeas = async (
contract: string,
ownerWallet: string,
page: number = 1,
limit: number = 5,
filterType: CONTRACT_INTERFACES.ITradeIdeaIdeaKind[] | 'bypass' = 'bypass',
filterIncludeEncrypted: boolean = false,
bypassPaginationAndGetAll: boolean = false,
): Promise<IPaging<CONTRACT_INTERFACES.ITradeIdea>> => {
if (state.source === 'server') {
return ViewsSourceServerModule.listOwnedIdeas(
contract,
ownerWallet,
page,
limit,
filterType,
filterIncludeEncrypted,
bypassPaginationAndGetAll,
)
} else {
return ViewsSourceMessModule.listOwnedIdeas(
contract,
ownerWallet,
page,
limit,
filterType,
filterIncludeEncrypted,
bypassPaginationAndGetAll,
)
}
}
const listStrategyIdeas = async (
strategyUniqueKey: string,
page: number = 1,
limit: number = 5,
filterType: CONTRACT_INTERFACES.ITradeIdeaIdeaKind[] | 'bypass' = 'bypass',
filterIncludeEncrypted: boolean = true,
bypassPaginationAndGetAll: boolean = false,
) => {
if (state.source === 'server') {
return ViewsSourceServerModule.listStrategyIdeas(
strategyUniqueKey,
page,
limit,
filterType,
filterIncludeEncrypted,
bypassPaginationAndGetAll,
)
} else {
return ViewsSourceMessModule.listStrategyIdeas(
strategyUniqueKey,
page,
limit,
filterType,
filterIncludeEncrypted,
bypassPaginationAndGetAll,
)
}
}
const getStrategyPortfolio = async (
strategyUniqueKey: string,
): Promise<IPortfolioView | 'ENCRYPTED'> => {
if (state.source === 'server') {
return ViewsSourceServerModule.getStrategyPortfolio(strategyUniqueKey)
} else {
return ViewsSourceMessModule.getStrategyPortfolio(strategyUniqueKey)
}
}
const listStrategyRebalances = async (
strategyUniqueKey: string,
page: number = 1,
limit: number = 5,
): Promise<IPaging<IPortfolioRebalanceOpaque>> => {
if (state.source === 'server') {
return ViewsSourceServerModule.listStrategyRebalances(
strategyUniqueKey,
page,
limit,
)
} else {
return ViewsSourceMessModule.listStrategyRebalances(
strategyUniqueKey,
page,
limit,
)
}
}
const getStrategyRebalance = async (
strategyUniqueKey: string,
rebalanceId: string,
): Promise<IPortfolioRebalanceOpaque> => {
if (state.source === 'server') {
return ViewsSourceServerModule.getStrategyRebalance(
strategyUniqueKey,
rebalanceId,
)
} else {
return ViewsSourceMessModule.getStrategyRebalance(
strategyUniqueKey,
rebalanceId,
)
}
}
const listStrategyRebalancesInPeriod = async (
strategyUniqueKey: string,
start: number,
end: number,
page: number = 1,
limit: number = 5,
): Promise<IPaging<IPortfolioRebalanceOpaque>> => {
if (state.source === 'server') {
return ViewsSourceServerModule.listStrategyRebalancesInPeriod(
strategyUniqueKey,
start,
end,
page,
limit,
)
} else {
return ViewsSourceMessModule.listStrategyRebalancesInPeriod(
strategyUniqueKey,
start,
end,
page,
limit,
)
}
}
/*
const listIdeaNftsByStrategy = async (
uniqueStrategyReference: string,
): Promise<v4.MyIdeaKeys[]> => {
return requestor<v4.MyIdeaKeys[]>(
'GET',
`/v4/strategies/${uniqueStrategyReference}/nfts`,
)
}
const listIdeaStagesIndexes = async (
uniqueStrategyReference: string,
ideaKey: number,
page: number = 1,
limit: number = 5,
): Promise<number[]> =>
requestor<number[]>(
'GET',
`/v4/strategies/${uniqueStrategyReference}/ideas/${ideaKey}/stages?page=${page}&limit=${limit}`,
)
const getIdeaByNftId = async (
contract: string,
nftId: string,
): Promise<v4.ITradeIdea> =>
requestor<v4.ITradeIdea>(
'GET',
`/v4/contracts/${contract}/ideaStagesByNft/${nftId}`,
)
*/
export const ViewsSourceGateModule = {
listContracts,
getCreator,
listCreatorStrategies,
listStrategiesWithAccessibleIdeasBy,
listStrategiesSubscribedToBy,
listStrategiesPublic,
listIdeasUniqueIndexesByLatest,
listLatestIdeas,
listLatestPublicIdeas,
listCreatorIdeas,
listOwnedIdeas,
getStrategy,
getStrategyWithCreator,
// listIdeaStagesIndexes,
// listIdeasIndexes,
getIdeaByUniqueContractAndId,
getIdeaByUniqueId,
getPublicStrategyWithCreator,
listStrategyIdeas,
getStrategyPortfolio,
listStrategyRebalances,
getStrategyRebalance,
listStrategyRebalancesInPeriod,
}