Skip to content

Commit

Permalink
feat: Create a 'publish' event
Browse files Browse the repository at this point in the history
feat: Add events module

feat: Enhance events module, usage of IntoKey trait

fix: Fix the "publish" event bug

ResourceLimitExceeded: Must put the wasm_hash instead of the wasm_binary in the event data
  • Loading branch information
asanson1404 committed Feb 5, 2024
1 parent 426f5d3 commit bed7d8c
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 16 deletions.
41 changes: 41 additions & 0 deletions contracts/smartdeploy/src/events.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use loam_sdk::soroban_sdk::{
self, contracttype, Env, Address, IntoVal, Val, String
};
use loam_sdk::IntoKey;
use crate::{
version::{ Version, Update },
metadata::ContractMetadata
};

#[contracttype]
#[derive(IntoKey)]
pub struct Publish {
pub published_name: String,
pub author: Address,
pub hash: soroban_sdk::BytesN<32>,
pub repo: ContractMetadata,
pub kind: Update,
}

#[contracttype]
#[derive(IntoKey)]
pub struct Deploy {
pub published_name: String,
pub deployed_name: String,
pub version: Version,
pub deployer: Address,
pub contract_id: Address,
}

pub trait EventPublishable {
/// Publish an event on the blockchain
fn publish_event(self, env: &Env);
}

impl<T> EventPublishable for T
where T: soroban_sdk::IntoKey + IntoVal<Env, Val>,
{
fn publish_event(self, env: &Env) {
env.events().publish((T::into_key(),), self);
}
}
1 change: 1 addition & 0 deletions contracts/smartdeploy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod metadata;
pub mod registry;
pub mod util;
pub mod version;
pub mod events;

use error::Error;
use version::Version;
Expand Down
13 changes: 3 additions & 10 deletions contracts/smartdeploy/src/registry/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
registry::Publishable,
util::{hash_string, MAX_BUMP},
version::Version,
events::{Deploy, EventPublishable},
Contract,
};

Expand All @@ -24,14 +25,6 @@ loam_sdk::import_contract!(core_riff);
// loam_sdk::soroban_sdk::contractimport!(file = "../../target/loam/core_riff.wasm",);
// }

#[contracttype]
pub struct DeployEventData {
published_name: String,
deployed_name: String,
version: Version,
deployer: Address,
contract_id: Address,
}

#[contracttype(export = false)]
pub struct ContractRegistry(pub Map<String, Address>);
Expand Down Expand Up @@ -96,14 +89,14 @@ impl IsDeployable for ContractRegistry {
},
Ok,
)?;
let deploy_datas = DeployEventData {
let deploy_data = Deploy {
published_name: contract_name,
deployed_name,
version,
deployer: owner,
contract_id: address.clone(),
};
env().events().publish((symbol_short!("deploy"),), deploy_datas);
deploy_data.publish_event(env());

Ok(address)
}
Expand Down
20 changes: 16 additions & 4 deletions contracts/smartdeploy/src/registry/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
metadata::{ContractMetadata, PublishedContract, PublishedWasm},
util::MAX_BUMP,
version::{self, Version, INITAL_VERSION},
events::{Publish, EventPublishable},
};

use super::IsPublishable;
Expand Down Expand Up @@ -77,13 +78,13 @@ impl IsPublishable for WasmRegistry {
) -> Result<(), Error> {
let mut contract = self
.find_contract(contract_name.clone())
.unwrap_or_else(|_| PublishedContract::new(author));
.unwrap_or_else(|_| PublishedContract::new(author.clone()));
contract.author.require_auth();
let keys = contract.versions.keys();
let last_version = keys.last().unwrap_or_default();

last_version.log();
let new_version = last_version.clone().update(&kind.unwrap_or_default());
let new_version = last_version.clone().update(&kind.clone().unwrap_or_default());
new_version.log();

let metadata = if let Some(repo) = repo {
Expand All @@ -94,9 +95,20 @@ impl IsPublishable for WasmRegistry {
contract.get(Some(last_version))?.metadata
};
let hash = env().deployer().upload_contract_wasm(wasm);
let published_binary = PublishedWasm { hash, metadata };
let published_binary = PublishedWasm { hash: hash.clone(), metadata: metadata.clone() };
contract.versions.set(new_version, published_binary);
self.set_contract(contract_name, contract);
self.set_contract(contract_name.clone(), contract);

// Publish a publish event
let publish_data = Publish {
published_name: contract_name,
author,
hash,
repo: metadata,
kind: kind.unwrap_or_default(),
};
publish_data.publish_event(env());

Ok(())
}

Expand Down
57 changes: 56 additions & 1 deletion contracts/smartdeploy/src/test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#![cfg(test)]
use super::*;
use crate::{error::Error, SorobanContract, SorobanContractClient};
use loam_sdk::soroban_sdk::{testutils::Address as _, Address, Bytes, Env, String};
use loam_sdk::soroban_sdk::{
testutils::{ Address as _, Events },
Address, Bytes, Env, String, IntoVal,
vec,
};
extern crate std;

// The contract that will be deployed by the Publisher contract.
Expand Down Expand Up @@ -52,6 +57,56 @@ fn handle_error_cases() {
// std::println!("{res:?}");
}

#[test]
fn publish_deploy_events() {

let (env, client, address) = &init();
env.mock_all_auths();

let published_name = String::from_str(env, "contract_a");

let bytes = Bytes::from_slice(env, contract::WASM);

client.publish(&published_name, address, &bytes, &None, &None);

let publish_data = events::Publish {
published_name: published_name.clone(),
author: address.clone(),
hash: env.deployer().upload_contract_wasm(bytes),
repo: metadata::ContractMetadata::default(),
kind: version::Update::default(),
};

let deployed_name = String::from_str(env, "deployed_contract_a");

let contract_id = client.deploy(&published_name, &Some(version::INITAL_VERSION), &deployed_name, address, &None, &None);

let deploy_data = events::Deploy {
published_name,
deployed_name,
version: version::INITAL_VERSION,
deployer: address.clone(),
contract_id,
};

assert_eq!(
env.events().all(),
vec![
&env,
(
client.address.clone(),
(String::from_str(env, "Publish"),).into_val(env),
publish_data.into_val(env)
),
(
client.address.clone(),
(String::from_str(env, "Deploy"),).into_val(env),
deploy_data.into_val(env)
),
]
);
}

// #[test]
// fn returns_most_recent_version() {
// let (env, client, address) = &init();
Expand Down
2 changes: 1 addition & 1 deletion contracts/smartdeploy/src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl Version {
}

#[contracttype]
#[derive(Default)]
#[derive(Default, Clone)]
pub enum Update {
#[default]
Patch,
Expand Down

0 comments on commit bed7d8c

Please sign in to comment.