github.com/igggame/nebulas-go@v2.1.0+incompatible/nebtestkit/cases/contract/transferFromContract.test.js (about)

     1  "use strict";
     2  
     3  var Wallet = require("nebulas");
     4  var HttpRequest = require("../../node-request.js");
     5  var TestNetConfig = require("../testnet_config.js");
     6  var Neb = Wallet.Neb;
     7  var Transaction = Wallet.Transaction;
     8  var FS = require("fs");
     9  var expect = require('chai').expect;
    10  var Unit = Wallet.Unit;
    11  
    12  // mocha cases/contract/xxx testneb2 -t 2000000
    13  var args = process.argv.splice(2);
    14  var env = args[1];
    15  if (env == null){
    16      env = "local";
    17  }
    18  var testNetConfig = new TestNetConfig(env);
    19  
    20  var neb = new Neb();
    21  var ChainID = testNetConfig.ChainId;
    22  var sourceAccount = testNetConfig.sourceAccount;
    23  var coinbase = testNetConfig.coinbase;
    24  var apiEndPoint = testNetConfig.apiEndPoint;
    25  neb.setRequest(new HttpRequest(apiEndPoint));
    26  
    27  var contractAddress;
    28  var toAddress = Wallet.Account.NewAccount();
    29  var nonce;
    30  var contractNonce = 0;
    31  
    32  /*
    33   * set this value according to the status of your testnet.
    34   * the smaller the value, the faster the test, with the risk of causing error
    35   */
    36  
    37  var maxCheckTime = 30;
    38  var checkTimes = 0;
    39  var beginCheckTime;
    40  
    41  console.log("env:", env);
    42  
    43  function checkTransaction(hash, callback) {
    44      if (checkTimes === 0) {
    45          beginCheckTime = new Date().getTime();
    46      }
    47      checkTimes += 1;
    48      if (checkTimes > maxCheckTime) {
    49          console.log("check tx receipt timeout:" + hash);
    50          checkTimes = 0;
    51          callback();
    52          return;
    53      }
    54  
    55      neb.api.getTransactionReceipt(hash).then(function (resp) {
    56  
    57          console.log("tx receipt status:" + resp.status);
    58          if (resp.status === 2) {
    59              setTimeout(function () {
    60                  checkTransaction(hash, callback);
    61              }, 2000);
    62          } else {
    63              checkTimes = 0;
    64              var endCheckTime = new Date().getTime();
    65              console.log("check tx time: : " + (endCheckTime - beginCheckTime) / 1000);
    66              callback(resp);
    67          }
    68      }).catch(function (err) {
    69          console.log("fail to get tx receipt hash: " + hash);
    70          console.log("it may becuase the tx is being packing, we are going on to check it!");
    71         // console.log(err);
    72          setTimeout(function () {
    73              checkTransaction(hash, callback);
    74          }, 2000);
    75      });
    76  }
    77  
    78  describe('test transfer from contract', function () {
    79      before('0. deploy contract', function (done) {
    80          try {
    81              neb.api.getAccountState(sourceAccount.getAddressString()).then(function(resp) {
    82                  console.log("----step0. get source account state: " + JSON.stringify(resp));
    83                  var contractSource = FS.readFileSync("../nf/nvm/test/transfer_value_from_contract.js", "UTF-8");
    84                  var contract = {
    85                      'source': contractSource,
    86                      "sourceType": "js",
    87                      "arges": ''
    88                  };
    89                  nonce = parseInt(resp.nonce);
    90                  nonce = nonce + 1;
    91                  var tx = new Transaction(ChainID, sourceAccount, sourceAccount, 0, nonce, 1000000, 20000000, contract);
    92                  tx.signTransaction();
    93                  return neb.api.sendRawTransaction(tx.toProtoString());
    94              }).then(function(resp) {
    95                  console.log("----step1. deploy contract: " + JSON.stringify(resp));
    96                  contractAddress = resp.contract_address;
    97                  checkTransaction(resp.txhash, function(resp) {
    98                      expect(resp).to.not.be.a('undefined');
    99                      console.log("----step2. have been on chain");
   100                      done();
   101                  });
   102              }).catch(function(err) {
   103                  console.log("unexpected err: " + err);
   104                  done(err);
   105              });
   106          } catch (err) {
   107              console.log("unexpected err: " + err);
   108              done(err);
   109          }
   110      });
   111  
   112      it ('1. send 10 nas to contract address', function (done) {
   113          nonce = nonce + 1;
   114          console.log(contractAddress);
   115          var tx = new Transaction(ChainID, sourceAccount, contractAddress, Unit.nasToBasic(10), nonce, 1000000, 2000000);
   116          // tx.to = contractAddress;
   117          tx.signTransaction();
   118          console.log(tx.toString());
   119          // console.log("silent_debug");
   120          neb.api.sendRawTransaction(tx.toProtoString()).then(function(resp) {
   121              console.log("----step3. send nas to contract address: ", resp);
   122              checkTransaction(resp.txhash, function(resp) {
   123                  expect(resp).to.not.be.a('undefined');
   124                  console.log("----step4. have been on chain");
   125                  done();
   126              });
   127          }).catch(function(err) {
   128              console.log("unexpected err: " + err);
   129              done(err);
   130          });
   131      });
   132  
   133      it ('2. send 5 nas from contract address to toAddress', function (done) {
   134          nonce = nonce + 1;
   135          var contract = {
   136              "function": "transferSpecialValue",
   137              "args": "[\"" + toAddress.getAddressString() + "\", \"5000000000000000000\"]"
   138          };
   139          var tx = new Transaction(ChainID, sourceAccount, contractAddress, 0, nonce, 1000000, 2000000, contract);
   140          tx.signTransaction();
   141          console.log(tx.toString);
   142          neb.api.sendRawTransaction(tx.toProtoString()).then(function (resp) {
   143              console.log("----step5. call transferSpecialValue function", JSON.stringify(resp));
   144              var hash = resp.txhash;
   145              checkTransaction(resp.txhash, function(resp) {
   146                  try {
   147                      expect(resp).to.not.be.a('undefined');
   148                      console.log("----step6. have be on chain");
   149                      neb.api.getAccountState(toAddress.getAddressString()).then(function(resp) {
   150                          expect(resp.balance).equal("5000000000000000000");
   151                          return neb.api.getAccountState(contractAddress);
   152                      }).then(function(resp){
   153                          expect(resp.balance).equal("5000000000000000000");
   154                          return neb.api.getEventsByHash(hash);
   155                      }).then(function(resp){
   156                          console.log(JSON.stringify(resp));
   157                          expect(resp.events[0].topic).equal("chain.transferFromContract");
   158                          done();
   159                      }).catch(function(err){
   160                          console.log("unexpected err :", err);
   161                          done(err);
   162                      });
   163                  } catch(err) {
   164                      console.log("unexpected err :", err);
   165                      done(err);
   166                  }
   167              });
   168          }).catch(function(err){
   169              console.log("unexpected err :", err);
   170              done(err);
   171          });
   172      });
   173  
   174      it ('3. send 5 nas to contract and try to send 11 nas from contract toAddress',
   175       function (done) {
   176          nonce = nonce + 1;
   177          var contract = {
   178              "function": "transferSpecialValue",
   179              "args": "[\"" + toAddress.getAddressString() + "\", \"11000000000000000000\"]"
   180          };
   181          var tx = new Transaction(ChainID, sourceAccount, contractAddress, Unit.nasToBasic(5), nonce, 1000000, 2000000, contract);
   182          tx.signTransaction();
   183          neb.api.sendRawTransaction(tx.toProtoString()).then(function (resp) {
   184              console.log("----step7. call transferSpecialValue function", JSON.stringify(resp));
   185              var hash = resp.txhash;
   186              checkTransaction(resp.txhash, function(resp) {
   187                  try {
   188                      expect(resp).to.not.be.a('undefined');
   189                      expect(resp.status).to.be.equal(0);
   190                      console.log("----step8. have be on chain");
   191                      neb.api.getAccountState(toAddress.getAddressString()).then(function(resp) {
   192                          expect(resp.balance).equal("5000000000000000000");
   193                          return neb.api.getAccountState(contractAddress);
   194                      }).then(function(resp){
   195                          expect(resp.balance).equal("5000000000000000000");
   196                          return neb.api.getEventsByHash(hash);
   197                      }).then(function(resp){
   198                          console.log("======", JSON.stringify(resp))
   199                          expect(resp.events[0].topic).equal("chain.transferFromContract");
   200                          expect(JSON.parse(resp.events[0].data).error).equal("failed to sub balace from contract address");
   201                          done();
   202                      }).catch(function(err){
   203                          console.log("unexpected err :", err);
   204                          done(err);
   205                      });
   206                  } catch (err) {
   207                      console.log("unexpected error: ", err);
   208                      done(err);
   209                  }
   210  
   211              });
   212          }).catch(function(err){
   213              console.log("unexpected err :", err);
   214              done(err);
   215          });
   216      });
   217  
   218      it ('4. send 5 nas to contract and try to send 10 nas from contract toAddress',
   219      function (done) {
   220         nonce = nonce + 1;
   221         var contract = {
   222             "function": "transferSpecialValue",
   223             "args": "[\"" + toAddress.getAddressString() + "\", \"10000000000000000000\"]"
   224         };
   225         var tx = new Transaction(ChainID, sourceAccount, contractAddress, Unit.nasToBasic(5), nonce, 1000000, 2000000, contract);
   226         tx.signTransaction();
   227         neb.api.sendRawTransaction(tx.toProtoString()).then(function (resp) {
   228             console.log("----step9. call transferSpecialValue function", JSON.stringify(resp));
   229             checkTransaction(resp.txhash, function(resp) {
   230                 try {
   231                     expect(resp).to.not.be.a('undefined');
   232                     expect(resp.status).to.be.equal(1);
   233                     console.log("----step10. have be on chain");
   234                     neb.api.getAccountState(toAddress.getAddressString()).then(function(resp) {
   235                         expect(resp.balance).equal("15000000000000000000");
   236                         return neb.api.getAccountState(contractAddress);
   237                     }).then(function(resp){
   238                         expect(resp.balance).equal("0");
   239                         done();
   240                     }).catch(function(err){
   241                         console.log("unexpected err :", err);
   242                         done(err);
   243                     });
   244                 } catch (err) {
   245                     console.log("unexpected error: ", err);
   246                     done(err);
   247                 }
   248  
   249             });
   250         }).catch(function(err){
   251             console.log("unexpected err :", err);
   252             done(err);
   253         });
   254     });
   255  });