github.com/igggame/nebulas-go@v2.1.0+incompatible/nebtestkit/cases/testnet/NRC20.test.js (about) 1 'use strict'; 2 3 var Wallet = require('../../../cmd/console/neb.js/lib/wallet.js'); 4 var sleep = require("system-sleep"); 5 var HttpRequest = require("../../node-request"); 6 var FS = require("fs"); 7 8 var args = process.argv.splice(2); 9 10 if (args.length !=3 ){ 11 console.log("please input args 0:env(local,testneb1,testneb2) 1:address number(concurrency) 2:sendtimes") 12 return; 13 } 14 15 var env = args[0]; // local testneb1 testneb2 16 17 const AddressNumber = parseInt(args[1]); 18 const SendTimes = parseInt(args[2]); 19 20 if (AddressNumber <=0 || SendTimes <=0 ){ 21 22 console.log("please input correct AddressNumber and SendTimes"); 23 return; 24 } 25 26 var Neb = Wallet.Neb; 27 var neb = new Neb(); 28 29 var ChainID; 30 var source, deploy; 31 32 //local 33 if (env == 'local'){ 34 neb.setRequest(new HttpRequest("http://127.0.0.1:8685"));//https://testnet.nebulas.io 35 ChainID = 100; 36 source = new Wallet.Account("a6e5eb290e1438fce79f5cb8774a72621637c2c9654c8b2525ed1d7e4e73653f"); 37 }else if(env == 'testneb1'){ 38 neb.setRequest(new HttpRequest("http://35.182.48.19:8685")); 39 ChainID = 1001; 40 source = new Wallet.Account("43181d58178263837a9a6b08f06379a348a5b362bfab3631ac78d2ac771c5df3"); 41 }else if(env == "testneb2"){ 42 neb.setRequest(new HttpRequest("http://34.205.26.12:8685")); 43 ChainID = 1002; 44 source = new Wallet.Account("43181d58178263837a9a6b08f06379a348a5b362bfab3631ac78d2ac771c5df3"); 45 }else{ 46 console.log("please input correct env local testneb1 testneb2") 47 return; 48 } 49 50 var lastnonce = 0; 51 52 // new account to get address 53 var accountArray = new Array(); 54 for (var i = 0; i < AddressNumber; i++) { 55 var account = Wallet.Account.NewAccount(); 56 //var hash = account.getAddressString(); 57 accountArray.push(account); 58 } 59 60 neb.api.getAccountState(source.getAddressString()).then(function (resp) { 61 console.log("source account state:" + JSON.stringify(resp)); 62 lastnonce = parseInt(resp.nonce); 63 64 deploy = Wallet.Account.NewAccount(); 65 66 var accounts = new Array(); 67 accounts.push(deploy); 68 cliamTokens(accounts, neb.nasToBasic(10), function () { 69 deployContract(); 70 }); 71 72 }); 73 74 function cliamTokens(accounts, value, done) { 75 for (var i = 0; i < accounts.length; i++) { 76 sendTransaction(source, accounts[i], value, ++lastnonce); 77 sleep(30); 78 } 79 checkCliamTokens(done); 80 } 81 82 function checkCliamTokens(done) { 83 var intervalAccount = setInterval(function () { 84 neb.api.getAccountState(source.getAddressString()).then(function (resp) { 85 // console.log("master accountState resp:" + JSON.stringify(resp)); 86 var nonce = parseInt(resp.nonce); 87 console.log("check cliam tokens lastnonce:", lastnonce); 88 89 if (lastnonce <= nonce){ 90 clearInterval(intervalAccount); 91 92 done(); 93 } 94 }); 95 }, 2000); 96 } 97 98 function deployContract(){ 99 100 // create contract 101 var source = FS.readFileSync("../nf/nvm/test/NRC20.js", "utf-8"); 102 var contract = { 103 "source": source, 104 "sourceType": "js", 105 "args": "[\"StandardToken\", \"NRC\", 18, \"1000000000\"]" 106 }; 107 108 var transaction = new Wallet.Transaction(ChainID, deploy, deploy, "0", 1, "0", "20000000000", contract); 109 transaction.signTransaction(); 110 var rawTx = transaction.toProtoString(); 111 112 // console.log("contract:" + rawTx); 113 114 neb.api.sendRawTransaction(rawTx).then(function (resp) { 115 console.log("send raw contract transaction resp:" + JSON.stringify(resp)); 116 117 checkContractDeployed(resp.txhash); 118 }); 119 } 120 121 function checkContractDeployed(txhash){ 122 123 var retry = 0; 124 125 // contract status and get contract_address 126 var interval = setInterval(function () { 127 // console.log("getTransactionReceipt hash:"+txhash); 128 neb.api.getTransactionReceipt(txhash).then(function (resp) { 129 130 console.log("deploy status:" + resp.status); 131 132 if(resp.status && resp.status === 1) { 133 clearInterval(interval); 134 135 console.log("deploy private key:" + deploy.getPrivateKeyString()); 136 console.log("deploy address:" + deploy.getAddressString()); 137 console.log("deploy contract address:" + resp.contract_address); 138 // console.log("deploy receipt:" + JSON.stringify(resp)); 139 140 checkNRCBalance(deploy.getAddressString(), resp.contract_address); 141 142 sendContractTransactions(resp.contract_address); 143 } 144 }).catch(function (err) { 145 retry++; 146 console.log("retry:", retry); 147 if (retry > 10) { 148 console.log(JSON.stringify(err.error)); 149 clearInterval(interval); 150 } 151 }); 152 153 }, 2000); 154 } 155 156 157 function sendTransaction(from, address, value, nonce) { 158 var transaction = new Wallet.Transaction(ChainID, from, address, value, nonce); 159 transaction.signTransaction(); 160 var rawTx = transaction.toProtoString(); 161 neb.api.sendRawTransaction(rawTx).then(function (resp) { 162 console.log("send raw transaction resp:" + JSON.stringify(resp)); 163 }); 164 } 165 166 function checkNRCBalance(address, contractAddress) { 167 var contract = { 168 "function": "balanceOf", 169 "args": "[\"" + address + "\"]" 170 }; 171 172 neb.api.call(address, contractAddress, "0", 2, "0", "0", contract).then(function (resp) { 173 console.log("balance of NRC:" + JSON.stringify(resp)); 174 }); 175 } 176 177 function sendContractTransactions(contract) { 178 neb.api.getAccountState(deploy.getAddressString()).then(function (resp) { 179 180 console.log("contract state:", JSON.stringify(resp)); 181 182 var nonce = parseInt(resp.nonce); 183 184 for (var i = 0; i < accountArray.length; i++) { 185 sendContractTransaction(0, deploy, accountArray[i].getAddressString(), nonce, contract); 186 } 187 188 sleep(10000); 189 190 for (var i = 0; i < accountArray.length; i++) { 191 checkNRCBalance(accountArray[i].getAddressString(), contract); 192 } 193 }); 194 } 195 196 function sendContractTransaction(sendtimes, from, to, nonce, contract) { 197 if(sendtimes < SendTimes) { 198 var call = { 199 "function": "transfer", 200 "args": "[\"" + to + "\", "+ "10" + "]" 201 }; 202 203 console.log("send contract nonce:",nonce); 204 var transaction = new Wallet.Transaction(ChainID, from, contract, "0", ++nonce, "0", "2000000000", call); 205 transaction.signTransaction(); 206 var rawTx = transaction.toProtoString(); 207 neb.api.sendRawTransaction(rawTx).then(function (resp) { 208 console.log("send raw contract transaction resp:" + JSON.stringify(resp)); 209 sendtimes++; 210 if(resp.txhash) { 211 sendContractTransaction(sendtimes, from, to, nonce, contract); 212 } 213 }); 214 } 215 216 }