Skip to content

Commit

Permalink
feat(CA): filling initial transaction metadata (#864)
Browse files Browse the repository at this point in the history
  • Loading branch information
geekbrother authored Dec 11, 2024
1 parent 8c513b5 commit 52c80b7
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ build = "build.rs"
[dependencies]
wc = { git = "https://github.com/WalletConnect/utils-rs.git", tag = "v0.9.0", features = ["alloc", "analytics", "future", "http", "metrics", "geoip", "geoblock", "rate_limit"] }
relay_rpc = { git = "https://github.com/WalletConnect/WalletConnectRust.git", tag = "v0.32.0", features = ["cacao"] }
yttrium = { git = "https://github.com/reown-com/yttrium.git", rev = "65928ebce99862cb6fe3cc8468fb7add2aaf5a3e" }
yttrium = { git = "https://github.com/reown-com/yttrium.git", rev = "58f2d00e03f36f65d2138c1693fb931afab450f1" }

# Async
async-trait = "0.1.82"
Expand Down
5 changes: 5 additions & 0 deletions integration/chain_orchestrator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ describe('Chain abstraction orchestrator', () => {
if (BigInt(fundingFrom.bridgingFee) != BigInt(fundingFrom.amount - amount_to_topup)){
throw new Error(`Expected bridging fee is incorrect. `);
}
// Check the metadata initialTransaction
const initialTransaction = data.metadata.initialTransaction
expect(initialTransaction.symbol).toBe(usdc_token_symbol)
expect(initialTransaction.transferTo).toBe(receiver_address.toLowerCase())
expect(initialTransaction.tokenContract).toBe(usdc_contract_optimism.toLowerCase())

// Check the metadata checkIn
expect(typeof data.metadata.checkIn).toBe('number')
Expand Down
24 changes: 14 additions & 10 deletions src/handlers/chain_agnostic/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {
crate::{error::RpcError, handlers::MessageSource, utils::crypto::get_erc20_contract_balance},
alloy::primitives::{utils::Unit, Address, U256},
alloy::primitives::{Address, U256},
ethers::types::H160 as EthersH160,
phf::phf_map,
serde::{Deserialize, Serialize},
Expand All @@ -27,7 +27,7 @@ pub static BRIDGING_AVAILABLE_ASSETS: phf::Map<&'static str, phf::Map<&'static s
"eip155:42161" => "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
},
};
pub const USDC_DECIMALS: Unit = Unit::new(6).expect("Invalid decimals is used for USDC");
pub const USDC_DECIMALS: u8 = 6;

/// The status polling interval in ms for the client
pub const STATUS_POLLING_INTERVAL: u64 = 3000; // 3 seconds
Expand Down Expand Up @@ -55,14 +55,18 @@ pub enum BridgingStatus {
Error,
}

/// Check is the address is supported bridging asset
pub fn is_supported_bridging_asset(chain_id: String, contract: Address) -> bool {
BRIDGING_AVAILABLE_ASSETS.entries().any(|(_, chain_map)| {
chain_map.entries().any(|(chain, contract_address)| {
*chain == chain_id
/// Check is the address is supported bridging asset and return the token symbol and decimals
pub fn find_supported_bridging_asset(chain_id: &str, contract: Address) -> Option<(String, u8)> {
for (symbol, chain_map) in BRIDGING_AVAILABLE_ASSETS.entries() {
for (chain, contract_address) in chain_map.entries() {
if *chain == chain_id
&& contract == Address::from_str(contract_address).unwrap_or_default()
})
})
{
return Some((symbol.to_string(), USDC_DECIMALS));
}
}
}
None
}

/// Checking ERC20 balances for given address for provided ERC20 contracts
Expand Down Expand Up @@ -93,7 +97,7 @@ pub struct BridgingAsset {
pub chain_id: String,
pub token_symbol: String,
pub contract_address: Address,
pub decimals: Unit,
pub decimals: u8,
pub current_balance: U256,
}

Expand Down
32 changes: 22 additions & 10 deletions src/handlers/chain_agnostic/route.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use {
super::{
super::HANDLER_TASK_METRICS, check_bridging_for_erc20_transfer,
is_supported_bridging_asset, BridgingStatus, StorageBridgingItem, BRIDGING_AMOUNT_SLIPPAGE,
STATUS_POLLING_INTERVAL,
find_supported_bridging_asset, BridgingStatus, StorageBridgingItem,
BRIDGING_AMOUNT_SLIPPAGE, STATUS_POLLING_INTERVAL,
},
crate::{
analytics::MessageSource,
Expand Down Expand Up @@ -31,9 +31,9 @@ use {
wc::future::FutureExt,
yttrium::chain_abstraction::api::{
route::{
BridgingError, FundingMetadata, Metadata, RouteQueryParams, RouteRequest,
RouteResponse, RouteResponseAvailable, RouteResponseError, RouteResponseNotRequired,
RouteResponseSuccess,
BridgingError, FundingMetadata, InitialTransactionMetadata, Metadata, RouteQueryParams,
RouteRequest, RouteResponse, RouteResponseAvailable, RouteResponseError,
RouteResponseNotRequired, RouteResponseSuccess,
},
Transaction,
},
Expand Down Expand Up @@ -98,13 +98,18 @@ async fn handler_internal(
}

// Check if the destination address is supported ERC20 asset contract
if !is_supported_bridging_asset(request_payload.transaction.chain_id.clone(), to_address) {
error!("The destination address is not a supported bridging asset contract");
return Ok(no_bridging_needed_response.into_response());
}
// Attempt to destructure the result into symbol and decimals using a match expression
let (initial_tx_token_symbol, initial_tx_token_decimals) =
match find_supported_bridging_asset(&request_payload.transaction.chain_id, to_address) {
Some((symbol, decimals)) => (symbol, decimals),
None => {
error!("The destination address is not a supported bridging asset contract");
return Ok(no_bridging_needed_response.into_response());
}
};

// Decode the ERC20 transfer function data
let (_erc20_receiver, erc20_transfer_value) = decode_erc20_transfer_data(&transaction_data)?;
let (erc20_receiver, erc20_transfer_value) = decode_erc20_transfer_data(&transaction_data)?;

// Get the current balance of the ERC20 token and check if it's enough for the transfer
// without bridging or calculate the top-up value
Expand Down Expand Up @@ -344,6 +349,13 @@ async fn handler_internal(
decimals: bridge_decimals,
}],
check_in: STATUS_POLLING_INTERVAL,
initial_transaction: InitialTransactionMetadata {
transfer_to: erc20_receiver,
amount: erc20_transfer_value,
token_contract: to_address,
symbol: initial_tx_token_symbol,
decimals: initial_tx_token_decimals,
},
},
},
)))
Expand Down

0 comments on commit 52c80b7

Please sign in to comment.