diff --git a/agorata_contracts/Cargo.toml b/agorata_contracts/Cargo.toml index 4ede10c..05642e4 100644 --- a/agorata_contracts/Cargo.toml +++ b/agorata_contracts/Cargo.toml @@ -6,3 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +serde = { version = "1.0", features = ["derive"] } diff --git a/agorata_contracts/src/lib.rs b/agorata_contracts/src/lib.rs index 1b4a90c..061bd89 100644 --- a/agorata_contracts/src/lib.rs +++ b/agorata_contracts/src/lib.rs @@ -1,8 +1,8 @@ +extern crate serde; + +pub mod message; + #[cfg(test)] mod tests { - #[test] - fn it_works() { - let result = 2 + 2; - assert_eq!(result, 4); - } + } diff --git a/agorata_contracts/src/message.rs b/agorata_contracts/src/message.rs new file mode 100644 index 0000000..8be9642 --- /dev/null +++ b/agorata_contracts/src/message.rs @@ -0,0 +1,50 @@ +use serde::{Deserialize, Serialize}; + +/// For now, only TON addresses are supported. +/// See [here](https://ton.org/docs/#/howto/step-by-step) for docs. +/// Later, multiple blockchains will be added. +/// Also, another networks might be added to TON as separate workchains, thus they will be supported automatically. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +pub struct Address { + /// Workchain id. + pub workchain: i32, + /// The address inside the workchain (64-512 bits depending on the workchain). + /// `address.0` is the length in bytes, the rest is the address. + pub address: (u8, [u8; 32], [u8; 32]), +} + +impl Address { + /// Creates a new address. + pub fn new(workchain: i32, address_vec: Vec) -> Self { + let address_len = address_vec.len(); + let address_bytes = address_vec.as_slice(); + Address { + workchain, + address: (address_len as u8, address_bytes[0..32].try_into().unwrap(), address_bytes[32..64].try_into().unwrap()), + } + } + + /// Get address in the workchain as bytes. + pub fn address_as_bytes(&self) -> Vec { + let mut address = self.address.1[..self.address.0.min(32) as usize].to_vec(); + address.extend_from_slice(&self.address.2[..(self.address.0 as i16 - 32).max(0) as usize]); + address + } +} + +/// 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. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct Message { + /// Sender address. + pub source: Address, + /// Destination address. + pub destination: Address, + /// Data of the message. + pub data: Vec, + /// Amount of TON coins in the message. + pub amount: i64, + /// Other currencies. + pub amounts: Vec<(String, i64)>, + +}