github.com/igggame/nebulas-go@v2.1.0+incompatible/nf/nvm/v8/lib/1.1.0/blockchain.js (about)

     1  // Copyright (C) 2017 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // the go-nebulas library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU General Public License
    16  // along with the go-nebulas library.  If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  
    19  'use strict';
    20  
    21  var callFunc = function () {
    22      var func = arguments[0];
    23      if (typeof(func) != "string") {
    24          throw("Inner Call: function name should be a string");
    25      }
    26  
    27      var args = new Array();
    28      for (var i = 1; i < arguments.length; i++) {
    29          args.push(arguments[i]);
    30      }
    31      var result =  _native_blockchain.runContractSource(this.address, func, this.v.toString(10), JSON.stringify(args));
    32      if (result) {
    33          return JSON.parse(result);
    34      } else {
    35          console.log("sys log: Unexpected error");
    36          return null;
    37      }
    38  }
    39  
    40  var dumpContract = function (address, v, methods) {
    41      this.address = address;
    42      this.v = v;
    43  }
    44  
    45  dumpContract.prototype = {
    46      call: callFunc,
    47  }
    48  
    49  var Contract = function(address) {
    50      //check args
    51      if (typeof(address) != "string") {
    52          throw("Inner Call: contract address should be a string");
    53      }
    54  
    55      this.v = new BigNumber(0);
    56      this.address = address;
    57  
    58      var src = _native_blockchain.getContractSource(address); //TODO: change the interface getContactSource
    59      if (src == null) {
    60          throw("Inner Call: no contract at this address");
    61      }   
    62  }
    63  
    64  Contract.prototype = {
    65      value: function (value) {
    66          var v = value || 0;
    67          v = new BigNumber(v);
    68          
    69          if (!v.isInteger() || v.lessThan(0)) {
    70              throw("Inner Call: invalid value");
    71          }
    72  
    73          return new dumpContract(this.address, v);
    74      },
    75  
    76      call: callFunc,
    77  }
    78  
    79  var Blockchain = function () {
    80      Object.defineProperty(this, "nativeBlockchain", {
    81          configurable: false,
    82          enumerable: false,
    83          get: function(){
    84              return _native_blockchain;
    85          }
    86      });
    87      this.Contract = Contract;
    88  };
    89  
    90  Blockchain.prototype = {
    91      AccountAddress: 0x57,
    92      ContractAddress: 0x58,
    93  
    94      blockParse: function (str) {
    95          var block = JSON.parse(str);
    96          block.seed = "";
    97          if (block != null) {
    98              var fb = Object.freeze(block);
    99              Object.defineProperty(this, "block", {
   100                  configurable: false,
   101                  enumerable: false,
   102                  get: function(){
   103                      return fb;
   104                  }
   105              });
   106          }
   107      },
   108      transactionParse: function (str) {
   109          var tx = JSON.parse(str);
   110          if (tx != null) {
   111              var value = tx.value === undefined || tx.value.length === 0 ? "0" : tx.value;
   112              tx.value = new BigNumber(value);
   113              var gasPrice = tx.gasPrice === undefined || tx.gasPrice.length === 0 ? "0" : tx.gasPrice;
   114              tx.gasPrice = new BigNumber(gasPrice);
   115              var gasLimit = tx.gasLimit === undefined || tx.gasLimit.length === 0 ? "0" : tx.gasLimit;
   116              tx.gasLimit = new BigNumber(gasLimit);
   117              
   118              var ft = Object.freeze(tx);
   119              Object.defineProperty(this, "transaction", {
   120                  configurable: false,
   121                  enumerable: false,
   122                  get: function(){
   123                      return ft;
   124                  }
   125              });
   126          }
   127      },
   128      transfer: function (address, value) {
   129          if (!Uint.isUint(value)) {
   130              if (!(value instanceof BigNumber)) {
   131                  value = new BigNumber(value);
   132              }
   133              if (value.isNaN() || value.isNegative() || !value.isFinite()) {
   134                  throw new Error("invalid value");
   135              }
   136          }
   137         
   138          var ret = this.nativeBlockchain.transfer(address, value.toString(10));
   139          return ret == 0;
   140      },
   141  
   142      verifyAddress: function (address) {
   143          return this.nativeBlockchain.verifyAddress(address);
   144      },
   145  
   146      getAccountState: function(address) {
   147          if (address) {
   148              var result =  this.nativeBlockchain.getAccountState(address);
   149              if (result) {
   150                  return JSON.parse(result);
   151              } else {
   152                  throw "getAccountState: invalid address";
   153              }
   154          } else {
   155              throw "getAccountState:  inValid address";
   156          }
   157      },
   158      
   159      getPreBlockHash: function (offset) {
   160          offset = parseInt(offset);
   161          if (!offset) {
   162              throw "getPreBlockHash: invalid offset"
   163          }
   164          
   165          if (offset <= 0) {
   166              throw "getPreBlockHash: offset should large than 0"
   167          }
   168  
   169          if (offset >= this.block.height) {
   170              throw "getPreBlockHash: block not exist"
   171          }
   172          
   173          return this.nativeBlockchain.getPreBlockHash(offset);
   174      },
   175  
   176      getPreBlockSeed: function (offset) {
   177          offset = parseInt(offset);
   178          if (!offset) {
   179              throw "getPreBlockSeed: invalid offset"
   180          }
   181          
   182          if (offset <= 0) {
   183              throw "getPreBlockSeed: offset should large than 0"
   184          }
   185          
   186          if (offset >= this.block.height) {
   187              throw "getPreBlockSeed: block not exist"
   188          }
   189  
   190          return this.nativeBlockchain.getPreBlockSeed(offset);
   191      },
   192  
   193      getLatestNebulasRank: function (address) {
   194          return this.nativeBlockchain.getLatestNebulasRank(address);
   195      },
   196  
   197      getLatestNebulasRankSummary: function () {
   198          var summary = this.nativeBlockchain.getLatestNebulasRankSummary();
   199          return JSON.parse(summary);
   200      }
   201  };
   202  module.exports = new Blockchain();