Skip to content

Commit

Permalink
Merge pull request #166 from rainlanguage/2024-01-31-fix-compose-rain…
Browse files Browse the repository at this point in the history
…docbody

fix(common): compose rainlang with required entrypoints
  • Loading branch information
thedavidmeister authored Feb 2, 2024
2 parents 28ec407 + c2529f2 commit acca44e
Showing 1 changed file with 33 additions and 20 deletions.
53 changes: 33 additions & 20 deletions crates/common/src/add_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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}")]
Expand All @@ -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:
Expand All @@ -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,
}

Expand Down Expand Up @@ -157,7 +164,7 @@ impl AddOrderArgs {
&self,
rpc_url: String,
deployer: Address,
rainlang: &str,
rainlang: String,
) -> Result<(Vec<u8>, Vec<U256>), AddOrderArgsError> {
let client = ReadableClientHttp::new_from_url(rpc_url)
.map_err(AddOrderArgsError::ReadableClientError)?;
Expand All @@ -167,15 +174,15 @@ 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)?;

Ok((rainlang_parsed.bytecode, rainlang_parsed.constants))
}

/// Generate RainlangSource meta
fn try_generate_meta(&self, rainlang: &str) -> Result<Vec<u8>, AddOrderArgsError> {
fn try_generate_meta(&self, rainlang: String) -> Result<Vec<u8>, AddOrderArgsError> {
let meta_doc = RainMetaDocumentV1Item {
payload: ByteBuf::from(rainlang.as_bytes()),
magic: KnownMagic::RainlangSourceV1,
Expand All @@ -196,15 +203,16 @@ impl AddOrderArgs {
async fn try_into_call(&self, rpc_url: String) -> Result<addOrderCall, AddOrderArgsError> {
// 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 {
Expand Down Expand Up @@ -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
]
);
}
Expand Down

0 comments on commit acca44e

Please sign in to comment.