Skip to content

Commit

Permalink
v1.1.5 : Merge develop to master (#226)
Browse files Browse the repository at this point in the history
Co-authored-by: Samuele Landi <samuele.landi@kryptotel.net>
  • Loading branch information
abhath-labs and samuelelandi committed Apr 27, 2023
1 parent ab98a1b commit 2ba88de
Show file tree
Hide file tree
Showing 29 changed files with 2,980 additions and 281 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ jobs:
target: wasm32-unknown-unknown

- name: Unit tests
run: cargo test -p bitgreen-parachain --features runtime-benchmarks
run: cargo test -p bitgreen-parachain

build-docker-image:
# The type of runner that the job will run on
Expand Down
28 changes: 24 additions & 4 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions pallets/carbon-credits-pool/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,11 @@ impl pallet_carbon_credits::Config for Test {
type ForceOrigin = frame_system::EnsureRoot<u64>;
type ItemId = u32;
type ProjectId = u32;
type MaxCoordinatesLength = ConstU32<8>;
type GroupId = u32;
type KYCProvider = KYCMembership;
type MarketplaceEscrow = MarketplaceEscrowAccount;
type MaxCoordinatesLength = ConstU32<8>;
type MaxAuthorizedAccountCount = ConstU32<2>;
type MaxDocumentCount = ConstU32<2>;
type MaxGroupSize = MaxGroupSize;
Expand Down
117 changes: 114 additions & 3 deletions pallets/carbon-credits/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ use sp_runtime::traits::{AccountIdConversion, CheckedAdd, CheckedSub, One, Zero}
use sp_std::{cmp, convert::TryInto, vec::Vec};

use crate::{
AssetIdLookup, AuthorizedAccounts, BatchRetireDataList, BatchRetireDataOf, Config, Error,
Event, NextAssetId, NextItemId, NextProjectId, Pallet, ProjectCreateParams, ProjectDetail,
Projects, RetiredCarbonCreditsData, RetiredCredits,
AssetIdLookup, AuthorizedAccounts, BatchGroupOf, BatchRetireDataList, BatchRetireDataOf,
Config, Error, Event, NextAssetId, NextItemId, NextProjectId, Pallet, ProjectCreateParams,
ProjectDetail, Projects, RetiredCarbonCreditsData, RetiredCredits,
};

impl<T: Config> Pallet<T> {
Expand Down Expand Up @@ -291,6 +291,117 @@ impl<T: Config> Pallet<T> {
})
}

/// Update a project that has already been approved, this function only allows the owner to
/// update certain fields of the project description, once approved the project cannot modify
/// the batch groups data.
pub fn update_project(
admin: T::AccountId,
project_id: T::ProjectId,
params: ProjectCreateParams<T>,
) -> DispatchResult {
let now = frame_system::Pallet::<T>::block_number();

Projects::<T>::try_mutate(project_id, |project| -> DispatchResult {
let project = project.as_mut().ok_or(Error::<T>::ProjectNotFound)?;

// non approved project needs to be resubmitted
ensure!(project.approved, Error::<T>::CannotUpdateUnapprovedProject);

// only originator can resubmit
ensure!(project.originator == admin, Error::<T>::NotAuthorised);

let new_project = ProjectDetail {
originator: admin,
name: params.name,
description: params.description,
location: params.location,
images: params.images,
videos: params.videos,
documents: params.documents,
registry_details: params.registry_details,
sdg_details: params.sdg_details,
royalties: params.royalties,
// we don't allow editing of the project batch data
batch_groups: project.batch_groups.clone(),
created: project.created,
updated: Some(now),
approved: project.approved,
};

*project = new_project;

// emit event
Self::deposit_event(Event::ProjectUpdated { project_id });

Ok(())
})
}

/// Add a new batch group to the project, this can only be done by the originator
pub fn do_add_batch_group(
admin: T::AccountId,
project_id: T::ProjectId,
mut batch_group: BatchGroupOf<T>,
) -> DispatchResult {
Projects::<T>::try_mutate(project_id, |project| -> DispatchResult {
let project = project.as_mut().ok_or(Error::<T>::ProjectNotFound)?;

// non approved project needs to be resubmitted
ensure!(project.approved, Error::<T>::CannotUpdateUnapprovedProject);

// only originator can resubmit
ensure!(project.originator == admin, Error::<T>::NotAuthorised);

let mut batch_group_map = project.batch_groups.clone();

let group_id: u32 = batch_group_map.len() as u32;

let mut group_total_supply: T::Balance = Zero::zero();

for batch in batch_group.batches.iter() {
ensure!(
batch.total_supply > Zero::zero(),
Error::<T>::CannotCreateProjectWithoutCredits
);

ensure!(
batch.minted == Zero::zero(),
Error::<T>::CannotCreateProjectWithoutCredits
);

ensure!(
batch.retired == Zero::zero(),
Error::<T>::CannotCreateProjectWithoutCredits
);

group_total_supply = group_total_supply
.checked_add(&batch.total_supply)
.ok_or(Error::<T>::Overflow)?;
}

ensure!(
group_total_supply > Zero::zero(),
Error::<T>::CannotCreateProjectWithoutCredits
);

// sort batch data in ascending order of issuance year
batch_group.batches.sort_by(|x, y| x.issuance_year.cmp(&y.issuance_year));
batch_group.total_supply = group_total_supply;

// insert the group to BTreeMap
batch_group_map
.try_insert(group_id.into(), batch_group.clone())
.map_err(|_| Error::<T>::TooManyGroups)?;

project.batch_groups = batch_group_map;

// emit event
Self::deposit_event(Event::BatchGroupAdded { project_id, group_id: group_id.into() });

Ok(())
})
}

pub fn mint_carbon_credits(
_sender: T::AccountId,
project_id: T::ProjectId,
Expand Down
42 changes: 42 additions & 0 deletions pallets/carbon-credits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,18 @@ pub mod pallet {
/// Details of the retired token
retire_data: BatchRetireDataList<T>,
},
/// A project details has been updated
ProjectUpdated {
/// The ProjectId of the updated project
project_id: T::ProjectId,
},
/// A new batch group was added to the project
BatchGroupAdded {
/// The ProjectId of the updated project
project_id: T::ProjectId,
/// GroupId of the new batch group
group_id: T::GroupId,
},
}

// Errors inform users that something went wrong.
Expand Down Expand Up @@ -340,6 +352,8 @@ pub mod pallet {
TooManyGroups,
/// the group does not exist
GroupNotFound,
/// Can only update an approved project, use resubmit for rejected projects
CannotUpdateUnapprovedProject,
}

#[pallet::call]
Expand Down Expand Up @@ -572,6 +586,34 @@ pub mod pallet {
Projects::<T>::take(project_id);
Ok(())
}

/// Modify the details of an approved project
/// Can only be called by the ProjectOwner
#[transactional]
#[pallet::weight(T::WeightInfo::create())]
pub fn update_project_details(
origin: OriginFor<T>,
project_id: T::ProjectId,
params: ProjectCreateParams<T>,
) -> DispatchResult {
let sender = ensure_signed(origin)?;
Self::check_kyc_approval(&sender)?;
Self::update_project(sender, project_id, params)
}

/// Add a new batch group to the project
/// Can only be called by the ProjectOwner
#[transactional]
#[pallet::weight(T::WeightInfo::create())]
pub fn add_batch_group(
origin: OriginFor<T>,
project_id: T::ProjectId,
batch_group: BatchGroupOf<T>,
) -> DispatchResult {
let sender = ensure_signed(origin)?;
Self::check_kyc_approval(&sender)?;
Self::do_add_batch_group(sender, project_id, batch_group)
}
}
}

Expand Down
Loading

0 comments on commit 2ba88de

Please sign in to comment.