asserts.rs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // SPDX-License-Identifier: Apache-2.0
  2. use crate::{node, Contract, WriteContract};
  3. use contract_transcode::ContractMessageTranscoder;
  4. use hex::FromHex;
  5. use parity_scale_codec::{Decode, Encode};
  6. use sp_core::hexdisplay::AsBytesRef;
  7. use subxt::error::MetadataError;
  8. use crate::API;
  9. #[tokio::test]
  10. async fn case() -> anyhow::Result<()> {
  11. let api = API::new().await?;
  12. let mut contract = Contract::new("./contracts/asserts.contract")?;
  13. contract
  14. .deploy(
  15. &api,
  16. sp_keyring::AccountKeyring::Alice,
  17. 0,
  18. &|t: &ContractMessageTranscoder| t.encode::<_, String>("new", []).unwrap(),
  19. )
  20. .await?;
  21. let rv = contract
  22. .try_call(
  23. &api,
  24. sp_keyring::AccountKeyring::Alice,
  25. 0,
  26. &|t: &ContractMessageTranscoder| t.encode::<_, String>("var", []).unwrap(),
  27. )
  28. .await?;
  29. let output = i64::decode(&mut rv.as_bytes_ref())?;
  30. assert!(output == 1);
  31. // read should fail
  32. let res = contract
  33. .try_call(
  34. &api,
  35. sp_keyring::AccountKeyring::Alice,
  36. 0,
  37. &|t: &ContractMessageTranscoder| t.encode::<_, String>("test_assert_rpc", []).unwrap(),
  38. )
  39. .await;
  40. assert!(res.unwrap_err().to_string().contains("I refuse"));
  41. // write should failed
  42. let res = contract
  43. .call(
  44. &api,
  45. sp_keyring::AccountKeyring::Alice,
  46. 0,
  47. &|t: &ContractMessageTranscoder| t.encode::<_, String>("test_assert_rpc", []).unwrap(),
  48. )
  49. .await
  50. .unwrap_err();
  51. assert!(res
  52. .to_string()
  53. .contains("ModuleError { index: 8, error: [26, 0, 0, 0] }"));
  54. // state should not change after failed operation
  55. let rv = contract
  56. .try_call(
  57. &api,
  58. sp_keyring::AccountKeyring::Alice,
  59. 0,
  60. &|t: &ContractMessageTranscoder| t.encode::<_, String>("var", []).unwrap(),
  61. )
  62. .await?;
  63. let output = i64::decode(&mut rv.as_bytes_ref())?;
  64. assert!(output == 1);
  65. Ok(())
  66. }