Skip to content

Commit

Permalink
Create binance_smart_chain.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Aug 7, 2024
1 parent 6ac66d8 commit 3503017
Showing 1 changed file with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Binance Smart Chain Rust implementation

use tokio::prelude::*;
use serde::{Serialize, Deserialize};
use reqwest::Client;

// Binance Smart Chain configuration
#[derive(Debug, Serialize, Deserialize)]
struct BinanceSmartChainConfig {
node_url: String,
api_key: String,
api_secret: String,
}

// Binance Smart Chain implementation
struct BinanceSmartChain {
config: BinanceSmartChainConfig,
client: Client,
}

impl BinanceSmartChain {
async fn new(config: BinanceSmartChainConfig) -> Result<Self, String> {
let client = Client::new();
Ok(Self { config, client })
}

async fn get_block_number(&self) -> Result<u64, String> {
let url = format!("{}/block_number", self.config.node_url);
let response = self.client.get(&url).header("Authorization", format!("Bearer {}", self.config.api_key)).send().await?;
let block_number: u64 = response.json().await?;
Ok(block_number)
}

async fn send_transaction(&self, tx: &Transaction) -> Result<String, String> {
let url = format!("{}/send_transaction", self.config.node_url);
let response = self.client.post(&url).header("Authorization", format!("Bearer {}", self.config.api_key)).json(tx).send().await?;
let tx_hash: String = response.json().await?;
Ok(tx_hash)
}
}

// Example usage
#[tokio::main]
async fn main() {
let config = BinanceSmartChainConfig {
node_url: "https://binance-smart-chain-node.com".to_string(),
api_key: "YOUR_API_KEY".to_string(),
api_secret: "YOUR_API_SECRET".to_string(),
};
let binance_smart_chain = BinanceSmartChain::new(config).await.unwrap();
let block_number = binance_smart_chain.get_block_number().await.unwrap();
println!("Binance Smart Chain 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 = binance_smart_chain.send_transaction(&tx).await.unwrap();
println!("Binance Smart Chain transaction hash: {}", tx_hash);
}

0 comments on commit 3503017

Please sign in to comment.