/
watcher.go
52 lines (43 loc) 路 1.12 KB
/
watcher.go
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
// Copyright 2023 The AthanorLabs/atomic-swap Authors
// SPDX-License-Identifier: LGPL-3.0-only
package xmrtaker
import (
"errors"
contracts "github.com/athanorlabs/atomic-swap/ethereum"
pcommon "github.com/athanorlabs/atomic-swap/protocol"
ethtypes "github.com/ethereum/go-ethereum/core/types"
)
func (s *swapState) runContractEventWatcher() {
for {
select {
case <-s.ctx.Done():
return
case l := <-s.logClaimedCh:
eventSent, err := s.handleClaimedLogs(&l)
if err != nil {
log.Errorf("failed to handle ready logs: %s", err)
}
if eventSent {
log.Debugf("EventETHClaimed sent, returning from event watcher")
return
}
}
}
}
func (s *swapState) handleClaimedLogs(l *ethtypes.Log) (bool, error) {
err := pcommon.CheckSwapID(l, claimedTopic, s.contractSwapID)
if errors.Is(err, pcommon.ErrLogNotForUs) {
return false, nil
}
if err != nil {
return false, err
}
sk, err := contracts.GetSecretFromLog(l, claimedTopic)
if err != nil {
return false, err
}
// contract was set to ready, send EventReady
event := newEventETHClaimed(sk)
s.eventCh <- event
return true, <-event.errCh
}