diff --git a/crates/common/src/add_order.rs b/crates/common/src/add_order.rs index e6be13953..4956ed7e8 100644 --- a/crates/common/src/add_order.rs +++ b/crates/common/src/add_order.rs @@ -4,7 +4,7 @@ use alloy_ethers_typecast::transaction::{ WriteTransactionStatus, }; use alloy_primitives::{hex::FromHexError, Address, U256}; -use dotrain::{RainDocument, Store}; +use dotrain::{ComposeError, RainDocument, Store}; use rain_interpreter_dispair::{DISPair, DISPairError}; use rain_interpreter_parser::{Parser, ParserError, ParserV1}; use rain_meta::{ @@ -17,6 +17,8 @@ use std::sync::{Arc, RwLock}; use strict_yaml_rust::{scanner::ScanError, StrictYaml, StrictYamlLoader}; use thiserror::Error; +static REQUIRED_DOTRAIN_BODY_ENTRYPOINTS: [&str; 2] = ["calculate-io", "handle-io"]; + #[derive(Error, Debug)] pub enum AddOrderArgsError { #[error("frontmatter is not valid strict yaml: {0}")] @@ -39,11 +41,13 @@ pub enum AddOrderArgsError { TransactionArgs(#[from] TransactionArgsError), #[error(transparent)] RainMetaError(#[from] RainMetaError), + #[error(transparent)] + ComposeError(#[from] ComposeError), } pub struct AddOrderArgs { - /// Body of a Dotrain file describing an addOrder call - /// File should have [strict yaml] frontmatter of the following structure + /// Text of a dotrain file describing an addOrder call + /// Text MUST have strict yaml frontmatter of the following structure /// /// ```yaml /// orderbook: @@ -58,6 +62,9 @@ pub struct AddOrderArgs { /// decimals: 8 /// vaultId: 0x5678 /// ``` + /// + /// Text MUST have valid dotrain body succeding frontmatter. + /// The dotrain body must contain two entrypoints: `calulate-io` and `handle-io`. pub dotrain: String, } @@ -157,7 +164,7 @@ impl AddOrderArgs { &self, rpc_url: String, deployer: Address, - rainlang: &str, + rainlang: String, ) -> Result<(Vec, Vec), AddOrderArgsError> { let client = ReadableClientHttp::new_from_url(rpc_url) .map_err(AddOrderArgsError::ReadableClientError)?; @@ -167,7 +174,7 @@ impl AddOrderArgs { let parser: ParserV1 = dispair.clone().into(); let rainlang_parsed = parser - .parse_text(rainlang, client) + .parse_text(rainlang.as_str(), client) .await .map_err(AddOrderArgsError::ParserError)?; @@ -175,7 +182,7 @@ impl AddOrderArgs { } /// Generate RainlangSource meta - fn try_generate_meta(&self, rainlang: &str) -> Result, AddOrderArgsError> { + fn try_generate_meta(&self, rainlang: String) -> Result, AddOrderArgsError> { let meta_doc = RainMetaDocumentV1Item { payload: ByteBuf::from(rainlang.as_bytes()), magic: KnownMagic::RainlangSourceV1, @@ -196,15 +203,16 @@ impl AddOrderArgs { async fn try_into_call(&self, rpc_url: String) -> Result { // Parse file into dotrain document let meta_store = Arc::new(RwLock::new(Store::default())); - let raindoc = RainDocument::create(self.dotrain.clone(), Some(meta_store), None); + let dotrain_doc = RainDocument::create(self.dotrain.clone(), Some(meta_store), None); + let rainlang = dotrain_doc.compose(&REQUIRED_DOTRAIN_BODY_ENTRYPOINTS)?; // Prepare call let (deployer, valid_inputs, valid_outputs) = - self.try_parse_frontmatter(raindoc.front_matter().as_str())?; + self.try_parse_frontmatter(dotrain_doc.front_matter().as_str())?; let (bytecode, constants) = self - .try_parse_rainlang(rpc_url, deployer, raindoc.body()) + .try_parse_rainlang(rpc_url, deployer, rainlang.clone()) .await?; - let meta = self.try_generate_meta(raindoc.body())?; + let meta = self.try_generate_meta(rainlang)?; Ok(addOrderCall { config: OrderConfigV2 { @@ -296,26 +304,31 @@ orderbook: #[test] fn test_try_generate_meta() { - let rainlang = " + let dotrain_body = String::from( + " +#calculate-io max-amount: 100e18, price: 2e18; +#handle-io max-amount: 100e18, price: 2e18; -"; +", + ); let args = AddOrderArgs { dotrain: "".into() }; - let meta_bytes = args.try_generate_meta(rainlang).unwrap(); + let meta_bytes = args.try_generate_meta(dotrain_body).unwrap(); assert_eq!( meta_bytes, vec![ - 255, 10, 137, 198, 116, 238, 120, 116, 163, 0, 88, 68, 10, 109, 97, 120, 45, 97, - 109, 111, 117, 110, 116, 58, 32, 49, 48, 48, 101, 49, 56, 44, 10, 112, 114, 105, - 99, 101, 58, 32, 50, 101, 49, 56, 59, 10, 10, 109, 97, 120, 45, 97, 109, 111, 117, - 110, 116, 58, 32, 49, 48, 48, 101, 49, 56, 44, 10, 112, 114, 105, 99, 101, 58, 32, - 50, 101, 49, 56, 59, 10, 1, 27, 255, 19, 16, 158, 65, 51, 111, 242, 2, 120, 24, 97, - 112, 112, 108, 105, 99, 97, 116, 105, 111, 110, 47, 111, 99, 116, 101, 116, 45, - 115, 116, 114, 101, 97, 109 + 255, 10, 137, 198, 116, 238, 120, 116, 163, 0, 88, 93, 10, 35, 99, 97, 108, 99, + 117, 108, 97, 116, 101, 45, 105, 111, 10, 109, 97, 120, 45, 97, 109, 111, 117, 110, + 116, 58, 32, 49, 48, 48, 101, 49, 56, 44, 10, 112, 114, 105, 99, 101, 58, 32, 50, + 101, 49, 56, 59, 10, 10, 35, 104, 97, 110, 100, 108, 101, 45, 105, 111, 10, 109, + 97, 120, 45, 97, 109, 111, 117, 110, 116, 58, 32, 49, 48, 48, 101, 49, 56, 44, 10, + 112, 114, 105, 99, 101, 58, 32, 50, 101, 49, 56, 59, 10, 1, 27, 255, 19, 16, 158, + 65, 51, 111, 242, 2, 120, 24, 97, 112, 112, 108, 105, 99, 97, 116, 105, 111, 110, + 47, 111, 99, 116, 101, 116, 45, 115, 116, 114, 101, 97, 109 ] ); }