how to write a token smart contract to run on bsc network in rust

To write a token smart contract to run on the Binance Smart Chain network in Rust, you can use the ink! framework. ink! is an Ethereum-compatible smart contract development framework that can also be used to write smart contracts for the BSC network.

Here's an example token smart contract written in Rust using the ink! framework:

main.rs
use ink_lang::contract;

#[contract]
mod token {
    use ink_storage::{
        collections::HashMap as StorageHashMap,
        traits::{PackedLayout, SpreadLayout},
        vec::Vec as StorageVec,
    };

    #[ink(storage)]
    pub struct ERC20 {
        total_supply: storage::Value<u128>,
        balances: StorageHashMap<AccountId, u128>,
        allowances: StorageHashMap<(AccountId, AccountId), u128>,
    }

    impl ERC20 {
        #[ink(constructor)]
        pub fn new(initial_supply: u128) -> Self {
            let mut balances = StorageHashMap::new();
            balances.insert(Self::env().caller(), initial_supply);

            Self {
                total_supply: storage::Value::new(initial_supply),
                balances,
                allowances: StorageHashMap::new(),
            }
        }

        #[ink(message)]
        pub fn total_supply(&self) -> u128 {
            *self.total_supply
        }

        #[ink(message)]
        pub fn balance_of(&self, owner: AccountId) -> u128 {
            *self.balances.get(&owner).unwrap_or(&0)
        }

        #[ink(message)]
        pub fn allowance(&self, owner: AccountId, spender: AccountId) -> u128 {
            *self.allowances.get(&(owner, spender)).unwrap_or(&0)
        }

        #[ink(message)]
        pub fn transfer(&mut self, to: AccountId, value: u128) -> bool {
            let from = self.env().caller();
            let from_balance = *self.balances.get(&from).unwrap_or(&0);

            if from_balance < value {
                return false;
            }

            self.balances.insert(from, from_balance - value);

            let to_balance = *self.balances.get(&to).unwrap_or(&0);
            self.balances.insert(to, to_balance + value);

            self.env().emit_event(Transfer {
                from: Some(from),
                to: Some(to),
                value,
            });

            true
        }

        #[ink(message)]
        pub fn approve(&mut self, spender: AccountId, value: u128) -> bool {
            let owner = self.env().caller();
            self.allowances.insert((owner, spender), value);

            self.env().emit_event(Approval {
                owner: Some(owner),
                spender: Some(spender),
                value,
            });

            true
        }

        #[ink(message)]
        pub fn transfer_from(&mut self, from: AccountId, to: AccountId, value: u128) -> bool {
            let caller = self.env().caller();
            let allowance = *self.allowances.get(&(from, caller)).unwrap_or(&0);

            if allowance < value {
                return false;
            }

            let from_balance = *self.balances.get(&from).unwrap_or(&0);
            if from_balance < value {
                return false;
            }

            self.allowances.insert((from, caller), allowance - value);
            self.balances.insert(from, from_balance - value);

            let to_balance = *self.balances.get(&to).unwrap_or(&0);
            self.balances.insert(to, to_balance + value);

            self.env().emit_event(Transfer {
                from: Some(from),
                to: Some(to),
                value,
            });

            true
        }
    }

    #[ink(event)]
    pub struct Transfer {
        #[ink(topic)]
        from: Option<AccountId>,
        #[ink(topic)]
        to: Option<AccountId>,
        value: u128,
    }

    #[ink(event)]
    pub struct Approval {
        #[ink(topic)]
        owner: Option<AccountId>,
        #[ink(topic)]
        spender: Option<AccountId>,
        value: u128,
    }
}
3618 chars
131 lines

In this example, we define an ERC20 struct that represents an ERC-20 token. The ERC20 struct contains a total_supply value, a balances map that stores the balance of each account, and an allowances map that stores the approved amount an account can spend.

The ERC20 struct implements several methods that allow users to interact with the ERC-20 token. For example, the transfer method allows a user to transfer tokens to another account, and the approve method allows a user to grant another account permission to spend their tokens.

Finally, the Transfer and Approval events enable external parties to detect when a token transfer has occurred or when an approving authority has granted permission for a specific transaction.

This is just a basic example of a token smart contract, but more complex contracts can be written using the ink! framework to customize the specifics of the token.

gistlibby LogSnag