Skip to content

Commit

Permalink
[fortuna] Add legacy transaction support (#1126)
Browse files Browse the repository at this point in the history
* gr

* support legacy transactions

* doc
  • Loading branch information
jayantk authored Oct 31, 2023
1 parent fe7984d commit f0a077a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
1 change: 1 addition & 0 deletions fortuna/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ chains:
eos-evm-testnet:
geth_rpc_addr: https://api.testnet.evm.eosnetwork.com/
contract_addr: 0xD42c7a708E74AD19401D907a14146F006c851Ee3
legacy_tx: true
4 changes: 4 additions & 0 deletions fortuna/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,8 @@ pub struct EthereumConfig {

/// Address of a Pyth Randomness contract to interact with.
pub contract_addr: Address,

/// Use the legacy transaction format (for networks without EIP 1559)
#[serde(default)]
pub legacy_tx: bool,
}
42 changes: 39 additions & 3 deletions fortuna/src/ethereum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ use {
EthLogDecode,
},
core::types::Address,
middleware::SignerMiddleware,
middleware::{
transformer::{
Transformer,
TransformerError,
TransformerMiddleware,
},
SignerMiddleware,
},
prelude::TransactionRequest,
providers::{
Http,
Middleware,
Expand All @@ -21,6 +29,7 @@ use {
LocalWallet,
Signer,
},
types::transaction::eip2718::TypedTransaction,
},
sha3::{
Digest,
Expand All @@ -33,9 +42,29 @@ use {
// contract in the same repo.
abigen!(PythRandom, "src/abi.json");

pub type SignablePythContract = PythRandom<SignerMiddleware<Provider<Http>, LocalWallet>>;
pub type SignablePythContract = PythRandom<
TransformerMiddleware<SignerMiddleware<Provider<Http>, LocalWallet>, LegacyTxTransformer>,
>;
pub type PythContract = PythRandom<Provider<Http>>;

/// Transformer that converts a transaction into a legacy transaction if use_legacy_tx is true.
#[derive(Clone, Debug)]
pub struct LegacyTxTransformer {
use_legacy_tx: bool,
}

impl Transformer for LegacyTxTransformer {
fn transform(&self, tx: &mut TypedTransaction) -> Result<(), TransformerError> {
if self.use_legacy_tx {
let legacy_request: TransactionRequest = (*tx).clone().into();
*tx = legacy_request.into();
Ok(())
} else {
Ok(())
}
}
}

impl SignablePythContract {
pub async fn from_config(
chain_config: &EthereumConfig,
Expand All @@ -44,14 +73,21 @@ impl SignablePythContract {
let provider = Provider::<Http>::try_from(&chain_config.geth_rpc_addr)?;
let chain_id = provider.get_chainid().await?;

let transformer = LegacyTxTransformer {
use_legacy_tx: chain_config.legacy_tx,
};

let wallet__ = private_key
.clone()
.parse::<LocalWallet>()?
.with_chain_id(chain_id.as_u64());

Ok(PythRandom::new(
chain_config.contract_addr,
Arc::new(SignerMiddleware::new(provider, wallet__)),
Arc::new(TransformerMiddleware::new(
SignerMiddleware::new(provider, wallet__),
transformer,
)),
))
}

Expand Down

0 comments on commit f0a077a

Please sign in to comment.