fvm_actor_utils/syscalls/
fvm_syscalls.rs

1use anyhow::Result;
2use fvm_ipld_blockstore::Blockstore;
3use fvm_ipld_encoding::ipld_block::IpldBlock;
4use fvm_sdk;
5use fvm_shared::sys::SendFlags;
6use fvm_shared::{address::Address, MethodNum, Response};
7
8use super::Syscalls;
9use crate::util::ActorRuntime;
10
11/// Runtime that delegates to [`fvm_sdk`] allowing actors to be deployed on-chain.
12#[derive(Default, Debug, Clone, Copy)]
13pub struct FvmSyscalls {}
14
15impl Syscalls for FvmSyscalls {
16    fn root(&self) -> Result<cid::Cid, super::NoStateError> {
17        fvm_sdk::sself::root().map_err(|_| super::NoStateError)
18    }
19
20    fn set_root(&self, cid: &cid::Cid) -> Result<(), super::NoStateError> {
21        fvm_sdk::sself::set_root(cid).map_err(|_| super::NoStateError)
22    }
23
24    fn receiver(&self) -> fvm_shared::ActorID {
25        fvm_sdk::message::receiver()
26    }
27
28    fn caller(&self) -> fvm_shared::ActorID {
29        fvm_sdk::message::caller()
30    }
31
32    fn send(
33        &self,
34        to: &Address,
35        method: MethodNum,
36        params: Option<IpldBlock>,
37        value: fvm_shared::econ::TokenAmount,
38    ) -> fvm_sdk::SyscallResult<Response> {
39        match fvm_sdk::send::send(to, method, params, value, None, SendFlags::empty()) {
40            Ok(res) => Ok(Response { exit_code: res.exit_code, return_data: res.return_data }),
41            Err(err) => Err(err),
42        }
43    }
44
45    fn resolve_address(&self, addr: &Address) -> Option<fvm_shared::ActorID> {
46        fvm_sdk::actor::resolve_address(addr)
47    }
48}
49
50impl<S: Syscalls + Clone, BS: Blockstore + Clone> ActorRuntime<S, BS> {
51    pub fn new_fvm_runtime() -> ActorRuntime<FvmSyscalls, crate::blockstore::Blockstore> {
52        ActorRuntime { syscalls: FvmSyscalls::default(), blockstore: crate::blockstore::Blockstore }
53    }
54}