Skip to content

Commit

Permalink
Fix: restore and ethereum websocket disconnetct
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Oct 6, 2021
1 parent 6d3f1cf commit 832131c
Show file tree
Hide file tree
Showing 9 changed files with 533 additions and 463 deletions.
2 changes: 1 addition & 1 deletion cmd/watch_tower/config.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ethereum:
erc20_address: 0x189737A901f57Fc52640F9B96E61019981C6c1eb
user_address: 0x5660A5b91E41033304cB98c8e4895dCd086EC08b
min_payoff: "0"
restore: false
restore: true
types:
- redeem
- refund
Expand Down
57 changes: 50 additions & 7 deletions cmd/watch_tower/swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
type Swap struct {
HashedSecret chain.Hex
Secret chain.Hex
Contract string
Status Status
RefundTime time.Time
Initiator Leg
Expand All @@ -22,8 +21,7 @@ type Swap struct {
// NewSwap -
func NewSwap(event chain.Event) *Swap {
return &Swap{
HashedSecret: event.HashedSecret,
Contract: event.Contract,
HashedSecret: event.HashedSecret(),
Status: StatusEmpty,
}
}
Expand All @@ -39,7 +37,7 @@ func (swap *Swap) log() {

// FromInitEvent -
func (swap *Swap) FromInitEvent(event chain.InitEvent) {
if swap.HashedSecret != event.HashedSecret {
if swap.HashedSecret != event.HashedSecret() {
return
}

Expand All @@ -50,20 +48,25 @@ func (swap *Swap) FromInitEvent(event chain.InitEvent) {
swap.Initiator = Leg{
ChainType: event.Chain,
Address: event.Initiator,
Contract: event.ContractAddress,
Status: StatusInitiated,
}
swap.Acceptor = Leg{
Address: event.Initiator,
Address: event.Participant,
Status: StatusEmpty,
}
swap.Status = StatusInitiatedOnce
case StatusInitiatedOnce:
swap.Acceptor.ChainType = event.Chain
swap.Acceptor.Contract = event.ContractAddress
swap.Acceptor.Status = StatusInitiated
swap.Status = StatusInitiated
}
}

// FromRedeemEvent -
func (swap *Swap) FromRedeemEvent(event chain.RedeemEvent) {
if swap.HashedSecret != event.HashedSecret {
if swap.HashedSecret != event.HashedSecret() {
return
}
if swap.Secret == "" {
Expand All @@ -76,11 +79,18 @@ func (swap *Swap) FromRedeemEvent(event chain.RedeemEvent) {
case StatusRedeemedOnce:
swap.Status = StatusRedeemed
}

if swap.Acceptor.Contract == event.ContractAddress && swap.Acceptor.ChainType == event.Chain {
swap.Acceptor.Status = StatusRedeemed
}
if swap.Initiator.Contract == event.ContractAddress && swap.Initiator.ChainType == event.Chain {
swap.Initiator.Status = StatusRedeemed
}
}

// FromRefundEvent -
func (swap *Swap) FromRefundEvent(event chain.RefundEvent) {
if swap.HashedSecret != event.HashedSecret {
if swap.HashedSecret != event.HashedSecret() {
return
}
switch swap.Status {
Expand All @@ -89,12 +99,45 @@ func (swap *Swap) FromRefundEvent(event chain.RefundEvent) {
case StatusRefundedOnce:
swap.Status = StatusRefunded
}

if swap.Acceptor.Contract == event.ContractAddress && swap.Acceptor.ChainType == event.Chain {
swap.Acceptor.Status = StatusRefunded
}
if swap.Initiator.Contract == event.ContractAddress && swap.Initiator.ChainType == event.Chain {
swap.Initiator.Status = StatusRefunded
}
}

// Leg -
func (swap *Swap) Leg() *Leg {
if swap.Acceptor.ChainType == chain.ChainTypeUnknown || swap.Initiator.ChainType == chain.ChainTypeUnknown {
return nil
}

if swap.Acceptor.IsFinished() && swap.Initiator.IsFinished() {
return nil
}

switch {
case swap.Acceptor.Status > swap.Initiator.Status:
return &swap.Initiator
case swap.Acceptor.Status < swap.Initiator.Status:
return &swap.Acceptor
}
return nil
}

// Leg -
type Leg struct {
ChainType chain.ChainType
Address string
Contract string
Status Status
}

// IsFinished -
func (leg Leg) IsFinished() bool {
return leg.Status == StatusRedeemed || leg.Status == StatusRefunded
}

// Status -
Expand Down
Loading

0 comments on commit 832131c

Please sign in to comment.