import { ICeloAccountBalances , ICeloTransaction , ICeloTransactionTags , IQuery , NetworkDefinition , } from "@anthem/utils" ; import { PaginationParams } from "../resolvers/resolvers" ; import { AxiosUtil , getHostFromNetworkName } from "../tools/axios-utils" ; import { fetchProposalTitleFromID } from "./icarus-wings" ; /** ===========================================================================
 * Types & Config
 * ============================================================================
 */ interface CeloDelegation { group : string ; totalVotes : string ; activeVotes : string ; pendingVotes : string ; } interface CeloAccountSnapshot { snapshotDate : string ; address : string ; height : string ; snapshotReward : string ; snapshotCommission : string ; availableGoldBalance : string ; totalLockedGoldBalance : string ; nonVotingLockedGoldBalance : string ; votingLockedGoldBalance : string ; pendingWithdrawalBalance : string ; celoUSDValue : string ; delegations : CeloDelegation [ ] ; } interface CeloTransactionResponse { blockNumber : number ; hash : string ; timestamp : string ; from : string ; to : string ; tags : ICeloTransactionTags [ ] ; logs : any ; details : { nonce : number ; gasLimit : number ; gasPrice : number ; gasUsed : number ; feeCurrency : string | null ; gatewayFeeRecipient : string | null ; gatewayFee : number ; to : string ; value : number ; raw : any ; } ; } interface CeloGovernanceProposalHistoryResponse { queuedProposals : GenericProposalHistory [ ] ; approvalProposals : GenericProposalHistory [ ] ; referendumProposals : GenericProposalHistory [ ] ; executionProposals : GenericProposalHistory [ ] ; expiredProposals : GenericProposalHistory [ ] ; } interface GenericProposalHistory { proposalID : number ; stage : string ; proposer : string ; description : string ; } /** ===========================================================================
 * Celo REST API Utils
 * ----------------------------------------------------------------------------
 * This file contains the utils for fetching Celo Network data.
 * ============================================================================
 */ /**
 * Fetch Celo account balances.
 */ const fetchAccountBalances = async ( address , network ) : Promise < ICeloAccountBalances > => { const host = getHostFromNetworkName ( network . name ) ; const url = `${ host }/accounts/${ address }` ; const response = await AxiosUtil . get < ICeloAccountBalances > ( url ) ; return response ; } ; /**
 * Fetch account history.
 */ const fetchAccountHistory = async ( address , network ) : Promise < IQuery [ "celoAccountHistory" ] > => { const host = getHostFromNetworkName ( network . name ) ; const url = `${ host }/accounts/${ address }/history` ; const response = await AxiosUtil . get < CeloAccountSnapshot [ ] > ( url ) ; return response ; } ; /**
 * Fetch transactions history.
 */ const fetchTransactions = async ( args ) : Promise < IQuery [ "celoTransactions" ] > => { const { address , network , startingPage , pageSize } = args ; const host = getHostFromNetworkName ( network . name ) ; const params = `limit=${ pageSize + 1 }&page=${ startingPage }` ; const url = `${ host }/accounts/${ address }/transactions?${ params }` ; const response = await AxiosUtil . get < CeloTransactionResponse [ ] > ( url ) ; const pages = response . slice ( 0 , pageSize ) ; const moreResultsExist = response . length > pageSize ; const formattedResponse = pages . map ( formatCeloTransaction ) ; return { limit : pageSize , moreResultsExist , page : startingPage , data : formattedResponse , } ; } ; /**
 * Fetch governance tagged transactions.
 */ const fetchGovernanceTransactions = async ( address ) : Promise < IQuery [ "celoGovernanceTransactions" ] > => { const host = getHostFromNetworkName ( "CELO" ) ; const url = `${ host }/accounts/${ address }/transactions?filter_by_source=governance` ; const response = await AxiosUtil . get < CeloTransactionResponse [ ] > ( url ) ; const formattedResponse = response . map ( formatCeloTransaction ) ; return formattedResponse ; } ; /**
 * Fetch a single transaction by hash.
 */ const fetchTransaction = async ( hash ) : Promise < ICeloTransaction > => { const host = getHostFromNetworkName ( "CELO" ) ; const path = `system/transactions/${ hash }` ; const url = `${ host }/${ path }` ; const response = await AxiosUtil . get < { transaction : CeloTransactionResponse ; } > ( url ) ; if ( response && response . transaction ) { return formatCeloTransaction ( response . transaction ) ; } else { throw new Error ( `No transaction found for hash: ${ hash }` ) ; } } ; /**
 * Fetch system balances.
 */ const fetchSystemBalances = async ( ) : Promise < IQuery [ "celoSystemBalances" ] > => { const host = getHostFromNetworkName ( "CELO" ) ; const url = `${ host }/system/balances` ; return AxiosUtil . get < IQuery [ "celoSystemBalances" ] > ( url ) ; } ; /**
 * Fetch system history.
 */ const fetchSystemHistory = async ( ) : Promise < IQuery [ "celoSystemHistory" ] > => { const host = getHostFromNetworkName ( "CELO" ) ; const url = `${ host }/system/history` ; return AxiosUtil . get < IQuery [ "celoSystemHistory" ] > ( url ) ; } ; /**
 * Fetch system validator groups list.
 */ const fetchValidatorGroups = async ( ) : Promise < IQuery [ "celoValidatorGroups" ] > => { const host = getHostFromNetworkName ( "CELO" ) ; const url = `${ host }/system/validator_groups` ; return AxiosUtil . get < IQuery [ "celoValidatorGroups" ] > ( url ) ; } ; /**
 * Fetch current governance proposals
 */ const fetchGovernanceProposals = async ( ) : Promise < IQuery [ "celoGovernanceProposals" ] > => { const host = getHostFromNetworkName ( "CELO" ) ; const url = `${ host }/system/proposals_history` ; const response = await AxiosUtil . get < CeloGovernanceProposalHistoryResponse > ( url ) ; return addGistContentToProposals ( response ) ; } ; /**
 * Fetch network summary stats for Celo.
 */ const fetchNetworkSummaryStats = async ( ) => { return { inflation : null , expectedReward : null , totalSupply : 1.84448e26 , } ; } ; /** ===========================================================================
 * Utils
 * ============================================================================
 */ /**
 * Return the tags as a JSON string to avoid creating a GraphQL union
 * type for the tags data.
 */ const stringifyTags = ( tag ) => { return { eventname : tag . eventname , source : tag . source , parameters : JSON . stringify ( tag . parameters ) , } ; } ; /**
 * Transform Celo transaction response data.
 */ const formatCeloTransaction = ( tx ) => { const { raw , ... details } = tx . details ; const result = { ... tx , details , tags : tx . tags . map ( stringifyTags ) , } ; return result ; } ; /**
 * Transform the proposal history by fetching and adding the GitHub gist
 * content to the proposals.
 */ const addGistContentToProposals = async ( proposalHistory ) => { const entries = Object . entries ( proposalHistory ) ; const result = { } ; for ( const [ key , value ] of entries ) { const gists = await fetchGistContentForProposalList ( value ) ; result [ key ] = gists ; } return result as IQuery [ "celoGovernanceProposals" ] ; } ; /**
 * Fetch the actual gist content for each proposal.
 */ const fetchGistContentForProposalList = async ( proposals ) : Promise < GenericProposalHistory [ ] > => { return Promise . all ( proposals . map ( async ( proposal ) => { const { proposalID } = proposal ; const { title , contents } = await fetchProposalTitleFromID ( proposalID ) ; const gist = proposal . description ; return { ... proposal , gist , title , description : contents , } ; } ) ) ; } ; /** ===========================================================================
 * Export
 * ============================================================================
 */ const CELO = { fetchAccountBalances , fetchAccountHistory , fetchTransactions , fetchTransaction , fetchGovernanceTransactions , fetchSystemBalances , fetchSystemHistory , fetchValidatorGroups , fetchGovernanceProposals , fetchNetworkSummaryStats , } ; export default CELO ;