PullPayment.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. contract('PullPayment', function(accounts) {
  2. it("can't call asyncSend externally", function(done) {
  3. return PullPaymentMock.new()
  4. .then(function(ppc) {
  5. assert.isUndefined(ppc.asyncSend);
  6. })
  7. .then(done);
  8. });
  9. it("can record an async payment correctly", function(done) {
  10. var ppce;
  11. var AMOUNT = 100;
  12. return PullPaymentMock.new()
  13. .then(function(_ppce) {
  14. ppce = _ppce;
  15. ppce.callSend(accounts[0], AMOUNT)
  16. })
  17. .then(function() {
  18. return ppce.payments(accounts[0]);
  19. })
  20. .then(function(paymentsToAccount0) {
  21. assert.equal(paymentsToAccount0, AMOUNT);
  22. })
  23. .then(done);
  24. });
  25. it("can add multiple balances on one account", function(done) {
  26. var ppce;
  27. return PullPaymentMock.new()
  28. .then(function(_ppce) {
  29. ppce = _ppce;
  30. return ppce.callSend(accounts[0], 200)
  31. })
  32. .then(function() {
  33. return ppce.callSend(accounts[0], 300)
  34. })
  35. .then(function() {
  36. return ppce.payments(accounts[0]);
  37. })
  38. .then(function(paymentsToAccount0) {
  39. assert.equal(paymentsToAccount0, 500);
  40. })
  41. .then(done);
  42. });
  43. it("can add balances on multiple accounts", function(done) {
  44. var ppce;
  45. return PullPaymentMock.new()
  46. .then(function(_ppce) {
  47. ppce = _ppce;
  48. return ppce.callSend(accounts[0], 200)
  49. })
  50. .then(function() {
  51. return ppce.callSend(accounts[1], 300)
  52. })
  53. .then(function() {
  54. return ppce.payments(accounts[0]);
  55. })
  56. .then(function(paymentsToAccount0) {
  57. assert.equal(paymentsToAccount0, 200);
  58. })
  59. .then(function() {
  60. return ppce.payments(accounts[1]);
  61. })
  62. .then(function(paymentsToAccount0) {
  63. assert.equal(paymentsToAccount0, 300);
  64. })
  65. .then(done);
  66. });
  67. it("can withdraw payment", function(done) {
  68. var ppce;
  69. var AMOUNT = 17*1e18;
  70. var payee = accounts[1];
  71. var initialBalance = web3.eth.getBalance(payee);
  72. return PullPaymentMock.new({value: AMOUNT})
  73. .then(function(_ppce) {
  74. ppce = _ppce;
  75. return ppce.callSend(payee, AMOUNT);
  76. })
  77. .then(function() {
  78. return ppce.payments(payee);
  79. })
  80. .then(function(paymentsToAccount0) {
  81. assert.equal(paymentsToAccount0, AMOUNT);
  82. })
  83. .then(function() {
  84. return ppce.withdrawPayments({from: payee});
  85. })
  86. .then(function() {
  87. return ppce.payments(payee);
  88. })
  89. .then(function(paymentsToAccount0) {
  90. assert.equal(paymentsToAccount0, 0);
  91. var balance = web3.eth.getBalance(payee);
  92. assert(Math.abs(balance-initialBalance-AMOUNT) < 1e16);
  93. })
  94. .then(done);
  95. });
  96. });