@@ -149,3 +149,67 @@ func TestOrderedTransactions(t *testing.T) {
149149 },
150150 }, timeseries )
151151}
152+
153+ // TestOrderedTransactionsWithFailedTransactions tests that the cumulative balance takes into
154+ // account failed transactions. Ethereum transactions can be mined and fail anyway due to a too low
155+ // gas limit, in which case the amount is not transferred, but the fees are still paid.
156+ func TestOrderedTransactionsWithFailedTransactions (t * testing.T ) {
157+ tt := func (t time.Time ) * time.Time { return & t }
158+ fee := coin .NewAmountFromInt64 (1 )
159+ txs := []* TransactionData {
160+ {
161+ Timestamp : tt (time .Date (2020 , 9 , 15 , 12 , 0 , 0 , 0 , time .UTC )),
162+ Height : 15 ,
163+ Type : TxTypeReceive ,
164+ Amount : coin .NewAmountFromInt64 (100 ),
165+ },
166+ {
167+ Timestamp : tt (time .Date (2020 , 9 , 10 , 12 , 0 , 0 , 0 , time .UTC )),
168+ Height : 10 ,
169+ Type : TxTypeReceive ,
170+ Amount : coin .NewAmountFromInt64 (200 ),
171+ },
172+ {
173+ Timestamp : tt (time .Date (2020 , 9 , 20 , 12 , 0 , 0 , 0 , time .UTC )),
174+ Height : 20 ,
175+ Type : TxTypeReceive ,
176+ Amount : coin .NewAmountFromInt64 (300 ),
177+ },
178+ {
179+ Timestamp : tt (time .Date (2020 , 9 , 21 , 12 , 0 , 0 , 0 , time .UTC )),
180+ Height : 21 ,
181+ Type : TxTypeSendSelf ,
182+ Amount : coin .NewAmountFromInt64 (50 ),
183+ Fee : & fee ,
184+ },
185+ {
186+ Timestamp : tt (time .Date (2020 , 9 , 22 , 12 , 0 , 0 , 0 , time .UTC )),
187+ Height : 22 ,
188+ Type : TxTypeSend ,
189+ Amount : coin .NewAmountFromInt64 (50 ),
190+ Fee : & fee ,
191+ Status : TxStatusFailed ,
192+ },
193+ {
194+ Timestamp : tt (time .Date (2020 , 9 , 23 , 12 , 0 , 0 , 0 , time .UTC )),
195+ Height : 23 ,
196+ Type : TxTypeReceive ,
197+ Amount : coin .NewAmountFromInt64 (1000 ),
198+ Fee : & fee ,
199+ Status : TxStatusFailed ,
200+ },
201+ }
202+
203+ ordered := NewOrderedTransactions (txs )
204+ expectedBalances := []int64 {
205+ 598 , // failed receive tx, nothing changes
206+ 598 , // failed send tx, only fee deducted
207+ 599 ,
208+ 600 ,
209+ 300 ,
210+ 200 ,
211+ }
212+ for i := range ordered {
213+ require .Equal (t , coin .NewAmountFromInt64 (expectedBalances [i ]), ordered [i ].Balance , i )
214+ }
215+ }
0 commit comments