lib.rs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. use darkfi_serial::{deserialize, SerialDecodable, SerialEncodable};
  2. use darkfi_sdk::{
  3. crypto::Nullifier,
  4. entrypoint,
  5. error::{ContractError, ContractResult},
  6. msg,
  7. pasta::pallas,
  8. state::nullifier_exists,
  9. };
  10. // An example of deserializing the payload into a struct
  11. #[derive(SerialEncodable, SerialDecodable)]
  12. pub struct Args {
  13. pub a: u64,
  14. pub b: u64,
  15. }
  16. // This is the main entrypoint function where the payload is fed.
  17. // Through here, you can branch out into different functions inside
  18. // this library.
  19. entrypoint!(process_instruction);
  20. fn process_instruction(_state: &[u8], ix: &[u8]) -> ContractResult {
  21. // Deserialize the payload into `Args`.
  22. let args: Args = deserialize(ix)?;
  23. if args.a < args.b {
  24. // Returning custom errors
  25. return Err(ContractError::Custom(69))
  26. }
  27. let sum = args.a + args.b;
  28. // Publicly logged messages
  29. msg!("Hello from the VM runtime!");
  30. msg!("Sum: {:?}", sum);
  31. // Querying of ledger state available from the VM host
  32. let nf = Nullifier::from(pallas::Base::from(0x10));
  33. msg!("Contract Nullifier: {:?}", nf);
  34. if nullifier_exists(&nf)? {
  35. msg!("Nullifier exists");
  36. } else {
  37. msg!("Nullifier doesn't exist");
  38. }
  39. Ok(())
  40. }