Skip to content

Commit

Permalink
fix(revert): reverting transaction schema change
Browse files Browse the repository at this point in the history
  • Loading branch information
geekbrother committed Dec 18, 2024
1 parent 0bc557f commit 420b282
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 45 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 = "befa0cb" }
yttrium = { git = "https://github.com/reown-com/yttrium.git", rev = "0684124" }

# Async
async-trait = "0.1.82"
Expand Down
37 changes: 29 additions & 8 deletions integration/chain_orchestrator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ describe('Chain abstraction orchestrator', () => {
from: from_address_with_funds,
to: usdc_contract_optimism,
value: "0x00", // Zero native tokens
input: data_encoded,
gas: "0x00",
gasPrice: "0x00",
data: data_encoded,
nonce: "0x00",
maxFeePerGas: "0x00",
maxPriorityFeePerGas: "0x00",
chainId: chain_id_optimism,
}
}
Expand All @@ -70,7 +75,12 @@ describe('Chain abstraction orchestrator', () => {
from: empty_wallet_address,
to: usdc_contract_optimism,
value: "0x00", // Zero native tokens
input: data_encoded,
gas: "0x00",
gasPrice: "0x00",
data: data_encoded,
nonce: "0x00",
maxFeePerGas: "0x00",
maxPriorityFeePerGas: "0x00",
chainId: chain_id_optimism,
}
}
Expand All @@ -96,7 +106,12 @@ describe('Chain abstraction orchestrator', () => {
from: from_address_with_funds,
to: usdc_contract_optimism,
value: "0x00", // Zero native tokens
input: data_encoded,
gas: "0x00",
gasPrice: "0x00",
data: data_encoded,
nonce: "0x00",
maxFeePerGas: "0x00",
maxPriorityFeePerGas: "0x00",
chainId: chain_id_optimism,
}
}
Expand All @@ -122,7 +137,12 @@ describe('Chain abstraction orchestrator', () => {
from: from_address_with_funds,
to: usdc_contract_optimism,
value: "0x00", // Zero native tokens
input: data_encoded,
gas: "0x00",
gasPrice: "0x00",
data: data_encoded,
nonce: "0x00",
maxFeePerGas: "0x00",
maxPriorityFeePerGas: "0x00",
chainId: chain_id_optimism,
}
}
Expand All @@ -142,8 +162,8 @@ describe('Chain abstraction orchestrator', () => {
const approvalTransaction = data.transactions[0]
expect(approvalTransaction.chainId).toBe(chain_id_base)
expect(approvalTransaction.nonce).not.toBe("0x00")
expect(() => BigInt(approvalTransaction.gasLimit)).not.toThrow();
const decodedData = erc20Interface.decodeFunctionData('approve', approvalTransaction.input);
expect(() => BigInt(approvalTransaction.gas)).not.toThrow();
const decodedData = erc20Interface.decodeFunctionData('approve', approvalTransaction.data);
if (decodedData.amount < BigInt(amount_to_topup_with_fees)) {
throw new Error(`Expected amount is lower then the minimal required`);
}
Expand All @@ -152,13 +172,14 @@ describe('Chain abstraction orchestrator', () => {
const bridgingTransaction = data.transactions[1]
expect(bridgingTransaction.chainId).toBe(chain_id_base)
expect(bridgingTransaction.nonce).not.toBe("0x00")
expect(() => BigInt(approvalTransaction.gasLimit)).not.toThrow();
expect(() => BigInt(approvalTransaction.gas)).not.toThrow();

// Check for the initialTransaction
const initialTransaction = data.initialTransaction;
expect(initialTransaction.from).toBe(from_address_with_funds.toLowerCase());
expect(initialTransaction.to).toBe(usdc_contract_optimism.toLowerCase());
expect(initialTransaction.gasLimit).not.toBe("0x00");
expect(initialTransaction.gas).not.toBe("0x00");


// Check the metadata fundingFrom
const fundingFrom = data.metadata.fundingFrom[0]
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/chain_agnostic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ pub async fn get_assets_changes_from_simulation(
transaction.chain_id.clone(),
transaction.from,
transaction.to,
transaction.input.clone(),
transaction.data.clone(),
state_overrides,
metrics,
)
Expand Down
57 changes: 26 additions & 31 deletions src/handlers/chain_agnostic/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use {
state::AppState,
storage::irn::OperationType,
utils::crypto::{
convert_alloy_address_to_h160, decode_erc20_transfer_data, get_erc20_balance, get_nonce,
convert_alloy_address_to_h160, decode_erc20_transfer_data, get_erc20_balance,
get_gas_price, get_nonce,
},
},
alloy::primitives::{Address, U256, U64},
Expand Down Expand Up @@ -71,30 +72,11 @@ async fn handler_internal(
.validate_project_access_and_quota(query_params.project_id.as_ref())
.await?;

let mut initial_transaction = Transaction {
from: request_payload.transaction.from,
to: request_payload.transaction.to,
value: request_payload.transaction.value,
gas_limit: U64::from(DEFAULT_GAS),
input: request_payload.transaction.input.clone(),
nonce: U64::ZERO,
chain_id: request_payload.transaction.chain_id.clone(),
};

let mut initial_transaction = request_payload.transaction.clone();
let from_address = initial_transaction.from;
let to_address = initial_transaction.to;
let transfer_value = initial_transaction.value;

// Calculate the initial transaction nonce
let intial_transaction_nonce = get_nonce(
&initial_transaction.chain_id.clone(),
from_address,
query_params.project_id.as_ref(),
MessageSource::ChainAgnosticCheck,
)
.await?;
initial_transaction.nonce = intial_transaction_nonce;

let no_bridging_needed_response: Json<RouteResponse> = Json(RouteResponse::Success(
RouteResponseSuccess::NotRequired(RouteResponseNotRequired {
initial_transaction: initial_transaction.clone(),
Expand All @@ -110,7 +92,7 @@ async fn handler_internal(
);
return Ok(no_bridging_needed_response.into_response());
}
let transaction_data = initial_transaction.input.clone();
let transaction_data = initial_transaction.data.clone();

// Decode the ERC20 transfer function data or use the simulation
// to get the transfer asset and amount
Expand Down Expand Up @@ -208,8 +190,7 @@ async fn handler_internal(
}
};
// Estimated gas multiplied by the slippage
initial_transaction.gas_limit =
U64::from((gas_used * (100 + ESTIMATED_GAS_SLIPPAGE as u64)) / 100);
initial_transaction.gas = U64::from((gas_used * (100 + ESTIMATED_GAS_SLIPPAGE as u64)) / 100);

// Check if the destination address is supported ERC20 asset contract
// Attempt to destructure the result into symbol and decimals using a match expression
Expand Down Expand Up @@ -348,6 +329,14 @@ async fn handler_internal(
)
.await?;

// Getting the current gas price
let gas_price = get_gas_price(
&bridge_chain_id.clone(),
query_params.project_id.as_ref(),
MessageSource::ChainAgnosticCheck,
)
.await?;

// TODO: Implement gas estimation using `eth_estimateGas` for each transaction
let mut routes = Vec::new();

Expand Down Expand Up @@ -384,12 +373,15 @@ async fn handler_internal(
from: approval_tx.from,
to: approval_tx.to,
value: U256::ZERO,
gas_limit: U64::from(DEFAULT_GAS),
input: approval_tx.data,
nonce: current_nonce,
gas_price: U256::from(gas_price),
gas: U64::from(DEFAULT_GAS),
data: approval_tx.data,
nonce: U64::from(current_nonce),
max_fee_per_gas: request_payload.transaction.max_fee_per_gas,
max_priority_fee_per_gas: request_payload.transaction.max_priority_fee_per_gas,
chain_id: format!("eip155:{}", bridge_tx.chain_id),
});
current_nonce += U64::from(1);
current_nonce += 1;
}
}

Expand All @@ -398,9 +390,12 @@ async fn handler_internal(
from: from_address,
to: bridge_tx.tx_target,
value: bridge_tx.value,
gas_limit: U64::from(DEFAULT_GAS),
input: bridge_tx.tx_data,
nonce: current_nonce,
gas_price: U256::from(gas_price),
gas: U64::from(DEFAULT_GAS),
data: bridge_tx.tx_data,
nonce: U64::from(current_nonce),
max_fee_per_gas: request_payload.transaction.max_fee_per_gas,
max_priority_fee_per_gas: request_payload.transaction.max_priority_fee_per_gas,
chain_id: format!("eip155:{}", bridge_tx.chain_id),
});

Expand Down
6 changes: 3 additions & 3 deletions src/utils/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use {
crate::{analytics::MessageSource, error::RpcError},
alloy::{
network::Ethereum,
primitives::{Address, U256 as AlloyU256, U64 as AlloyU64},
primitives::{Address, U256 as AlloyU256},
providers::{Provider as AlloyProvider, ReqwestProvider},
rpc::json_rpc::Id,
sol,
Expand Down Expand Up @@ -514,14 +514,14 @@ pub async fn get_nonce(
wallet: Address,
rpc_project_id: &str,
source: MessageSource,
) -> Result<AlloyU64, CryptoUitlsError> {
) -> Result<u64, CryptoUitlsError> {
let provider =
ReqwestProvider::<Ethereum>::new_http(get_rpc_url(chain_id, rpc_project_id, source)?);
let nonce = provider
.get_transaction_count(wallet)
.await
.map_err(|e| CryptoUitlsError::ProviderError(format!("{}", e)))?;
Ok(AlloyU64::from(nonce))
Ok(nonce)
}

/// Call entry point v07 getUserOpHash contract and get the userOperation hash
Expand Down

0 comments on commit 420b282

Please sign in to comment.