You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

listtransactions.py 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/env python2
  2. # Copyright (c) 2014-2015 The Bitcoin Core developers
  3. # Distributed under the MIT software license, see the accompanying
  4. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. # Exercise the listtransactions API
  6. from test_framework.test_framework import BitcoinTestFramework
  7. from test_framework.util import *
  8. def check_array_result(object_array, to_match, expected):
  9. """
  10. Pass in array of JSON objects, a dictionary with key/value pairs
  11. to match against, and another dictionary with expected key/value
  12. pairs.
  13. """
  14. num_matched = 0
  15. for item in object_array:
  16. all_match = True
  17. for key,value in to_match.items():
  18. if item[key] != value:
  19. all_match = False
  20. if not all_match:
  21. continue
  22. for key,value in expected.items():
  23. if item[key] != value:
  24. raise AssertionError("%s : expected %s=%s"%(str(item), str(key), str(value)))
  25. num_matched = num_matched+1
  26. if num_matched == 0:
  27. raise AssertionError("No objects matched %s"%(str(to_match)))
  28. class ListTransactionsTest(BitcoinTestFramework):
  29. def run_test(self):
  30. # Simple send, 0 to 1:
  31. txid = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.1)
  32. self.sync_all()
  33. check_array_result(self.nodes[0].listtransactions(),
  34. {"txid":txid},
  35. {"category":"send","account":"","amount":Decimal("-0.1"),"confirmations":0})
  36. check_array_result(self.nodes[1].listtransactions(),
  37. {"txid":txid},
  38. {"category":"receive","account":"","amount":Decimal("0.1"),"confirmations":0})
  39. # mine a block, confirmations should change:
  40. self.nodes[0].generate(1)
  41. self.sync_all()
  42. check_array_result(self.nodes[0].listtransactions(),
  43. {"txid":txid},
  44. {"category":"send","account":"","amount":Decimal("-0.1"),"confirmations":1})
  45. check_array_result(self.nodes[1].listtransactions(),
  46. {"txid":txid},
  47. {"category":"receive","account":"","amount":Decimal("0.1"),"confirmations":1})
  48. # send-to-self:
  49. txid = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 0.2)
  50. check_array_result(self.nodes[0].listtransactions(),
  51. {"txid":txid, "category":"send"},
  52. {"amount":Decimal("-0.2")})
  53. check_array_result(self.nodes[0].listtransactions(),
  54. {"txid":txid, "category":"receive"},
  55. {"amount":Decimal("0.2")})
  56. # sendmany from node1: twice to self, twice to node2:
  57. send_to = { self.nodes[0].getnewaddress() : 0.11,
  58. self.nodes[1].getnewaddress() : 0.22,
  59. self.nodes[0].getaccountaddress("from1") : 0.33,
  60. self.nodes[1].getaccountaddress("toself") : 0.44 }
  61. txid = self.nodes[1].sendmany("", send_to)
  62. self.sync_all()
  63. check_array_result(self.nodes[1].listtransactions(),
  64. {"category":"send","amount":Decimal("-0.11")},
  65. {"txid":txid} )
  66. check_array_result(self.nodes[0].listtransactions(),
  67. {"category":"receive","amount":Decimal("0.11")},
  68. {"txid":txid} )
  69. check_array_result(self.nodes[1].listtransactions(),
  70. {"category":"send","amount":Decimal("-0.22")},
  71. {"txid":txid} )
  72. check_array_result(self.nodes[1].listtransactions(),
  73. {"category":"receive","amount":Decimal("0.22")},
  74. {"txid":txid} )
  75. check_array_result(self.nodes[1].listtransactions(),
  76. {"category":"send","amount":Decimal("-0.33")},
  77. {"txid":txid} )
  78. check_array_result(self.nodes[0].listtransactions(),
  79. {"category":"receive","amount":Decimal("0.33")},
  80. {"txid":txid, "account" : "from1"} )
  81. check_array_result(self.nodes[1].listtransactions(),
  82. {"category":"send","amount":Decimal("-0.44")},
  83. {"txid":txid, "account" : ""} )
  84. check_array_result(self.nodes[1].listtransactions(),
  85. {"category":"receive","amount":Decimal("0.44")},
  86. {"txid":txid, "account" : "toself"} )
  87. multisig = self.nodes[1].createmultisig(1, [self.nodes[1].getnewaddress()])
  88. self.nodes[0].importaddress(multisig["redeemScript"], "watchonly", False, True)
  89. txid = self.nodes[1].sendtoaddress(multisig["address"], 0.1)
  90. self.nodes[1].generate(1)
  91. self.sync_all()
  92. assert(len(self.nodes[0].listtransactions("watchonly", 100, 0, False)) == 0)
  93. check_array_result(self.nodes[0].listtransactions("watchonly", 100, 0, True),
  94. {"category":"receive","amount":Decimal("0.1")},
  95. {"txid":txid, "account" : "watchonly"} )
  96. if __name__ == '__main__':
  97. ListTransactionsTest().main()