星云链之NRC20代币合约解读

1、合约地址:Contract n1jPoCuquckUonUEzpL3zt8VggGDk4nZT2k

2、合约代码:

// ==== wittcism.com Token Smart Contract for Nebulas ====
// Name: TEST COIN
// Symbol: TEST
// Decimal: 8
// TotalSupply: 2,000,000,000 (2 billion)
// Deploy Parameter: ["TEST COIN","TEST", 8, 2000000000]

'use strict';

var Allowed = function(obj) {
    this.allowed = {};
    this.parse(obj);
}

Allowed.prototype = {
    toString: function() {
        //将 JavaScript 对象转换为字符串
        return JSON.stringify(this.allowed);
    },
   
    //将obj的value转换为大数
    parse: function(obj) {
        if (typeof obj != "undefined") {
            var data = JSON.parse(obj);
            for (var key in data) {
                this.allowed[key] = new BigNumber(data[key]);
            }
        }
    },
    //通过key,获得value
    get: function(key) {
        return this.allowed[key];
    },
    //修改、添加allow
    set: function(key, value) {
        this.allowed[key] = new BigNumber(value);
    }
}

//代币
var StandardToken = function() {
    //LocalContractStorage,可以存储数字,字符串,JavaScript对象,存储数据只能在智能合约内使用,其他合约不能读取存储的内容。
    //为StandardToken,定义多个集合
    LocalContractStorage.defineProperties(this, {
        _name: null,
        _symbol: null,
        _decimals: null,
        _totalSupply: {
            parse: function(value) {
                return new BigNumber(value);
            },
            stringify: function(o) {
                return o.toString(10);
            }
        }
    });

    LocalContractStorage.defineMapProperties(this, {
        "balances": {
            parse: function(value) {
                return new BigNumber(value);
            },
            stringify: function(o) {
                return o.toString(10);
            }
        },
        "allowed": {
            parse: function(value) {
                return new Allowed(value);
            },
            stringify: function(o) {
                return o.toString();
            }
        }
    });
};

//初始化,获取传入的参数:["TEST COIN","TEST", 8, 2000000000]
StandardToken.prototype = {
    init: function(name, symbol, decimals, totalSupply) {
        this._name = name;
        this._symbol = symbol;
        this._decimals = decimals | 0;
        this._totalSupply = new BigNumber(totalSupply).mul(new BigNumber(10).pow(decimals));

        var from = Blockchain.transaction.from;
        //将资产绑定到源地址上
        this.balances.set(from, this._totalSupply);
        this.transferEvent(true, from, from, this._totalSupply);
    },

    // Returns the name of the token
    name: function() {
        return this._name;
    },

    // Returns the symbol of the token
    symbol: function() {
        return this._symbol;
    },

    // Returns the number of decimals the token uses
    decimals: function() {
        return this._decimals;
    },

    totalSupply: function() {
        return this._totalSupply.toString(10);
    },

    balanceOf: function(owner) {
        var balance = this.balances.get(owner);

        if (balance instanceof BigNumber) {
            return balance.toString(10);
        } else {
            return "0";
        }
    },

    //转账
    transfer: function(to, value) {
        value = new BigNumber(value);
        //value小于0
        if (value.lt(0)) {
            throw new Error("invalid value.");
        }
        //from执行合约的当前交易的源地址
        var from = Blockchain.transaction.from;
        //获取源地址的余额
        var balance = this.balances.get(from) || new BigNumber(0);
        //余额小于0
        if (balance.lt(value)) {
            throw new Error("transfer failed.");
        }
        //sub
        this.balances.set(from, balance.sub(value));
        var toBalance = this.balances.get(to) || new BigNumber(0);
        this.balances.set(to, toBalance.add(value));

        this.transferEvent(true, from, to, value);
    },

    transferFrom: function(from, to, value) {
        var spender = Blockchain.transaction.from;
        var balance = this.balances.get(from) || new BigNumber(0);

        var allowed = this.allowed.get(from) || new Allowed();
        var allowedValue = allowed.get(spender) || new BigNumber(0);
        value = new BigNumber(value);

        if (value.gte(0) && balance.gte(value) && allowedValue.gte(value)) {

            this.balances.set(from, balance.sub(value));

            // update allowed value
            allowed.set(spender, allowedValue.sub(value));
            this.allowed.set(from, allowed);

            var toBalance = this.balances.get(to) || new BigNumber(0);
            this.balances.set(to, toBalance.add(value));

            this.transferEvent(true, from, to, value);
        } else {
            throw new Error("transfer failed.");
        }
    },

    transferEvent: function(status, from, to, value) {
        Event.Trigger(this.name(), {
            Status: status,
            Transfer: {
                from: from,
                to: to,
                value: value
            }
        });
    },

    approve: function(spender, currentValue, value) {
        var from = Blockchain.transaction.from;

        var oldValue = this.allowance(from, spender);
        if (oldValue != currentValue.toString()) {
            throw new Error("current approve value mistake.");
        }

        var balance = new BigNumber(this.balanceOf(from));
        var value = new BigNumber(value);

        if (value.lt(0) || balance.lt(value)) {
            throw new Error("invalid value.");
        }

        var owned = this.allowed.get(from) || new Allowed();
        owned.set(spender, value);

        this.allowed.set(from, owned);

        this.approveEvent(true, from, spender, value);
    },

    approveEvent: function(status, from, spender, value) {
        Event.Trigger(this.name(), {
            Status: status,
            Approve: {
                owner: from,
                spender: spender,
                value: value
            }
        });
    },

    allowance: function(owner, spender) {
        var owned = this.allowed.get(owner);

        if (owned instanceof Allowed) {
            var spender = owned.get(spender);
            if (typeof spender != "undefined") {
                return spender.toString(10);
            }
        }
        return "0";
    }
};

module.exports = StandardToken;

 

 

 

 

 

你可能感兴趣的:(区块链)