星雲鏈之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;

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章