Browse Source

Started writing the contract representation

master
Lev 2 years ago
parent
commit
18e393dde5
  1. 1
      agorata_contracts/Cargo.toml
  2. 10
      agorata_contracts/src/lib.rs
  3. 50
      agorata_contracts/src/message.rs

1
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"] }

10
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);
}
}

50
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<u8>) -> 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<u8> {
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<u8>,
/// Amount of TON coins in the message.
pub amount: i64,
/// Other currencies.
pub amounts: Vec<(String, i64)>,
}
Loading…
Cancel
Save