Skip to main content

tempo_precompiles/tip20_factory/
dispatch.rs

1//! ABI dispatch for the [`TIP20Factory`] precompile.
2
3use crate::{Precompile, dispatch_call, input_cost, mutate, tip20_factory::TIP20Factory, view};
4use alloy::{primitives::Address, sol_types::SolInterface};
5use revm::precompile::{PrecompileError, PrecompileResult};
6use tempo_contracts::precompiles::ITIP20Factory::ITIP20FactoryCalls;
7
8impl Precompile for TIP20Factory {
9    fn call(&mut self, calldata: &[u8], msg_sender: Address) -> PrecompileResult {
10        self.storage
11            .deduct_gas(input_cost(calldata.len()))
12            .map_err(|_| PrecompileError::OutOfGas)?;
13
14        dispatch_call(
15            calldata,
16            ITIP20FactoryCalls::abi_decode,
17            |call| match call {
18                ITIP20FactoryCalls::createToken(call) => {
19                    mutate(call, msg_sender, |s, c| self.create_token(s, c))
20                }
21                ITIP20FactoryCalls::isTIP20(call) => view(call, |c| self.is_tip20(c.token)),
22                ITIP20FactoryCalls::getTokenAddress(call) => {
23                    view(call, |c| self.get_token_address(c))
24                }
25            },
26        )
27    }
28}
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33    use crate::{
34        storage::{StorageCtx, hashmap::HashMapStorageProvider},
35        test_util::{assert_full_coverage, check_selector_coverage},
36    };
37    use tempo_contracts::precompiles::ITIP20Factory::ITIP20FactoryCalls;
38
39    #[test]
40    fn tip20_factory_test_selector_coverage() {
41        let mut storage = HashMapStorageProvider::new(1);
42
43        StorageCtx::enter(&mut storage, || {
44            let mut factory = TIP20Factory::new();
45
46            let unsupported = check_selector_coverage(
47                &mut factory,
48                ITIP20FactoryCalls::SELECTORS,
49                "ITIP20Factory",
50                ITIP20FactoryCalls::name_by_selector,
51            );
52
53            assert_full_coverage([unsupported]);
54        })
55    }
56}