@ixily/activ
Version:
Alpha Capture Trade Idea Verification. Blockchain ownership proven trade ideas and strategies.
466 lines (437 loc) • 13.8 kB
text/typescript
import {
CONTRACT_INTERFACES,
IPaging,
IPortfolioRebalanceOpaque,
IPortfolioView,
IStrategyWithCreator,
LogModule as log,
uniqueKeySplit,
ISupportedBlockchainNetwork,
} from '../..'
import { ConfigModule } from './config.module'
type IMethod = 'GET' | 'POST' | 'PUT' | 'DELETE'
const DEFAULT_CACHE_VIEWS_SERVER_URL = 'cache-views.ixily.io'
const state = {
baseUrlServer: DEFAULT_CACHE_VIEWS_SERVER_URL,
customServers: false as boolean,
}
ConfigModule.getConfigUpdatedStream().subscribe((config) => {
if (config !== undefined) {
if (config.dataSource !== undefined) {
if (config.dataSource.publicCache.source === 'server') {
state.baseUrlServer =
config.dataSource.publicCache.customServerUrl ||
DEFAULT_CACHE_VIEWS_SERVER_URL
if (state.baseUrlServer !== DEFAULT_CACHE_VIEWS_SERVER_URL) {
state.customServers = true
}
}
}
}
})
// using fetch
const requestor = async <T>(
network: ISupportedBlockchainNetwork,
method: IMethod,
path: string,
body?: any,
): Promise<T> => {
const url = state.customServers
? `${state.baseUrlServer}${path}`
: network === 'polygon'
? `https://${state.baseUrlServer}${path}`
: `https://${network}-${state.baseUrlServer}${path}`
log.dev('url', url)
let markAsOneError: boolean = false
let theResponse: any
// insecure by skipping ssl
const valueAns = await fetch(url, {
method,
mode: 'cors',
body: body !== undefined ? JSON.stringify(body) : undefined,
headers: {
'Content-Type': 'application/json',
},
})
.then((response) => {
log.dev('response')
log.dev(response)
if (response.status < 200 || response.status >= 300) {
markAsOneError = true
}
theResponse = response
return response.json()
})
.catch((jsonConversionError) => {
log.dev('jsonConversionError')
log.dev(jsonConversionError)
return theResponse?.status || theResponse
})
if (markAsOneError) {
const errorMsg =
valueAns?.error ||
(typeof valueAns === 'string' ? valueAns : JSON.stringify(valueAns))
throw new Error(errorMsg)
}
return valueAns
}
/* */
const listContracts = async (
network: ISupportedBlockchainNetwork = 'polygon',
): Promise<string[]> => requestor<string[]>(network, 'GET', '/v4/contracts')
const getCreator = async (
contract: string,
creatorWallet: string,
): Promise<CONTRACT_INTERFACES.ITradeIdeaCreator> => {
const split = uniqueKeySplit(contract)
return requestor<CONTRACT_INTERFACES.ITradeIdeaCreator>(
split[0] as ISupportedBlockchainNetwork,
'GET',
`/v4/contracts/${contract}/creators/${creatorWallet}`,
)
}
const getIdeaByUniqueId = async (
uniqueIdeaIndex: string,
): Promise<CONTRACT_INTERFACES.ITradeIdea> => {
const split = uniqueKeySplit(uniqueIdeaIndex)
return requestor<CONTRACT_INTERFACES.ITradeIdea>(
split[0] as ISupportedBlockchainNetwork,
'GET',
`/v4/ideas/${uniqueIdeaIndex}`,
)
}
const listCreatorStrategies = async (
contract: string,
creatorWallet: string,
page: number = 1,
limit: number = 5,
): Promise<IPaging<IStrategyWithCreator>> => {
const split = uniqueKeySplit(contract)
const paginated = await requestor<IPaging<IStrategyWithCreator>>(
split[0] as ISupportedBlockchainNetwork,
'GET',
`/v4/contracts/${contract}/creators/${creatorWallet}/strategies?page=${page}&limit=${limit}`,
)
log.dev('paginated', paginated)
/*
const paginatedInflated: IPaging<CONTRACT_INTERFACES.ITradeIdeaStrategy> = {
...paginated,
data: await Promise.all(
paginated.data.map((uniqueStrategyReference) =>
(async (): Promise<CONTRACT_INTERFACES.ITradeIdeaStrategy> => {
// console.log('uniqueStrategyReference')
// console.log(uniqueStrategyReference)
const { strategyKey, creatorAddress } =
decomposeUniqueStrategyReference(
uniqueStrategyReference as string,
)
// console.log('strategyKey')
// console.log(strategyKey)
// console.log('creatorAddress')
// console.log(creatorAddress)
const chain =
await ProvableIdeasModule.chain.getValidatedStrategyChainWithCache(
strategyKey,
creatorAddress,
)
// console.log('chain')
// console.log(chain)
// throw new Error('dsfadfa')
const stg = chain.cache.cachedStrategy.strategy
stg.uniqueKey = uniqueStrategyReference
return stg
})(),
),
),
}
// console.log('paginatedInflated')
// console.log(paginatedInflated)
return paginatedInflated
*/
return paginated
}
const getPublicStrategyWithCreator = async (
uniqueStrategyKey: string,
): Promise<IStrategyWithCreator> => {
const split = uniqueKeySplit(uniqueStrategyKey)
return requestor<IStrategyWithCreator>(
split[0] as ISupportedBlockchainNetwork,
'GET',
`/v4/strategies/${uniqueStrategyKey}`,
)
}
const listStrategiesWithAccessibleIdeasBy = async (
contract: string,
accessorWallet: string,
page: number = 1,
limit: number = 5,
): Promise<IPaging<IStrategyWithCreator>> => {
const split = uniqueKeySplit(contract)
return requestor<IPaging<IStrategyWithCreator>>(
split[0] as ISupportedBlockchainNetwork,
'GET',
`/v4/contracts/${contract}/accessors/${accessorWallet}/strategies?page=${page}&limit=${limit}`,
)
}
const listStrategiesSubscribedToBy = async (
contract: string,
accessorWallet: string,
page: number = 1,
limit: number = 5,
): Promise<IPaging<IStrategyWithCreator>> => {
const split = uniqueKeySplit(contract)
return requestor<IPaging<IStrategyWithCreator>>(
split[0] as ISupportedBlockchainNetwork,
'GET',
`/v4/contracts/${contract}/subscribers/${accessorWallet}/strategies?page=${page}&limit=${limit}`,
)
}
const listStrategiesPublic = async (
contract?: string,
page: number = 1,
limit: number = 5,
network: ISupportedBlockchainNetwork = 'polygon',
): Promise<IPaging<IStrategyWithCreator>> => {
if (contract !== undefined) {
const split = uniqueKeySplit(contract)
return requestor<IPaging<IStrategyWithCreator>>(
split[0] as ISupportedBlockchainNetwork,
'GET',
`/v4/strategies?page=${page}&limit=${limit}` +
(contract !== undefined ? '&contract=' + contract : ''),
)
} else {
return requestor<IPaging<IStrategyWithCreator>>(
network,
'GET',
`/v4/strategies?page=${page}&limit=${limit}` +
(contract !== undefined ? '&contract=' + contract : ''),
)
}
}
const listIdeasUniqueIndexesByLatest = async (
contract: string,
page: number = 1,
limit: number = 5,
): Promise<IPaging<string>> => {
const split = uniqueKeySplit(contract)
return requestor<IPaging<string>>(
split[0] as ISupportedBlockchainNetwork,
'GET',
`/v4/contracts/${contract}/ideaIndexes?page=${page}&limit=${limit}`,
)
}
const listLatestIdeas = async (
contract: string,
page: number = 1,
limit: number = 5,
): Promise<IPaging<CONTRACT_INTERFACES.ITradeIdea>> => {
const split = uniqueKeySplit(contract)
return requestor<IPaging<CONTRACT_INTERFACES.ITradeIdea>>(
split[0] as ISupportedBlockchainNetwork,
'GET',
`/v4/contracts/${contract}/ideas?page=${page}&limit=${limit}`,
)
}
const listLatestPublicIdeas = async (
contract?: string,
page: number = 1,
limit: number = 5,
network: ISupportedBlockchainNetwork = 'polygon',
): Promise<IPaging<CONTRACT_INTERFACES.ITradeIdea>> => {
if (contract !== undefined) {
const split = uniqueKeySplit(contract)
return requestor<IPaging<CONTRACT_INTERFACES.ITradeIdea>>(
split[0] as ISupportedBlockchainNetwork,
'GET',
`/v4/ideas?page=${page}&limit=${limit}` +
(contract !== undefined ? '&contract=' + contract : ''),
)
} else {
return requestor<IPaging<CONTRACT_INTERFACES.ITradeIdea>>(
network,
'GET',
`/v4/ideas?page=${page}&limit=${limit}` +
(contract !== undefined ? '&contract=' + contract : ''),
)
}
}
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>> => {
const split = uniqueKeySplit(contract)
const filterTypeFormat: string =
filterType === 'bypass' ? 'bypass' : filterType.join(',')
const filterIncludeEncryptedFormat: string = filterIncludeEncrypted
? 'true'
: 'false'
const bypassPaginationAndGetAllFormat: string = bypassPaginationAndGetAll
? 'true'
: 'false'
return requestor<IPaging<CONTRACT_INTERFACES.ITradeIdea>>(
split[0] as ISupportedBlockchainNetwork,
'GET',
`/v4/contracts/${contract}/creators/${creatorWallet}/ideas?page=${page}&limit=${limit}&filterType=${filterTypeFormat}&filterIncludeEncrypted=${filterIncludeEncryptedFormat}&bypassPaginationAndGetAll=${bypassPaginationAndGetAllFormat}`,
)
}
const listOwnedIdeas = async (
contract: string,
ownerWallet: string,
page: number = 1,
limit: number = 5,
filterType: CONTRACT_INTERFACES.ITradeIdeaIdeaKind[] | 'bypass' = 'bypass',
filterIncludeEncrypted: boolean = true,
bypassPaginationAndGetAll: boolean = false,
): Promise<IPaging<CONTRACT_INTERFACES.ITradeIdea>> => {
const split = uniqueKeySplit(contract)
const filterTypeFormat: string =
filterType === 'bypass' ? 'bypass' : filterType.join(',')
const filterIncludeEncryptedFormat: string = filterIncludeEncrypted
? 'true'
: 'false'
const bypassPaginationAndGetAllFormat: string = bypassPaginationAndGetAll
? 'true'
: 'false'
return requestor<IPaging<CONTRACT_INTERFACES.ITradeIdea>>(
split[0] as ISupportedBlockchainNetwork,
'GET',
`/v4/contracts/${contract}/owners/${ownerWallet}/ideas?page=${page}&limit=${limit}&filterType=${filterTypeFormat}&filterIncludeEncrypted=${filterIncludeEncryptedFormat}&bypassPaginationAndGetAll=${bypassPaginationAndGetAllFormat}`,
)
}
const listStrategyIdeas = async (
strategyUniqueKey: string,
page: number = 1,
limit: number = 5,
filterType: CONTRACT_INTERFACES.ITradeIdeaIdeaKind[] | 'bypass' = 'bypass',
filterIncludeEncrypted: boolean = true,
bypassPaginationAndGetAll: boolean = false,
) => {
const split = uniqueKeySplit(strategyUniqueKey)
const filterTypeFormat: string =
filterType === 'bypass' ? 'bypass' : filterType.join(',')
const filterIncludeEncryptedFormat: string = filterIncludeEncrypted
? 'true'
: 'false'
const bypassPaginationAndGetAllFormat: string = bypassPaginationAndGetAll
? 'true'
: 'false'
return requestor<IPaging<CONTRACT_INTERFACES.ITradeIdea>>(
split[0] as ISupportedBlockchainNetwork,
'GET',
`/v4/strategies/${strategyUniqueKey}/ideas?page=${page}&limit=${limit}&filterType=${filterTypeFormat}&filterIncludeEncrypted=${filterIncludeEncryptedFormat}&bypassPaginationAndGetAll=${bypassPaginationAndGetAllFormat}`,
)
}
const getStrategyPortfolio = async (
strategyUniqueKey: string,
): Promise<IPortfolioView> => {
const split = uniqueKeySplit(strategyUniqueKey)
return requestor<IPortfolioView>(
split[0] as ISupportedBlockchainNetwork,
'GET',
`/v4/strategies/${strategyUniqueKey}/portfolio`,
)
}
const listStrategyRebalances = async (
strategyUniqueKey: string,
page: number = 1,
limit: number = 5,
): Promise<IPaging<IPortfolioRebalanceOpaque>> => {
const split = uniqueKeySplit(strategyUniqueKey)
return requestor<IPaging<IPortfolioRebalanceOpaque>>(
split[0] as ISupportedBlockchainNetwork,
'GET',
`/v4/strategies/${strategyUniqueKey}/rebalances?page=${page}&limit=${limit}`,
)
}
const getStrategyRebalance = async (
strategyUniqueKey: string,
rebalanceId: string,
): Promise<IPortfolioRebalanceOpaque> => {
const split = uniqueKeySplit(strategyUniqueKey)
return requestor<IPortfolioRebalanceOpaque>(
split[0] as ISupportedBlockchainNetwork,
'GET',
`/v4/strategies/${strategyUniqueKey}/rebalances/${rebalanceId}`,
)
}
const listStrategyRebalancesInPeriod = async (
strategyUniqueKey: string,
startDate: number,
endDate: number,
page: number = 1,
limit: number = 5,
): Promise<IPaging<IPortfolioRebalanceOpaque>> => {
const split = uniqueKeySplit(strategyUniqueKey)
return requestor<IPaging<IPortfolioRebalanceOpaque>>(
split[0] as ISupportedBlockchainNetwork,
'GET',
`/v4/strategies/${strategyUniqueKey}/rebalancesInPeriod?startDate=${startDate}&endDate=${endDate}&page=${page}&limit=${limit}`,
)
}
/*
const listIdeasIndexes = async (
contract: string,
page: number = 1,
limit: number = 5,
): Promise<number[]> =>
requestor<number[]>(
'GET',
`/v4/contracts/${contract}/ideas?page=${page}&limit=${limit}`,
)
const listIdeaNftsByStrategy = async (
uniqueStrategyReference: string,
): Promise<CONTRACT_INTERFACES.MyIdeaKeys[]> => {
return requestor<CONTRACT_INTERFACES.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<CONTRACT_INTERFACES.ITradeIdea> =>
requestor<CONTRACT_INTERFACES.ITradeIdea>(
'GET',
`/v4/contracts/${contract}/ideaStagesByNft/${nftId}`,
)
*/
export const ViewsSourceServerModule = {
listContracts,
getCreator,
listCreatorStrategies,
listStrategiesWithAccessibleIdeasBy,
listStrategiesSubscribedToBy,
listStrategiesPublic,
listIdeasUniqueIndexesByLatest,
listLatestIdeas,
listLatestPublicIdeas,
listCreatorIdeas,
listOwnedIdeas,
getIdeaByUniqueId,
getPublicStrategyWithCreator,
listStrategyIdeas,
getStrategyPortfolio,
listStrategyRebalances,
getStrategyRebalance,
listStrategyRebalancesInPeriod,
// listIdeasIndexes,
// listIdeaStagesIndexes,
// getIdeaByNftId,
// listIdeaNftsByStrategy,
}