Killable.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. contract('Killable', function(accounts) {
  2. //from https://gist.github.com/xavierlepretre/88682e871f4ad07be4534ae560692ee6
  3. web3.eth.getTransactionReceiptMined = function (txnHash, interval) {
  4. var transactionReceiptAsync;
  5. interval = interval ? interval : 500;
  6. transactionReceiptAsync = function(txnHash, resolve, reject) {
  7. try {
  8. var receipt = web3.eth.getTransactionReceipt(txnHash);
  9. if (receipt == null) {
  10. setTimeout(function () {
  11. transactionReceiptAsync(txnHash, resolve, reject);
  12. }, interval);
  13. } else {
  14. resolve(receipt);
  15. }
  16. } catch(e) {
  17. reject(e);
  18. }
  19. };
  20. if (Array.isArray(txnHash)) {
  21. var promises = [];
  22. txnHash.forEach(function (oneTxHash) {
  23. promises.push(web3.eth.getTransactionReceiptMined(oneTxHash, interval));
  24. });
  25. return Promise.all(promises);
  26. } else {
  27. return new Promise(function (resolve, reject) {
  28. transactionReceiptAsync(txnHash, resolve, reject);
  29. });
  30. }
  31. };
  32. it("should send balance to owner after death", function(done) {
  33. var initBalance, newBalance, owner, address, killable, kBalance;
  34. web3.eth.sendTransaction({from: web3.eth.coinbase, to: accounts[0], value: web3.toWei('50','ether')}, function(err, result) {
  35. if(err)
  36. console.log("ERROR:" + err);
  37. else {
  38. console.log(result);
  39. }
  40. })
  41. return Killable.new({from: accounts[0], value: web3.toWei('10','ether')})
  42. .then(function(_killable) {
  43. killable = _killable;
  44. return killable.owner();
  45. })
  46. .then(function(_owner) {
  47. owner = _owner;
  48. initBalance = web3.eth.getBalance(owner);
  49. kBalance = web3.eth.getBalance(killable.address);
  50. })
  51. .then(function() {
  52. return killable.kill({from: owner});
  53. })
  54. .then(function (txnHash) {
  55. return web3.eth.getTransactionReceiptMined(txnHash);
  56. })
  57. .then(function() {
  58. newBalance = web3.eth.getBalance(owner);
  59. })
  60. .then(function() {
  61. assert.isTrue(newBalance > initBalance);
  62. })
  63. .then(done);
  64. });
  65. });