Skip to content

Commit

Permalink
Merge pull request #32 from airgap-it/release/0.2.0
Browse files Browse the repository at this point in the history
Release/0.2.0
  • Loading branch information
RomarQ authored Jun 5, 2023
2 parents c933fe5 + 1247d93 commit 0c1c6b9
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 80 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
branches:
- main
- develop

env:
CARGO_TERM_COLOR: always

Expand All @@ -25,7 +25,7 @@ jobs:
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
toolchain: nightly-2023-03-14
components: rustfmt, clippy
target: wasm32-unknown-unknown
override: true
Expand Down
Empty file added .rustfmt.toml
Empty file.
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tezos-contract/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tezos-contract"
version = "0.1.4"
version = "0.2.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
84 changes: 40 additions & 44 deletions tezos-contract/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ use tezos_operation::operations::{Entrypoint, Parameters, Transaction};
use tezos_rpc::{
client::TezosRpc,
http::Http,
models::{block::BlockId, contract::UnparsingMode},
models::{
block::BlockId,
contract::{ContractScript, UnparsingMode},
},
};

use crate::{utils::AnyAnnotationValue, Error, Result};
Expand Down Expand Up @@ -56,60 +59,36 @@ pub use self::{
/// let contract_address: ContractHash = "KT1J4CiyWPmtFPXAjpgBezM5hoVHXHNzWBHK".try_into().unwrap();
/// let contract = rpc.contract_at(contract_address, None).await.unwrap();
/// let ledger = contract.storage().big_maps().get_by_name("ledger").unwrap();
/// let balance: Nat = ledger.get_value(pair(vec![try_string("tz1YY1LvD6TFH4z74pvxPQXBjAKHE5tB5Q8f").unwrap(), 0u8.into()]), None).await.unwrap().try_into().unwrap();
/// let balance: Nat = ledger.get_value(&rpc, pair(vec![try_string("tz1YY1LvD6TFH4z74pvxPQXBjAKHE5tB5Q8f").unwrap(), 0u8.into()]), None).await.unwrap().try_into().unwrap();
/// }
///
/// ```
#[derive(Debug, Clone)]
pub struct Contract<'a, HttpClient: Http> {
pub struct Contract {
address: ContractHash,
storage: Storage<'a, HttpClient>,
client: &'a TezosRpc<HttpClient>,

storage: Storage,
entrypoints: MappedEntrypoints,
}

impl<'a, HttpClient: Http> Contract<'a, HttpClient> {
impl Contract {
pub fn address(&self) -> &ContractHash {
&self.address
}

pub fn storage(&self) -> &Storage<'a, HttpClient> {
pub fn storage(&self) -> &Storage {
&self.storage
}

pub fn client(&self) -> &'a TezosRpc<HttpClient> {
self.client
}

pub(crate) async fn new(
pub(crate) fn new(
address: ContractHash,
client: &'a TezosRpc<HttpClient>,
block_id: Option<&BlockId>,
) -> Result<Contract<'a, HttpClient>> {
let generic_address: Address = (&address).into();
let mut request = client
.get_contract_script(&generic_address)
.unparsing_mode(UnparsingMode::Optimized_legacy);
if let Some(block_id) = block_id {
request = request.block_id(block_id);
}
let script = request.send().await?;
let parameter: Parameter = script
.code
.values()
.iter()
.nth(0)
.ok_or(Error::InvalidContractScript)?
.clone()
.try_into()?;
let entrypoints = MappedEntrypoints::new(parameter)?;
return Ok(Contract {
script: ContractScript,
entrypoints: MappedEntrypoints,
) -> Result<Contract> {
Ok(Contract {
address,
storage: Storage::new(script, client)?,
client,
storage: Storage::new(script)?,
entrypoints,
});
})
}

pub fn call(
Expand Down Expand Up @@ -470,21 +449,38 @@ impl CompatibleWith<Type> for DataSome {
}

#[async_trait]
pub trait ContractFetcher<'a, HttpClient: Http + Sync> {
pub trait ContractFetcher {
async fn contract_at(
&'a self,
&self,
address: ContractHash,
block_id: Option<&BlockId>,
) -> Result<Contract<'a, HttpClient>>;
) -> Result<Contract>;
}

#[async_trait]
impl<'a, HttpClient: Http + Sync> ContractFetcher<'a, HttpClient> for TezosRpc<HttpClient> {
impl<HttpClient: Http + Sync> ContractFetcher for TezosRpc<HttpClient> {
async fn contract_at(
&'a self,
&self,
address: ContractHash,
block_id: Option<&BlockId>,
) -> Result<Contract<'a, HttpClient>> {
Contract::<'a, HttpClient>::new(address, self, block_id).await
) -> Result<Contract> {
let generic_address: Address = (&address).into();
let mut request = self
.get_contract_script(&generic_address)
.unparsing_mode(UnparsingMode::Optimized_legacy);
if let Some(block_id) = block_id {
request = request.block_id(block_id);
}
let script = request.send().await?;
let parameter: Parameter = script
.code
.values()
.iter()
.nth(0)
.ok_or(Error::InvalidContractScript)?
.clone()
.try_into()?;
let entrypoints = MappedEntrypoints::new(parameter)?;
Contract::new(address, script, entrypoints)
}
}
30 changes: 16 additions & 14 deletions tezos-contract/src/contract/big_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@ use tezos_rpc::{
use crate::{utils::AnyAnnotationValue, Result};

#[derive(Debug, Clone)]
pub struct BigMap<'a, HttpClient: Http> {
pub struct BigMap {
pub id: u32,
pub name: Option<String>,
pub key_type: Type,
pub value_type: Type,
client: &'a TezosRpc<HttpClient>,
}

impl<'a, HttpClient: Http> BigMap<'a, HttpClient> {
pub(crate) fn new(big_map_type: BigMapType, id: u32, client: &'a TezosRpc<HttpClient>) -> Self {
impl BigMap {
pub(crate) fn new(big_map_type: BigMapType, id: u32) -> Self {
let name: Option<String> = big_map_type
.metadata()
.any_annotation_value()
Expand All @@ -35,16 +34,19 @@ impl<'a, HttpClient: Http> BigMap<'a, HttpClient> {
name,
key_type: *big_map_type.key_type,
value_type: *big_map_type.value_type,
client,
}
}

pub async fn get_value(&self, key: Michelson, block_id: Option<&BlockId>) -> Result<Micheline> {
pub async fn get_value<HttpClient: Http>(
&self,
client: &TezosRpc<HttpClient>,
key: Michelson,
block_id: Option<&BlockId>,
) -> Result<Micheline> {
let packed_key = key.pack(Some(&self.key_type))?;
let hashed = Tezos::default().get_crypto().blake2b(&packed_key, 32)?;
let script_expr: ScriptExprHash = (&hashed).try_into()?;
let mut request = self
.client
let mut request = client
.get_big_map_value(self.id, &script_expr)
.unparsing_mode(UnparsingMode::Optimized_legacy);
if let Some(block_id) = block_id {
Expand All @@ -58,16 +60,16 @@ impl<'a, HttpClient: Http> BigMap<'a, HttpClient> {
}

#[derive(Debug, Clone)]
pub struct BigMapContainer<'a, HttpClient: Http> {
big_maps: Vec<BigMap<'a, HttpClient>>,
pub struct BigMapContainer {
big_maps: Vec<BigMap>,
}

impl<'a, HttpClient: Http> BigMapContainer<'a, HttpClient> {
pub fn new(big_maps: Vec<BigMap<'a, HttpClient>>) -> Self {
impl BigMapContainer {
pub fn new(big_maps: Vec<BigMap>) -> Self {
BigMapContainer { big_maps }
}

pub fn get_by_name(&self, name: &str) -> Option<&BigMap<'a, HttpClient>> {
pub fn get_by_name(&self, name: &str) -> Option<&BigMap> {
self.big_maps.iter().find(|big_map| {
big_map
.name
Expand All @@ -76,7 +78,7 @@ impl<'a, HttpClient: Http> BigMapContainer<'a, HttpClient> {
})
}

pub fn get_by_index(&self, index: usize) -> Option<&BigMap<'a, HttpClient>> {
pub fn get_by_index(&self, index: usize) -> Option<&BigMap> {
if index < self.big_maps.len() {
return Some(&self.big_maps[index]);
}
Expand Down
16 changes: 8 additions & 8 deletions tezos-contract/src/contract/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@ use tezos_michelson::{
},
MichelinePacker,
};
use tezos_rpc::{client::TezosRpc, http::Http, models::contract::ContractScript};
use tezos_rpc::models::contract::ContractScript;

use crate::{utils::AnyAnnotationValue, Error, Result};

use super::big_map::{BigMap, BigMapContainer};

#[derive(Debug, Clone)]
pub struct Storage<'a, HttpClient: Http> {
big_maps: BigMapContainer<'a, HttpClient>,
pub struct Storage {
big_maps: BigMapContainer,
mapped: MappedStorage,
}

impl<'a, HttpClient: Http> Storage<'a, HttpClient> {
pub fn big_maps(&self) -> &BigMapContainer<'a, HttpClient> {
impl Storage {
pub fn big_maps(&self) -> &BigMapContainer {
&self.big_maps
}

pub fn new(script: ContractScript, client: &'a TezosRpc<HttpClient>) -> Result<Self> {
pub fn new(script: ContractScript) -> Result<Self> {
let storage_type: TypeStorage = script
.code
.normalized()
Expand All @@ -37,9 +37,9 @@ impl<'a, HttpClient: Http> Storage<'a, HttpClient> {
.try_into()?;
let storage_type = *storage_type.r#type;
let storage_value: Data = script.storage.normalized().try_into()?;
let mut big_maps = Vec::<BigMap<'a, HttpClient>>::new();
let mut big_maps = Vec::<BigMap>::new();
let mapped = MappedStorage::new(storage_type, storage_value, |big_map_type, id| {
big_maps.push(BigMap::new(big_map_type, id, client));
big_maps.push(BigMap::new(big_map_type, id));
})?;
Ok(Self {
big_maps: BigMapContainer::new(big_maps),
Expand Down
3 changes: 2 additions & 1 deletion tezos-contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
//! let rpc = TezosRpc::new("https://testnet-tezos.giganode.io".into());
//! let contract = rpc.contract_at("KT1HNqxFJxnmUcX8wF915wxxaAAU4ixDwWQ7".try_into()?, None).await?;
//! let big_map = contract.storage().big_maps().get_by_name("ledger").unwrap();
//! let big_map_value = big_map.get_value(try_string("my_key").unwrap(), None).await?;
//! let big_map_value = big_map.get_value(&rpc, try_string("my_key").unwrap(), None).await?;
//! Ok(())
//! }
//! ```
Expand Down Expand Up @@ -267,6 +267,7 @@ mod test {

let balance: Nat = ledger
.get_value(
&rpc,
pair(vec![
try_string("tz1YY1LvD6TFH4z74pvxPQXBjAKHE5tB5Q8f")?,
0u8.into(),
Expand Down
2 changes: 1 addition & 1 deletion tezos-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tezos-core"
version = "0.1.4"
version = "0.2.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
2 changes: 1 addition & 1 deletion tezos-michelson/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tezos-michelson"
version = "0.1.4"
version = "0.2.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
2 changes: 1 addition & 1 deletion tezos-operation/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tezos-operation"
version = "0.1.4"
version = "0.2.0"
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion tezos-rpc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tezos-rpc"
version = "0.1.4"
version = "0.2.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
4 changes: 3 additions & 1 deletion tezos-rpc/src/models/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ pub enum OperationContent {
TxRollupRejection(TxRollupRejection),
TransferTicket(TransferTicket),
TxRollupDispatchTickets(TxRollupDispatchTickets),
Unknown(serde_json::Value), // must be the last one
}

impl From<tezos_operation::operations::OperationContent> for OperationContent {
Expand Down Expand Up @@ -215,7 +216,8 @@ impl TryFrom<OperationContent> for tezos_operation::operations::OperationContent
| OperationContent::TxRollupRemoveCommitment(_)
| OperationContent::TxRollupRejection(_)
| OperationContent::TransferTicket(_)
| OperationContent::TxRollupDispatchTickets(_) => Err(Error::OperationNotSupported),
| OperationContent::TxRollupDispatchTickets(_)
| OperationContent::Unknown(_) => Err(Error::OperationNotSupported),
}
}
}
Expand Down

0 comments on commit 0c1c6b9

Please sign in to comment.