forked from curvefi/curve-contract
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_transferFrom.py
164 lines (101 loc) · 5.09 KB
/
test_transferFrom.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/python3
import brownie
def test_sender_balance_decreases(alice, bob, charlie, token):
sender_balance = token.balanceOf(alice)
amount = sender_balance // 4
token.approve(bob, amount, {"from": alice})
token.transferFrom(alice, charlie, amount, {"from": bob})
assert token.balanceOf(alice) == sender_balance - amount
def test_receiver_balance_increases(alice, bob, charlie, token):
receiver_balance = token.balanceOf(charlie)
amount = token.balanceOf(alice) // 4
token.approve(bob, amount, {"from": alice})
token.transferFrom(alice, charlie, amount, {"from": bob})
assert token.balanceOf(charlie) == receiver_balance + amount
def test_caller_balance_not_affected(alice, bob, charlie, token):
caller_balance = token.balanceOf(bob)
amount = token.balanceOf(alice)
token.approve(bob, amount, {"from": alice})
token.transferFrom(alice, charlie, amount, {"from": bob})
assert token.balanceOf(bob) == caller_balance
def test_caller_approval_affected(alice, bob, charlie, token):
approval_amount = token.balanceOf(alice)
transfer_amount = approval_amount // 4
token.approve(bob, approval_amount, {"from": alice})
token.transferFrom(alice, charlie, transfer_amount, {"from": bob})
assert token.allowance(alice, bob) == approval_amount - transfer_amount
def test_receiver_approval_not_affected(alice, bob, charlie, token):
approval_amount = token.balanceOf(alice)
transfer_amount = approval_amount // 4
token.approve(bob, approval_amount, {"from": alice})
token.approve(charlie, approval_amount, {"from": alice})
token.transferFrom(alice, charlie, transfer_amount, {"from": bob})
assert token.allowance(alice, charlie) == approval_amount
def test_total_supply_not_affected(alice, bob, charlie, token):
total_supply = token.totalSupply()
amount = token.balanceOf(alice)
token.approve(bob, amount, {"from": alice})
token.transferFrom(alice, charlie, amount, {"from": bob})
assert token.totalSupply() == total_supply
def test_returns_true(alice, bob, charlie, token):
amount = token.balanceOf(alice)
token.approve(bob, amount, {"from": alice})
tx = token.transferFrom(alice, charlie, amount, {"from": bob})
assert tx.return_value is True
def test_transfer_full_balance(alice, bob, charlie, token):
amount = token.balanceOf(alice)
receiver_balance = token.balanceOf(charlie)
token.approve(bob, amount, {"from": alice})
token.transferFrom(alice, charlie, amount, {"from": bob})
assert token.balanceOf(alice) == 0
assert token.balanceOf(charlie) == receiver_balance + amount
def test_transfer_zero_tokens(alice, bob, charlie, token):
sender_balance = token.balanceOf(alice)
receiver_balance = token.balanceOf(charlie)
token.approve(bob, sender_balance, {"from": alice})
token.transferFrom(alice, charlie, 0, {"from": bob})
assert token.balanceOf(alice) == sender_balance
assert token.balanceOf(charlie) == receiver_balance
def test_transfer_zero_tokens_without_approval(alice, bob, charlie, token):
sender_balance = token.balanceOf(alice)
receiver_balance = token.balanceOf(charlie)
token.transferFrom(alice, charlie, 0, {"from": bob})
assert token.balanceOf(alice) == sender_balance
assert token.balanceOf(charlie) == receiver_balance
def test_insufficient_balance(alice, bob, charlie, token):
balance = token.balanceOf(alice)
token.approve(bob, balance + 1, {"from": alice})
with brownie.reverts():
token.transferFrom(alice, charlie, balance + 1, {"from": bob})
def test_insufficient_approval(alice, bob, charlie, token):
balance = token.balanceOf(alice)
token.approve(bob, balance - 1, {"from": alice})
with brownie.reverts():
token.transferFrom(alice, charlie, balance, {"from": bob})
def test_no_approval(alice, bob, charlie, token):
balance = token.balanceOf(alice)
with brownie.reverts():
token.transferFrom(alice, charlie, balance, {"from": bob})
def test_revoked_approval(alice, bob, charlie, token):
balance = token.balanceOf(alice)
token.approve(bob, balance, {"from": alice})
token.approve(bob, 0, {"from": alice})
with brownie.reverts():
token.transferFrom(alice, charlie, balance, {"from": bob})
def test_transfer_to_self(alice, bob, token):
sender_balance = token.balanceOf(alice)
amount = sender_balance // 4
token.approve(alice, sender_balance, {"from": alice})
token.transferFrom(alice, alice, amount, {"from": alice})
assert token.balanceOf(alice) == sender_balance
assert token.allowance(alice, alice) == sender_balance - amount
def test_transfer_to_self_no_approval(alice, bob, token):
amount = token.balanceOf(alice)
with brownie.reverts():
token.transferFrom(alice, alice, amount, {"from": alice})
def test_transfer_event_fires(alice, bob, charlie, token):
amount = token.balanceOf(alice)
token.approve(bob, amount, {"from": alice})
tx = token.transferFrom(alice, charlie, amount, {"from": bob})
assert len(tx.events) == 1
assert tx.events["Transfer"].values() == [alice, charlie, amount]