Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
stoemelinkz authored and tekkac committed Nov 26, 2024
1 parent 387f274 commit 79bb7b8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
30 changes: 18 additions & 12 deletions src/components/resale/resale_handler.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -199,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 @@ -215,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 @@ -235,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 @@ -335,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 @@ -372,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 @@ -399,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
16 changes: 8 additions & 8 deletions src/components/vintage/vintage.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ mod VintageComponent {
fn get_carbon_vintage(
self: @ComponentState<TContractState>, token_id: u256
) -> CarbonVintage {
self.Vintage_vintages.read(token_id)
self.Vintage_vintages.entry(token_id).read()
}

fn get_initial_cc_supply(self: @ComponentState<TContractState>, token_id: u256) -> u256 {
Expand Down Expand Up @@ -148,7 +148,7 @@ mod VintageComponent {
) {
self.assert_only_role(OWNER_ROLE);

let mut vintage: CarbonVintage = self.Vintage_vintages.read(token_id);
let mut vintage: CarbonVintage = self.Vintage_vintages.entry(token_id).read();
let old_supply = vintage.supply;

if (new_cc_supply == old_supply) {
Expand All @@ -167,7 +167,7 @@ mod VintageComponent {
vintage.created = vintage.created + diff;
}
vintage.supply = new_cc_supply;
self.Vintage_vintages.write(token_id, vintage);
self.Vintage_vintages.entry(token_id).write(vintage);

self
.emit(
Expand All @@ -184,10 +184,10 @@ mod VintageComponent {
self.assert_only_role(OWNER_ROLE);

let new_status: CarbonVintageType = status.try_into().expect('Invalid status');
let mut vintage: CarbonVintage = self.Vintage_vintages.read(token_id);
let mut vintage: CarbonVintage = self.Vintage_vintages.entry(token_id).read();
let old_status = vintage.status;
vintage.status = new_status;
self.Vintage_vintages.write(token_id, vintage);
self.Vintage_vintages.entry(token_id).write(vintage);

self
.emit(
Expand Down Expand Up @@ -215,15 +215,15 @@ mod VintageComponent {
}
let supply = *yearly_absorptions.at(index);
let token_id = (index + 1).into();
let old_vintage = self.Vintage_vintages.read(token_id);
let old_vintage = self.Vintage_vintages.entry(token_id).read();
let mut vintage = CarbonVintage {
year: (start_year + index).into(),
supply: supply,
failed: 0,
created: 0,
status: CarbonVintageType::Projected,
};
self.Vintage_vintages.write(token_id, vintage);
self.Vintage_vintages.entry(token_id).write(vintage);

self
.emit(
Expand Down Expand Up @@ -264,7 +264,7 @@ mod VintageComponent {
status: CarbonVintageType::Projected,
};

self.Vintage_vintages.write(token_id, vintage);
self.Vintage_vintages.entry(token_id).write(vintage);
index += 1;
};
}
Expand Down

0 comments on commit 79bb7b8

Please sign in to comment.