use std::collections::HashMap;
use jsonrpsee::core::RpcResult;
use jsonrpsee::proc_macros::rpc;
use crate::api::types::{Base64Bytes, EthCall, GetLogsFilter};
use crate::db::types::{
AddressED, BlockResponseED, BytecodeED, LogED, TraceED, TxED, TxReceiptED, B256ED, U256ED,
};
use crate::global::{CARGO_PKG_VERSION, CONFIG, INDEXER_ADDRESS};
use crate::types::RawBytes;
lazy_static::lazy_static! {
pub static ref INDEXER_METHODS: Vec<String> = vec![
"brc20_mine".to_string(),
"brc20_deploy".to_string(),
"brc20_call".to_string(),
"brc20_deposit".to_string(),
"brc20_withdraw".to_string(),
"brc20_initialise".to_string(),
"brc20_finaliseBlock".to_string(),
"brc20_transact".to_string(),
"brc20_reorg".to_string(),
"brc20_commitToDatabase".to_string(),
"brc20_clearCaches".to_string(),
];
}
#[rpc(server, client)]
pub trait Brc20ProgApi {
#[method(name = "brc20_version")]
async fn brc20_version(&self) -> RpcResult<String> {
Ok(CARGO_PKG_VERSION.to_string())
}
#[method(name = "brc20_mine")]
async fn brc20_mine(&self, block_count: u64, timestamp: u64) -> RpcResult<()>;
#[method(name = "brc20_deploy")]
async fn brc20_deploy(
&self,
from_pkscript: String,
data: Option<RawBytes>,
base64_data: Option<Base64Bytes>,
timestamp: u64,
hash: B256ED,
tx_idx: u64,
inscription_id: Option<String>,
inscription_byte_len: Option<u64>,
) -> RpcResult<TxReceiptED>;
#[method(name = "brc20_call")]
async fn brc20_call(
&self,
from_pkscript: String,
contract_address: Option<AddressED>,
contract_inscription_id: Option<String>,
data: Option<RawBytes>,
base64_data: Option<Base64Bytes>,
timestamp: u64,
hash: B256ED,
tx_idx: u64,
inscription_id: Option<String>,
inscription_byte_len: Option<u64>,
) -> RpcResult<Option<TxReceiptED>>;
#[method(name = "brc20_transact")]
async fn brc20_transact(
&self,
raw_tx_data: Option<RawBytes>,
base64_raw_tx_data: Option<Base64Bytes>,
timestamp: u64,
hash: B256ED,
tx_idx: u64,
inscription_id: Option<String>,
inscription_byte_len: Option<u64>,
) -> RpcResult<Vec<TxReceiptED>>;
#[method(name = "brc20_deposit")]
async fn brc20_deposit(
&self,
to_pkscript: String,
ticker: String,
amount: U256ED,
timestamp: u64,
hash: B256ED,
tx_idx: u64,
inscription_id: Option<String>,
) -> RpcResult<TxReceiptED>;
#[method(name = "brc20_withdraw")]
async fn brc20_withdraw(
&self,
from_pkscript: String,
ticker: String,
amount: U256ED,
timestamp: u64,
hash: B256ED,
tx_idx: u64,
inscription_id: Option<String>,
) -> RpcResult<TxReceiptED>;
#[method(name = "brc20_balance")]
async fn brc20_balance(&self, pkscript: String, ticker: String) -> RpcResult<String>;
#[method(name = "brc20_initialise")]
async fn brc20_initialise(
&self,
genesis_hash: B256ED,
genesis_timestamp: u64,
genesis_height: u64,
) -> RpcResult<()>;
#[method(name = "brc20_getTxReceiptByInscriptionId")]
async fn brc20_get_tx_receipt_by_inscription_id(
&self,
inscription_id: String,
) -> RpcResult<Option<TxReceiptED>>;
#[method(name = "brc20_getInscriptionIdByTxHash")]
async fn brc20_get_inscription_id_by_tx_hash(
&self,
transaction: B256ED,
) -> RpcResult<Option<String>>;
#[method(name = "brc20_getInscriptionIdByContractAddress")]
async fn brc20_get_inscription_id_by_contract_address(
&self,
contract_address: AddressED,
) -> RpcResult<Option<String>>;
#[method(name = "brc20_finaliseBlock")]
async fn brc20_finalise_block(
&self,
timestamp: u64,
hash: B256ED,
block_tx_count: u64,
) -> RpcResult<()>;
#[method(name = "brc20_reorg")]
async fn brc20_reorg(&self, latest_valid_block_number: u64) -> RpcResult<()>;
#[method(name = "brc20_commitToDatabase")]
async fn brc20_commit_to_database(&self) -> RpcResult<()>;
#[method(name = "brc20_clearCaches")]
async fn brc20_clear_caches(&self) -> RpcResult<()>;
#[method(name = "eth_blockNumber")]
async fn eth_block_number(&self) -> RpcResult<String>;
#[method(name = "eth_getBlockByNumber")]
async fn eth_get_block_by_number(
&self,
block: String,
is_full: Option<bool>,
) -> RpcResult<BlockResponseED>;
#[method(name = "eth_getBlockByHash")]
async fn eth_get_block_by_hash(
&self,
block: B256ED,
is_full: Option<bool>,
) -> RpcResult<BlockResponseED>;
#[method(name = "eth_getTransactionCount")]
async fn eth_get_transaction_count(
&self,
account: AddressED,
block: String,
) -> RpcResult<String>;
#[method(name = "eth_getBlockTransactionCountByNumber")]
async fn eth_get_block_transaction_count_by_number(&self, block: String) -> RpcResult<String>;
#[method(name = "eth_getBlockTransactionCountByHash")]
async fn eth_get_block_transaction_count_by_hash(&self, block: B256ED) -> RpcResult<String>;
#[method(name = "eth_getLogs")]
async fn eth_get_logs(&self, filter: GetLogsFilter) -> RpcResult<Vec<LogED>>;
#[method(name = "eth_call")]
async fn eth_call(&self, eth_call: EthCall, block: Option<String>) -> RpcResult<String>;
#[method(name = "eth_estimateGas")]
async fn eth_estimate_gas(&self, eth_call: EthCall, block: Option<String>)
-> RpcResult<String>;
#[method(name = "eth_getStorageAt")]
async fn eth_get_storage_at(&self, contract: AddressED, location: U256ED) -> RpcResult<String>;
#[method(name = "eth_getCode")]
async fn eth_get_code(&self, contract: AddressED) -> RpcResult<BytecodeED>;
#[method(name = "eth_getTransactionReceipt")]
async fn eth_get_transaction_receipt(
&self,
transaction: B256ED,
) -> RpcResult<Option<TxReceiptED>>;
#[method(name = "debug_traceTransaction")]
async fn debug_trace_transaction(&self, transaction: B256ED) -> RpcResult<Option<TraceED>>;
#[method(name = "eth_getTransactionByHash")]
async fn eth_get_transaction_by_hash(&self, transaction: B256ED) -> RpcResult<Option<TxED>>;
#[method(name = "eth_getTransactionByBlockNumberAndIndex")]
async fn eth_get_transaction_by_block_number_and_index(
&self,
number: u64,
index: Option<u64>,
) -> RpcResult<Option<TxED>>;
#[method(name = "eth_getTransactionByBlockHashAndIndex")]
async fn eth_get_transaction_by_block_hash_and_index(
&self,
hash: B256ED,
index: Option<u64>,
) -> RpcResult<Option<TxED>>;
#[method(name = "eth_chainId")]
async fn eth_chain_id(&self) -> RpcResult<String> {
Ok(format!("0x{:x}", CONFIG.read().chain_id))
}
#[method(name = "eth_maxPriorityFeePerGas")]
async fn eth_max_priority_fee_per_gas(&self) -> RpcResult<String> {
Ok("0x0".to_string())
}
#[method(name = "eth_blobBaseFee")]
async fn eth_blob_base_fee(&self) -> RpcResult<String> {
Ok("0x0".to_string())
}
#[method(name = "eth_getBalance")]
async fn eth_get_balance(&self, _address: AddressED, _block: String) -> RpcResult<String> {
Ok("0x0".to_string())
}
#[method(name = "eth_getUncleCountByBlockNumber")]
async fn eth_get_uncle_count_by_block_number(&self, _number: u64) -> RpcResult<String> {
Ok("0x0".to_string())
}
#[method(name = "eth_getUncleCountByBlockHash")]
async fn eth_get_uncle_count_by_block_hash(&self, _hash: B256ED) -> RpcResult<String> {
Ok("0x0".to_string())
}
#[method(name = "eth_getUncleByBlockNumberAndIndex")]
async fn eth_get_uncle_by_block_number_and_index(
&self,
_number: u64,
_index: u64,
) -> RpcResult<Option<String>> {
Ok(None)
}
#[method(name = "eth_getUncleByBlockHashAndIndex")]
async fn eth_get_uncle_by_block_hash_and_index(
&self,
_hash: B256ED,
_index: u64,
) -> RpcResult<Option<String>> {
Ok(None)
}
#[method(name = "net_version")]
async fn net_version(&self) -> RpcResult<String> {
Ok("4252433230".to_string())
}
#[method(name = "eth_accounts")]
async fn eth_accounts(&self) -> RpcResult<Vec<String>> {
Ok(vec![INDEXER_ADDRESS.to_string()])
}
#[method(name = "eth_gasPrice")]
async fn eth_gas_price(&self) -> RpcResult<String> {
Ok("0x0".to_string())
}
#[method(name = "eth_syncing")]
async fn eth_syncing(&self) -> RpcResult<bool> {
Ok(false)
}
#[method(name = "txpool_content")]
async fn txpool_content(
&self,
) -> RpcResult<HashMap<String, HashMap<AddressED, HashMap<u64, TxED>>>>;
#[method(name = "txpool_contentFrom")]
async fn txpool_content_from(
&self,
from: AddressED,
) -> RpcResult<HashMap<String, HashMap<AddressED, HashMap<u64, TxED>>>>;
#[method(name = "debug_getRawHeader")]
async fn debug_get_raw_header(&self, block_hash_or_number: String)
-> RpcResult<Option<String>>;
#[method(name = "debug_getRawBlock")]
async fn debug_get_raw_block(&self, block_hash_or_number: String) -> RpcResult<Option<String>>;
#[method(name = "debug_getRawReceipts")]
async fn debug_get_raw_receipts(
&self,
block_hash_or_number: String,
) -> RpcResult<Option<Vec<String>>>;
}