-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into token/spike
- Loading branch information
Showing
5 changed files
with
144 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,5 @@ Cargo.lock | |
# These are backup files generated by rustfmt | ||
**/*.rs.bk | ||
|
||
.vscode | ||
# IDE specific user-config | ||
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
[workspace] | ||
|
||
members = [ | ||
"fvm_dispatch", | ||
"fil_token", | ||
"testing/fil_token_integration", | ||
"testing/fil_token_integration/actors/wfil_token_actor", | ||
"fvm_dispatch", | ||
"fil_token", | ||
"testing/fil_token_integration", | ||
"testing/fil_token_integration/actors/wfil_token_actor", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,55 @@ | ||
use crate::hash::{Hasher, MethodHasher}; | ||
use thiserror::Error; | ||
|
||
use crate::hash::{Hasher, MethodNameErr, MethodResolver}; | ||
|
||
use fvm_ipld_encoding::RawBytes; | ||
use fvm_sdk::{send, SyscallResult}; | ||
use fvm_sdk::{send, sys::ErrorNumber}; | ||
use fvm_shared::{address::Address, econ::TokenAmount, receipt::Receipt}; | ||
|
||
/// Utility to invoke standard methods on deployed actors | ||
#[derive(Default)] | ||
pub struct MethodDispatcher<T: Hasher> { | ||
method_hasher: MethodHasher<T>, | ||
pub struct MethodMessenger<T: Hasher> { | ||
method_resolver: MethodResolver<T>, | ||
} | ||
|
||
#[derive(Error, PartialEq, Debug)] | ||
pub enum MethodMessengerError { | ||
#[error("error when calculating method name: `{0}`")] | ||
MethodName(MethodNameErr), | ||
#[error("error sending message: `{0}`")] | ||
Syscall(ErrorNumber), | ||
} | ||
|
||
impl From<ErrorNumber> for MethodMessengerError { | ||
fn from(e: ErrorNumber) -> Self { | ||
Self::Syscall(e) | ||
} | ||
} | ||
|
||
impl From<MethodNameErr> for MethodMessengerError { | ||
fn from(e: MethodNameErr) -> Self { | ||
Self::MethodName(e) | ||
} | ||
} | ||
|
||
impl<T: Hasher> MethodDispatcher<T> { | ||
/// Create a new MethodDispatcher with a given hasher | ||
impl<T: Hasher> MethodMessenger<T> { | ||
/// Creates a new method messenger using a specified hashing function (blake2b by default) | ||
pub fn new(hasher: T) -> Self { | ||
Self { | ||
method_hasher: MethodHasher::new(hasher), | ||
method_resolver: MethodResolver::new(hasher), | ||
} | ||
} | ||
|
||
/// Call a method on another actor by conventional name | ||
/// Calls a method (by name) on a specified actor by constructing and publishing the underlying | ||
/// on-chain Message | ||
pub fn call_method( | ||
&self, | ||
to: &Address, | ||
method: &str, | ||
params: RawBytes, | ||
value: TokenAmount, | ||
) -> SyscallResult<Receipt> { | ||
let method = self.method_hasher.method_number(method); | ||
send::send(to, method, params, value) | ||
) -> Result<Receipt, MethodMessengerError> { | ||
let method = self.method_resolver.method_number(method)?; | ||
send::send(to, method, params, value).map_err(MethodMessengerError::from) | ||
} | ||
} |