import { deriveNetworkFromAddress , getNetworkDefinitionFromIdentifier , ICosmosAccountBalances , ICosmosAccountBalancesQueryVariables , ICosmosAccountHistoryQueryVariables , ICosmosAccountInformationQueryVariables , ICosmosDistributionCommunityPoolQueryVariables , ICosmosDistributionParametersQueryVariables , ICosmosGovernanceParametersTallyingQueryVariables , ICosmosGovernanceParametersVotingQueryVariables , ICosmosGovernanceProposalsQueryVariables , ICosmosLatestBlockQueryVariables , ICosmosRewardsByValidatorQueryVariables , ICosmosSlashingParametersQueryVariables , ICosmosStakingParametersQueryVariables , ICosmosStakingPoolQueryVariables , ICosmosTransactionQueryVariables , ICosmosTransactionsQueryVariables , ICosmosValidatorDistributionQueryVariables , ICosmosValidatorSetsQueryVariables , ICosmosValidatorsQueryVariables , IQuery , NETWORK_NAME , } from "@anthem/utils" ; import COSMOS_EXTRACTOR from "../sources/cosmos-extractor" ; import COSMOS_SDK from "../sources/cosmos-sdk" ; import EXCHANGE_DATA_API from "../sources/fiat-price-data" ; import { blockUnsupportedNetworks , standardizeTimestamps , validatePaginationParams , } from "../tools/server-utils" ; /** =======================================================================
 * Cosmos Resolvers
 * ========================================================================
 */ const COSMOS_NETWORKS = new Set ( [ "COSMOS" , "TERRA" , "KAVA" ] ) ; const CosmosResolvers = { cosmosAccountHistory : async ( _ , args ) : Promise < IQuery [ "cosmosAccountHistory" ] > => { const { address , fiat } = args ; const network = deriveNetworkFromAddress ( address ) ; blockUnsupportedNetworks ( network , COSMOS_NETWORKS , "portfolio" ) ; const fiatPriceHistory = await EXCHANGE_DATA_API . fetchPortfolioFiatPriceHistory ( fiat , network ) ; const requestArgs = { network , address , } ; const [ delegatorRewards , delegations , unbondings , balanceHistory , validatorCommissions , ] = await Promise . all ( [ COSMOS_EXTRACTOR . getPortfolioDelegatorRewards ( requestArgs ) , COSMOS_EXTRACTOR . getPortfolioDelegations ( requestArgs ) , COSMOS_EXTRACTOR . getPortfolioUnbondings ( requestArgs ) , COSMOS_EXTRACTOR . getPortfolioBalanceHistory ( requestArgs ) , COSMOS_EXTRACTOR . getPortfolioValidatorRewards ( requestArgs ) , ] ) ; return { delegatorRewards : standardizeTimestamps ( delegatorRewards ) , delegations : standardizeTimestamps ( delegations ) , unbondings : standardizeTimestamps ( unbondings ) , balanceHistory : standardizeTimestamps ( balanceHistory ) , validatorCommissions : standardizeTimestamps ( validatorCommissions ) , fiatPriceHistory , } ; } , cosmosTransaction : async ( _ , args ) : Promise < IQuery [ "cosmosTransaction" ] > => { const { hash } = args ; const network = getNetworkDefinitionFromIdentifier ( args . network ) ; blockUnsupportedNetworks ( network , COSMOS_NETWORKS , "transactions" ) ; return COSMOS_EXTRACTOR . getTransactionByHash ( hash , network ) ; } , cosmosTransactions : async ( _ , args ) : Promise < IQuery [ "cosmosTransactions" ] > => { const { address , startingPage , pageSize } = args ; const size = validatePaginationParams ( pageSize , 25 ) ; const start = validatePaginationParams ( startingPage , 1 ) ; const network = deriveNetworkFromAddress ( address ) ; blockUnsupportedNetworks ( network , COSMOS_NETWORKS , "transactions" ) ; const params = { address , network , pageSize : size , startingPage : start , } ; return COSMOS_EXTRACTOR . getTransactions ( params ) ; } , cosmosRewardsByValidator : async ( _ , args ) : Promise < IQuery [ "cosmosRewardsByValidator" ] > => { const { address } = args ; const network = deriveNetworkFromAddress ( address ) ; return COSMOS_SDK . fetchAvailableRewards ( address , network ) ; } , cosmosAccountBalances : async ( _ , args ) : Promise < IQuery [ "cosmosAccountBalances" ] > => { const { address } = args ; const network = deriveNetworkFromAddress ( address ) ; const [ balance , delegations , rewards , unbonding , commissions , ] = await Promise . all ( [ COSMOS_SDK . fetchBalance ( address , network ) , COSMOS_SDK . fetchDelegations ( address , network ) , COSMOS_SDK . fetchRewards ( address , network ) , COSMOS_SDK . fetchUnbondingDelegations ( address , network ) , COSMOS_SDK . fetchCommissionsForValidator ( address , network ) , ] ) ; const result = { balance , delegations , rewards , unbonding , commissions , } ; const balances = result ; return balances ; } , cosmosAccountInformation : async ( _ , args ) : Promise < IQuery [ "cosmosAccountInformation" ] > => { /**
         * TODO: There a "validator vesting accounts" on Kava which have a
         * different return type for this API.
         */ const { address } = args ; const network = deriveNetworkFromAddress ( address ) ; return COSMOS_SDK . fetchAccountInformation ( address , network ) ; } , cosmosValidatorDistribution : async ( _ , args ) : Promise < IQuery [ "cosmosValidatorDistribution" ] > => { const { validatorAddress } = args ; const network = deriveNetworkFromAddress ( validatorAddress ) ; return COSMOS_SDK . fetchValidatorDistribution ( validatorAddress , network ) ; } , cosmosValidators : async ( _ , args ) : Promise < IQuery [ "cosmosValidators" ] > => { const network = getNetworkDefinitionFromIdentifier ( args . network ) ; blockUnsupportedNetworks ( network , COSMOS_NETWORKS ) ; return COSMOS_SDK . fetchValidators ( network ) ; } , cosmosValidatorSets : async ( _ , args ) : Promise < IQuery [ "cosmosValidatorSets" ] > => { const network = getNetworkDefinitionFromIdentifier ( args . network ) ; return COSMOS_SDK . fetchValidatorSets ( network ) ; } , cosmosLatestBlock : async ( _ , args ) : Promise < IQuery [ "cosmosLatestBlock" ] > => { const network = getNetworkDefinitionFromIdentifier ( args . network ) ; return COSMOS_SDK . fetchLatestBlock ( network ) ; } , cosmosStakingPool : async ( _ , args ) : Promise < IQuery [ "cosmosStakingPool" ] > => { const network = getNetworkDefinitionFromIdentifier ( args . network ) ; return COSMOS_SDK . fetchStakingPool ( network ) ; } , cosmosStakingParameters : async ( _ , args ) : Promise < IQuery [ "cosmosStakingParameters" ] > => { const network = getNetworkDefinitionFromIdentifier ( args . network ) ; return COSMOS_SDK . fetchStakingParameters ( network ) ; } , cosmosGovernanceProposals : async ( _ , args ) : Promise < IQuery [ "cosmosGovernanceProposals" ] > => { const network = getNetworkDefinitionFromIdentifier ( args . network ) ; return COSMOS_SDK . fetchGovernanceProposals ( network ) ; } , cosmosGovernanceParametersDeposit : async ( _ , args ) : Promise < IQuery [ "cosmosGovernanceParametersDeposit" ] > => { const network = getNetworkDefinitionFromIdentifier ( args . network ) ; return COSMOS_SDK . fetchGovernanceParametersDeposit ( network ) ; } , cosmosGovernanceParametersTallying : async ( _ , args ) : Promise < IQuery [ "cosmosGovernanceParametersTallying" ] > => { const network = getNetworkDefinitionFromIdentifier ( args . network ) ; return COSMOS_SDK . fetchGovernanceParametersTallying ( network ) ; } , cosmosGovernanceParametersVoting : async ( _ , args ) : Promise < IQuery [ "cosmosGovernanceParametersVoting" ] > => { const network = getNetworkDefinitionFromIdentifier ( args . network ) ; return COSMOS_SDK . fetchGovernanceParametersVoting ( network ) ; } , cosmosSlashingParameters : async ( _ , args ) : Promise < IQuery [ "cosmosSlashingParameters" ] > => { const network = getNetworkDefinitionFromIdentifier ( args . network ) ; return COSMOS_SDK . fetchSlashingParameters ( network ) ; } , cosmosDistributionCommunityPool : async ( _ , args ) : Promise < IQuery [ "cosmosDistributionCommunityPool" ] > => { const network = getNetworkDefinitionFromIdentifier ( args . network ) ; return COSMOS_SDK . fetchDistributionCommunityPool ( network ) ; } , cosmosDistributionParameters : async ( _ , args ) : Promise < IQuery [ "cosmosDistributionParameters" ] > => { const network = getNetworkDefinitionFromIdentifier ( args . network ) ; return COSMOS_SDK . fetchDistributionParameters ( network ) ; } , } ; /** ===========================================================================
 * Export
 * ============================================================================
 */ export default CosmosResolvers ; /** ===========================================================================
 * Multi Network Support Note
 *
 * [~] addressHistoryData
 * [ ] rewardsByValidator
 * [ ] fiatPriceHistory - Crypto Compare API data for TERRA and KAVA???
 * [?] dailyPercentChange
 * [~] accountBalances (TERRA results unreliable)
 * [x] accountInformation - minor differences on Kava
 * [~] transaction
 * [~] transactions
 * [x] validatorDistribution
 * [x] validators
 * [x] validatorSets (not TERRA)
 * [x] latestBlock
 * [x] stakingPool (not TERRA)
 * [x] stakingParameters
 * [x] governanceProposals (not TERRA)
 * [x] governanceParametersDeposit (not TERRA)
 * [x] governanceParametersTallying (not TERRA)
 * [x] governanceParametersVoting (not TERRA)
 * [x] slashingParameters
 * [x] distributionCommunityPool (not TERRA)
 * [x] distributionParameters (not TERRA)
 * [x] prices
 * [x] coins
 * [x] fiatCurrencies
 * ============================================================================
 */