fragment DataSourceFilter on Filter {
  key {
    name
    type
    numberDecimalPlaces
  }
}

fragment DataSource on DataSourceSpec {
  id
  data {
    sourceType {
      ... on DataSourceDefinitionExternal {
        sourceType {
          ... on DataSourceSpecConfiguration {
            signers {
              signer {
                ... on PubKey {
                  key
                }
                ... on ETHAddress {
                  address
                }
              }
            }
            filters {
              ...DataSourceFilter
            }
          }
        }
      }
      ... on DataSourceDefinitionInternal {
        sourceType {
          ... on DataSourceSpecConfigurationTime {
            conditions {
              operator
              value
            }
          }
          ... on DataSourceSpecConfigurationTimeTrigger {
            __typename
            triggers {
              initial
              every
            }
            conditions {
              operator
              value
            }
          }
        }
      }
    }
  }
}

fragment Future on Future {
  quoteName
  settlementAsset {
    id
    symbol
    name
    decimals
    quantum
  }
  dataSourceSpecForSettlementData {
    ...DataSource
  }
  dataSourceSpecForTradingTermination {
    ...DataSource
  }
  dataSourceSpecBinding {
    settlementDataProperty
    tradingTerminationProperty
  }
}

fragment Perpetual on Perpetual {
  quoteName
  settlementAsset {
    id
    symbol
    name
    decimals
    quantum
  }
  dataSourceSpecForSettlementData {
    ...DataSource
  }
  dataSourceSpecForSettlementSchedule {
    ...DataSource
  }
  dataSourceSpecBinding {
    settlementDataProperty
    settlementScheduleProperty
  }
}

query MarketInfo($marketId: ID!) {
  market(id: $marketId) {
    id
    decimalPlaces
    positionDecimalPlaces
    state
    tradingMode
    proposal {
      id
      rationale {
        title
        description
      }
    }
    marketTimestamps {
      open
      close
    }
    openingAuction {
      durationSecs
      volume
    }
    accountsConnection {
      edges {
        node {
          type
          asset {
            id
          }
          balance
        }
      }
    }
    fees {
      factors {
        makerFee
        infrastructureFee
        liquidityFee
      }
    }
    priceMonitoringSettings {
      parameters {
        triggers {
          horizonSecs
          probability
          auctionExtensionSecs
        }
      }
    }
    riskFactors {
      market
      short
      long
    }
    liquidityMonitoringParameters {
      triggeringRatio
      targetStakeParameters {
        timeWindow
        scalingFactor
      }
    }
    tradableInstrument {
      instrument {
        id
        name
        code
        metadata {
          tags
        }
        product {
          ... on Future {
            ...Future
          }
          ... on Perpetual {
            ...Perpetual
          }
        }
      }
      riskModel {
        ... on LogNormalRiskModel {
          tau
          riskAversionParameter
          params {
            r
            sigma
            mu
          }
        }
        ... on SimpleRiskModel {
          params {
            factorLong
            factorShort
          }
        }
      }
      marginCalculator {
        scalingFactors {
          searchLevel
          initialMargin
          collateralRelease
        }
      }
    }
  }
}
