Skip to content

Commit

Permalink
Merge pull request #68 from ltonetwork/burn-transaction
Browse files Browse the repository at this point in the history
Burn transaction
  • Loading branch information
jasny authored Mar 28, 2022
2 parents 8375155 + cadc159 commit dd54b7a
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/lto/transactions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from lto.transactions.transfer import Transfer
from lto.transactions.register import Register
from lto.transactions.data import Data
from lto.transactions.burn import Burn

def from_data(data):
if data['type'] == 4:
Expand All @@ -36,5 +37,7 @@ def from_data(data):
return CancelSponsorship.from_data(data)
elif data['type'] == 20:
return Register.from_data(data)
elif data['type'] == 21:
return Burn.from_data(data)
else:
raise Exception('Incorrect transaction Type')
71 changes: 71 additions & 0 deletions src/lto/transactions/burn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import base58
import struct
from lto import crypto
from lto.transaction import Transaction


class Burn(Transaction):
TYPE = 15
DEFAULT_FEE = 100000000
DEFAULT_VERSION = 3

def __init__(self, amount):
super().__init__()
if amount < 1:
raise Exception("Minimum burn amount = 1")
self.amount = amount
self.version = self.DEFAULT_VERSION
self.tx_fee = self.DEFAULT_FEE

def __to_binary_v3(self):
return (self.TYPE.to_bytes(1, 'big') +
b'\3' +
crypto.str2bytes(self.chain_id) +
struct.pack(">Q", self.timestamp) +
crypto.key_type_id(self.sender_key_type) +
base58.b58decode(self.sender_public_key) +
struct.pack(">Q", self.tx_fee) +
struct.pack(">Q", self.amount))

def to_binary(self):
if self.version == 3:
return self.__to_binary_v3()
else:
raise Exception('Incorrect Version')

def to_json(self):
return (crypto.merge_dicts({
"type": self.TYPE,
"version": self.version,
"id": self.id if self.id else "",
"sender": self.sender,
"senderKeyType": self.sender_key_type,
"senderPublicKey": self.sender_public_key,
"fee": self.tx_fee,
"timestamp": self.timestamp,
"amount": self.amount,
"proofs": self.proofs,
"height": self.height if self.height else ""
}, self._sponsor_json()))

@staticmethod
def from_data(data):
tx = Burn(data['amount'])
tx.type = data['type']
tx.version = data['version']
tx.id = data['id'] if 'id' in data else ''
tx.sender = data['sender'] if 'sender' in data else ''
tx.sender_key_type = data['senderKeyType'] if 'senderKeyType' in data else 'ed25519'
tx.sender_public_key = data['senderPublicKey']
tx.fee = data['fee']
tx.timestamp = data['timestamp']
tx.amount = data['amount']
tx.proofs = data['proofs'] if 'proofs' in data else []
tx.height = data['height'] if 'height' in data else ''

if "sponsor_public_key" in data:
tx.sponsor = data['sponsor']
tx.sponsor_public_key = data['sponsorPublicKey']
tx.sponsor_key_type = data['sponsorKeyType']

return tx

0 comments on commit dd54b7a

Please sign in to comment.