Skip to content

Commit e3c15c0

Browse files
authored
feat: add amount to HTLC lock constructor (#116)
1 parent edecd2a commit e3c15c0

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

crypto/transactions/builder/htlc_lock.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ class HtlcLock(BaseTransactionBuilder):
66

77
transaction_type = TRANSACTION_HTLC_LOCK
88

9-
def __init__(self, recipient_id, secret_hash, expiration_type, expiration_value, vendorField=None, fee=None):
9+
def __init__(self, recipient_id, amount, secret_hash, expiration_type, expiration_value, vendorField=None, fee=None):
1010
"""Create a timelock transaction
1111
1212
Args:
1313
recipient_id (str): recipient identifier
14+
amount (int): amount of coins you want to transfer
1415
secret_hash (str): a hash of the secret. The SAME hash must be used in the corresponding “claim” transaction
1516
expiration_type (int): type of the expiration. Either block height or network epoch timestamp based
1617
expiration_value (int): Expiration of transaction in seconds or height depending on expiration_type
@@ -20,6 +21,11 @@ def __init__(self, recipient_id, secret_hash, expiration_type, expiration_value,
2021
super().__init__()
2122

2223
self.transaction.recipientId = recipient_id
24+
25+
if type(amount) == int and amount > 0:
26+
self.transaction.amount = amount
27+
else:
28+
raise ValueError('Amount is not valid')
2329

2430
self.transaction.typeGroup = self.get_type_group()
2531

tests/transactions/builder/test_htlc_lock.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,56 @@
22
from crypto.constants import TRANSACTION_HTLC_LOCK, TRANSACTION_TYPE_GROUP
33
from crypto.networks.devnet import Devnet
44
from crypto.transactions.builder.htlc_lock import HtlcLock
5+
import pytest
56

67
set_network(Devnet)
78

89

10+
def test_htlc_lock_transation_amount_not_int():
11+
with pytest.raises(ValueError):
12+
"""Test error handling in constructor for non-integer amount
13+
"""
14+
HtlcLock(
15+
recipient_id='AGeYmgbg2LgGxRW2vNNJvQ88PknEJsYizC',
16+
amount='bad amount number',
17+
secret_hash='0f128d401958b1b30ad0d10406f47f9489321017b4614e6cb993fc63913c5454',
18+
expiration_type=1,
19+
expiration_value=1573455822
20+
)
21+
22+
23+
def test_htlc_lock_transation_amount_zero():
24+
with pytest.raises(ValueError):
25+
"""Test error handling in constructor for non-integer amount
26+
"""
27+
HtlcLock(
28+
recipient_id='AGeYmgbg2LgGxRW2vNNJvQ88PknEJsYizC',
29+
amount=0,
30+
secret_hash='0f128d401958b1b30ad0d10406f47f9489321017b4614e6cb993fc63913c5454',
31+
expiration_type=1,
32+
expiration_value=1573455822
33+
)
34+
35+
36+
def test_htlc_lock_transation_amount_negative():
37+
with pytest.raises(ValueError):
38+
"""Test error handling in constructor for non-integer amount
39+
"""
40+
HtlcLock(
41+
recipient_id='AGeYmgbg2LgGxRW2vNNJvQ88PknEJsYizC',
42+
amount=-5,
43+
secret_hash='0f128d401958b1b30ad0d10406f47f9489321017b4614e6cb993fc63913c5454',
44+
expiration_type=1,
45+
expiration_value=1573455822
46+
)
47+
48+
949
def test_htlc_lock_transaction():
1050
"""Test if timelock transaction gets built
1151
"""
1252
transaction = HtlcLock(
1353
recipient_id='AGeYmgbg2LgGxRW2vNNJvQ88PknEJsYizC',
54+
amount=200000000,
1455
secret_hash='0f128d401958b1b30ad0d10406f47f9489321017b4614e6cb993fc63913c5454',
1556
expiration_type=1,
1657
expiration_value=1573455822
@@ -22,6 +63,7 @@ def test_htlc_lock_transaction():
2263
transaction_dict = transaction.to_dict()
2364

2465
assert transaction_dict['recipientId'] == 'AGeYmgbg2LgGxRW2vNNJvQ88PknEJsYizC'
66+
assert transaction_dict['amount'] == 200000000
2567
assert transaction_dict['nonce'] == 1
2668
assert transaction_dict['signature']
2769
assert transaction_dict['type'] is TRANSACTION_HTLC_LOCK

0 commit comments

Comments
 (0)