用move编写ERC20合约

maco
发布于 阅读 748
module Sender::Token{

    use aptos_framework::signer;

    struct ERC20 has drop,store,key{
        decimal: u128,
        name: u128,
        symbol: u128,
        totalSupply: u128,
    }
    struct BalanceOf has drop,store,key{
        balance:u128,
    }
    struct Approve has drop,store,key{
        approve_to:address,
        approve_value:u128,
    }
    // Set Token name,decimal,symbol,totalSupply
    public entry fun initialize(account:&signer,_decimal:u128,_name:u128,_symbol:u128,_totalSupply:u128) acquires ERC20,BalanceOf{
        let account_addr = signer::address_of(account);
        let erc20 = borrow_global_mut<Sender::Token::ERC20>(account_addr);
        erc20.decimal = _decimal;
        erc20.name = _name;
        erc20.symbol = _symbol;
        erc20.totalSupply = _totalSupply;
        let balance_account = borrow_global_mut<Sender::Token::BalanceOf>(account_addr).balance;
        balance_account = _totalSupply;
    }
    public entry fun transfer(from:&signer,to:address,value:u128):bool acquires BalanceOf{
        let account_from = signer::address_of(from);
        let balance_from = borrow_global_mut<Sender::Token::BalanceOf>(account_from).balance;
        if(balance_from > value){
            balance_from = balance_from - value;
            let balance_to = borrow_global_mut<Sender::Token::BalanceOf>(to).balance;
            balance_to = balance_to + value;
            return true
        };
        return false
    } 
    public entry fun balance(account:&signer):u128 acquires BalanceOf {
        let account_addr = signer::address_of(account);
        let balance = borrow_global_mut<Sender::Token::BalanceOf>(account_addr).balance;
        return balance
    }
    public entry fun decimal(account:&signer):u128 acquires ERC20{
        let account_addr = signer::address_of(account);
        let decimal = borrow_global_mut<Sender::Token::ERC20>(account_addr).decimal;
        return decimal
    }
    public entry fun name(account:&signer):u128 acquires ERC20{
        let account_addr = signer::address_of(account);
        let name = borrow_global_mut<Sender::Token::ERC20>(account_addr).name;
        return name
    }
    public entry fun symbol(account:&signer):u128 acquires ERC20{
        let account_addr = signer::address_of(account);
        let symbol = borrow_global_mut<Sender::Token::ERC20>(account_addr).symbol;
        return symbol
    }
    public entry fun totalSupply(account:&signer):u128 acquires ERC20{
        let account_addr = signer::address_of(account);
        let totalSupply = borrow_global_mut<Sender::Token::ERC20>(account_addr).totalSupply;
        return totalSupply
    }
    public entry fun transferFrom(from:address,to:address,value:u128):bool acquires BalanceOf,Approve{
       // check approve value
       let approve = borrow_global_mut<Sender::Token::Approve>(from);
       if(approve.approve_to != to){
           abort 0;
       };
       if(approve.approve_value < value){
           abort 0;
       };
       // transfer
       let balance_from = borrow_global_mut<Sender::Token::BalanceOf>(from).balance;
       if(balance_from > value){
           balance_from = balance_from - value;
           let balance_to = borrow_global_mut<Sender::Token::BalanceOf>(to).balance;
           balance_to = balance_to + value;
           return true
       };
       return false
   }
}


4条评论
greyireland
greyireland
public entry fun symbol(account:&signer):u128 acquires ERC20{ let account_addr = signer::address_of(account); let symbol = borrow_global_mut<Sender::Token::ERC20>(account_addr).symbol; return symbol } 普通用户拿不到这个资源吧
点赞 1
评论
maco
是的,这里是只有合约部署者才能拿到,只需要修改传入的account为一个普通用户地址,那么所有人就都可以拿到了。
点赞 1
评论
greyireland
greyireland
good
点赞
评论
去中心信仰者
优雅的代码
点赞
评论
ω
ω
哇!!!
点赞
评论