diff --git a/crates/sdk/libs/near/src/lib.rs b/crates/sdk/libs/near/src/lib.rs index ff0e6b348..742ccb262 100644 --- a/crates/sdk/libs/near/src/lib.rs +++ b/crates/sdk/libs/near/src/lib.rs @@ -1,6 +1,6 @@ use jsonrpc::Response; use query::{QueryResponseKind, RpcQueryRequest}; -use types::{BlockId, StoreKey}; +use types::{BlockId, FunctionArgs, StoreKey}; use views::QueryRequest; mod jsonrpc; @@ -133,4 +133,34 @@ impl Client { Err(e) => Err(format!("Error: {}, Code: {}", e.message, e.code,)), } } + + pub fn call_function( + &self, + account_id: &str, + method_name: &str, + args: FunctionArgs, + block_id: BlockId, + ) -> Result { + let request = RpcQueryRequest { + block_id, + request: QueryRequest::CallFunction { + account_id: account_id.to_string(), + method_name: method_name.to_string(), + args, + }, + }; + + let response: Response = + self.client.call("query", request)?; + + match response.data { + Ok(r) => { + if let QueryResponseKind::CallResult(cr) = r.kind { + return Ok(cr); + } + return Err("Unexpected response returned.".to_string()); + } + Err(e) => Err(format!("Error: {}, Code: {}", e.message, e.code,)), + } + } } diff --git a/crates/sdk/libs/near/src/query.rs b/crates/sdk/libs/near/src/query.rs index 36f9dc4d5..215f9d0b1 100644 --- a/crates/sdk/libs/near/src/query.rs +++ b/crates/sdk/libs/near/src/query.rs @@ -1,7 +1,8 @@ use crate::{ types::{BlockHash, BlockHeight, BlockId}, views::{ - AccessKeyList, AccessKeyView, AccountView, ContractCodeView, QueryRequest, ViewStateResult, + AccessKeyList, AccessKeyView, AccountView, CallResult, ContractCodeView, QueryRequest, + ViewStateResult, }, }; @@ -28,4 +29,5 @@ pub enum QueryResponseKind { ViewState(ViewStateResult), AccessKey(AccessKeyView), AccessKeyList(AccessKeyList), + CallResult(CallResult), } diff --git a/crates/sdk/libs/near/src/types.rs b/crates/sdk/libs/near/src/types.rs index e48a25994..9c9fe67bc 100644 --- a/crates/sdk/libs/near/src/types.rs +++ b/crates/sdk/libs/near/src/types.rs @@ -23,3 +23,8 @@ pub struct StoreValue(#[serde_as(as = "Base64")] pub Vec); #[derive(serde::Serialize, serde::Deserialize, Clone, Debug)] #[serde(transparent)] pub struct StoreKey(#[serde_as(as = "Base64")] pub Vec); + +#[serde_as] +#[derive(serde::Serialize, Clone, Debug)] +#[serde(transparent)] +pub struct FunctionArgs(#[serde_as(as = "Base64")] Vec); diff --git a/crates/sdk/libs/near/src/views.rs b/crates/sdk/libs/near/src/views.rs index a51d360fa..b7acd5451 100644 --- a/crates/sdk/libs/near/src/views.rs +++ b/crates/sdk/libs/near/src/views.rs @@ -3,7 +3,9 @@ use std::sync::Arc; use serde_with::base64::Base64; use serde_with::serde_as; -use crate::types::{AccountId, BlockHeight, Nonce, StorageUsage, StoreKey, StoreValue}; +use crate::types::{ + AccountId, BlockHeight, FunctionArgs, Nonce, StorageUsage, StoreKey, StoreValue, +}; #[derive(serde::Serialize, Debug)] #[serde(tag = "request_type", rename_all = "snake_case")] @@ -28,6 +30,12 @@ pub enum QueryRequest { ViewAccessKeyList { account_id: AccountId, }, + CallFunction { + account_id: AccountId, + method_name: String, + #[serde(rename = "args_base64")] + args: FunctionArgs, + }, } #[derive(serde::Deserialize, Debug, Clone)] @@ -89,3 +97,9 @@ pub struct AccessKeyInfoView { pub public_key: String, pub access_key: AccessKeyView, } + +#[derive(serde::Deserialize, Debug, Clone)] +pub struct CallResult { + pub result: Vec, + pub logs: Vec, +}