Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fortuna] Add legacy transaction support #1126

Merged
merged 4 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading