github.com/igggame/nebulas-go@v2.1.0+incompatible/nebtestkit/cases/contract/contract.nrc20.test.js (about) 1 'use strict'; 2 3 var sleep = require("system-sleep"); 4 var HttpRequest = require("../../node-request"); 5 var FS = require("fs"); 6 var TestNetConfig = require("../testnet_config.js"); 7 8 9 var expect = require('chai').expect; 10 var BigNumber = require('bignumber.js'); 11 12 var Nebulas = require('nebulas'); 13 var Account = Nebulas.Account; 14 var Transaction = Nebulas.Transaction; 15 var CryptoUtils = Nebulas.CryptoUtils; 16 var Utils = Nebulas.Utils; 17 var Neb = Nebulas.Neb; 18 19 20 var testCases = new Array(); 21 var caseIndex = 0; 22 23 24 // mocha cases/contract/xxx testneb1 -t 200000 25 var args = process.argv.splice(2); 26 var env = args[1]; 27 var testNetConfig = new TestNetConfig(env); 28 29 30 var neb = new Neb(); 31 var ChainID = testNetConfig.ChainId; 32 var originSource = testNetConfig.sourceAccount; 33 var coinbase = testNetConfig.coinbase; 34 var apiEndPoint = testNetConfig.apiEndPoint; 35 neb.setRequest(new HttpRequest(apiEndPoint)); 36 37 var coinState, source, deploy, from, fromState, contractAddr; 38 39 var lastnonce = 0; 40 41 function prepareSource(done) { 42 neb.api.getAccountState(originSource.getAddressString()).then(function (resp) { 43 console.log("prepare source account state:" + JSON.stringify(resp)); 44 var nonce = parseInt(resp.nonce); 45 46 source = Account.NewAccount(); 47 48 var tx = new Transaction(ChainID, originSource, source, neb.nasToBasic(1000), nonce + 1, "1000000", "200000"); 49 tx.signTransaction(); 50 51 console.log("cliam source tx:", tx.toString()); 52 53 return neb.api.sendRawTransaction(tx.toProtoString()); 54 }).then(function (resp) { 55 console.log("send Raw Tx:" + JSON.stringify(resp)); 56 expect(resp).to.be.have.property('txhash'); 57 checkTransaction(resp.txhash, function (receipt) { 58 console.log("tx receipt : " + JSON.stringify(receipt)); 59 expect(receipt).to.be.have.property('status').equal(1); 60 61 done(); 62 }); 63 }).catch(function (err) { 64 done(err); 65 }); 66 } 67 68 function prepareContractCall(testCase, done) { 69 neb.api.getAccountState(source.getAddressString()).then(function (resp) { 70 console.log("source account state:" + JSON.stringify(resp)); 71 lastnonce = parseInt(resp.nonce); 72 73 var accounts = new Array(); 74 var values = new Array(); 75 if (Utils.isNull(contractAddr)) { 76 deploy = Account.NewAccount(); 77 accounts.push(deploy); 78 values.push(neb.nasToBasic(1)); 79 } 80 81 if (typeof testCase.testInput.from !== "undefined") { 82 accounts.push(testCase.testInput.from); 83 values.push(neb.nasToBasic(1)); 84 } 85 86 if (typeof testCase.testInput.to !== "undefined") { 87 accounts.push(testCase.testInput.to); 88 values.push(neb.nasToBasic(1)); 89 } 90 91 if (accounts.length > 0) { 92 cliamTokens(accounts, values, function () { 93 if (Utils.isNull(contractAddr)) { 94 deployContract(done); 95 } else { 96 done(); 97 } 98 }); 99 } else { 100 done(); 101 } 102 103 }); 104 } 105 106 function cliamTokens(accounts, values, done) { 107 for (var i = 0; i < accounts.length; i++) { 108 // console.log("acc:"+accounts[i].getAddressString()+"value:"+values[i]); 109 sendTransaction(source, accounts[i], values[i], ++lastnonce); 110 sleep(30); 111 } 112 checkCliamTokens(done); 113 } 114 115 function sendTransaction(from, address, value, nonce) { 116 var transaction = new Transaction(ChainID, from, address, value, nonce, "1000000", "2000000"); 117 transaction.signTransaction(); 118 var rawTx = transaction.toProtoString(); 119 // console.log("send transaction:", transaction.toString()); 120 neb.api.sendRawTransaction(rawTx).then(function (resp) { 121 console.log("send raw transaction resp:" + JSON.stringify(resp)); 122 }); 123 } 124 125 function checkCliamTokens(done) { 126 var intervalAccount = setInterval(function () { 127 neb.api.getAccountState(source.getAddressString()).then(function (resp) { 128 // console.log("master accountState resp:" + JSON.stringify(resp)); 129 var nonce = parseInt(resp.nonce); 130 console.log("check cliam tokens nonce:", lastnonce); 131 132 if (lastnonce <= nonce){ 133 console.log("cliam tokens success"); 134 clearInterval(intervalAccount); 135 136 done(); 137 } 138 }); 139 }, 2000); 140 } 141 142 function deployContract(done){ 143 144 // create contract 145 var source = FS.readFileSync("../nf/nvm/test/NRC20.js", "utf-8"); 146 var contract = { 147 "source": source, 148 "sourceType": "js", 149 "args": "[\"StandardToken\", \"NRC\", 18, \"1000000000\"]" 150 }; 151 152 var transaction = new Transaction(ChainID, deploy, deploy, "0", 1, "10000000", "2000000", contract); 153 transaction.signTransaction(); 154 var rawTx = transaction.toProtoString(); 155 156 // console.log("contract:" + rawTx); 157 158 neb.api.sendRawTransaction(rawTx).then(function (resp) { 159 console.log("deploy contract:" + JSON.stringify(resp)); 160 161 checkTransaction(resp.txhash, done); 162 }); 163 } 164 165 function checkTransaction(txhash, done){ 166 167 var retry = 0; 168 var maxRetry = 20; 169 170 // contract status and get contract_address 171 var interval = setInterval(function () { 172 // console.log("getTransactionReceipt hash:"+txhash); 173 neb.api.getTransactionReceipt(txhash).then(function (resp) { 174 retry++; 175 176 console.log("check transaction status:" + resp.status); 177 178 if(resp.status && resp.status === 1) { 179 clearInterval(interval); 180 181 if (resp.contract_address) { 182 console.log("deploy private key:" + deploy.getPrivateKeyString()); 183 console.log("deploy address:" + deploy.getAddressString()); 184 console.log("deploy privatekey:" + deploy.getPrivateKeyString()); 185 console.log("deploy contract address:" + resp.contract_address); 186 // console.log("deploy receipt:" + JSON.stringify(resp)); 187 188 contractAddr = resp.contract_address; 189 190 // checkNRCBalance(resp.from, resp.contract_address); 191 } 192 193 done(resp); 194 } else if (resp.status && resp.status === 2) { 195 if (retry > maxRetry) { 196 console.log("check transaction time out"); 197 clearInterval(interval); 198 done(resp); 199 } 200 } else { 201 clearInterval(interval); 202 console.log("transaction execution failed"); 203 done(resp); 204 } 205 }).catch(function (err) { 206 retry++; 207 console.log("check transaction not found retry"); 208 if (retry > maxRetry) { 209 console.log(JSON.stringify(err.error)); 210 clearInterval(interval); 211 done(err); 212 } 213 }); 214 215 }, 2000); 216 } 217 218 function testCall(testInput, testExpect, done) { 219 var contract = { 220 "function": testInput.function, 221 "args": testInput.args 222 }; 223 var from = Account.NewAccount(); 224 neb.api.call(from.getAddressString(), contractAddr, "0", 1, "1000000", "2000000", contract).then(function (resp) { 225 var result = JSON.parse(resp.result); 226 console.log("result:", result); 227 expect(result).to.equal(testExpect.result); 228 done(); 229 }).catch(function (err) { 230 if (testExpect.exeFailed) { 231 console.log("call failed:", err.message); 232 done(); 233 } else { 234 done(err); 235 } 236 }); 237 } 238 239 function testTransfer(testInput, testExpect, done) { 240 var from = (Utils.isNull(testInput.from)) ? deploy : testInput.from; 241 var to = Account.NewAccount(); 242 var fromBalance, toBalance; 243 244 balanceOfNRC20(from.getAddressString()).then(function(resp) { 245 fromBalance = JSON.parse(resp.result); 246 console.log("from balance:", fromBalance); 247 248 return balanceOfNRC20(to.getAddressString()); 249 }).then(function (resp) { 250 toBalance = JSON.parse(resp.result); 251 console.log("to balance:", toBalance); 252 253 return neb.api.getAccountState(from.getAddressString()); 254 }).then(function (resp) { 255 console.log("from state:", JSON.stringify(resp)); 256 257 var args = testInput.args; 258 if (!Utils.isNull(testInput.transferValue)) { 259 if (testInput.transferValue === "from.balance") { 260 testInput.transferValue = fromBalance; 261 } 262 args = "[\""+ to.getAddressString() +"\", \""+ testInput.transferValue +"\"]"; 263 } 264 265 var contract = { 266 "function": "transfer", 267 "args": args 268 }; 269 var tx = new Transaction(ChainID, from, contractAddr, "0", parseInt(resp.nonce) + 1, "1000000", "2000000", contract); 270 tx.signTransaction(); 271 272 console.log("raw tx:", tx.toString()); 273 return neb.api.sendRawTransaction(tx.toProtoString()); 274 }).then(function (resp) { 275 console.log("send raw tx:", resp); 276 checkTransaction(resp.txhash, function (receipt) { 277 var resetContract = false; 278 try { 279 expect(receipt).to.be.have.property('status').equal(testExpect.status); 280 281 balanceOfNRC20(from.getAddressString()).then(function (resp) { 282 var balance = JSON.parse(resp.result); 283 console.log("after from balance:", balance); 284 285 if (testExpect.status === 1) { 286 var balanceNumber = new BigNumber(fromBalance).sub(testInput.transferValue); 287 expect(balanceNumber.toString(10)).to.equal(balance); 288 } else { 289 expect(balance).to.equal(fromBalance); 290 } 291 292 if (balance === "0") { 293 resetContract = true; 294 } 295 296 return balanceOfNRC20(to.getAddressString()); 297 }).then(function (resp) { 298 var balance = JSON.parse(resp.result); 299 console.log("after to balance:", balance); 300 301 if (testExpect.status === 1) { 302 var balanceNumber = new BigNumber(toBalance).plus(testInput.transferValue); 303 expect(balanceNumber.toString(10)).to.equal(balance); 304 } else { 305 expect(toBalance).to.equal(balance); 306 } 307 308 return neb.api.getEventsByHash(receipt.hash); 309 }).then(function (events) { 310 // console.log("tx events:", events); 311 for (var i = 0; i < events.events.length; i++) { 312 var event = events.events[i]; 313 console.log("tx event:", event); 314 if (event.topic == "chain.transactionResult") { 315 var result = JSON.parse(event.data); 316 expect(result.status).to.equal(testExpect.status); 317 } 318 } 319 if (resetContract) { 320 contractAddr = null; 321 } 322 done(); 323 }).catch(function (err) { 324 if (resetContract) { 325 contractAddr = null; 326 } 327 done(err); 328 }) 329 } catch (err) { 330 if (resetContract) { 331 contractAddr = null; 332 } 333 done(err); 334 } 335 }); 336 }).catch(function(err) { 337 done(err); 338 }); 339 } 340 341 function testApprove(testInput, testExpect, done) { 342 var from = (Utils.isNull(testInput.from)) ? deploy : testInput.from; 343 var to = Account.NewAccount(); 344 var fromAllowance, fromBalance, fromState; 345 346 allowanceOfNRC20(from.getAddressString(), to.getAddressString()).then(function (resp) { 347 fromAllowance = JSON.parse(resp.result); 348 console.log("allowance:", fromAllowance); 349 350 return balanceOfNRC20(from.getAddressString()); 351 }).then(function (resp) { 352 fromBalance = JSON.parse(resp.result); 353 console.log("balance:", fromBalance); 354 355 return neb.api.getAccountState(from.getAddressString()); 356 }).then(function (resp) { 357 fromState = resp; 358 console.log("from state:", resp); 359 360 var args = testInput.args; 361 if (!Utils.isNull(testInput.approveValue)) { 362 if (testInput.approveValue === "from.balance") { 363 testInput.approveValue = fromBalance; 364 } 365 var currentValue = fromAllowance; 366 if (!Utils.isNull(testInput.currentValue)) { 367 currentValue = testInput.currentValue; 368 } 369 args = "[\""+ to.getAddressString() +"\", \""+ currentValue +"\", \""+ testInput.approveValue +"\"]"; 370 } 371 372 var contract = { 373 "function": "approve", 374 "args": args 375 }; 376 var tx = new Transaction(ChainID, from, contractAddr, "0", parseInt(resp.nonce) + 1, "1000000", "2000000", contract); 377 tx.signTransaction(); 378 379 console.log("raw tx:", tx.toString()); 380 return neb.api.sendRawTransaction(tx.toProtoString()); 381 }).then(function (resp) { 382 console.log("send raw tx:", resp); 383 384 checkTransaction(resp.txhash, function (receipt) { 385 try { 386 expect(receipt).to.be.have.property('status').equal(testExpect.status); 387 388 balanceOfNRC20(from.getAddressString()).then(function (resp) { 389 var balance = JSON.parse(resp.result); 390 console.log("after from balance:", balance); 391 expect(balance).to.equal(fromBalance); 392 393 return allowanceOfNRC20(from.getAddressString(), to.getAddressString()); 394 }).then(function (resp) { 395 var allownance = JSON.parse(resp.result); 396 console.log("after from allownance:", allownance); 397 if (testExpect.status === 1) { 398 expect(allownance).to.equal(testInput.approveValue); 399 } 400 401 return neb.api.getEventsByHash(receipt.hash); 402 }).then(function (events) { 403 // console.log("tx events:", events); 404 for (var i = 0; i < events.events.length; i++) { 405 var event = events.events[i]; 406 console.log("tx event:", event); 407 if (event.topic == "chain.transactionResult") { 408 var result = JSON.parse(event.data); 409 expect(result.status).to.equal(testExpect.status); 410 } 411 } 412 done(); 413 }).catch(function (err) { 414 done(err); 415 }) 416 } catch (err) { 417 done(err); 418 } 419 }); 420 421 }).catch(function (err) { 422 done(err); 423 }); 424 } 425 426 function testTransferFrom(testInput, testExpect, done) { 427 var from = (Utils.isNull(testInput.from)) ? deploy : testInput.from; 428 var to = (Utils.isNull(testInput.to)) ? Account.NewAccount() : testInput.to; 429 var deployAllowance, deployBalance, deployState, fromBalance, toBalance; 430 431 allowanceOfNRC20(deploy.getAddressString(), from.getAddressString()).then(function (resp) { 432 deployAllowance = JSON.parse(resp.result); 433 console.log("deploy allowance:", deployAllowance); 434 435 return balanceOfNRC20(deploy.getAddressString()); 436 }).then(function (resp) { 437 deployBalance = JSON.parse(resp.result); 438 console.log("deploy balance:", deployBalance); 439 440 return balanceOfNRC20(from.getAddressString()); 441 }).then(function (resp) { 442 fromBalance = JSON.parse(resp.result); 443 console.log("from balance:", fromBalance); 444 445 return balanceOfNRC20(to.getAddressString()); 446 }).then(function (resp) { 447 toBalance = JSON.parse(resp.result); 448 console.log("to balance:", toBalance); 449 450 return neb.api.getAccountState(deploy.getAddressString()); 451 }).then(function (resp) { 452 deployState = resp; 453 console.log("deploy state:", resp); 454 455 return neb.api.getAccountState(from.getAddressString()); 456 }).then(function (resp) { 457 var fromState = resp; 458 console.log("from state:", resp); 459 460 approveNRC20(testInput, deployState, from, deployAllowance, function (resp) { 461 if (!Utils.isNull(resp)) { 462 if (resp instanceof Error) { 463 done(resp); 464 } 465 if (!(resp.status && resp.status === 1)) { 466 done(new Error("approve failed")); 467 } 468 } 469 470 allowanceOfNRC20(deploy.getAddressString(), from.getAddressString()).then(function (resp) { 471 deployAllowance = JSON.parse(resp.result); 472 console.log("deploy allowance:", deployAllowance); 473 474 var args = testInput.args; 475 if (!Utils.isNull(testInput.transferValue)) { 476 args = "[\""+ deploy.getAddressString() +"\", \""+ to.getAddressString() +"\", \""+ testInput.transferValue +"\"]"; 477 } 478 479 var contract = { 480 "function": "transferFrom", 481 "args": args 482 }; 483 var tx = new Transaction(ChainID, from, contractAddr, "0", parseInt(fromState.nonce) + 1, "1000000", "2000000", contract); 484 tx.signTransaction(); 485 486 console.log("raw tx:", tx.toString()); 487 return neb.api.sendRawTransaction(tx.toProtoString()); 488 }).then(function (resp) { 489 console.log("send raw tx:", resp); 490 491 checkTransaction(resp.txhash, function (receipt) { 492 try { 493 expect(receipt).to.be.have.property('status').equal(testExpect.status); 494 495 balanceOfNRC20(deploy.getAddressString()).then(function (resp) { 496 var balance = JSON.parse(resp.result); 497 console.log("after deploy balance:", balance); 498 499 500 if (testExpect.status === 1) { 501 var balanceNumber = new BigNumber(deployBalance).sub(testInput.transferValue); 502 expect(balanceNumber.toString(10)).to.equal(balance); 503 } else { 504 expect(balance).to.equal(deployBalance); 505 } 506 507 return balanceOfNRC20(from.getAddressString()); 508 }).then(function (resp) { 509 var balance = JSON.parse(resp.result); 510 console.log("after from balance:", balance); 511 expect(balance).to.equal(fromBalance); 512 513 return allowanceOfNRC20(deploy.getAddressString(), from.getAddressString()); 514 }).then(function (resp) { 515 var allownance = JSON.parse(resp.result); 516 console.log("after deploy allownance:", allownance); 517 if (testExpect.status === 1) { 518 var allownanceNumber = new BigNumber(deployAllowance).sub(testInput.transferValue); 519 expect(allownanceNumber.toString(10)).to.equal(allownance); 520 } else { 521 expect(deployAllowance).to.equal(allownance); 522 } 523 524 return balanceOfNRC20(to.getAddressString()); 525 }).then(function (resp) { 526 var balance = JSON.parse(resp.result); 527 console.log("after to balance:", balance); 528 529 if (testExpect.status === 1) { 530 var balanceNumber = new BigNumber(toBalance).plus(testInput.transferValue); 531 expect(balanceNumber.toString(10)).to.equal(balance); 532 } else { 533 expect(toBalance).to.equal(balance); 534 } 535 536 return neb.api.getEventsByHash(receipt.hash); 537 }).then(function (events) { 538 // console.log("tx events:", events); 539 for (var i = 0; i < events.events.length; i++) { 540 var event = events.events[i]; 541 console.log("tx event:", event); 542 if (event.topic == "chain.transactionResult") { 543 var result = JSON.parse(event.data); 544 expect(result.status).to.equal(testExpect.status); 545 } 546 } 547 done(); 548 }).catch(function (err) { 549 done(err); 550 }) 551 } catch (err) { 552 done(err); 553 } 554 }); 555 }); 556 }); 557 }).catch(function (err) { 558 done(err); 559 }); 560 } 561 562 function approveNRC20(testInput, deployState, from, currentValue, done) { 563 if (!Utils.isNull(testInput.approveValue)) { 564 var approveValue = testInput.approveValue; 565 var args = "[\""+ from.getAddressString() +"\", \""+ currentValue +"\", \""+ approveValue +"\"]"; 566 567 var contract = { 568 "function": "approve", 569 "args": args 570 }; 571 var tx = new Transaction(ChainID, deploy, contractAddr, "0", parseInt(deployState.nonce) + 1, "1000000", "2000000", contract); 572 tx.signTransaction(); 573 // console.log("approve tx:", tx.toString()); 574 neb.api.sendRawTransaction(tx.toProtoString()).then(function (resp) { 575 console.log("approve tx:", resp); 576 577 checkTransaction(resp.txhash, done); 578 }).catch(function (err) { 579 done(err); 580 }); 581 } else { 582 done(); 583 } 584 } 585 586 function balanceOfNRC20(address) { 587 var contract = { 588 "function": "balanceOf", 589 "args": "[\"" + address + "\"]" 590 }; 591 return neb.api.call(address, contractAddr, "0", 1, "1000000", "200000", contract) 592 } 593 594 function allowanceOfNRC20(owner, spender) { 595 var contract = { 596 "function": "allowance", 597 "args": "[\"" + owner + "\", \""+ spender +"\"]" 598 }; 599 return neb.api.call(owner, contractAddr, "0", 1, "1000000", "2000000", contract) 600 } 601 602 var testCase = { 603 "name": "1. name", 604 "testInput": { 605 isCall: true, 606 function: "name", 607 args: "" 608 }, 609 "testExpect": { 610 result: "StandardToken" 611 } 612 }; 613 testCases.push(testCase); 614 615 616 var testCase = { 617 "name": "2. symbol", 618 "testInput": { 619 isCall: true, 620 function: "symbol", 621 args: "" 622 }, 623 "testExpect": { 624 result: "NRC" 625 } 626 }; 627 testCases.push(testCase); 628 629 testCase = { 630 "name": "3. decimals", 631 "testInput": { 632 isCall: true, 633 function: "decimals", 634 args: "" 635 }, 636 "testExpect": { 637 result: 18 638 } 639 }; 640 testCases.push(testCase); 641 642 testCase = { 643 "name": "4. totalSupply", 644 "testInput": { 645 isCall: true, 646 function: "totalSupply", 647 args: "" 648 }, 649 "testExpect": { 650 result: "1000000000000000000000000000" 651 } 652 }; 653 testCases.push(testCase); 654 655 testCase = { 656 "name": "5. balanceOf args empty", 657 "testInput": { 658 isCall: true, 659 function: "balanceOf", 660 args: "" 661 }, 662 "testExpect": { 663 result: "0" 664 } 665 }; 666 testCases.push(testCase); 667 668 testCase = { 669 "name": "6. balanceOf args format err", 670 "testInput": { 671 isCall: true, 672 function: "balanceOf", 673 args: "[1" 674 }, 675 "testExpect": { 676 exeFailed: true, 677 result: "0" 678 } 679 }; 680 testCases.push(testCase); 681 682 testCase = { 683 "name": "7. balanceOf args arr", 684 "testInput": { 685 isCall: true, 686 function: "balanceOf", 687 args: "[1]" 688 }, 689 "testExpect": { 690 result: "0" 691 } 692 }; 693 testCases.push(testCase); 694 695 testCase = { 696 "name": "8. balanceOf address no balance", 697 "testInput": { 698 isCall: true, 699 function: "balanceOf", 700 args: "[\"1a263547d167c74cf4b8f9166cfa244de0481c514a45aa2c\"]" 701 }, 702 "testExpect": { 703 result: "0" 704 } 705 }; 706 testCases.push(testCase); 707 708 testCase = { 709 "name": "9. balanceOf address have balance", 710 "testInput": { 711 isCall: true, 712 function: "balanceOf", 713 args: "" 714 }, 715 "testExpect": { 716 result: "0" 717 } 718 }; 719 testCases.push(testCase); 720 721 testCase = { 722 "name": "10. allowance args empty", 723 "testInput": { 724 isCall: true, 725 function: "allowance", 726 args: "" 727 }, 728 "testExpect": { 729 result: "0" 730 } 731 }; 732 testCases.push(testCase); 733 734 testCase = { 735 "name": "11. allowance args less", 736 "testInput": { 737 isCall: true, 738 function: "allowance", 739 args: "[\"1a263547d167c74cf4b8f9166cfa244de0481c514a45aa2c\"]" 740 }, 741 "testExpect": { 742 result: "0" 743 } 744 }; 745 testCases.push(testCase); 746 747 testCase = { 748 "name": "12. allowance args format err", 749 "testInput": { 750 isCall: true, 751 function: "allowance", 752 args: "[\"1a263547d167c74cf4b8f9166cfa244de0481c514a45aa2c\"" 753 }, 754 "testExpect": { 755 exeFailed: true, 756 result: "0" 757 } 758 }; 759 testCases.push(testCase); 760 761 testCase = { 762 "name": "13. allowance args err", 763 "testInput": { 764 isCall: true, 765 function: "allowance", 766 args: "[\"1a263547d167c74cf4b8f9166cfa244de0481c514a45aa2c\", 1]" 767 }, 768 "testExpect": { 769 result: "0" 770 } 771 }; 772 testCases.push(testCase); 773 774 testCase = { 775 "name": "14. allowance args no allowance", 776 "testInput": { 777 isCall: true, 778 function: "allowance", 779 args: "[\"1a263547d167c74cf4b8f9166cfa244de0481c514a45aa2c\",\"1a263547d167c74cf4b8f9166cfa244de0481c514a45aa2c\"]" 780 }, 781 "testExpect": { 782 result: "0" 783 } 784 }; 785 testCases.push(testCase); 786 787 testCase = { 788 "name": "15. allowance correct", 789 "testInput": { 790 isCall: true, 791 function: "allowance", 792 args: "" 793 }, 794 "testExpect": { 795 result: "0" 796 } 797 }; 798 testCases.push(testCase); 799 800 testCase = { 801 "name": "16. transfer args empty", 802 "testInput": { 803 isTransfer: true, 804 function: "transfer", 805 args: "" 806 }, 807 "testExpect": { 808 status: 0 809 } 810 }; 811 testCases.push(testCase); 812 813 testCase = { 814 "name": "17. transfer args less", 815 "testInput": { 816 isTransfer: true, 817 function: "transfer", 818 args: "[\"1a263547d167c74cf4b8f9166cfa244de0481c514a45aa2c\"]" 819 }, 820 "testExpect": { 821 status: 0 822 } 823 }; 824 testCases.push(testCase); 825 826 testCase = { 827 "name": "18. transfer args err", 828 "testInput": { 829 isTransfer: true, 830 function: "transfer", 831 args: "[0]" 832 }, 833 "testExpect": { 834 status: 0 835 } 836 }; 837 testCases.push(testCase); 838 839 testCase = { 840 "name": "19. transfer value = 0", 841 "testInput": { 842 isTransfer: true, 843 transferValue: "0", 844 function: "transfer", 845 args: "" 846 }, 847 "testExpect": { 848 status: 1 849 } 850 }; 851 testCases.push(testCase); 852 853 testCase = { 854 "name": "20. transfer value < balance ", 855 "testInput": { 856 isTransfer: true, 857 transferValue: "1", 858 function: "transfer", 859 args: "" 860 }, 861 "testExpect": { 862 status: 1 863 } 864 }; 865 testCases.push(testCase); 866 867 testCase = { 868 "name": "21. transfer value = balance ", 869 "testInput": { 870 isTransfer: true, 871 transferValue: "from.balance", 872 function: "transfer", 873 args: "" 874 }, 875 "testExpect": { 876 status: 1 877 } 878 }; 879 testCases.push(testCase); 880 881 testCase = { 882 "name": "22. transfer value > balance ", 883 "testInput": { 884 isTransfer: true, 885 transferValue: "100000000000000000000000000000000", 886 function: "transfer", 887 args: "" 888 }, 889 "testExpect": { 890 status: 0 891 } 892 }; 893 testCases.push(testCase); 894 895 testCase = { 896 "name": "23. transfer balance = 0 & value = 0", 897 "testInput": { 898 isTransfer: true, 899 from: Account.NewAccount(), 900 transferValue: "0", 901 function: "transfer", 902 args: "" 903 }, 904 "testExpect": { 905 status: 1 906 } 907 }; 908 testCases.push(testCase); 909 910 testCase = { 911 "name": "24. approve args empty", 912 "testInput": { 913 isApprove: true, 914 function: "approve", 915 args: "" 916 }, 917 "testExpect": { 918 status: 0 919 } 920 }; 921 testCases.push(testCase); 922 923 testCase = { 924 "name": "25. approve args less", 925 "testInput": { 926 isApprove: true, 927 function: "approve", 928 args: "" 929 }, 930 "testExpect": { 931 status: 0 932 } 933 }; 934 testCases.push(testCase); 935 936 testCase = { 937 "name": "26. approve args err", 938 "testInput": { 939 isApprove: true, 940 function: "approve", 941 args: "" 942 }, 943 "testExpect": { 944 status: 0 945 } 946 }; 947 testCases.push(testCase); 948 949 testCase = { 950 "name": "27. approve balance = 0 & value = 0", 951 "testInput": { 952 isApprove: true, 953 from: Account.NewAccount(), 954 function: "approve", 955 approveValue: "0", 956 args: "" 957 }, 958 "testExpect": { 959 status: 1 960 } 961 }; 962 testCases.push(testCase); 963 964 testCase = { 965 "name": "28. approve balance < value", 966 "testInput": { 967 isApprove: true, 968 approveValue: "1000000000000000000000000000000", 969 function: "approve", 970 args: "" 971 }, 972 "testExpect": { 973 status: 0 974 } 975 }; 976 testCases.push(testCase); 977 978 testCase = { 979 "name": "29. approve balance > value", 980 "testInput": { 981 isApprove: true, 982 approveValue: "1", 983 function: "approve", 984 args: "" 985 }, 986 "testExpect": { 987 status: 1 988 } 989 }; 990 testCases.push(testCase); 991 992 testCase = { 993 "name": "30. approve balance = value", 994 "testInput": { 995 isApprove: true, 996 approveValue: "from.balance", 997 function: "approve", 998 args: "" 999 }, 1000 "testExpect": { 1001 status: 1 1002 } 1003 }; 1004 testCases.push(testCase); 1005 1006 testCase = { 1007 "name": "31. approve value < 0", 1008 "testInput": { 1009 isApprove: true, 1010 approveValue: "-1", 1011 function: "approve", 1012 args: "" 1013 }, 1014 "testExpect": { 1015 status: 1 1016 } 1017 }; 1018 testCases.push(testCase); 1019 1020 testCase = { 1021 "name": "32. approve currentValue not correct", 1022 "testInput": { 1023 isApprove: true, 1024 currentValue: "123123", 1025 approveValue: "1", 1026 function: "approve", 1027 args: "" 1028 }, 1029 "testExpect": { 1030 status: 0 1031 } 1032 }; 1033 testCases.push(testCase); 1034 1035 testCase = { 1036 "name": "33. transferFrom args empty", 1037 "testInput": { 1038 isTransferFrom: true, 1039 from: Account.NewAccount(), 1040 function: "transferFrom", 1041 args: "" 1042 }, 1043 "testExpect": { 1044 status: 0 1045 } 1046 }; 1047 testCases.push(testCase); 1048 1049 testCase = { 1050 "name": "34. transferFrom args less", 1051 "testInput": { 1052 isTransferFrom: true, 1053 from: Account.NewAccount(), 1054 function: "transferFrom", 1055 args: "[\"1a263547d167c74cf4b8f9166cfa244de0481c514a45aa2c\"]" 1056 }, 1057 "testExpect": { 1058 status: 0 1059 } 1060 }; 1061 testCases.push(testCase); 1062 1063 testCase = { 1064 "name": "35. transferFrom args err", 1065 "testInput": { 1066 isTransferFrom: true, 1067 from: Account.NewAccount(), 1068 function: "transferFrom", 1069 args: "[\"1a263547d167c74cf4b8f9166cfa244de0481c514a45aa2c\", 1]" 1070 }, 1071 "testExpect": { 1072 status: 0 1073 } 1074 }; 1075 testCases.push(testCase); 1076 1077 testCase = { 1078 "name": "36. transferFrom no approve", 1079 "testInput": { 1080 isTransferFrom: true, 1081 from: Account.NewAccount(), 1082 transferValue: "1", 1083 function: "transferFrom", 1084 args: "" 1085 }, 1086 "testExpect": { 1087 status: 0 1088 } 1089 }; 1090 testCases.push(testCase); 1091 1092 testCase = { 1093 "name": "37. transferFrom approve < value", 1094 "testInput": { 1095 isTransferFrom: true, 1096 from: Account.NewAccount(), 1097 approveValue: "10000", 1098 transferValue: "10000000000000000000000000000", 1099 function: "transferFrom", 1100 args: "" 1101 }, 1102 "testExpect": { 1103 status: 0 1104 } 1105 }; 1106 testCases.push(testCase); 1107 1108 testCase = { 1109 "name": "38. transferFrom approve > value", 1110 "testInput": { 1111 isTransferFrom: true, 1112 from: Account.NewAccount(), 1113 approveValue: "10", 1114 transferValue: "1", 1115 function: "transferFrom", 1116 args: "" 1117 }, 1118 "testExpect": { 1119 status: 1 1120 } 1121 }; 1122 testCases.push(testCase); 1123 1124 testCase = { 1125 "name": "39. transferFrom approve = value", 1126 "testInput": { 1127 isTransferFrom: true, 1128 from: Account.NewAccount(), 1129 approveValue: "1", 1130 transferValue: "1", 1131 function: "transferFrom", 1132 args: "" 1133 }, 1134 "testExpect": { 1135 status: 1 1136 } 1137 }; 1138 testCases.push(testCase); 1139 1140 testCase = { 1141 "name": "40. transferFrom approve = value = 0", 1142 "testInput": { 1143 isTransferFrom: true, 1144 from: Account.NewAccount(), 1145 approveValue: "0", 1146 transferValue: "0", 1147 function: "transferFrom", 1148 args: "" 1149 }, 1150 "testExpect": { 1151 status: 1 1152 } 1153 }; 1154 testCases.push(testCase); 1155 1156 describe('contract call test', function () { 1157 before(function (done) { 1158 prepareSource(done); 1159 }); 1160 1161 // var testCase = testCases[37]; 1162 // it(testCase.name, function (done) { 1163 // prepareContractCall(testCase, function (err) { 1164 // if (err instanceof Error) { 1165 // done(err); 1166 // } else { 1167 // if (testCase.testInput.isCall) { 1168 // testCall(testCase.testInput, testCase.testExpect, done); 1169 // } else if (testCase.testInput.isTransfer) { 1170 // testTransfer(testCase.testInput, testCase.testExpect, done); 1171 // } else if (testCase.testInput.isApprove) { 1172 // testApprove(testCase.testInput, testCase.testExpect, done); 1173 // } else if (testCase.testInput.isTransferFrom) { 1174 // testTransferFrom(testCase.testInput, testCase.testExpect, done); 1175 // } 1176 // } 1177 // }); 1178 // }); 1179 1180 for (var i = 0; i < testCases.length; i++) { 1181 1182 it(testCases[i].name, function (done) { 1183 var testCase = testCases[caseIndex]; 1184 prepareContractCall(testCase, function (err) { 1185 if (err instanceof Error) { 1186 done(err); 1187 } else { 1188 if (testCase.testInput.isCall) { 1189 testCall(testCase.testInput, testCase.testExpect, done); 1190 } else if (testCase.testInput.isTransfer) { 1191 testTransfer(testCase.testInput, testCase.testExpect, done); 1192 } else if (testCase.testInput.isApprove) { 1193 testApprove(testCase.testInput, testCase.testExpect, done); 1194 } else if (testCase.testInput.isTransferFrom) { 1195 testTransferFrom(testCase.testInput, testCase.testExpect, done); 1196 } 1197 } 1198 }); 1199 }); 1200 } 1201 1202 afterEach(function () { 1203 caseIndex++; 1204 }); 1205 });