Skip to content

Commit

Permalink
Merge pull request #21 from casper-ecosystem/restructure
Browse files Browse the repository at this point in the history
Restructure project to avoid using casper-contract in tests
  • Loading branch information
Fraser999 authored Mar 4, 2024
2 parents ad46529 + 53e413c commit 5a89ba0
Show file tree
Hide file tree
Showing 28 changed files with 159 additions and 129 deletions.
5 changes: 0 additions & 5 deletions .travis.yml

This file was deleted.

7 changes: 0 additions & 7 deletions Cargo.toml

This file was deleted.

35 changes: 18 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ALL_CONTRACTS = cep85 cep85-test-contract
CONTRACT_TARGET_DIR = target/wasm32-unknown-unknown/release
PINNED_TOOLCHAIN := $(shell cat rust-toolchain)
CONTRACT_TARGET_DIR = contracts/target/wasm32-unknown-unknown/release
PINNED_TOOLCHAIN := $(shell cat contracts/rust-toolchain)

prepare:
rustup target add wasm32-unknown-unknown
Expand All @@ -9,10 +9,15 @@ prepare:

.PHONY: build-contract
build-contract:
cargo build --release --target wasm32-unknown-unknown $(patsubst %,-p %, $(ALL_CONTRACTS))
cd contracts/cep85 && cargo build --release
wasm-strip $(CONTRACT_TARGET_DIR)/cep85.wasm

.PHONY: build-all-contracts
build-all-contracts:
cd contracts && cargo build --release $(patsubst %,-p %, $(ALL_CONTRACTS))
$(foreach WASM, $(ALL_CONTRACTS), wasm-strip $(CONTRACT_TARGET_DIR)/$(subst -,_,$(WASM)).wasm ;)

setup-test: build-contract
setup-test: build-all-contracts
mkdir -p tests/wasm
cp $(CONTRACT_TARGET_DIR)/cep85.wasm tests/wasm
cp $(CONTRACT_TARGET_DIR)/cep85_test_contract.wasm tests/wasm
Expand All @@ -21,24 +26,20 @@ test: setup-test
cd tests && cargo test --release

clippy:
cd contract && cargo clippy --target wasm32-unknown-unknown --bins -- -D warnings
cd contract && cargo clippy --lib -- -D warnings
cd contract && cargo clippy --no-default-features --lib -- -D warnings
cd test-contract && cargo clippy --target wasm32-unknown-unknown -- -D warnings
cd contracts && cargo clippy --bins -- -D warnings
cd contracts && cargo clippy --lib -- -D warnings
cd contracts && cargo clippy --lib --no-default-features -- -D warnings
cd tests && cargo clippy --all-targets -- -D warnings

check-lint: clippy
cd contract && cargo fmt -- --check
cd test-contract && cargo fmt -- --check
cd tests && cargo fmt -- --check
cd contracts && cargo fmt -- --check
cd tests && cargo +$(PINNED_TOOLCHAIN) fmt -- --check

lint: clippy
cd contract && cargo fmt
cd test-contract && cargo fmt
cd tests && cargo fmt
format:
cd contracts && cargo fmt
cd tests && cargo +$(PINNED_TOOLCHAIN) fmt

clean:
cd contract && cargo clean
cd test-contract && cargo clean
cd contracts && cargo clean
cd tests && cargo clean
rm -rf tests/wasm
File renamed without changes.
17 changes: 17 additions & 0 deletions contracts/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[workspace]
members = ["cep85", "test"]
resolver = "2"

[workspace.package]
edition = "2021"
homepage = "https://casperlabs.io"
license-file = "../LICENSE"
readme = "../README.md"
repository = "https://github.com/casper-ecosystem/cep-85"

[workspace.dependencies]
casper-types = "3.0.0"

[profile.release]
codegen-units = 1
lto = true
13 changes: 6 additions & 7 deletions contract/Cargo.toml → contracts/cep85/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
[package]
name = "cep85"
version = "1.0.0"
edition = "2018"
description = "A library for developing CEP-85 tokens for the Casper network."
readme = "README.md"
documentation = "https://docs.rs/casper-cep-85"
homepage = "https://casperlabs.io"
repository = "https://github.com/casper-ecosystem/cep-85"
license-file = "../LICENSE"
edition.workspace = true
homepage.workspace = true
license-file.workspace = true
readme.workspace = true
repository.workspace = true

[dependencies]
casper-contract = { version = "3.0.0", optional = true }
casper-types = "3.0.0"
casper-types.workspace = true
casper-event-standard = { version = "0.4.0", default-features = false }
hex = { version = "0.4.3", default-features = false }

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 11 additions & 5 deletions contract/src/events.rs → contracts/cep85/src/events.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use crate::{
constants::ARG_EVENTS_MODE, modalities::EventsMode, security::SecurityBadge,
utils::get_stored_value,
};
use crate::security::SecurityBadge;
#[cfg(feature = "contract-support")]
use crate::{constants::ARG_EVENTS_MODE, modalities::EventsMode, utils::get_stored_value};
use alloc::{collections::BTreeMap, string::String, vec::Vec};
#[cfg(feature = "contract-support")]
use casper_contract::unwrap_or_revert::UnwrapOrRevert;
use casper_event_standard::{emit, Event, Schemas};
use casper_event_standard::Event;
#[cfg(feature = "contract-support")]
use casper_event_standard::{emit, Schemas};
use casper_types::{Key, U256};
#[cfg(feature = "contract-support")]
use core::convert::TryFrom;

#[derive(Debug)]
Expand All @@ -22,6 +25,7 @@ pub enum Event {
Upgrade(Upgrade),
}

#[cfg(feature = "contract-support")]
pub fn record_event_dictionary(event: Event) {
let events_mode: EventsMode =
EventsMode::try_from(get_stored_value::<u8>(ARG_EVENTS_MODE)).unwrap_or_revert();
Expand Down Expand Up @@ -178,6 +182,7 @@ impl Upgrade {
}
}

#[cfg(feature = "contract-support")]
fn ces(event: Event) {
match event {
Event::Mint(ev) => emit(ev),
Expand All @@ -193,6 +198,7 @@ fn ces(event: Event) {
}
}

#[cfg(feature = "contract-support")]
pub fn init_events() {
let events_mode =
EventsMode::try_from(get_stored_value::<u8>(ARG_EVENTS_MODE)).unwrap_or_revert();
Expand Down
9 changes: 3 additions & 6 deletions contract/src/lib.rs → contracts/cep85/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,16 @@ extern crate alloc;
pub mod constants;
pub mod entry_points;
pub mod error;
pub mod events;
pub mod modalities;
pub mod security;
pub mod utils;

#[cfg(feature = "contract-support")]
pub mod balances;
#[cfg(feature = "contract-support")]
pub mod events;
#[cfg(feature = "contract-support")]
pub mod operators;
#[cfg(feature = "contract-support")]
pub mod security;
#[cfg(feature = "contract-support")]
pub mod supply;
#[cfg(feature = "contract-support")]
pub mod uri;
#[cfg(feature = "contract-support")]
pub mod utils;
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 11 additions & 2 deletions contract/src/security.rs → contracts/cep85/src/security.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
use alloc::{collections::BTreeMap, vec, vec::Vec};
#[cfg(feature = "contract-support")]
use alloc::collections::BTreeMap;
use alloc::{vec, vec::Vec};
#[cfg(feature = "contract-support")]
use casper_contract::{contract_api::runtime::revert, unwrap_or_revert::UnwrapOrRevert};
#[cfg(feature = "contract-support")]
use casper_types::Key;
use casper_types::{
bytesrepr::{self, FromBytes, ToBytes},
CLTyped, Key,
CLTyped,
};

#[cfg(feature = "contract-support")]
use crate::{
constants::DICT_SECURITY_BADGES,
error::Cep85Error,
Expand Down Expand Up @@ -53,6 +59,7 @@ impl FromBytes for SecurityBadge {
}
}

#[cfg(feature = "contract-support")]
pub fn sec_check(allowed_badge_list: Vec<SecurityBadge>) {
let (caller, caller_package) = get_verified_caller();
let caller_badge = get_security_badge(&caller);
Expand All @@ -69,13 +76,15 @@ pub fn sec_check(allowed_badge_list: Vec<SecurityBadge>) {
revert(Cep85Error::InsufficientRights);
}

#[cfg(feature = "contract-support")]
fn get_security_badge(entity: &Key) -> Option<SecurityBadge> {
get_dictionary_value_from_key(
DICT_SECURITY_BADGES,
&hex::encode(entity.to_bytes().unwrap_or_revert()),
)
}

#[cfg(feature = "contract-support")]
pub fn change_sec_badge(badge_map: &BTreeMap<Key, SecurityBadge>) {
for (&user, &badge) in badge_map {
set_dictionary_value_for_key(
Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 5a89ba0

Please sign in to comment.