Skip to content

Commit c3ae281

Browse files
committed
eth: add experimental kimap_get function
1 parent 5d74500 commit c3ae281

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ homepage = "https://kinode.org"
88
repository = "https://github.com/kinode-dao/process_lib"
99

1010
[dependencies]
11-
alloy-primitives = "0.7.0"
11+
alloy-primitives = "0.7.5"
12+
alloy-sol-macro = "0.7.5"
13+
alloy-sol-types = "0.7.5"
1214
alloy = { version = "0.1.1", features = [
1315
"json-rpc",
1416
"rpc-types",

src/eth.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ pub use alloy::rpc::types::{
66
Block, BlockId, BlockNumberOrTag, FeeHistory, Filter, FilterBlockOption, Log, Transaction,
77
TransactionReceipt,
88
};
9+
use alloy_primitives::FixedBytes;
910
pub use alloy_primitives::{Address, BlockHash, BlockNumber, Bytes, TxHash, U128, U256, U64, U8};
11+
use alloy_sol_macro::sol;
12+
use alloy_sol_types::SolCall;
1013
use serde::{Deserialize, Serialize};
1114
use std::collections::{HashMap, HashSet};
15+
use std::str::FromStr;
1216

1317
//
1418
// types mirrored from runtime module
@@ -180,6 +184,20 @@ impl std::cmp::PartialEq<str> for NodeOrRpcUrl {
180184
}
181185
}
182186

187+
/// KiMap address. (simulation mode flag in process_lib?)
188+
const KIMAP: &str = "0x0165878A594ca255338adfa4d48449f69242Eb8F";
189+
190+
// Sol structures for KiMap requests
191+
sol! {
192+
function get (
193+
bytes32 entryhash
194+
) external view returns (
195+
address tokenBoundAccount,
196+
address tokenOwner,
197+
bytes memory note
198+
);
199+
}
200+
183201
/// An EVM chain provider. Create this object to start making RPC calls.
184202
/// Set the chain_id to determine which chain to call: requests will fail
185203
/// unless the node this process is running on has access to a provider
@@ -554,6 +572,38 @@ impl Provider {
554572
self.send_request_and_parse_response::<Bytes>(action)
555573
}
556574

575+
/// Gets an entry from the KiMap.
576+
///
577+
/// # Parameters
578+
/// - `entryhash`: The entry to get from the KiMap.
579+
/// # Returns
580+
/// A `Result<(Address, Address, Option<Bytes>), EthError>` representing the TBA, owner, and value if the entry is a note.
581+
pub fn get(&self, entryhash: &str) -> Result<(Address, Address, Option<Bytes>), EthError> {
582+
let get_call = getCall {
583+
entryhash: FixedBytes::<32>::from_str(entryhash)
584+
.map_err(|_| EthError::InvalidParams)?,
585+
}
586+
.abi_encode();
587+
588+
let tx_req = TransactionRequest::default()
589+
.input(TransactionInput::new(get_call.into()))
590+
.to(Address::from_str(KIMAP).unwrap());
591+
592+
let res_bytes = self.call(tx_req, None)?;
593+
594+
// note need new EthErrors for this! :)
595+
let res = getCall::abi_decode_returns(&res_bytes, false)
596+
.map_err(|_| EthError::RpcMalformedResponse)?;
597+
598+
let note_data = if res.note == Bytes::default() {
599+
None
600+
} else {
601+
Some(res.note)
602+
};
603+
604+
Ok((res.tokenBoundAccount, res.tokenOwner, note_data))
605+
}
606+
557607
/// Sends a raw transaction to the network.
558608
///
559609
/// # Parameters

0 commit comments

Comments
 (0)