Killable.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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", async function() {
  33. let initBalance, newBalance, owner, address, killable, kBalance, txnHash, receiptMined;
  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. killable = await Killable.new({from: accounts[0], value: web3.toWei('10','ether')});
  42. owner = await killable.owner();
  43. initBalance = web3.eth.getBalance(owner);
  44. kBalance = web3.eth.getBalance(killable.address);
  45. txnHash = await killable.kill({from: owner});
  46. receiptMined = await web3.eth.getTransactionReceiptMined(txnHash);
  47. newBalance = web3.eth.getBalance(owner);
  48. assert.isTrue(newBalance > initBalance);
  49. });
  50. });