api.rs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. use {
  2. crate::{
  3. ethereum::PythContract,
  4. state::HashChainState,
  5. },
  6. axum::{
  7. http::StatusCode,
  8. response::{
  9. IntoResponse,
  10. Response,
  11. },
  12. },
  13. ethers::core::types::Address,
  14. std::sync::Arc,
  15. };
  16. pub use {
  17. index::*,
  18. revelation::*,
  19. };
  20. mod index;
  21. mod revelation;
  22. /// The state of the randomness service for a single blockchain.
  23. #[derive(Clone)]
  24. pub struct ApiState {
  25. /// The hash chain(s) required to serve random numbers for this blockchain
  26. pub state: Arc<HashChainState>,
  27. /// The EVM contract where the protocol is running.
  28. pub contract: Arc<PythContract>,
  29. /// The EVM address of the provider that this server is operating for.
  30. pub provider_address: Address,
  31. }
  32. pub enum RestError {
  33. /// The caller passed a sequence number that isn't within the supported range
  34. InvalidSequenceNumber,
  35. /// The caller requested a random value that can't currently be revealed (because it
  36. /// hasn't been committed to on-chain)
  37. NoPendingRequest,
  38. /// The server cannot currently communicate with the blockchain, so is not able to verify
  39. /// which random values have been requested.
  40. TemporarilyUnavailable,
  41. /// A catch-all error for all other types of errors that could occur during processing.
  42. Unknown,
  43. }
  44. impl IntoResponse for RestError {
  45. fn into_response(self) -> Response {
  46. match self {
  47. RestError::InvalidSequenceNumber => (
  48. StatusCode::BAD_REQUEST,
  49. "The sequence number is out of the permitted range",
  50. )
  51. .into_response(),
  52. RestError::NoPendingRequest => (
  53. StatusCode::FORBIDDEN,
  54. "The random value cannot currently be retrieved",
  55. )
  56. .into_response(),
  57. RestError::TemporarilyUnavailable => (
  58. StatusCode::SERVICE_UNAVAILABLE,
  59. "This service is temporarily unavailable",
  60. )
  61. .into_response(),
  62. RestError::Unknown => (
  63. StatusCode::INTERNAL_SERVER_ERROR,
  64. "An unknown error occurred processing the request",
  65. )
  66. .into_response(),
  67. }
  68. }
  69. }