Skip to content

Commit

Permalink
Merge pull request #118 from julienbrs/track-rebased-cc
Browse files Browse the repository at this point in the history
Track rebased cc
  • Loading branch information
julienbrs authored Jul 22, 2024
2 parents ac6935b + 0983c6a commit b15a77d
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/components/vintage/interface.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ trait IVintage<TContractState> {
/// Returns the vintage details with the given token_id.
fn get_carbon_vintage(self: @TContractState, token_id: u256) -> CarbonVintage;

// Get the initial supply of carbon credits for a vintage, before any rebases
fn get_initial_cc_supply(self: @TContractState, token_id: u256) -> u128;

/// Get number of decimal for total supply to have a carbon credit
fn get_cc_decimals(self: @TContractState) -> u8;

Expand Down
23 changes: 22 additions & 1 deletion src/components/vintage/vintage.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ mod VintageComponent {
self.Vintage_vintages.read(token_id)
}

fn get_initial_cc_supply(self: @ComponentState<TContractState>, token_id: u256) -> u128 {
self.get_carbon_vintage(token_id).supply
+ self.get_carbon_vintage(token_id).failed
- self.get_carbon_vintage(token_id).created
}


fn rebase_vintage(
ref self: ComponentState<TContractState>, token_id: u256, new_cc_supply: u128
Expand All @@ -128,10 +134,20 @@ mod VintageComponent {
let mut vintage: CarbonVintage = self.Vintage_vintages.read(token_id);
let old_supply = vintage.supply;

if (new_cc_supply == old_supply) {
return ();
}

// Negative rebase, failed carbon credits
if new_cc_supply < old_supply {
let diff = old_supply - new_cc_supply;
vintage.supply = new_cc_supply;
vintage.failed = vintage.failed + diff;
} // Positive rebase, created carbon credits
else {
let diff = new_cc_supply - old_supply;
vintage.supply = new_cc_supply;
vintage.created = vintage.created + diff;
}
vintage.supply = new_cc_supply;
self.Vintage_vintages.write(token_id, vintage);
Expand Down Expand Up @@ -188,6 +204,7 @@ mod VintageComponent {
year: (start_year + index).into(),
supply: supply,
failed: 0,
created: 0,
status: CarbonVintageType::Projected,
};
self.Vintage_vintages.write(index.into(), vintage);
Expand Down Expand Up @@ -215,7 +232,11 @@ mod VintageComponent {
break ();
}
let vintage: CarbonVintage = CarbonVintage {
year: index.into(), supply: 0, failed: 0, status: CarbonVintageType::Projected,
year: index.into(),
supply: 0,
failed: 0,
created: 0,
status: CarbonVintageType::Projected,
};
// [Effect] Store values
self.Vintage_vintages.write(index.into(), vintage);
Expand Down
19 changes: 14 additions & 5 deletions src/models/carbon_vintage.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ struct CarbonVintage {
year: u32,
/// The total supply of Carbon Credit for this vintage.
supply: u128,
/// The total amount of Carbon Credit that failed during audits.
/// The total amount of Carbon Credit that was failed during audits.
failed: u128,
/// The total amount of Carbon Credit that was created during audits.
created: u128,
/// The status of the Carbon Credit of this Vintage.
status: CarbonVintageType,
}

impl CarbonVintageDisplay of Display<CarbonVintage> {
fn fmt(self: @CarbonVintage, ref f: Formatter) -> Result<(), Error> {
let str: ByteArray = format!(
"CarbonVintage(year: {}, supply: {}, failed: {}, status: {})",
"CarbonVintage(year: {}, supply: {}, failed: {}, created: {}, status: {})",
self.year,
self.supply,
self.failed,
self.created,
self.status
);
f.buffer.append(@str);
Expand Down Expand Up @@ -94,21 +97,27 @@ mod Test {
assert_eq!(carbon_vintage.year, 0);
assert_eq!(carbon_vintage.supply, 0);
assert_eq!(carbon_vintage.failed, 0);
assert_eq!(carbon_vintage.created, 0);
assert_eq!(carbon_vintage.status, CarbonVintageType::Unset);
}

#[test]
fn test_carbon_vintage_display() {
let carbon_vintage: CarbonVintage = Default::default();
let res = format!("{}", carbon_vintage);
assert_eq!(res, "CarbonVintage(year: 0, supply: 0, failed: 0, status: Unset)");
assert_eq!(res, "CarbonVintage(year: 0, supply: 0, failed: 0, created: 0, status: Unset)");

let carbon_vintage = CarbonVintage {
year: 2024, supply: 1000000000, failed: 10000, status: CarbonVintageType::Audited
year: 2024,
supply: 1000000000,
failed: 10000,
created: 500,
status: CarbonVintageType::Audited
};
let res = format!("{}", carbon_vintage);
assert_eq!(
res, "CarbonVintage(year: 2024, supply: 1000000000, failed: 10000, status: Audited)"
res,
"CarbonVintage(year: 2024, supply: 1000000000, failed: 10000, created: 500, status: Audited)"
);
}

Expand Down
1 change: 0 additions & 1 deletion tests/test_project.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,6 @@ fn fuzz_test_transfer_high_supply_high_amount(
fn test_project_metadata_update() {
let owner_address: ContractAddress = contract_address_const::<'OWNER'>();
let (project_address, _) = default_setup_and_deploy();
let vintages = IVintageDispatcher { contract_address: project_address };
let project_contract = IProjectDispatcher { contract_address: project_address };
let erc1155_meta = IERC1155MetadataURIDispatcher { contract_address: project_address };
let base_uri: ByteArray = format!("{}", 'uri');
Expand Down
41 changes: 41 additions & 0 deletions tests/test_vintage.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ fn test_set_vintages() {
year: (starting_year + index.into()),
supply: *yearly_absorptions.at(index),
failed: 0,
created: 0,
status: CarbonVintageType::Projected,
};
assert(*vintage == expected__cc_vintage, 'vintage not set correctly');
Expand All @@ -300,6 +301,7 @@ fn test_set_vintages() {
year: (starting_year.into() + index.into()),
supply: 0,
failed: 0,
created: 0,
status: CarbonVintageType::Unset,
};
assert(*vintage == expected__cc_vintage, 'vintage not set correctly');
Expand Down Expand Up @@ -338,6 +340,45 @@ fn test_get_carbon_vintage() {
};
}

/// get_initial_cc_supply
#[test]
fn test_get_initial_cc_supply() {
let (project_address, _) = default_setup_and_deploy();
let vintages = IVintageDispatcher { contract_address: project_address };

// initial supply should be equal to supply before any rebases
let cc_vintages = vintages.get_cc_vintages();
let mut index = 0;
loop {
if index == cc_vintages.len() {
break;
}
let token_id: u256 = index.into();
let vintage = vintages.get_carbon_vintage(token_id);
let initial_supply = vintages.get_initial_cc_supply(token_id);
assert(initial_supply == vintage.supply, 'Initial supply error');
index += 1;
};

// Do one positive rebase and check if initial supply is correct
let token_id: u256 = 1;
let initial_supply = vintages.get_carbon_vintage(token_id).supply;
let diff = 50000;
let new_cc_supply: u128 = initial_supply + diff;
vintages.rebase_vintage(token_id, new_cc_supply);
let fetched_initial_supply = vintages.get_initial_cc_supply(token_id);
assert(vintages.get_carbon_vintage(token_id).created == diff, 'Created field error');
assert(fetched_initial_supply == initial_supply, 'Initial supply error');

// Do one negative rebase and check if initial supply is correct
let new_cc_supply: u128 = new_cc_supply - diff;
vintages.rebase_vintage(token_id, new_cc_supply);
let fetched_initial_supply = vintages.get_initial_cc_supply(token_id);
assert(fetched_initial_supply == initial_supply, 'Initial supply error');
let diff = initial_supply - new_cc_supply + vintages.get_carbon_vintage(token_id).created;
assert(vintages.get_carbon_vintage(token_id).failed == diff, 'Failed field error');
}

#[test]
fn test_get_carbon_vintage_non_existent_token_id() {
let (project_address, _) = default_setup_and_deploy();
Expand Down

0 comments on commit b15a77d

Please sign in to comment.