templates.test_address_comparison

 1import smartpy as sp
 2
 3
 4@sp.module
 5def main():
 6    def same_underlying_address(params):
 7        (a, b) = params
 8        sp.cast(a, sp.address)
 9        sp.cast(b, sp.address)
10        b1 = sp.slice(6, 22, sp.pack(a))
11        b2 = sp.slice(6, 22, sp.pack(b))
12        return b1 == b2
13
14    class TestAddressComparison(sp.Contract):
15        def __init__(self, x, y):
16            self.data.x = x
17            self.data.y = y
18
19        @sp.entrypoint
20        def test(self, params):
21            assert same_underlying_address(params)
22
23        @sp.entrypoint
24        def ep(self, a):
25            assert self.isKT1(a), "Not KT1"
26            self.data.x = a < sp.address("KT1XvNYseNDJJ6Kw27qhSEDF8ys8JhDopzfG")
27            self.data.y = a >= sp.address("KT18amZmM5W7qDWVt2pH6uj7sCEd3kbzLrHT")
28
29        @sp.private()
30        def isKT1(self, address):
31            return address <= sp.address(
32                "KT1XvNYseNDJJ6Kw27qhSEDF8ys8JhDopzfG"
33            ) and address >= sp.address("KT18amZmM5W7qDWVt2pH6uj7sCEd3kbzLrHT")
34
35
36@sp.add_test()
37def test():
38    scenario = sp.test_scenario("TestAddressComparison", main)
39    c1 = main.TestAddressComparison(x=False, y=False)
40
41    scenario += c1
42
43    alice = sp.test_account("alice")
44    c1.ep(alice.address, _valid=False)
45    c1.ep(sp.address("KT1TezoooozzSmartPyzzSTATiCzzzwwBFA1"))
46
47    c1.test(
48        (
49            sp.address("KT1WD5PV1i1HQTFhNUxVGNjRda63trNyshwU"),
50            sp.address("KT1WD5PV1i1HQTFhNUxVGNjRda63trNyshwU"),
51        )
52    )
53
54    c1.test(
55        (
56            sp.address("KT1WD5PV1i1HQTFhNUxVGNjRda63trNyshwU"),
57            sp.address("KT1WD5PV1i1HQTFhNUxVGNjRda63trNyshwU%a"),
58        )
59    )
60
61    c1.test(
62        (
63            sp.address("KT1WD5PV1i1HQTFhNUxVGNjRda63trNyshwU"),
64            sp.address("KT1WD5PV1i1HQTFhNUxVGNjRda63trNyshwU%"),
65        )
66    )
67
68    c1.test(
69        (
70            sp.address("KT1WD5PV1i1HQTFhNUxVGNjRda63trNyshwU"),
71            sp.address("tz1Z6Uk4qfdAJLJuCdGzL8aheqedW8sBQv2T"),
72        ),
73        _valid=False,
74    )
75
76    c1.test(
77        (
78            sp.address("KT1WD5PV1i1HQTFhNUxVGNjRda63trNyshwU"),
79            sp.address("KT1AYAtnyeZKifkjv5ooKXsKuWWbpECMgoUC"),
80        ),
81        _valid=False,
82    )
83
84    c1.test(
85        (
86            sp.address("KT1WD5PV1i1HQTFhNUxVGNjRda63trNyshwU"),
87            sp.address("KT1AYAtnyeZKifkjv5ooKXsKuWWbpECMgoUC%"),
88        ),
89        _valid=False,
90    )