Change representation of blockchain config in Instance API response

Allowed multiple blockchain configs.
This commit is contained in:
silverpill 2022-08-31 19:38:35 +00:00
parent b357b9bfa0
commit 26ae1036de
3 changed files with 86 additions and 58 deletions

View file

@ -918,36 +918,36 @@ components:
description: Post character limit. description: Post character limit.
type: integer type: integer
example: 5000 example: 5000
blockchain_id: blockchains:
description: Information about blockchain integrations.
type: array
items:
type: object
properties:
chain_id:
description: CAIP-2 chain ID. description: CAIP-2 chain ID.
type: string type: string
nullable: true
example: 'eip155:1' example: 'eip155:1'
blockchain_explorer_url: chain_metadata:
description: Blockchain explorer base URL. description: Additional information about blockchain
type: string type: object
nullable: true nullable: true
blockchain_contract_address: contract_address:
description: Blockchain contract address. description: Blockchain contract address.
type: string type: string
nullable: true nullable: true
blockchain_features: features:
description: Blockchain contract features. description: Blockchain features.
type: object type: object
nullable: true
properties: properties:
minter: minter:
description: Minter feature flag. description: Minter feature flag.
type: boolean type: boolean
example: true example: true
subscription: subscriptions:
description: Subscription feature flag. description: Subscriptions feature flag.
type: boolean type: boolean
example: true example: true
blockchain_info:
description: Additional information about blockchain
type: object
nullable: true
ipfs_gateway_url: ipfs_gateway_url:
description: IPFS gateway URL. description: IPFS gateway URL.
type: string type: string

View file

@ -2,6 +2,10 @@ mod blockchain;
mod environment; mod environment;
mod main; mod main;
pub use blockchain::{EthereumConfig, MoneroConfig}; pub use blockchain::{
BlockchainConfig,
EthereumConfig,
MoneroConfig,
};
pub use environment::Environment; pub use environment::Environment;
pub use main::{parse_config, Config, Instance}; pub use main::{parse_config, Config, Instance};

View file

@ -1,14 +1,22 @@
use serde::Serialize; use serde::Serialize;
use serde_json::{to_value, Value}; use serde_json::{to_value, Value};
use crate::config::Config; use crate::config::{BlockchainConfig, Config};
use crate::ethereum::contracts::ContractSet; use crate::ethereum::contracts::ContractSet;
use crate::mastodon_api::MASTODON_API_VERSION; use crate::mastodon_api::MASTODON_API_VERSION;
#[derive(Serialize)] #[derive(Serialize)]
struct BlockchainFeatures { struct BlockchainFeatures {
minter: bool, minter: bool,
subscription: bool, subscriptions: bool,
}
#[derive(Serialize)]
struct BlockchainInfo {
chain_id: String,
chain_metadata: Option<Value>,
contract_address: Option<String>,
features: BlockchainFeatures,
} }
#[derive(Serialize)] #[derive(Serialize)]
@ -22,11 +30,7 @@ pub struct InstanceInfo {
login_message: String, login_message: String,
post_character_limit: usize, post_character_limit: usize,
blockchain_id: Option<String>, blockchains: Vec<BlockchainInfo>,
blockchain_explorer_url: Option<String>,
blockchain_contract_address: Option<String>,
blockchain_features: Option<BlockchainFeatures>,
blockchain_info: Option<Value>,
ipfs_gateway_url: Option<String>, ipfs_gateway_url: Option<String>,
} }
@ -40,17 +44,45 @@ fn get_full_api_version(version: &str) -> String {
impl InstanceInfo { impl InstanceInfo {
pub fn create(config: &Config, maybe_blockchain: Option<&ContractSet>) -> Self { pub fn create(config: &Config, maybe_blockchain: Option<&ContractSet>) -> Self {
let ethereum_config = config.blockchain() let mut blockchains = vec![];
.and_then(|conf| conf.ethereum_config()); match config.blockchain() {
let blockchain_features = maybe_blockchain.map(|contract_set| { Some(BlockchainConfig::Ethereum(ethereum_config)) => {
let features = if let Some(contract_set) = maybe_blockchain {
BlockchainFeatures { BlockchainFeatures {
minter: contract_set.collectible.is_some(), minter: contract_set.collectible.is_some(),
subscription: contract_set.subscription.is_some(), subscriptions: contract_set.subscription.is_some(),
} }
}); } else {
let maybe_blockchain_info = ethereum_config BlockchainFeatures {
.and_then(|conf| conf.chain_metadata.as_ref()) minter: false,
subscriptions: false,
}
};
let maybe_chain_metadata = ethereum_config
.chain_metadata.as_ref()
.and_then(|metadata| to_value(metadata).ok()); .and_then(|metadata| to_value(metadata).ok());
blockchains.push(BlockchainInfo {
chain_id: ethereum_config.chain_id.to_string(),
chain_metadata: maybe_chain_metadata,
contract_address:
Some(ethereum_config.contract_address.clone()),
features: features,
});
},
Some(BlockchainConfig::Monero(monero_config)) => {
let features = BlockchainFeatures {
minter: false,
subscriptions: true,
};
blockchains.push(BlockchainInfo {
chain_id: monero_config.chain_id.to_string(),
chain_metadata: None,
contract_address: None,
features: features,
})
},
None => (),
};
Self { Self {
uri: config.instance().host(), uri: config.instance().host(),
title: config.instance_title.clone(), title: config.instance_title.clone(),
@ -60,15 +92,7 @@ impl InstanceInfo {
registrations: config.registrations_open, registrations: config.registrations_open,
login_message: config.login_message.clone(), login_message: config.login_message.clone(),
post_character_limit: config.post_character_limit, post_character_limit: config.post_character_limit,
blockchain_id: ethereum_config blockchains: blockchains,
.map(|val| val.chain_id.to_string()),
blockchain_explorer_url: ethereum_config
.and_then(|conf| conf.chain_metadata.as_ref())
.and_then(|metadata| metadata.explorer_url.clone()),
blockchain_contract_address: ethereum_config
.map(|val| val.contract_address.clone()),
blockchain_features: blockchain_features,
blockchain_info: maybe_blockchain_info,
ipfs_gateway_url: config.ipfs_gateway_url.clone(), ipfs_gateway_url: config.ipfs_gateway_url.clone(),
} }
} }