test_tmpl_sig.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. from algosdk.encoding import decode_address
  2. import pytest
  3. from algosdk.future import transaction
  4. from algosdk.logic import get_application_address
  5. from algosdk.error import AlgodHTTPError
  6. @pytest.fixture(scope='module')
  7. def correct_app_id(portal_core, client, creator):
  8. return portal_core.createTestApp(client,creator)
  9. @pytest.fixture(scope='module')
  10. def incorrect_app_id(portal_core, client, creator):
  11. return portal_core.createTestApp(client,creator)
  12. @pytest.fixture(scope='module')
  13. def tmpl_lsig(portal_core, client, correct_app_id, creator, suggested_params):
  14. appAddress = get_application_address(correct_app_id)
  15. tsig = portal_core.tsig
  16. lsig = tsig.populate(
  17. {
  18. "TMPL_APP_ID": correct_app_id,
  19. "TMPL_APP_ADDRESS": decode_address(appAddress).hex(),
  20. "TMPL_ADDR_IDX": 0,
  21. "TMPL_EMITTER_ID": b"emitter".hex(),
  22. }
  23. )
  24. txn = transaction.PaymentTxn(
  25. sender=creator.getAddress(),
  26. receiver=lsig.address(),
  27. amt=1000000,
  28. sp=suggested_params,
  29. )
  30. signedTxn = txn.sign(creator.getPrivateKey())
  31. client.send_transaction(signedTxn)
  32. portal_core.waitForTransaction(client, signedTxn.get_txid())
  33. return lsig
  34. def tests_rejection_on_payment(client, portal_core, tmpl_lsig, creator, suggested_params):
  35. with pytest.raises(AlgodHTTPError):
  36. feePayment = transaction.PaymentTxn(
  37. sender=creator.getAddress(),
  38. receiver=creator.getAddress(),
  39. amt=0,
  40. sp=suggested_params
  41. )
  42. feePayment.fee = 2 * feePayment.fee
  43. payment = transaction.PaymentTxn(
  44. sender=tmpl_lsig.address(),
  45. receiver=tmpl_lsig.address(),
  46. amt=0,
  47. sp=suggested_params
  48. )
  49. payment.fee = 0
  50. transaction.assign_group_id([feePayment, payment])
  51. signedFeePayment = feePayment.sign(creator.getPrivateKey())
  52. signedPayment = transaction.LogicSigTransaction(lsig=tmpl_lsig, transaction=payment)
  53. client.send_transactions([signedFeePayment, signedPayment])
  54. portal_core.waitForTransaction(client, signedPayment.get_txid())
  55. def tests_rejection_on_asset_transfer(client, portal_core, tmpl_lsig, creator, suggested_params):
  56. with pytest.raises(AlgodHTTPError):
  57. fee_payment = transaction.PaymentTxn(
  58. sender=creator.getAddress(),
  59. receiver=creator.getAddress(),
  60. amt=0,
  61. sp=suggested_params
  62. )
  63. fee_payment.fee = 2 * fee_payment.fee
  64. asset_transfer = transaction.AssetTransferTxn(
  65. index=1,
  66. sender=tmpl_lsig.address(),
  67. receiver=tmpl_lsig.address(),
  68. amt=0,
  69. sp=suggested_params
  70. )
  71. asset_transfer.fee = 0
  72. transaction.assign_group_id([fee_payment, asset_transfer])
  73. signedFeePayment = fee_payment.sign(creator.getPrivateKey())
  74. signedAssetTransfer = transaction.LogicSigTransaction(lsig=tmpl_lsig, transaction=asset_transfer)
  75. client.send_transactions([signedFeePayment, signedAssetTransfer])
  76. portal_core.waitForTransaction(client, signedAssetTransfer.get_txid())
  77. def tests_rejection_on_nop(client, portal_core, tmpl_lsig, correct_app_id, creator, suggested_params):
  78. with pytest.raises(AlgodHTTPError):
  79. fee_payment = transaction.PaymentTxn(
  80. sender=creator.getAddress(),
  81. receiver=creator.getAddress(),
  82. amt=0,
  83. sp=suggested_params
  84. )
  85. fee_payment.fee = 2 * fee_payment.fee
  86. noop = transaction.ApplicationCallTxn(
  87. index=correct_app_id,
  88. sender=tmpl_lsig.address(),
  89. sp=suggested_params,
  90. on_complete=transaction.OnComplete.NoOpOC
  91. )
  92. noop.fee = 0
  93. transaction.assign_group_id([fee_payment, noop])
  94. signedFeePayment = fee_payment.sign(creator.getPrivateKey())
  95. signedNoop = transaction.LogicSigTransaction(lsig=tmpl_lsig, transaction=noop)
  96. client.send_transactions([signedFeePayment, signedNoop])
  97. portal_core.waitForTransaction(client, signedNoop.get_txid())
  98. def tests_rejection_on_opt_in_to_incorrect_app(client, portal_core, tmpl_lsig, incorrect_app_id, creator, suggested_params):
  99. with pytest.raises(AlgodHTTPError):
  100. fee_payment = transaction.PaymentTxn(
  101. sender=creator.getAddress(),
  102. receiver=creator.getAddress(),
  103. amt=0,
  104. sp=suggested_params
  105. )
  106. fee_payment.fee = 2*fee_payment.fee
  107. opt_in = transaction.ApplicationCallTxn(
  108. index=incorrect_app_id,
  109. sender=tmpl_lsig.address(),
  110. sp=suggested_params,
  111. on_complete=transaction.OnComplete.OptInOC
  112. )
  113. opt_in.fee = 0
  114. transaction.assign_group_id([fee_payment, opt_in])
  115. signedFeePayment = fee_payment.sign(creator.getPrivateKey())
  116. signedOptIn = transaction.LogicSigTransaction(lsig=tmpl_lsig, transaction=opt_in)
  117. client.send_transactions([signedFeePayment, signedOptIn])
  118. portal_core.waitForTransaction(client, signedOptIn.get_txid())
  119. def tests_rejection_on_opt_in_to_correct_app_without_rekeying(client, portal_core, tmpl_lsig, correct_app_id, creator, suggested_params):
  120. with pytest.raises(AlgodHTTPError):
  121. fee_payment = transaction.PaymentTxn(
  122. sender=creator.getAddress(),
  123. receiver=creator.getAddress(),
  124. amt=0,
  125. sp=suggested_params
  126. )
  127. fee_payment.fee = 2*fee_payment.fee
  128. opt_in = transaction.ApplicationCallTxn(
  129. index=correct_app_id,
  130. sender=tmpl_lsig.address(),
  131. sp=suggested_params,
  132. on_complete=transaction.OnComplete.OptInOC
  133. )
  134. opt_in.fee = 0
  135. transaction.assign_group_id([fee_payment, opt_in])
  136. signedFeePayment = fee_payment.sign(creator.getPrivateKey())
  137. signedOptIn = transaction.LogicSigTransaction(lsig=tmpl_lsig, transaction=opt_in)
  138. client.send_transactions([signedFeePayment, signedOptIn])
  139. portal_core.waitForTransaction(client, signedOptIn.get_txid())
  140. def tests_rejection_on_opt_in_to_correct_app_with_rekeying_with_non_zero_fee(client, portal_core, tmpl_lsig, correct_app_id, suggested_params):
  141. with pytest.raises(AlgodHTTPError):
  142. txn = transaction.ApplicationCallTxn(
  143. index=correct_app_id,
  144. sender=tmpl_lsig.address(),
  145. sp=suggested_params,
  146. rekey_to=get_application_address(correct_app_id),
  147. on_complete=transaction.OnComplete.NoOpOC
  148. )
  149. signedTxn = transaction.LogicSigTransaction(lsig=tmpl_lsig, transaction=txn)
  150. client.send_transaction(signedTxn)
  151. portal_core.waitForTransaction(client, signedTxn.get_txid())
  152. def tests_success(client, portal_core, creator, tmpl_lsig, correct_app_id, suggested_params):
  153. fee_payment = transaction.PaymentTxn(
  154. sender=creator.getAddress(),
  155. receiver=creator.getAddress(),
  156. amt=0,
  157. sp=suggested_params
  158. )
  159. fee_payment.fee = 2*fee_payment.fee
  160. opt_in = transaction.ApplicationCallTxn(
  161. index=correct_app_id,
  162. sender=tmpl_lsig.address(),
  163. sp=suggested_params,
  164. rekey_to=get_application_address(correct_app_id),
  165. on_complete=transaction.OnComplete.OptInOC
  166. )
  167. opt_in.fee = 0
  168. transaction.assign_group_id([fee_payment, opt_in])
  169. signedFeePayment = fee_payment.sign(creator.getPrivateKey())
  170. signedOptIn = transaction.LogicSigTransaction(lsig=tmpl_lsig, transaction=opt_in)
  171. client.send_transactions([signedFeePayment, signedOptIn])
  172. portal_core.waitForTransaction(client, signedOptIn.get_txid())