MultisigWallet.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. contract('MultisigWallet', 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 passed address upon death', async function() {
  33. //Give account[0] 20 ether
  34. web3.eth.sendTransaction({from: web3.eth.coinbase, to: accounts[0], value: web3.toWei('20','ether')}, function(err, result) {
  35. if(err)
  36. console.log("ERROR:" + err);
  37. });
  38. let dailyLimit = 10;
  39. let ownersRequired = 2;
  40. //Create MultisigWallet contract with 10 ether
  41. let wallet = await MultisigWalletMock.new(accounts, ownersRequired, dailyLimit, {value: web3.toWei('10', 'ether')});
  42. //Get balances of owner and wallet after wallet creation.
  43. let ownerBalance = web3.eth.getBalance(accounts[0]);
  44. let walletBalance = web3.eth.getBalance(wallet.address);
  45. let hash = 1234;
  46. //Call kill function from two different owner accounts, satisfying owners required
  47. await wallet.kill(accounts[0], {data: hash});
  48. let txnHash = await wallet.kill(accounts[0], {from: accounts[1], data: hash});
  49. let receiptMined = await web3.eth.getTransactionReceiptMined(txnHash);
  50. //Get balances of owner and wallet after kill function is complete, compare with previous values
  51. let newOwnerBalance = web3.eth.getBalance(accounts[0]);
  52. let newWalletBalance = web3.eth.getBalance(wallet.address);
  53. assert.isTrue(newOwnerBalance > ownerBalance);
  54. assert.isTrue(newWalletBalance < walletBalance);
  55. });
  56. it('should execute transaction if below daily limit', async function() {
  57. //Give account[0] 20 ether
  58. web3.eth.sendTransaction({from: web3.eth.coinbase, to: accounts[0], value: web3.toWei('20','ether')}, function(err, result) {
  59. if(err)
  60. console.log("ERROR:" + err);
  61. });
  62. let dailyLimit = 10;
  63. let ownersRequired = 2;
  64. //Create MultisigWallet contract with 10 ether
  65. let wallet = await MultisigWalletMock.new(accounts, ownersRequired, dailyLimit, {value: web3.toWei('10', 'ether')});
  66. let accountBalance = web3.eth.getBalance(accounts[2]);
  67. let hash = 1234;
  68. //Owner account0 commands wallet to send 9 wei to account2
  69. let txnHash = await wallet.execute(accounts[2], 9, hash);
  70. let receiptMined = await web3.eth.getTransactionReceiptMined(txnHash);
  71. //Balance of account2 should have increased
  72. let newAccountBalance = web3.eth.getBalance(accounts[2]);
  73. assert.isTrue(newAccountBalance > accountBalance);
  74. });
  75. it('should prevent execution of transaction if above daily limit', async function() {
  76. //Give account[0] 20 ether
  77. web3.eth.sendTransaction({from: web3.eth.coinbase, to: accounts[0], value: web3.toWei('20','ether')}, function(err, result) {
  78. if(err)
  79. console.log("ERROR:" + err);
  80. });
  81. let dailyLimit = 10;
  82. let ownersRequired = 2;
  83. //Create MultisigWallet contract with 10 ether
  84. let wallet = await MultisigWalletMock.new(accounts, ownersRequired, dailyLimit, {value: web3.toWei('10', 'ether')});
  85. let accountBalance = web3.eth.getBalance(accounts[2]);
  86. let hash = 1234;
  87. //Owner account0 commands wallet to send 9 wei to account2
  88. let txnHash = await wallet.execute(accounts[2], 9, hash);
  89. let receiptMined = await web3.eth.getTransactionReceiptMined(txnHash);
  90. //Balance of account2 should have increased
  91. let newAccountBalance = web3.eth.getBalance(accounts[2]);
  92. assert.isTrue(newAccountBalance > accountBalance);
  93. accountBalance = newAccountBalance;
  94. hash = 4567;
  95. //Owner account0 commands wallet to send 2 more wei to account2, going over the daily limit of 10
  96. txnHash = await wallet.execute(accounts[2], 2, hash);
  97. receiptMined = await web3.eth.getTransactionReceiptMined(txnHash);
  98. //Balance of account2 should not change
  99. newAccountBalance = web3.eth.getBalance(accounts[2]);
  100. assert.equal(newAccountBalance.toString(), accountBalance.toString());
  101. });
  102. it('should execute transaction if above daily limit and enough owners approve', async function() {
  103. //Give account[0] 20 ether
  104. web3.eth.sendTransaction({from: web3.eth.coinbase, to: accounts[0], value: web3.toWei('20','ether')}, function(err, result) {
  105. if(err)
  106. console.log("ERROR:" + err);
  107. });
  108. let dailyLimit = 10;
  109. let ownersRequired = 2;
  110. //Create MultisigWallet contract with 10 ether
  111. let wallet = await MultisigWalletMock.new(accounts, ownersRequired, dailyLimit, {value: web3.toWei('10', 'ether')});
  112. let accountBalance = web3.eth.getBalance(accounts[2]);
  113. let hash = 1234;
  114. //Owner account0 commands wallet to send 11 wei to account2
  115. let txnHash = await wallet.execute(accounts[2], 11, hash);
  116. let receiptMined = await web3.eth.getTransactionReceiptMined(txnHash);
  117. //Balance of account2 should not change
  118. let newAccountBalance = web3.eth.getBalance(accounts[2]);
  119. assert.equal(newAccountBalance.toString(), accountBalance.toString());
  120. accountBalance = newAccountBalance;
  121. //Owner account1 commands wallet to send 11 wei to account2
  122. txnHash = await wallet.execute(accounts[2], 2, hash);
  123. receiptMined = await web3.eth.getTransactionReceiptMined(txnHash);
  124. //Balance of account2 should change
  125. newAccountBalance = web3.eth.getBalance(accounts[2]);
  126. assert.isTrue(newAccountBalance > accountBalance);
  127. });
  128. });