Skip to content

Commit

Permalink
fix: updates to fit the latest schema and initial tx nonce
Browse files Browse the repository at this point in the history
  • Loading branch information
geekbrother committed Dec 16, 2024
1 parent 119c0b0 commit 112f5b9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 34 deletions.
27 changes: 3 additions & 24 deletions integration/chain_orchestrator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,7 @@ describe('Chain abstraction orchestrator', () => {
from: from_address_with_funds,
to: usdc_contract_optimism,
value: "0x00", // Zero native tokens
gas: "0x00",
gasPrice: "0x00",
data: data_encoded,
nonce: "0x00",
maxFeePerGas: "0x00",
maxPriorityFeePerGas: "0x00",
chainId: chain_id_optimism,
}
}
Expand All @@ -75,12 +70,7 @@ describe('Chain abstraction orchestrator', () => {
from: empty_wallet_address,
to: usdc_contract_optimism,
value: "0x00", // Zero native tokens
gas: "0x00",
gasPrice: "0x00",
data: data_encoded,
nonce: "0x00",
maxFeePerGas: "0x00",
maxPriorityFeePerGas: "0x00",
chainId: chain_id_optimism,
}
}
Expand All @@ -106,12 +96,7 @@ describe('Chain abstraction orchestrator', () => {
from: from_address_with_funds,
to: usdc_contract_optimism,
value: "0x00", // Zero native tokens
gas: "0x00",
gasPrice: "0x00",
data: data_encoded,
nonce: "0x00",
maxFeePerGas: "0x00",
maxPriorityFeePerGas: "0x00",
chainId: chain_id_optimism,
}
}
Expand All @@ -137,12 +122,7 @@ describe('Chain abstraction orchestrator', () => {
from: from_address_with_funds,
to: usdc_contract_optimism,
value: "0x00", // Zero native tokens
gas: "0x00",
gasPrice: "0x00",
data: data_encoded,
nonce: "0x00",
maxFeePerGas: "0x00",
maxPriorityFeePerGas: "0x00",
chainId: chain_id_optimism,
}
}
Expand All @@ -162,7 +142,7 @@ 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.gas)).not.toThrow();
expect(() => BigInt(approvalTransaction.gasLimit)).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 @@ -172,14 +152,13 @@ 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.gas)).not.toThrow();
expect(() => BigInt(approvalTransaction.gasLimit)).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.gas).not.toBe("0x00");

expect(initialTransaction.gasLimit).not.toBe("0x00");

// Check the metadata fundingFrom
const fundingFrom = data.metadata.fundingFrom[0]
Expand Down
33 changes: 26 additions & 7 deletions src/handlers/chain_agnostic/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ use {
state::AppState,
storage::irn::OperationType,
utils::crypto::{
convert_alloy_address_to_h160, decode_erc20_transfer_data, get_erc20_balance,
get_gas_price, get_nonce,
convert_alloy_address_to_h160, decode_erc20_transfer_data, get_erc20_balance, get_nonce,
},
},
alloy::primitives::{Address, U256, U64},
Expand Down Expand Up @@ -72,11 +71,30 @@ async fn handler_internal(
.validate_project_access_and_quota(query_params.project_id.as_ref())
.await?;

let mut initial_transaction = request_payload.transaction.clone();
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),
data: request_payload.transaction.data.clone(),
nonce: U64::ZERO,
chain_id: request_payload.transaction.chain_id.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 Down Expand Up @@ -188,7 +206,8 @@ 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_limit =
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 @@ -365,10 +384,10 @@ async fn handler_internal(
value: U256::ZERO,
gas_limit: U64::from(DEFAULT_GAS),
data: approval_tx.data,
nonce: U64::from(current_nonce),
nonce: current_nonce,
chain_id: format!("eip155:{}", bridge_tx.chain_id),
});
current_nonce += 1;
current_nonce += U64::from(1);
}
}

Expand All @@ -379,7 +398,7 @@ async fn handler_internal(
value: bridge_tx.value,
gas_limit: U64::from(DEFAULT_GAS),
data: bridge_tx.tx_data,
nonce: U64::from(current_nonce),
nonce: current_nonce,
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},
primitives::{Address, U256 as AlloyU256, U64 as AlloyU64},
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<u64, CryptoUitlsError> {
) -> Result<AlloyU64, 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(nonce)
Ok(AlloyU64::from(nonce))
}

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

0 comments on commit 112f5b9

Please sign in to comment.