Beispiel #1
0
    def parse_utxo_str(self, utxo_str):
        # Split raw utxo string
        utxo_str = utxo_str.split(maxsplit=2)

        # Get txHash and index
        self.txHash = utxo_str[0]
        self.index = int(utxo_str[1])

        # Create account
        self.account = Account()

        # Parse tokens component
        tokens_str = utxo_str[2].split('+')

        # Extract lovelace
        lovelace = int(tokens_str[0].replace('lovelace', '').strip())
        self.account = self.account.add_lovelace(lovelace)

        # Extract native_tokens
        if len(tokens_str) >= 2:
            native_assets_str = tokens_str[1:]
            for native_asset_str in native_assets_str:
                if native_asset_str.strip(
                ) == 'TxOutDatumHashNone' or native_asset_str.strip(
                ) == 'TxOutDatumNone':
                    continue
                qty, asset = native_asset_str.strip().split()
                qty = int(qty)
                self.account = self.account.add_native_token(asset, qty)
Beispiel #2
0
class UTXO(object):
    def __init__(self, utxo_str):
        self.txHash = None
        self.index = None
        self.account = None
        self.parse_utxo_str(utxo_str)

        assert self.txHash != None
        assert self.index != None
        assert self.account != None

    def parse_utxo_str(self, utxo_str):
        # Split raw utxo string
        utxo_str = utxo_str.split(maxsplit=2)

        # Get txHash and index
        self.txHash = utxo_str[0]
        self.index = int(utxo_str[1])

        # Create account
        self.account = Account()

        # Parse tokens component
        tokens_str = utxo_str[2].split('+')

        # Extract lovelace
        lovelace = int(tokens_str[0].replace('lovelace', '').strip())
        self.account = self.account.add_lovelace(lovelace)

        # Extract native_tokens
        if len(tokens_str) >= 2:
            native_assets_str = tokens_str[1:]
            for native_asset_str in native_assets_str:
                if native_asset_str.strip(
                ) == 'TxOutDatumHashNone' or native_asset_str.strip(
                ) == 'TxOutDatumNone':
                    continue
                qty, asset = native_asset_str.strip().split()
                qty = int(qty)
                self.account = self.account.add_native_token(asset, qty)

    def get_utxo_identifier(self):
        return f'{self.txHash}#{self.index}'

    def get_account(self):
        return self.account

    def __str__(self):
        return f'{self.get_utxo_identifier()}'

    def convert_to_receiver(self, addr):
        '''Converts UTXO to TxReceiver object, copying all contents over'''
        new_receiver = TxReceiver(addr)
        new_receiver.account = self.account.duplicate()
        return new_receiver

    def size(self):
        return self.account.size()
Beispiel #3
0
class BasicReceiver(object):
    def __init__(self):
        self.account = Account()

    def get_account(self):
        return self.account

    def add_ada(self, quantity):
        self.account = self.account.add_ada(quantity)

    def remove_ada(self, quantity):
        self.account = self.account.remove_ada(quantity)

    def add_lovelace(self, quantity):
        self.account = self.account.add_lovelace(quantity)

    def remove_lovelace(self, quantity):
        self.account = self.account.remove_lovelace(quantity)

    def add_native_token(self, token_id, quantity):
        self.account = self.account.add_native_token(token_id, quantity)

    def remove_native_token(self, token_id, quantity):
        self.account = self.account.remove_native_token(token_id, quantity)

    def set_ada(self, quantity):
        self.account = self.account.set_ada(quantity)

    def set_native_token(self, token_id, quantity):
        self.account = self.account.set_native_token(token_id, quantity)

    def duplicate(self):
        return copy.deepcopy(self)

    def transfer_to(self, other_receiver):
        '''Transfer the convents of this receiver to another receiver,
        emptying this one'''
        other_receiver.account = self.account + other_receiver.account
        self.account = Account()

    def get_lovelace(self):
        return self.account.get_lovelace()

    def get_native_token(self, token_id):
        return self.account.get_native_token(token_id)
Beispiel #4
0
 def test_lovelace_addition(self):
     account = Account().add_lovelace(1000)
     self.assertEqual(account.get_lovelace(), 1000)
     self.assertEqual(account.get_ada(), 0.001)
Beispiel #5
0
    def test_account_str(self):
        '''This test checks for proper conversion of Account objects to string representation'''

        # Only contains lovelace
        account = Account().add_lovelace(1500000)
        self.assertEqual(str(account), '1500000')

        # Contains lovelace and a native token
        account = Account().add_lovelace(1500000)
        account = account.add_native_token('12345.tokenA', 2)
        self.assertEqual(str(account), '1500000+"2 12345.tokenA"')

        # Contains lovelace and a native token (added separately)
        account = Account().add_lovelace(1500000)
        account = account.add_native_token('12345.tokenA', 2)
        account = account.add_native_token('12345.tokenA', 2)
        self.assertEqual(str(account), '1500000+"4 12345.tokenA"')

        # Contains lovelace and different native tokens
        account = Account().add_lovelace(1500000)
        account = account.add_native_token('12345.tokenA', 2)
        account = account.add_native_token('56789.tokenA', 2)
        self.assertEqual(str(account),
                         '1500000+"2 12345.tokenA + 2 56789.tokenA"')

        # Negative quantities for burning
        account = Account().add_lovelace(1500000)
        account = account.add_native_token('12345.tokenA', 2)
        account = account.remove_native_token('56789.tokenA', 2)
        self.assertEqual(str(account),
                         '1500000+"2 12345.tokenA + -2 56789.tokenA"')
Beispiel #6
0
 def test_account_addition(self):
     '''This test checks for the addition of lovelace'''
     account_a = Account().add_lovelace(1500000)
     account_b = Account().add_lovelace(1500000)
     account_c = Account().add_lovelace(3000000)
     self.assertEqual(account_a + account_b, account_c)
Beispiel #7
0
 def __init__(self):
     self.account = Account()
Beispiel #8
0
 def transfer_to(self, other_receiver):
     '''Transfer the convents of this receiver to another receiver,
     emptying this one'''
     other_receiver.account = self.account + other_receiver.account
     self.account = Account()