Skip to content

Commit

Permalink
Create token.py
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Dec 1, 2024
1 parent e5ff987 commit 5444e8d
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/blockchain/token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class Token:
def __init__(self, name, symbol, total_supply):
self.name = name
self.symbol = symbol
self.total_supply = total_supply
self.balances = {}
self.allowances = {}

def transfer(self, sender, recipient, amount):
if self.balances.get(sender, 0) >= amount:
self.balances[sender] -= amount
self.balances[recipient] = self.balances.get(recipient, 0) + amount
return True
return False

def approve(self, owner, spender, amount):
if owner not in self.allowances:
self.allowances[owner] = {}
self.allowances[owner][spender] = amount
return True

def transfer_from(self, spender, owner, recipient, amount):
if self.allowances.get(owner, {}).get(spender, 0 ) >= amount and self.balances.get(owner, 0) >= amount:
self.balances[owner] -= amount
self.balances[recipient] = self.balances.get(recipient, 0) + amount
self.allowances[owner][spender] -= amount
return True
return False

def balance_of(self, owner):
return self.balances.get(owner, 0)

def total_supply(self):
return self.total_supply

def mint(self, recipient, amount):
self.total_supply += amount
self.balances[recipient] = self.balances.get(recipient, 0) + amount

def burn(self, owner, amount):
if self.balances.get(owner, 0) >= amount:
self.balances[owner] -= amount
self.total_supply -= amount
return True
return False

0 comments on commit 5444e8d

Please sign in to comment.