Skip to content

Commit

Permalink
feat: add get_accounts_by_code_hash
Browse files Browse the repository at this point in the history
  • Loading branch information
0xdeafbeef committed Sep 26, 2024
1 parent c65a1fd commit 1d06f5f
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 1 deletion.
40 changes: 40 additions & 0 deletions client/examples/count.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use everscale_rpc_client::ClientOptions;

#[tokio::main]
async fn main() {
let codehash = std::env::args().nth(1).expect("codehash is required");
tracing_subscriber::fmt::SubscriberBuilder::default()
.with_max_level(tracing::Level::INFO)
.init();

let client = everscale_rpc_client::RpcClient::new(
["https://jrpc.everwallet.net/rpc".parse().unwrap()],
ClientOptions::default(),
)
.await
.unwrap();

let code_hash = hex::decode(codehash).unwrap().try_into().unwrap();
let count = count_accounts(&client, code_hash).await.unwrap();
println!("Accounts count: {}", count);
}

async fn count_accounts(
client: &everscale_rpc_client::RpcClient,
code_hash: [u8; 32],
) -> anyhow::Result<u32> {
let mut continuation = None;
let mut count = 0;
loop {
let addrs = client
.get_accounts_by_code_hash(code_hash, continuation.as_ref(), 100)
.await?;

continuation = addrs.last().cloned();
count += addrs.len() as u32;
if addrs.len() != 100 {
break;
}
}
Ok(count)
}
24 changes: 24 additions & 0 deletions client/src/jrpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,30 @@ where
}
}

async fn get_account_by_code_hash(
&self,
code_hash: [u8; 32],
continuation: Option<&MsgAddressInt>,
limit: u8,
) -> Result<Vec<MsgAddressInt>> {
let continuation = continuation.cloned();
let req = jrpc::GetAccountsByCodeHashRequest {
code_hash,
continuation,
limit,
};
let request = RpcRequest::JRPC(JrpcRequest {
method: "getAccountsByCodeHash",
params: &req,
});
let addrs: Vec<String> = self
.request(&request)
.await
.map(|x| x.result)
.context("Failed to get accounts by code hash")?;
addrs.into_iter().map(|x| x.parse()).collect()
}

async fn get_transactions(
&self,
limit: u8,
Expand Down
27 changes: 27 additions & 0 deletions client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,26 @@ impl RpcClient {
}
}

pub async fn get_accounts_by_code_hash(
&self,
code_hash: [u8; 32],
continuation: Option<&MsgAddressInt>,
limit: u8,
) -> Result<Vec<MsgAddressInt>> {
match self {
RpcClient::Jrpc(client) => {
client
.get_account_by_code_hash(code_hash, continuation, limit)
.await
}
RpcClient::Proto(client) => {
client
.get_account_by_code_hash(code_hash, continuation, limit)
.await
}
}
}

pub async fn run_local(
&self,
address: &MsgAddressInt,
Expand Down Expand Up @@ -360,6 +380,13 @@ where
time: u32,
) -> Result<Option<ExistingContract>, RunError>;

async fn get_account_by_code_hash(
&self,
code_hash: [u8; 32],
continuation: Option<&MsgAddressInt>,
limit: u8,
) -> Result<Vec<MsgAddressInt>>;

async fn run_local(
&self,
address: &MsgAddressInt,
Expand Down
27 changes: 27 additions & 0 deletions client/src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,33 @@ where
}
}

async fn get_account_by_code_hash(
&self,
code_hash: [u8; 32],
continuation: Option<&MsgAddressInt>,
limit: u8,
) -> Result<Vec<MsgAddressInt>> {
let request: RpcRequest<()> = RpcRequest::PROTO(rpc::Request {
call: Some(rpc::request::Call::GetAccountsByCodeHash(
rpc::request::GetAccountsByCodeHash {
code_hash: bytes::Bytes::copy_from_slice(&code_hash),
continuation: continuation.map(utils::addr_to_bytes),
limit: limit as u32,
},
)),
});

let response = self.request(&request).await?.into_inner();
match response.result {
Some(rpc::response::Result::GetAccounts(accounts)) => Ok(accounts
.account
.into_iter()
.filter_map(|x| utils::bytes_to_addr(&x).ok())
.collect()),
_ => Err(ClientError::InvalidResponse.into()),
}
}

async fn get_transactions(
&self,
limit: u8,
Expand Down
2 changes: 1 addition & 1 deletion server/src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl ProtoServer {
let config_response = rpc::response::GetBlockchainConfig {
global_id: block.global_id,
config: Bytes::from(config.write_to_bytes()?),
seqno,
seqno: Some(seqno),
};

Ok((Arc::new(key_block_response), Arc::new(config_response)))
Expand Down

0 comments on commit 1d06f5f

Please sign in to comment.