From b525e864afbad229ed06100d2ea3282c2d2d1065 Mon Sep 17 00:00:00 2001 From: martonp Date: Mon, 29 Nov 2021 15:45:47 +0700 Subject: [PATCH] Updates based on review. --- client/asset/eth/eth.go | 12 +++++------- client/asset/eth/eth_test.go | 11 +++++------ 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/client/asset/eth/eth.go b/client/asset/eth/eth.go index 0c875f3fd6..73277b0b37 100644 --- a/client/asset/eth/eth.go +++ b/client/asset/eth/eth.go @@ -727,22 +727,22 @@ func (r *swapReceipt) Expiration() time.Time { func (r *swapReceipt) Coin() asset.Coin { return &coin{ value: r.value, - id: dex.Bytes(r.txHash[:]), + id: r.txHash[:], } } // Contract returns the swap's secret hash. func (r *swapReceipt) Contract() dex.Bytes { - return dex.Bytes(r.secretHash[:]) + return r.secretHash[:] } // String returns a string representation of the swapReceipt. func (r *swapReceipt) String() string { - return fmt.Sprintf("tx hash: %x, secret hash: %x", r.txHash, r.secretHash) + return fmt.Sprintf("{ tx hash: %x, secret hash: %x }", r.txHash, r.secretHash) } // SignedRefund returns an empty byte array. ETH does not support a pre-signed -// redeem script becuase the nonce needed in the transaction cannot be previously +// redeem script because the nonce needed in the transaction cannot be previously // determined. func (*swapReceipt) SignedRefund() dex.Bytes { return dex.Bytes{} @@ -861,10 +861,8 @@ func (eth *ExchangeWallet) Swap(swaps *asset.Swaps) ([]asset.Receipt, asset.Coin eth.unlockFunds(swaps.Inputs) var change asset.Coin changeAmount := totalInputValue - totalUsedValue - if changeAmount > 0 { + if changeAmount > 0 && swaps.LockChange { change = eth.createAmountCoin(changeAmount) - } - if swaps.LockChange && change != nil { eth.lockFunds(asset.Coins{change}) } diff --git a/client/asset/eth/eth_test.go b/client/asset/eth/eth_test.go index e9c2ed7d0c..43326e044a 100644 --- a/client/asset/eth/eth_test.go +++ b/client/asset/eth/eth_test.go @@ -1080,10 +1080,6 @@ func TestSwap(t *testing.T) { t.Fatalf("%v: receipt coin value: %v != expected: %v", testName, receipt.Coin().Value(), contract.Value) } - if len(receipt.Contract()) != srveth.SecretHashSize { - t.Fatalf("%v: expected length of contract to be %v but got %v", - testName, srveth.SecretHashSize, len(receipt.Contract())) - } if !bytes.Equal(receipt.Contract(), contract.SecretHash[:]) { t.Fatalf("%v, contract: %x != secret hash in input: %x", testName, receipt.Contract(), contract.SecretHash) @@ -1170,8 +1166,11 @@ func TestSwap(t *testing.T) { gasNeededForSwaps(len(swaps.Contracts))*swaps.FeeRate if expectedChangeValue == 0 && changeCoin != nil { t.Fatalf("%v: change coin should be nil if change is 0", testName) - } else if expectedChangeValue > 0 && changeCoin == nil { - t.Fatalf("%v: change coin should not be nil if there is expected change", testName) + } else if expectedChangeValue > 0 && changeCoin == nil && swaps.LockChange { + t.Fatalf("%v: change coin should not be nil if there is expected change and change is locked", + testName) + } else if !swaps.LockChange && changeCoin != nil { + t.Fatalf("%v: change should be nil if LockChange==False", testName) } else if changeCoin != nil && changeCoin.Value() != expectedChangeValue { t.Fatalf("%v: expected change value %v != change coin value: %v", testName, expectedChangeValue, changeCoin.Value())