Skip to content

Commit

Permalink
Create ethereum.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Aug 7, 2024
1 parent 01bd415 commit 6ac66d8
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions projects/DAPIO/chain-integration/ethereum/ethereum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Ethereum Rust implementation

use tokio::prelude::*;
use serde::{Serialize, Deserialize};
use web3::types::{Address, TransactionRequest};
use web3::Web3;

// Ethereum configuration
#[derive(Debug, Serialize, Deserialize)]
struct EthereumConfig {
node_url: String,
api_key: String,
api_secret: String,
}

// Ethereum implementation
struct Ethereum {
config: EthereumConfig,
web3: Web3,
}

impl Ethereum {
async fn new(config: EthereumConfig) -> Result<Self, String> {
let web3 = Web3::new(&config.node_url);
Ok(Self { config, web3 })
}

async fn get_block_number(&self) -> Result<u64, String> {
let block_number = self.web3.eth_block_number().await?;
Ok(block_number)
}

async fn send_transaction(&self, tx: &Transaction) -> Result<String, String> {
let tx_request = TransactionRequest {
from: Address::from(tx.from.clone()),
to: Address::from(tx.to.clone()),
value: tx.value.into(),
gas: tx.gas.into(),
gas_price: tx.gas_price.into(),
..Default::default()
};
let tx_hash = self.web3.eth_send_transaction(tx_request).await?;
Ok(tx_hash)
}
}

// Example usage
#[tokio::main]
async fn main() {
let config = EthereumConfig {
node_url: "https://ethereum-node.com".to_string(),
api_key: "YOUR_API_KEY".to_string(),
api_secret: "YOUR_API_SECRET".to_string(),
};
let ethereum = Ethereum::new(config).await.unwrap();
let block_number = ethereum.get_block_number().await.unwrap();
println!("Ethereum block number: {}", block_number);
let tx = Transaction {
from: "0x1234567890abcdef".to_string(),
to: "0xfedcba9876543210".to_string(),
value: 100,
gas: 20000,
gas_price: 20,
};
let tx_hash = ethereum.send_transaction(&tx).await.unwrap();
println!("Ethereum transaction hash: {}", tx_hash);
}

0 comments on commit 6ac66d8

Please sign in to comment.