Skip to content

Commit

Permalink
add function call.
Browse files Browse the repository at this point in the history
  • Loading branch information
saeed-zil committed Jun 21, 2024
1 parent 4ed7b77 commit b13f56f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
32 changes: 31 additions & 1 deletion crates/sdk/libs/near/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<views::CallResult, String> {
let request = RpcQueryRequest {
block_id,
request: QueryRequest::CallFunction {
account_id: account_id.to_string(),
method_name: method_name.to_string(),
args,
},
};

let response: Response<query::RpcQueryResponse, String> =
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,)),
}
}
}
4 changes: 3 additions & 1 deletion crates/sdk/libs/near/src/query.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::{
types::{BlockHash, BlockHeight, BlockId},
views::{
AccessKeyList, AccessKeyView, AccountView, ContractCodeView, QueryRequest, ViewStateResult,
AccessKeyList, AccessKeyView, AccountView, CallResult, ContractCodeView, QueryRequest,
ViewStateResult,
},
};

Expand All @@ -28,4 +29,5 @@ pub enum QueryResponseKind {
ViewState(ViewStateResult),
AccessKey(AccessKeyView),
AccessKeyList(AccessKeyList),
CallResult(CallResult),
}
5 changes: 5 additions & 0 deletions crates/sdk/libs/near/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ pub struct StoreValue(#[serde_as(as = "Base64")] pub Vec<u8>);
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)]
#[serde(transparent)]
pub struct StoreKey(#[serde_as(as = "Base64")] pub Vec<u8>);

#[serde_as]
#[derive(serde::Serialize, Clone, Debug)]
#[serde(transparent)]
pub struct FunctionArgs(#[serde_as(as = "Base64")] Vec<u8>);
16 changes: 15 additions & 1 deletion crates/sdk/libs/near/src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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)]
Expand Down Expand Up @@ -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<u8>,
pub logs: Vec<String>,
}

0 comments on commit b13f56f

Please sign in to comment.