debot_ether_ether_utils/token/
bsc_token.rs

1// bsc_token.rs
2
3use super::token::{AnchorToken, BlockChain, Token};
4use ethers::{
5    signers::LocalWallet,
6    types::{Address, U256},
7};
8use ethers_middleware::{
9    providers::{Http, Provider},
10    NonceManagerMiddleware, SignerMiddleware,
11};
12use std::{error::Error, sync::Arc};
13
14#[derive(Clone)]
15pub struct BscToken {
16    anchor_token: AnchorToken,
17}
18
19#[async_trait::async_trait]
20impl Token for BscToken {
21    fn new(
22        block_chain: BlockChain,
23        provider: Arc<NonceManagerMiddleware<SignerMiddleware<Provider<Http>, LocalWallet>>>,
24        address: Address,
25        symbol_name: String,
26        decimals: Option<u8>,
27    ) -> Self {
28        Self {
29            anchor_token: AnchorToken::new(block_chain, provider, address, symbol_name, decimals),
30        }
31    }
32
33    fn clone_box(&self) -> Box<dyn Token> {
34        Box::new(self.clone())
35    }
36
37    fn block_chain(&self) -> BlockChain {
38        BlockChain::BscChain {
39            chain_id: self.anchor_token.block_chain_id(),
40        }
41    }
42
43    // Delegate the implementation of common methods to the AnchorToken
44    fn block_chain_id(&self) -> u64 {
45        self.anchor_token.block_chain_id()
46    }
47
48    fn address(&self) -> Address {
49        self.anchor_token.address()
50    }
51
52    fn symbol_name(&self) -> &str {
53        self.anchor_token.symbol_name()
54    }
55
56    fn decimals(&self) -> Option<u8> {
57        self.anchor_token.decimals()
58    }
59
60    async fn initialize(&mut self) -> Result<(), Box<dyn Error + Send + Sync>> {
61        self.anchor_token.initialize().await
62    }
63
64    async fn approve(
65        &self,
66        spender: Address,
67        amount: U256,
68    ) -> Result<(), Box<dyn Error + Send + Sync>> {
69        self.anchor_token.approve(spender, amount).await
70    }
71
72    async fn allowance(
73        &self,
74        owner: Address,
75        spender: Address,
76    ) -> Result<U256, Box<dyn Error + Send + Sync>> {
77        self.anchor_token.allowance(owner, spender).await
78    }
79
80    async fn balance_of(&self, owner: Address) -> Result<U256, Box<dyn Error + Send + Sync>> {
81        self.anchor_token.balance_of(owner).await
82    }
83
84    async fn transfer(
85        &self,
86        recipient: Address,
87        amount: U256,
88    ) -> Result<(), Box<dyn Error + Send + Sync>> {
89        self.anchor_token.transfer(recipient, amount).await
90    }
91}