-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve asset parsing to use named keys as issuer
Previously the issuer had to be a fully resolved `xdr::AccountId`, now it can be an `Address`, which then can be resolved. Thus allowing the following: `USDC:circle`
- Loading branch information
1 parent
31cde54
commit 0220df7
Showing
10 changed files
with
67 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,54 @@ | ||
use std::str::FromStr; | ||
|
||
use crate::xdr::{self, AlphaNum12, AlphaNum4, AssetCode}; | ||
use crate::{config::{address, locator}, xdr::{self, AlphaNum12, AlphaNum4, AssetCode}}; | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct Asset(pub xdr::Asset); | ||
pub enum Asset { | ||
Asset(AssetCode, address::Address), | ||
Native, | ||
} | ||
|
||
|
||
#[derive(thiserror::Error, Debug)] | ||
pub enum Error { | ||
#[error("cannot parse asset: {0}, expected format: 'native' or 'code:issuer'")] | ||
CannotParseAsset(String), | ||
|
||
#[error(transparent)] | ||
Xdr(#[from] xdr::Error), | ||
#[error(transparent)] | ||
Address(#[from] address::Error), | ||
} | ||
|
||
impl FromStr for Asset { | ||
type Err = Error; | ||
|
||
fn from_str(value: &str) -> Result<Self, Self::Err> { | ||
if value == "native" { | ||
return Ok(Asset(xdr::Asset::Native)); | ||
return Ok(Asset::Native); | ||
} | ||
let mut iter = value.splitn(2, ':'); | ||
let (Some(code), Some(issuer), None) = (iter.next(), iter.next(), iter.next()) else { | ||
return Err(Error::CannotParseAsset(value.to_string())); | ||
}; | ||
let issuer = issuer.parse()?; | ||
Ok(Asset(match code.parse()? { | ||
AssetCode::CreditAlphanum4(asset_code) => { | ||
xdr::Asset::CreditAlphanum4(AlphaNum4 { asset_code, issuer }) | ||
} | ||
AssetCode::CreditAlphanum12(asset_code) => { | ||
xdr::Asset::CreditAlphanum12(AlphaNum12 { asset_code, issuer }) | ||
} | ||
})) | ||
Ok(Asset::Asset(code.parse()?, issuer.parse()?)) | ||
} | ||
} | ||
|
||
impl From<Asset> for xdr::Asset { | ||
fn from(builder: Asset) -> Self { | ||
builder.0 | ||
} | ||
} | ||
|
||
impl From<&Asset> for xdr::Asset { | ||
fn from(builder: &Asset) -> Self { | ||
builder.clone().into() | ||
impl Asset { | ||
pub fn resolve(&self, locator: &locator::Args) -> Result<xdr::Asset, Error> { | ||
Ok(match self { | ||
Asset::Asset(code, issuer) => { | ||
let issuer = issuer.resolve_muxed_account(locator, None)?.account_id(); | ||
match code.clone() { | ||
AssetCode::CreditAlphanum4(asset_code) => { | ||
xdr::Asset::CreditAlphanum4(AlphaNum4 { asset_code, issuer }) | ||
} | ||
AssetCode::CreditAlphanum12(asset_code) => { | ||
xdr::Asset::CreditAlphanum12(AlphaNum12 { asset_code, issuer }) | ||
} | ||
} | ||
} | ||
Asset::Native => xdr::Asset::Native, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters