Skip to content

Commit 6b032e2

Browse files
committed
eth: separate kimap struct
1 parent c45e40a commit 6b032e2

File tree

1 file changed

+68
-29
lines changed

1 file changed

+68
-29
lines changed

src/eth.rs

Lines changed: 68 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -572,36 +572,14 @@ impl Provider {
572572
self.send_request_and_parse_response::<Bytes>(action)
573573
}
574574

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-
};
575+
/// Returns a KiMap instance with the default address using this provider.
576+
pub fn kimap(&self) -> KiMap {
577+
KiMap::default(self)
578+
}
603579

604-
Ok((res.tokenBoundAccount, res.tokenOwner, note_data))
580+
/// Returns a KiMap instance with a custom address using this provider.
581+
pub fn kimap_with_address(&self, address: Address) -> KiMap {
582+
KiMap::new(self, address)
605583
}
606584

607585
/// Sends a raw transaction to the network.
@@ -688,3 +666,64 @@ impl Provider {
688666
}
689667
}
690668
}
669+
670+
/// Helper struct for the KiMap.
671+
pub struct KiMap<'a> {
672+
provider: &'a Provider,
673+
address: Address,
674+
}
675+
676+
impl<'a> KiMap<'a> {
677+
/// Creates a new KiMap instance with a specified address.
678+
///
679+
/// # Arguments
680+
/// * `provider` - A reference to the Provider.
681+
/// * `address` - The address of the KiMap contract.
682+
pub fn new(provider: &'a Provider, address: Address) -> Self {
683+
Self { provider, address }
684+
}
685+
686+
/// Creates a new KiMap instance with the default address.
687+
///
688+
/// # Arguments
689+
/// * `provider` - A reference to the Provider.
690+
pub fn default(provider: &'a Provider) -> Self {
691+
Self::new(provider, Self::default_address())
692+
}
693+
694+
/// Returns the default KiMap contract address.
695+
pub fn default_address() -> Address {
696+
Address::from_str(KIMAP).unwrap()
697+
}
698+
699+
/// Gets an entry from the KiMap.
700+
///
701+
/// # Parameters
702+
/// - `entryhash`: The entry to get from the KiMap.
703+
/// # Returns
704+
/// A `Result<(Address, Address, Option<Bytes>), EthError>` representing the TBA, owner, and value if the entry is a note.
705+
pub fn get(&self, entryhash: &str) -> Result<(Address, Address, Option<Bytes>), EthError> {
706+
let get_call = getCall {
707+
entryhash: FixedBytes::<32>::from_str(entryhash)
708+
.map_err(|_| EthError::InvalidParams)?,
709+
}
710+
.abi_encode();
711+
712+
let tx_req = TransactionRequest::default()
713+
.input(TransactionInput::new(get_call.into()))
714+
.to(self.address);
715+
716+
let res_bytes = self.provider.call(tx_req, None)?;
717+
718+
let res = getCall::abi_decode_returns(&res_bytes, false)
719+
.map_err(|_| EthError::RpcMalformedResponse)?;
720+
721+
let note_data = if res.note == Bytes::default() {
722+
None
723+
} else {
724+
Some(res.note)
725+
};
726+
727+
Ok((res.tokenBoundAccount, res.tokenOwner, note_data))
728+
}
729+
}

0 commit comments

Comments
 (0)