sui_graphql_rpc/types/
move_function.rsuse async_graphql::*;
use sui_package_resolver::FunctionDef;
use crate::error::Error;
use super::{
move_module::MoveModule,
open_move_type::{abilities, MoveAbility, MoveVisibility, OpenMoveType},
sui_address::SuiAddress,
};
pub(crate) struct MoveFunction {
package: SuiAddress,
module: String,
name: String,
visibility: MoveVisibility,
is_entry: bool,
type_parameters: Vec<MoveFunctionTypeParameter>,
parameters: Vec<OpenMoveType>,
return_: Vec<OpenMoveType>,
checkpoint_viewed_at: u64,
}
#[derive(SimpleObject)]
pub(crate) struct MoveFunctionTypeParameter {
constraints: Vec<MoveAbility>,
}
#[Object]
impl MoveFunction {
async fn module(&self, ctx: &Context<'_>) -> Result<MoveModule> {
let Some(module) =
MoveModule::query(ctx, self.package, &self.module, self.checkpoint_viewed_at)
.await
.extend()?
else {
return Err(Error::Internal(format!(
"Failed to load module for function: {}::{}::{}",
self.package, self.module, self.name,
)))
.extend();
};
Ok(module)
}
async fn name(&self) -> &str {
&self.name
}
async fn visibility(&self) -> Option<&MoveVisibility> {
Some(&self.visibility)
}
async fn is_entry(&self) -> Option<bool> {
Some(self.is_entry)
}
async fn type_parameters(&self) -> Option<&Vec<MoveFunctionTypeParameter>> {
Some(&self.type_parameters)
}
async fn parameters(&self) -> Option<&Vec<OpenMoveType>> {
Some(&self.parameters)
}
#[graphql(name = "return")]
async fn return_(&self) -> Option<&Vec<OpenMoveType>> {
Some(&self.return_)
}
}
impl MoveFunction {
pub(crate) fn new(
package: SuiAddress,
module: String,
name: String,
def: FunctionDef,
checkpoint_viewed_at: u64,
) -> Self {
let type_parameters = def
.type_params
.into_iter()
.map(|constraints| MoveFunctionTypeParameter {
constraints: abilities(constraints),
})
.collect();
let parameters = def.parameters.into_iter().map(OpenMoveType::from).collect();
let return_ = def.return_.into_iter().map(OpenMoveType::from).collect();
MoveFunction {
package,
module,
name,
visibility: def.visibility.into(),
is_entry: def.is_entry,
type_parameters,
parameters,
return_,
checkpoint_viewed_at,
}
}
pub(crate) async fn query(
ctx: &Context<'_>,
address: SuiAddress,
module: &str,
function: &str,
checkpoint_viewed_at: u64,
) -> Result<Option<Self>, Error> {
let Some(module) = MoveModule::query(ctx, address, module, checkpoint_viewed_at).await?
else {
return Ok(None);
};
module.function_impl(function.to_string())
}
}