Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix legacy maps #159

Merged
merged 3 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/components/minter/mint.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
mod MintComponent {
// Starknet imports

use openzeppelin::token::erc20::interface::ERC20ABIDispatcherTrait;
use starknet::ContractAddress;
use starknet::{get_caller_address, get_contract_address};
use starknet::storage::{
StoragePointerReadAccess, StoragePointerWriteAccess, StoragePathEntry, Map
};

// External imports
use openzeppelin::token::erc20::interface::{IERC20Dispatcher, IERC20DispatcherTrait};
use openzeppelin::token::erc20::interface::{ERC20ABIDispatcher, ERC20ABIDispatcherTrait};
use openzeppelin::token::erc1155::interface::{IERC1155Dispatcher, IERC1155DispatcherTrait};

// Internal imports
Expand All @@ -33,7 +35,7 @@ mod MintComponent {
Mint_public_sale_open: bool,
Mint_min_money_amount_per_tx: u256,
Mint_unit_price: u256,
Mint_claimed_value: LegacyMap::<ContractAddress, u256>,
Mint_claimed_value: Map<ContractAddress, u256>,
Mint_max_mintable_cc: u256,
Mint_remaining_mintable_cc: u256,
Mint_cancel: bool,
Expand Down Expand Up @@ -270,7 +272,7 @@ mod MintComponent {
assert(isOwner, 'Caller is not the owner');

let token_address = self.Mint_payment_token_address.read();
let erc20 = IERC20Dispatcher { contract_address: token_address };
let erc20 = ERC20ABIDispatcher { contract_address: token_address };
let contract_address = get_contract_address();
let balance = erc20.balance_of(contract_address);

Expand All @@ -294,7 +296,7 @@ mod MintComponent {
let isOwner = project.only_owner(caller_address);
assert(isOwner, 'Caller is not the owner');

let erc20 = IERC20Dispatcher { contract_address: token_address };
let erc20 = ERC20ABIDispatcher { contract_address: token_address };
let success = erc20.transfer(recipient, amount);
assert(success, 'Transfer failed');

Expand Down Expand Up @@ -341,7 +343,7 @@ mod MintComponent {
let money_amount = total_cc_balance * unit_price / MULTIPLIER_TONS_TO_MGRAMS;

let token_address = self.Mint_payment_token_address.read();
let erc20 = IERC20Dispatcher { contract_address: token_address };
let erc20 = ERC20ABIDispatcher { contract_address: token_address };
let success = erc20.transfer(caller_address, money_amount);
assert(success, 'Transfer failed');

Expand Down Expand Up @@ -442,7 +444,7 @@ mod MintComponent {
};
// [Interaction] Pay
let token_address = self.Mint_payment_token_address.read();
let erc20 = IERC20Dispatcher { contract_address: token_address };
let erc20 = ERC20ABIDispatcher { contract_address: token_address };
let minter_address = get_contract_address();

let success = erc20.transfer_from(caller_address, minter_address, money_amount);
Expand Down
43 changes: 25 additions & 18 deletions src/components/offsetter/offset_handler.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ mod OffsetComponent {
// Starknet imports

use starknet::{ContractAddress, get_caller_address, get_contract_address};
use starknet::storage::{
StoragePointerReadAccess, StoragePointerWriteAccess, StoragePathEntry, Map
};

// Internal imports

Expand Down Expand Up @@ -39,11 +42,11 @@ mod OffsetComponent {
#[storage]
struct Storage {
Offsetter_carbonable_project_address: ContractAddress,
Offsetter_carbon_pending_retirement: LegacyMap<(u256, ContractAddress), u256>,
Offsetter_carbon_retired: LegacyMap<(u256, ContractAddress), u256>,
Offsetter_carbon_pending_retirement: Map<(u256, ContractAddress), u256>,
Offsetter_carbon_retired: Map<(u256, ContractAddress), u256>,
Offsetter_merkle_root: felt252,
Offsetter_allocations_claimed: LegacyMap<Allocation, bool>,
Offsetter_allocation_id: LegacyMap<ContractAddress, u256>
Offsetter_allocations_claimed: Map<Allocation, bool>,
Offsetter_allocation_id: Map<ContractAddress, u256>
}

#[event]
Expand Down Expand Up @@ -210,7 +213,7 @@ mod OffsetComponent {
let allocation = Allocation {
claimee: claimee, amount: amount, timestamp: timestamp, id: id
};
self.Offsetter_allocations_claimed.write(allocation, true);
self.Offsetter_allocations_claimed.entry(allocation).write(true);

// [Emit event]
self
Expand All @@ -222,26 +225,26 @@ mod OffsetComponent {
}

fn get_allocation_id(self: @ComponentState<TContractState>, from: ContractAddress) -> u256 {
self.Offsetter_allocation_id.read(from)
self.Offsetter_allocation_id.entry(from).read()
}

fn get_retirement(
self: @ComponentState<TContractState>, token_id: u256, from: ContractAddress
) -> u256 {
self.Offsetter_carbon_retired.read((token_id, from))
self.Offsetter_carbon_retired.entry((token_id, from)).read()
}


fn get_pending_retirement(
self: @ComponentState<TContractState>, address: ContractAddress, token_id: u256
) -> u256 {
self.Offsetter_carbon_pending_retirement.read((token_id, address))
self.Offsetter_carbon_pending_retirement.entry((token_id, address)).read()
}

fn get_carbon_retired(
self: @ComponentState<TContractState>, address: ContractAddress, token_id: u256
) -> u256 {
self.Offsetter_carbon_retired.read((token_id, address))
self.Offsetter_carbon_retired.entry((token_id, address)).read()
}

fn check_claimed(
Expand All @@ -255,7 +258,7 @@ mod OffsetComponent {
let allocation = Allocation {
claimee: claimee, amount: amount.into(), timestamp: timestamp.into(), id: id.into()
};
self.Offsetter_allocations_claimed.read(allocation)
self.Offsetter_allocations_claimed.entry(allocation).read()
}

fn set_merkle_root(ref self: ComponentState<TContractState>, root: felt252) {
Expand Down Expand Up @@ -289,16 +292,18 @@ mod OffsetComponent {
) {
let current_pending_retirement = self
.Offsetter_carbon_pending_retirement
.read((token_id, from));
.entry((token_id, from))
.read();

let new_pending_retirement = current_pending_retirement + amount;
self
.Offsetter_carbon_pending_retirement
.write((token_id, from), new_pending_retirement);
.entry((token_id, from))
.write(new_pending_retirement);

let current_allocation_id = self.Offsetter_allocation_id.read(from);
let current_allocation_id = self.Offsetter_allocation_id.entry(from).read();
let new_allocation_id = current_allocation_id + 1;
self.Offsetter_allocation_id.write(from, new_allocation_id);
self.Offsetter_allocation_id.entry(from).write(new_allocation_id);

// transfer the carbon credits to the project
let project = IProjectDispatcher {
Expand Down Expand Up @@ -330,13 +335,15 @@ mod OffsetComponent {
) {
let current_pending_retirement = self
.Offsetter_carbon_pending_retirement
.read((token_id, from));
.entry((token_id, from))
.read();
assert(current_pending_retirement >= amount, 'Not enough pending retirement');

let new_pending_retirement = current_pending_retirement - amount;
self
.Offsetter_carbon_pending_retirement
.write((token_id, from), new_pending_retirement);
.entry((token_id, from))
.write(new_pending_retirement);

self
.emit(
Expand All @@ -361,9 +368,9 @@ mod OffsetComponent {
let project = IProjectDispatcher { contract_address: project_address };
let amount_to_offset = project.cc_to_internal(amount, token_id);
project.burn(from, token_id, amount_to_offset);
let current_retirement = self.Offsetter_carbon_retired.read((token_id, from));
let current_retirement = self.Offsetter_carbon_retired.entry((token_id, from)).read();
let new_retirement = current_retirement + amount;
self.Offsetter_carbon_retired.write((token_id, from), new_retirement);
self.Offsetter_carbon_retired.entry((token_id, from)).write(new_retirement);

self
.emit(
Expand Down
48 changes: 29 additions & 19 deletions src/components/resale/resale_handler.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ mod ResaleComponent {
// Starknet imports

use starknet::{ContractAddress, get_caller_address, get_contract_address};
use starknet::storage::{
StoragePointerReadAccess, StoragePointerWriteAccess, StoragePathEntry, Map
};

// Internal imports

use carbon_v3::components::resale::interface::IResaleHandler;
use alexandria_merkle_tree::merkle_tree::{
Hasher, MerkleTree, MerkleTreeImpl, pedersen::PedersenHasherImpl, MerkleTreeTrait,
};
use carbon_v3::models::carbon_vintage::{CarbonVintage, CarbonVintageType};
use carbon_v3::components::vintage::interface::{IVintageDispatcher, IVintageDispatcherTrait};
use carbon_v3::components::erc1155::interface::{IERC1155Dispatcher, IERC1155DispatcherTrait};
Expand All @@ -21,6 +21,10 @@ mod ResaleComponent {
IExternalDispatcherTrait as IProjectDispatcherTrait
};

use alexandria_merkle_tree::merkle_tree::{
Hasher, MerkleTree, MerkleTreeImpl, pedersen::PedersenHasherImpl, MerkleTreeTrait,
};

// Roles
use openzeppelin::access::accesscontrol::interface::IAccessControl;

Expand All @@ -41,11 +45,11 @@ mod ResaleComponent {
#[storage]
struct Storage {
Resale_carbonable_project_address: ContractAddress,
Resale_carbon_pending_resale: LegacyMap<(u256, ContractAddress), u256>,
Resale_carbon_sold: LegacyMap<(u256, ContractAddress), u256>,
Resale_carbon_pending_resale: Map<(u256, ContractAddress), u256>,
Resale_carbon_sold: Map<(u256, ContractAddress), u256>,
Resale_merkle_root: felt252,
Resale_allocations_claimed: LegacyMap<Allocation, bool>,
Resale_allocation_id: LegacyMap<ContractAddress, u256>,
Resale_allocations_claimed: Map<Allocation, bool>,
Resale_allocation_id: Map<ContractAddress, u256>,
Resale_token_address: ContractAddress,
Resale_account_address: ContractAddress,
}
Expand Down Expand Up @@ -195,7 +199,7 @@ mod ResaleComponent {
let allocation = Allocation {
claimee: claimee, amount: amount, timestamp: timestamp, id: id
};
self.Resale_allocations_claimed.write(allocation, true);
self.Resale_allocations_claimed.entry(allocation).write(true);

self._claim_tokens(claimee, id.into(), amount.into());

Expand All @@ -211,13 +215,13 @@ mod ResaleComponent {
fn get_pending_resale(
self: @ComponentState<TContractState>, address: ContractAddress, token_id: u256
) -> u256 {
self.Resale_carbon_pending_resale.read((token_id, address))
self.Resale_carbon_pending_resale.entry((token_id, address)).read()
}

fn get_carbon_sold(
self: @ComponentState<TContractState>, address: ContractAddress, token_id: u256
) -> u256 {
self.Resale_carbon_sold.read((token_id, address))
self.Resale_carbon_sold.entry((token_id, address)).read()
}

fn check_claimed(
Expand All @@ -231,7 +235,7 @@ mod ResaleComponent {
let allocation = Allocation {
claimee: claimee, amount: amount.into(), timestamp: timestamp.into(), id: id.into()
};
self.Resale_allocations_claimed.read(allocation)
self.Resale_allocations_claimed.entry(allocation).read()
}

fn set_merkle_root(ref self: ComponentState<TContractState>, root: felt252) {
Expand Down Expand Up @@ -331,14 +335,17 @@ mod ResaleComponent {
token_id: u256,
amount: u256
) {
let current_pending_resale = self.Resale_carbon_pending_resale.read((token_id, from));
let current_pending_resale = self
.Resale_carbon_pending_resale
.entry((token_id, from))
.read();

let new_pending_resale = current_pending_resale + amount;
self.Resale_carbon_pending_resale.write((token_id, from), new_pending_resale);
self.Resale_carbon_pending_resale.entry((token_id, from)).write(new_pending_resale);

let current_allocation_id = self.Resale_allocation_id.read(from);
let current_allocation_id = self.Resale_allocation_id.entry(from).read();
let new_allocation_id = current_allocation_id + 1;
self.Resale_allocation_id.write(from, new_allocation_id);
self.Resale_allocation_id.entry(from).write(new_allocation_id);

// transfer the carbon credits to the project
let project = IProjectDispatcher {
Expand Down Expand Up @@ -368,11 +375,14 @@ mod ResaleComponent {
token_id: u256,
amount: u256
) {
let current_pending_resale = self.Resale_carbon_pending_resale.read((token_id, from));
let current_pending_resale = self
.Resale_carbon_pending_resale
.entry((token_id, from))
.read();
assert(current_pending_resale >= amount, Errors::NOT_ENOUGH_PENDING);

let new_pending_resale = current_pending_resale - amount;
self.Resale_carbon_pending_resale.write((token_id, from), new_pending_resale);
self.Resale_carbon_pending_resale.entry((token_id, from)).write(new_pending_resale);

self
.emit(
Expand All @@ -395,9 +405,9 @@ mod ResaleComponent {
self._remove_pending_resale(user, token_id, amount);

// [Effect] Update the carbon credits sold
let current_resale = self.Resale_carbon_sold.read((token_id, user));
let current_resale = self.Resale_carbon_sold.entry((token_id, user)).read();
let new_resale = current_resale + amount;
self.Resale_carbon_sold.write((token_id, user), new_resale);
self.Resale_carbon_sold.entry((token_id, user)).write(new_resale);

// [Effect] Transfer the tokens to the user
let token_address = self.Resale_token_address.read();
Expand Down
Loading
Loading