@ -1,5 +1,7 @@
use serde ::{ Deserialize , Serialize } ;
use agolang ::value ::Address ;
use agolang ::templates ::{ ComplexMatcher , Matcher } ;
use agolang ::value ::{ Address , Value } ;
use agolang ::value ::Type ;
/// The Message. For now, it's the TON Message. Later, it will be adapted to other blockchains.
/// See [here](https://github.com/ton-blockchain/ton/blob/master/crypto/block/block.tlb) for the full TON message schema (seizure warning) or [here](https://ton.org/docs/#/smart-contracts/messages) for a humane version.
@ -16,3 +18,57 @@ pub struct Message {
/// Other currencies.
pub amounts : Vec < ( String , i64 ) > ,
}
impl Message {
/// Generate a vec of values for checking it with a `ComplexMatcher`
pub fn to_vec_of_values ( & self ) -> Vec < Value > {
vec! [
Value ::Address ( self . source ) ,
Value ::Address ( self . destination ) ,
Value ::Data ( self . data . clone ( ) ) ,
Value ::Int ( self . amount ) ,
Value ::String ( self . amounts . first ( ) . unwrap_or ( & ( "" . to_string ( ) , 0 ) ) . 0. clone ( ) ) ,
Value ::Int ( self . amounts . first ( ) . unwrap_or ( & ( "" . to_string ( ) , 0 ) ) . 1 ) ,
]
}
}
/// A struct that represent a class of messages (or one message, whatever) for matching
///
/// For now, this can only match the first of amounts (todo)
#[ derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default) ]
pub struct MessageTemplate {
pub source : Matcher ,
pub destination : Matcher ,
pub data : Matcher ,
pub amount : Matcher ,
/// if `amounts` isn't empty, we check the name of the first token
pub token_name : Matcher ,
/// if `amounts` isn't empty, we check the value of the first token
pub token_value : Matcher ,
}
impl MessageTemplate {
/// Check the validity of the matcher
pub fn is_valid ( & self ) -> bool {
self . source . check_correctness_for_type ( Type ::Address )
& & self . destination . check_correctness_for_type ( Type ::Address )
& & self . data . check_correctness_for_type ( Type ::Data )
& & self . amount . check_correctness_for_type ( Type ::Int )
& & self . token_name . check_correctness_for_type ( Type ::String )
& & self . token_value . check_correctness_for_type ( Type ::Int )
}
/// Convert this to a `ComplexMatcher` that takes all the params
pub fn get_complex_matcher ( self ) -> ComplexMatcher {
ComplexMatcher ::Compose ( vec! [
ComplexMatcher ::Primitive ( self . source ) ,
ComplexMatcher ::Primitive ( self . destination ) ,
ComplexMatcher ::Primitive ( self . data ) ,
ComplexMatcher ::Primitive ( self . amount ) ,
ComplexMatcher ::Primitive ( self . token_name ) ,
ComplexMatcher ::Primitive ( self . token_value ) ,
] )
}
}