Skip to content

Commit

Permalink
Create CarbonCreditContract.py
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Jul 27, 2024
1 parent ec90629 commit 11317be
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions sidra_chain_integration/contracts/CarbonCreditContract.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from typing import Dict

class CarbonCreditContract:
def __init__(self):
self.credit_owners: Dict[str, int] = {}
self.credit_registry: Dict[str, Dict[str, str]] = {}

def mint_credit(self, owner: str, amount: int, project_id: str):
# Mint a new carbon credit
self.credit_owners[owner] = self.credit_owners.get(owner, 0) + amount
credit_id = f"CC-{len(self.credit_registry) + 1}"
self.credit_registry[credit_id] = {
"owner": owner,
"amount": amount,
"project_id": project_id
}

def transfer_credit(self, from_owner: str, to_owner: str, credit_id: str):
# Transfer a carbon credit from one owner to another
if credit_id not in self.credit_registry:
raise ValueError("Credit not found")
if self.credit_registry[credit_id]["owner"] != from_owner:
raise ValueError("Credit owner mismatch")
self.credit_registry[credit_id]["owner"] = to_owner
self.credit_owners[from_owner] -= self.credit_registry[credit_id]["amount"]
self.credit_owners[to_owner] = self.credit_owners.get(to_owner, 0) + self.credit_registry[credit_id]["amount"]

def retire_credit(self, owner: str, credit_id: str):
# Retire a carbon credit
if credit_id not in self.credit_registry:
raise ValueError("Credit not found")
if self.credit_registry[credit_id]["owner"] != owner:
raise ValueError("Credit owner mismatch")
del self.credit_registry[credit_id]
self.credit_owners[owner] -= self.credit_registry[credit_id]["amount"]

def get_credit_balance(self, owner: str):
# Get the carbon credit balance of an owner
return self.credit_owners.get(owner, 0)

def get_credit_info(self, credit_id: str):
# Get the information of a carbon credit
return self.credit_registry.get(credit_id, {})

0 comments on commit 11317be

Please sign in to comment.