Skip to content

Commit

Permalink
Create WalletModel.py
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Jul 26, 2024
1 parent 28d38a7 commit 8398b5c
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions coin/models/WalletModel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import hashlib
from typing import List, Dict

class WalletModel:
def __init__(self, address: str):
self.address = address
self.balance = 0
self.transactions = []

def generate_address(self) -> str:
# Generate new wallet address
return hashlib.sha256(f"{self.address}{self.balance}".encode()).hexdigest()

def send_coins(self, amount: float, receiver_address: str) -> None:
# Send coins to another wallet
self.balance -= amount
self.transactions.append({"amount": amount, "receiver_address": receiver_address})

def receive_coins(self, amount: float, sender_address: str) -> None:
# Receive coins from another wallet
self.balance += amount
self.transactions.append({"amount": amount, "sender_address": sender_address})

def update_balance(self) -> None:
# Update wallet balance
self.balance = sum(transaction["amount"] for transaction in self.transactions)

def to_dict(self) -> Dict:
# Convert wallet data to dictionary
return {
"address": self.address,
"balance": self.balance,
"transactions": self.transactions
}

0 comments on commit 8398b5c

Please sign in to comment.