-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
projects/DAPIO/chain-integration/binance-smart-chain/binance_smart_chain.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |