brc20-prog 0.15.8

BRC20 programmable module - Smart contract execution engine compatible with BRC20 standard
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
use std::collections::HashMap;

use alloy::primitives::keccak256;
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, CARGO_RUST_VERSION, CONFIG, INDEXER_ADDRESS};
use crate::types::RawBytes;

lazy_static::lazy_static! {
    // BRC20 Methods intended for the indexers, so they require auth
    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(),
        "debug_getBlockTraceString".to_string(), // Expensive, indexer-only debug method
        "debug_getBlockTraceHash".to_string(), // Expensive, indexer-only debug method
    ];
}

#[rpc(server, client)]
pub trait Brc20ProgApi {
    /// BRC20 Methods, these methods are intended for the indexers

    /// Returns current brc20_prog version
    #[method(name = "brc20_version")]
    async fn brc20_version(&self) -> RpcResult<String> {
        Ok(CARGO_PKG_VERSION.to_string())
    }

    /// Mines blocks for the given block count at the timestamp
    #[method(name = "brc20_mine")]
    async fn brc20_mine(&self, block_count: u64, timestamp: u64) -> RpcResult<()>;

    /// Deploys a contract with the given parameters
    #[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: String,
        inscription_byte_len: u64,
        op_return_tx_id: B256ED,
    ) -> RpcResult<TxReceiptED>;

    /// Calls a contract with the given parameters
    #[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: String,
        inscription_byte_len: u64,
        op_return_tx_id: B256ED,
    ) -> RpcResult<Option<TxReceiptED>>;

    /// Calls a contract with the given parameters
    #[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: String,
        inscription_byte_len: u64,
        op_return_tx_id: B256ED,
    ) -> RpcResult<Vec<TxReceiptED>>;

    /// Deposits brc20 tokens to the given address
    #[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: String,
    ) -> RpcResult<TxReceiptED>;

    /// Withdraws brc20 tokens from the given address
    #[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: String,
    ) -> RpcResult<TxReceiptED>;

    /// Checks BRC20 balance for given address
    #[method(name = "brc20_balance")]
    async fn brc20_balance(&self, pkscript: String, ticker: String) -> RpcResult<String>;

    /// Initialises the BRC20 prog module with the given genesis hash and timestamp
    #[method(name = "brc20_initialise")]
    async fn brc20_initialise(
        &self,
        genesis_hash: B256ED,
        genesis_timestamp: u64,
        genesis_height: u64,
    ) -> RpcResult<()>;

    /// Retrieves transaction receipt for given inscription id
    #[method(name = "brc20_getTxReceiptByInscriptionId")]
    async fn brc20_get_tx_receipt_by_inscription_id(
        &self,
        inscription_id: String,
    ) -> RpcResult<Option<TxReceiptED>>;

    /// Retrieves inscription id for given transaction hash
    #[method(name = "brc20_getInscriptionIdByTxHash")]
    async fn brc20_get_inscription_id_by_tx_hash(
        &self,
        transaction: B256ED,
    ) -> RpcResult<Option<String>>;

    /// Retrieves inscription id by contract address
    #[method(name = "brc20_getInscriptionIdByContractAddress")]
    async fn brc20_get_inscription_id_by_contract_address(
        &self,
        contract_address: AddressED,
    ) -> RpcResult<Option<String>>;

    /// Finalises the block with the given parameters
    #[method(name = "brc20_finaliseBlock")]
    async fn brc20_finalise_block(
        &self,
        timestamp: u64,
        hash: B256ED,
        block_tx_count: u64,
    ) -> RpcResult<()>;

    /// Reverts the state to the given latest valid block number
    #[method(name = "brc20_reorg")]
    async fn brc20_reorg(&self, latest_valid_block_number: u64) -> RpcResult<()>;

    /// Commits the state to the database
    #[method(name = "brc20_commitToDatabase")]
    async fn brc20_commit_to_database(&self) -> RpcResult<()>;

    /// Clears the caches, if used before committing to the database, data will be lost
    #[method(name = "brc20_clearCaches")]
    async fn brc20_clear_caches(&self) -> RpcResult<()>;

    ///
    ///
    /// Eth Methods
    ///
    ///

    /// Returns the latest block number in hex format
    #[method(name = "eth_blockNumber")]
    async fn eth_block_number(&self) -> RpcResult<String>;

    /// Returns the block information for the requested block number
    #[method(name = "eth_getBlockByNumber")]
    async fn eth_get_block_by_number(
        &self,
        block: String,
        is_full: Option<bool>,
    ) -> RpcResult<BlockResponseED>;

    /// Returns the block information for the requested block hash
    #[method(name = "eth_getBlockByHash")]
    async fn eth_get_block_by_hash(
        &self,
        block: B256ED,
        is_full: Option<bool>,
    ) -> RpcResult<BlockResponseED>;

    /// Returns the transaction count by address and block number
    #[method(name = "eth_getTransactionCount")]
    async fn eth_get_transaction_count(
        &self,
        account: AddressED,
        block: String,
    ) -> RpcResult<String>;

    /// Returns the transaction count by block number
    #[method(name = "eth_getBlockTransactionCountByNumber")]
    async fn eth_get_block_transaction_count_by_number(&self, block: String) -> RpcResult<String>;

    /// Returns the transaction count by block hash
    #[method(name = "eth_getBlockTransactionCountByHash")]
    async fn eth_get_block_transaction_count_by_hash(&self, block: B256ED) -> RpcResult<String>;

    /// Gets logs for the given filter
    #[method(name = "eth_getLogs")]
    async fn eth_get_logs(&self, filter: GetLogsFilter) -> RpcResult<Vec<LogED>>;

    /// Calls a contract with the given parameters
    #[method(name = "eth_call")]
    async fn eth_call(&self, eth_call: EthCall, block: Option<String>) -> RpcResult<String>;

    /// Estimates the gas for the given transaction
    #[method(name = "eth_estimateGas")]
    async fn eth_estimate_gas(&self, eth_call: EthCall, block: Option<String>)
        -> RpcResult<String>;

    /// Get storage for the given contract and memory location
    #[method(name = "eth_getStorageAt")]
    async fn eth_get_storage_at(&self, contract: AddressED, location: U256ED) -> RpcResult<String>;

    /// Returns the bytecode of the contract at the given address
    #[method(name = "eth_getCode")]
    async fn eth_get_code(&self, contract: AddressED) -> RpcResult<BytecodeED>;

    /// Returns the transaction receipt for the given transaction hash
    #[method(name = "eth_getTransactionReceipt")]
    async fn eth_get_transaction_receipt(
        &self,
        transaction: B256ED,
    ) -> RpcResult<Option<TxReceiptED>>;

    /// Returns the trace for the given transaction hash
    #[method(name = "debug_traceTransaction")]
    async fn debug_trace_transaction(&self, transaction: B256ED) -> RpcResult<Option<TraceED>>;

    /// Returns the block trace string for the given block number
    #[method(name = "debug_getBlockTraceString")]
    async fn debug_get_block_trace_string(&self, block: String) -> RpcResult<Option<String>>;

    /// Returns the trace hash for the given block number
    #[method(name = "debug_getBlockTraceHash")]
    async fn debug_get_block_trace_hash(&self, block: String) -> RpcResult<Option<String>>;

    /// Returns the transaction by hash
    #[method(name = "eth_getTransactionByHash")]
    async fn eth_get_transaction_by_hash(&self, transaction: B256ED) -> RpcResult<Option<TxED>>;

    /// Returns the transaction by block number and index
    #[method(name = "eth_getTransactionByBlockNumberAndIndex")]
    async fn eth_get_transaction_by_block_number_and_index(
        &self,
        number: u64,
        index: Option<u64>,
    ) -> RpcResult<Option<TxED>>;

    /// Returns the transaction by block hash and index
    #[method(name = "eth_getTransactionByBlockHashAndIndex")]
    async fn eth_get_transaction_by_block_hash_and_index(
        &self,
        hash: B256ED,
        index: Option<u64>,
    ) -> RpcResult<Option<TxED>>;

    ///
    ///
    /// Eth methods with static values
    ///
    ///

    /// Returns the chain id in hex format ("BRC20" in hex)
    #[method(name = "eth_chainId")]
    async fn eth_chain_id(&self) -> RpcResult<String> {
        Ok(format!("0x{:x}", CONFIG.read().chain_id))
    }

    /// Returns max priority fee per gas in hex format (0 in BRC20)
    #[method(name = "eth_maxPriorityFeePerGas")]
    async fn eth_max_priority_fee_per_gas(&self) -> RpcResult<String> {
        Ok("0x0".to_string())
    }

    /// Returns the blob base fee in hex format (0 in BRC20)
    #[method(name = "eth_blobBaseFee")]
    async fn eth_blob_base_fee(&self) -> RpcResult<String> {
        Ok("0x0".to_string())
    }

    /// Returns the balance of the account at the given address (0 in BRC20)
    #[method(name = "eth_getBalance")]
    async fn eth_get_balance(&self, _address: AddressED, _block: String) -> RpcResult<String> {
        Ok("0x0".to_string())
    }

    /// Returns the uncle count of the block at the given block number (0 in BRC20)
    #[method(name = "eth_getUncleCountByBlockNumber")]
    async fn eth_get_uncle_count_by_block_number(&self, _number: u64) -> RpcResult<String> {
        Ok("0x0".to_string())
    }

    /// Returns the uncle count of the block at the given block hash (0 in BRC20)
    #[method(name = "eth_getUncleCountByBlockHash")]
    async fn eth_get_uncle_count_by_block_hash(&self, _hash: B256ED) -> RpcResult<String> {
        Ok("0x0".to_string())
    }

    /// Returns the uncle by block number and index (null in BRC20)
    #[method(name = "eth_getUncleByBlockNumberAndIndex")]
    async fn eth_get_uncle_by_block_number_and_index(
        &self,
        _number: u64,
        _index: u64,
    ) -> RpcResult<Option<String>> {
        Ok(None)
    }

    /// Returns the uncle by block hash and index (null in BRC20)
    #[method(name = "eth_getUncleByBlockHashAndIndex")]
    async fn eth_get_uncle_by_block_hash_and_index(
        &self,
        _hash: B256ED,
        _index: u64,
    ) -> RpcResult<Option<String>> {
        Ok(None)
    }

    /// Returns net version
    #[method(name = "net_version")]
    async fn net_version(&self) -> RpcResult<String> {
        Ok("4252433230".to_string())
    }

    /// Returns the client version in format brc20_prog/{version}/{os}-{arch}/{rust_version}
    #[method(name = "web3_clientVersion")]
    fn web3_client_version(&self) -> RpcResult<String> {
        Ok(format!(
            "Brc20Prog/v{}/{}-{}/rs{}",
            *CARGO_PKG_VERSION,
            std::env::consts::OS,
            std::env::consts::ARCH,
            *CARGO_RUST_VERSION
        ))
    }

    /// Returns the keccak256 hash of the given bytes
    #[method(name = "web3_sha3")]
    async fn web3_sha3(&self, bytes: RawBytes) -> RpcResult<String> {
        Ok(format!(
            "0x{:x}",
            keccak256(bytes.value().map(|v| v.to_vec()).unwrap_or(vec![]))
        ))
    }

    /// Returns accounts (BRC20 indexer address)
    #[method(name = "eth_accounts")]
    async fn eth_accounts(&self) -> RpcResult<Vec<String>> {
        Ok(vec![INDEXER_ADDRESS.to_string()])
    }

    /// Returns the current gas price in hex format (0 in BRC20, as there's no gas token)
    #[method(name = "eth_gasPrice")]
    async fn eth_gas_price(&self) -> RpcResult<String> {
        Ok("0x0".to_string())
    }

    /// Returns syncing status (false in BRC20)
    #[method(name = "eth_syncing")]
    async fn eth_syncing(&self) -> RpcResult<bool> {
        Ok(false)
    }

    /// Returns txpool content
    #[method(name = "txpool_content")]
    async fn txpool_content(
        &self,
    ) -> RpcResult<HashMap<String, HashMap<AddressED, HashMap<u64, TxED>>>>;

    /// Returns txpool content filtered by from address
    #[method(name = "txpool_contentFrom")]
    async fn txpool_content_from(
        &self,
        from: AddressED,
    ) -> RpcResult<HashMap<String, HashMap<AddressED, HashMap<u64, TxED>>>>;

    /// Returns the raw header for the given block hash or number
    #[method(name = "debug_getRawHeader")]
    async fn debug_get_raw_header(&self, block_hash_or_number: String)
        -> RpcResult<Option<String>>;

    /// Returns the raw block for the given block hash or number
    #[method(name = "debug_getRawBlock")]
    async fn debug_get_raw_block(&self, block_hash_or_number: String) -> RpcResult<Option<String>>;

    /// Returns the raw receipts for the given block hash or number
    #[method(name = "debug_getRawReceipts")]
    async fn debug_get_raw_receipts(
        &self,
        block_hash_or_number: String,
    ) -> RpcResult<Option<Vec<String>>>;
}