abstract_std/objects/entry/
ans_entry_convertor.rsuse crate::{
constants::{ASSET_DELIMITER, TYPE_DELIMITER},
objects::{
ans_host::{AnsHostError, AnsHostResult},
AssetEntry, DexAssetPairing, LpToken, PoolMetadata,
},
};
pub struct AnsEntryConvertor<T> {
entry: T,
}
impl<T> AnsEntryConvertor<T> {
pub fn new(entry: T) -> Self {
Self { entry }
}
}
impl AnsEntryConvertor<LpToken> {
pub fn asset_entry(self) -> AssetEntry {
AssetEntry::from(self.entry.to_string())
}
pub fn dex_asset_pairing(self) -> AnsHostResult<DexAssetPairing> {
let mut assets = self.entry.assets;
assets.sort();
assets.reverse();
Ok(DexAssetPairing::new(
assets.pop().unwrap(),
assets.pop().unwrap(),
self.entry.dex.as_str(),
))
}
}
impl AnsEntryConvertor<PoolMetadata> {
pub fn lp_token(self) -> LpToken {
LpToken {
dex: self.entry.dex,
assets: self.entry.assets,
}
}
pub fn lp_token_asset(self) -> AssetEntry {
AnsEntryConvertor::new(self.lp_token()).asset_entry()
}
}
impl AnsEntryConvertor<AssetEntry> {
pub fn lp_token(self) -> AnsHostResult<LpToken> {
let segments = self
.entry
.as_str()
.split(TYPE_DELIMITER)
.collect::<Vec<_>>();
if segments.len() != 2 {
return Err(AnsHostError::FormattingError {
object: "lp token".to_string(),
expected: "type/asset1,asset2".to_string(),
actual: self.entry.to_string(),
});
}
let dex_name = segments[0].to_string();
let mut assets: Vec<AssetEntry> = segments[1]
.split(ASSET_DELIMITER)
.map(AssetEntry::from)
.collect();
assets.sort_unstable();
if assets.len() < 2 {
return Err(AnsHostError::FormattingError {
object: "lp token".into(),
expected: "at least 2 assets in LP token".into(),
actual: self.entry.to_string(),
});
}
Ok(LpToken {
dex: dex_name,
assets,
})
}
}