Skip to content

Commit 76efa54

Browse files
authored
Merge pull request #134 from kinode-dao/bp/kimap-errors
Bp/kimap errors
2 parents 74965dd + 11630ce commit 76efa54

File tree

3 files changed

+73
-6
lines changed

3 files changed

+73
-6
lines changed

src/eth.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ pub use alloy::rpc::types::{
1010
pub use alloy_primitives::{Address, BlockHash, BlockNumber, Bytes, TxHash, U128, U256, U64, U8};
1111
use serde::{Deserialize, Serialize};
1212
use std::collections::{HashMap, HashSet};
13+
use std::error::Error;
14+
use std::fmt;
1315

1416
/// Subscription kind. Pulled directly from alloy (https://github.com/alloy-rs/alloy).
1517
/// Why? Because alloy is not yet 1.0 and the types in this interface must be stable.
@@ -130,6 +132,24 @@ pub enum EthError {
130132
RpcMalformedResponse,
131133
}
132134

135+
impl fmt::Display for EthError {
136+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
137+
match self {
138+
EthError::RpcError(e) => write!(f, "RPC error: {:?}", e),
139+
EthError::MalformedRequest => write!(f, "Malformed request"),
140+
EthError::NoRpcForChain => write!(f, "No RPC provider for chain"),
141+
EthError::SubscriptionClosed(id) => write!(f, "Subscription {} closed", id),
142+
EthError::InvalidMethod(m) => write!(f, "Invalid method: {}", m),
143+
EthError::InvalidParams => write!(f, "Invalid parameters"),
144+
EthError::PermissionDenied => write!(f, "Permission denied"),
145+
EthError::RpcTimeout => write!(f, "RPC request timed out"),
146+
EthError::RpcMalformedResponse => write!(f, "RPC returned malformed response"),
147+
}
148+
}
149+
}
150+
151+
impl Error for EthError {}
152+
133153
/// The action type used for configuring eth:distro:sys. Only processes which have the "root"
134154
/// [`crate::Capability`] from eth:distro:sys can successfully send this action.
135155
#[derive(Clone, Debug, Serialize, Deserialize)]

src/kimap.rs

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ use alloy::rpc::types::request::{TransactionInput, TransactionRequest};
55
use alloy::{hex, primitives::keccak256};
66
use alloy_primitives::{Address, Bytes, FixedBytes, B256};
77
use alloy_sol_types::{SolCall, SolEvent, SolValue};
8+
use contract::tokenCall;
89
use serde::{Deserialize, Serialize};
10+
use std::error::Error;
11+
use std::fmt;
912
use std::str::FromStr;
1013

1114
/// kimap deployment address on optimism
@@ -210,12 +213,19 @@ pub mod contract {
210213

211214
function supportsInterface(bytes4 interfaceId) external view returns (bool);
212215

213-
/// Retrieves the address of the ERC-6551 implementation of the
214-
/// zeroth entry. This is set once at initialization.
216+
/// Gets the token identifier that owns this token-bound account (TBA).
217+
/// This is a core function of the ERC-6551 standard that returns the
218+
/// identifying information about the NFT that owns this account.
219+
/// The return values are constant and cannot change over time.
215220
///
216221
/// Returns:
217-
/// - implementation: The address of the ERC-6551 implementation.
218-
function get6551Implementation() external view returns (address);
222+
/// - chainId: The EIP-155 chain ID where the owning NFT exists
223+
/// - tokenContract: The contract address of the owning NFT
224+
/// - tokenId: The token ID of the owning NFT
225+
function token()
226+
external
227+
view
228+
returns (uint256 chainId, address tokenContract, uint256 tokenId);
219229
}
220230
}
221231

@@ -259,6 +269,21 @@ pub enum DecodeLogError {
259269
UnresolvedParent(String),
260270
}
261271

272+
impl fmt::Display for DecodeLogError {
273+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
274+
match self {
275+
DecodeLogError::UnexpectedTopic(topic) => write!(f, "Unexpected topic: {:?}", topic),
276+
DecodeLogError::InvalidName(name) => write!(f, "Invalid name: {}", name),
277+
DecodeLogError::DecodeError(err) => write!(f, "Decode error: {}", err),
278+
DecodeLogError::UnresolvedParent(parent) => {
279+
write!(f, "Could not resolve parent: {}", parent)
280+
}
281+
}
282+
}
283+
}
284+
285+
impl Error for DecodeLogError {}
286+
262287
/// Canonical function to determine if a kimap entry is valid. This should
263288
/// be used whenever reading a new kimap entry from a mints query, because
264289
/// while most frontends will enforce these rules, it is possible to post
@@ -514,6 +539,28 @@ impl Kimap {
514539
Ok((res.tba, res.owner, note_data))
515540
}
516541

542+
/// Gets a namehash from an existing TBA address.
543+
///
544+
/// # Parameters
545+
/// - `tba`: The TBA to get the namehash of.
546+
/// # Returns
547+
/// A `Result<String, EthError>` representing the namehash of the TBA.
548+
pub fn get_namehash_from_tba(&self, tba: Address) -> Result<String, EthError> {
549+
let token_call = tokenCall {}.abi_encode();
550+
551+
let tx_req = TransactionRequest::default()
552+
.input(TransactionInput::new(token_call.into()))
553+
.to(tba);
554+
555+
let res_bytes = self.provider.call(tx_req, None)?;
556+
557+
let res = tokenCall::abi_decode_returns(&res_bytes, false)
558+
.map_err(|_| EthError::RpcMalformedResponse)?;
559+
560+
let namehash: FixedBytes<32> = res.tokenId.into();
561+
Ok(format!("0x{}", hex::encode(namehash)))
562+
}
563+
517564
/// Create a filter for all mint events.
518565
pub fn mint_filter(&self) -> crate::eth::Filter {
519566
crate::eth::Filter::new()

src/net.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ pub enum IndexerRequests {
128128
#[derive(Clone, Debug, Serialize, Deserialize, Hash, Eq, PartialEq)]
129129
pub struct NamehashToNameRequest {
130130
pub hash: String,
131-
pub block: Option<u64>,
131+
pub block: u64,
132132
}
133133

134134
/// Response from `kns-indexer:kns-indexer:sys`.
@@ -226,7 +226,7 @@ where
226226
.body(
227227
serde_json::to_vec(&IndexerRequests::NamehashToName(NamehashToNameRequest {
228228
hash: namehash.into(),
229-
block,
229+
block: block.unwrap_or(0),
230230
}))
231231
.unwrap(),
232232
)

0 commit comments

Comments
 (0)