Skip to content

Commit

Permalink
adapt Payment
Browse files Browse the repository at this point in the history
  • Loading branch information
0xh3rman committed Aug 27, 2024
1 parent 8a1ee84 commit bae5831
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 58 deletions.
1 change: 0 additions & 1 deletion crates/gem_evm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod address;
pub mod erc2612;
pub mod erc681;
pub mod lido;
20 changes: 5 additions & 15 deletions crates/primitives/src/asset_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use typeshare::typeshare;
use crate::chain::Chain;
use gem_evm::address::EthereumAddress;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[typeshare(swift = "Equatable, Codable, Hashable")]
pub struct AssetId {
pub chain: Chain,
Expand All @@ -31,10 +31,7 @@ impl AssetId {
let split: Vec<&str> = asset_id.split('_').collect();
if split.len() == 1 {
if let Ok(chain) = asset_id.parse::<Chain>() {
return Some(AssetId {
chain,
token_id: None,
});
return Some(AssetId { chain, token_id: None });
}
} else if split.len() >= 2 {
if let Ok(chain) = split[0].parse::<Chain>() {
Expand All @@ -52,10 +49,7 @@ impl AssetId {
}

pub fn from_chain(chain: Chain) -> AssetId {
AssetId {
chain,
token_id: None,
}
AssetId { chain, token_id: None }
}

pub fn is_native(&self) -> bool {
Expand Down Expand Up @@ -125,12 +119,8 @@ mod tests {

#[test]
fn test_new_asset_id_with_coin_and_token_extra_underscore() {
let asset_id =
AssetId::new("ton_EQAvlWFDxGF2lXm67y4yzC17wYKD9A0guwPkMs1gOsM__NOT").unwrap();
let asset_id = AssetId::new("ton_EQAvlWFDxGF2lXm67y4yzC17wYKD9A0guwPkMs1gOsM__NOT").unwrap();
assert_eq!(asset_id.chain, Chain::Ton);
assert_eq!(
asset_id.token_id,
Some("EQAvlWFDxGF2lXm67y4yzC17wYKD9A0guwPkMs1gOsM__NOT".to_owned())
);
assert_eq!(asset_id.token_id, Some("EQAvlWFDxGF2lXm67y4yzC17wYKD9A0guwPkMs1gOsM__NOT".to_owned()));
}
}
13 changes: 12 additions & 1 deletion crates/gem_evm/src/erc681.rs → crates/primitives/src/erc681.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl TransactionRequest {
let main_parts: Vec<&str> = parts[0].split('/').collect();

// The first part should be the target address with optional chain id and pay prefix
let mut target_address = main_parts.get(0).ok_or(anyhow!("Missing target address"))?.to_string();
let mut target_address = main_parts.first().ok_or(anyhow!("Missing target address"))?.to_string();

let target_parts = target_address.split('@').collect::<Vec<&str>>();
let mut chain_id = None;
Expand Down Expand Up @@ -99,6 +99,17 @@ mod tests {
assert!(erc681.is_err());
}

#[test]
fn test_ens_name_uri() {
let uri = "ethereum:pay-gemwallet.eth@1";
let erc681 = TransactionRequest::parse(uri).unwrap();
assert_eq!(erc681.target_address, "gemwallet.eth");
assert_eq!(erc681.prefix.unwrap(), "pay");
assert_eq!(erc681.chain_id, Some(1));
assert_eq!(erc681.function_name, None);
assert_eq!(erc681.parameters.len(), 0);
}

#[test]
fn test_chain_id_uri() {
let uri = "ethereum:pay-0x32Be343B94f860124dC4fEe278FDCBD38C102D88@1";
Expand Down
1 change: 1 addition & 0 deletions crates/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,4 @@ pub mod erc2612;
pub mod explorers;
pub mod solana_token_program;
pub use self::solana_token_program::SolanaTokenProgramId;
pub mod erc681;
90 changes: 49 additions & 41 deletions crates/primitives/src/payment_decoder.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use crate::asset_id::AssetId;
use crate::erc681::{TransactionRequest, ETHEREUM_SCHEME};
use crate::Chain;
use anyhow::Error;
use std::collections::HashMap;

use crate::Chain;

#[derive(Debug, PartialEq)]
pub struct Payment {
pub address: String,
pub amount: Option<String>,
pub memo: Option<String>,
pub chain: Option<Chain>,
pub asset_id: Option<AssetId>,
}

#[derive(Debug)]
Expand All @@ -22,10 +23,9 @@ impl PaymentURLDecoder {
let scheme = chunks[0];
let path: &str = chunks[1];
let path_chunks: Vec<&str> = path.split('?').collect();

// TODO: https://github.com/ethereum/ercs/blob/master/ERCS/erc-681.md
let address = if scheme == "ethereum" && path_chunks[0].contains('@') {
path_chunks[0].split('@').collect::<Vec<&str>>()[0].to_string()
let address = if scheme == ETHEREUM_SCHEME {
let transaction_request = TransactionRequest::parse(path)?;
return Ok(transaction_request.into());
} else {
path_chunks[0].to_string()
};
Expand All @@ -35,7 +35,7 @@ impl PaymentURLDecoder {
address,
amount: None,
memo: None,
chain: None,
asset_id: None,
});
} else if path_chunks.len() == 2 {
let query = path_chunks[1];
Expand All @@ -47,7 +47,7 @@ impl PaymentURLDecoder {
address,
amount,
memo,
chain: None,
asset_id: None,
});
} else {
return Err(Error::msg("BIP21 format is incorrect"));
Expand All @@ -58,7 +58,7 @@ impl PaymentURLDecoder {
address: string.to_string(),
amount: None,
memo: None,
chain: None,
asset_id: None,
})
}

Expand All @@ -77,6 +77,30 @@ impl PaymentURLDecoder {
}
}

impl From<TransactionRequest> for Payment {
fn from(val: TransactionRequest) -> Self {
let address: String;
let amount: Option<String>;
let asset_id: Option<AssetId>;
// ERC20
if val.function_name == Some("transfer".to_string()) {
address = val.parameters.get("address").map(|x| x.to_string()).unwrap_or("".to_string());
amount = val.parameters.get("uint256").map(|x| x.to_string());
asset_id = Some(AssetId::from(Chain::Ethereum, Some(val.target_address)));
} else {
address = val.target_address;
amount = val.parameters.get("value").map(|x| x.to_string());
asset_id = Some(AssetId::from(Chain::Ethereum, None));
};
Self {
address,
amount,
memo: None,
asset_id,
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -89,7 +113,7 @@ mod tests {
address: "0x1f9090aaE28b8a3dCeaDf281B0F12828e676c326".to_string(),
amount: None,
memo: None,
chain: None,
asset_id: None,
}
);
}
Expand All @@ -102,19 +126,16 @@ mod tests {
address: "HA4hQMs22nCuRN7iLDBsBkboz2SnLM1WkNtzLo6xEDY5".to_string(),
amount: None,
memo: None,
chain: None,
asset_id: None,
}
);
assert_eq!(
PaymentURLDecoder::decode(
"solana:HA4hQMs22nCuRN7iLDBsBkboz2SnLM1WkNtzLo6xEDY5?amount=0.266232"
)
.unwrap(),
PaymentURLDecoder::decode("solana:HA4hQMs22nCuRN7iLDBsBkboz2SnLM1WkNtzLo6xEDY5?amount=0.266232").unwrap(),
Payment {
address: "HA4hQMs22nCuRN7iLDBsBkboz2SnLM1WkNtzLo6xEDY5".to_string(),
amount: Some("0.266232".to_string()),
memo: None,
chain: None,
asset_id: None,
}
);
}
Expand All @@ -127,72 +148,59 @@ mod tests {
address: "bc1pn6pua8a566z7t822kphpd2el45ntm23354c3krfmpe3nnn33lkcskuxrdl".to_string(),
amount: Some("0.00001".to_string()),
memo: None,
chain: None,
asset_id: None,
}
);

assert_eq!(
PaymentURLDecoder::decode(
"ethereum:0xA20d8935d61812b7b052E08f0768cFD6D81cB088?amount=0.01233&memo=test"
)
.unwrap(),
PaymentURLDecoder::decode("ethereum:0xA20d8935d61812b7b052E08f0768cFD6D81cB088?amount=0.01233&memo=test").unwrap(),
Payment {
address: "0xA20d8935d61812b7b052E08f0768cFD6D81cB088".to_string(),
amount: Some("0.01233".to_string()),
memo: Some("test".to_string()),
chain: None,
asset_id: None,
}
);

assert_eq!(
PaymentURLDecoder::decode(
"solana:3u3ta6yXYgpheLGc2GVF3QkLHAUwBrvX71Eg8XXjJHGw?amount=0.42301"
)
.unwrap(),
PaymentURLDecoder::decode("solana:3u3ta6yXYgpheLGc2GVF3QkLHAUwBrvX71Eg8XXjJHGw?amount=0.42301").unwrap(),
Payment {
address: "3u3ta6yXYgpheLGc2GVF3QkLHAUwBrvX71Eg8XXjJHGw".to_string(),
amount: Some("0.42301".to_string()),
memo: None,
chain: None,
asset_id: None,
}
);

assert_eq!(
PaymentURLDecoder::decode(
"ton:EQAzoUpalAaXnVm5MoiYWRZguLFzY0KxFjLv3MkRq5BXzyiQ?amount=0.00001"
)
.unwrap(),
PaymentURLDecoder::decode("ton:EQAzoUpalAaXnVm5MoiYWRZguLFzY0KxFjLv3MkRq5BXzyiQ?amount=0.00001").unwrap(),
Payment {
address: "EQAzoUpalAaXnVm5MoiYWRZguLFzY0KxFjLv3MkRq5BXzyiQ".to_string(),
amount: Some("0.00001".to_string()),
memo: None,
chain: None,
asset_id: None,
}
);
}

#[test]
fn test_eip681() {
assert_eq!(
PaymentURLDecoder::decode("ethereum:0xcB3028d6120802148f03d6c884D6AD6A210Df62A@0x38")
.unwrap(),
PaymentURLDecoder::decode("ethereum:0xcB3028d6120802148f03d6c884D6AD6A210Df62A@0x38").unwrap(),
Payment {
address: "0xcB3028d6120802148f03d6c884D6AD6A210Df62A".to_string(),
amount: None,
memo: None,
chain: None,
asset_id: None,
}
);
assert_eq!(
PaymentURLDecoder::decode(
"ethereum:0xcB3028d6120802148f03d6c884D6AD6A210Df62A@0x38?amount=1.23"
)
.unwrap(),
PaymentURLDecoder::decode("ethereum:0xcB3028d6120802148f03d6c884D6AD6A210Df62A@0x38?amount=1.23").unwrap(),
Payment {
address: "0xcB3028d6120802148f03d6c884D6AD6A210Df62A".to_string(),
amount: Some("1.23".to_string()),
memo: None,
chain: None,
asset_id: None,
}
);
}
Expand Down

0 comments on commit bae5831

Please sign in to comment.