Skip to content

Commit 76d5a28

Browse files
committed
review followup
1 parent 45c44b0 commit 76d5a28

18 files changed

Lines changed: 73 additions & 82 deletions

File tree

client/asset/eth/contractor.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,8 @@ func (c *contractorV0) vector(ctx context.Context, locator []byte) (*dexeth.Swap
221221
return nil, err
222222
}
223223
vector := &dexeth.SwapVector{
224-
From: swap.Participant,
225-
To: swap.Initiator,
224+
From: swap.Initiator,
225+
To: swap.Participant,
226226
Value: swap.Value,
227227
SecretHash: secretHash,
228228
LockTime: uint64(swap.LockTime.Unix()),
@@ -243,8 +243,8 @@ func (c *contractorV0) statusAndVector(ctx context.Context, locator []byte) (*de
243243
return nil, nil, err
244244
}
245245
vector := &dexeth.SwapVector{
246-
From: swap.Participant,
247-
To: swap.Initiator,
246+
From: swap.Initiator,
247+
To: swap.Participant,
248248
Value: swap.Value,
249249
SecretHash: secretHash,
250250
LockTime: uint64(swap.LockTime.Unix()),
@@ -452,7 +452,6 @@ func (c *erc20Contractor) balance(ctx context.Context) (*big.Int, error) {
452452
// allowance exposes the read-only allowance method of the erc20 token contract.
453453
func (c *erc20Contractor) allowance(ctx context.Context) (*big.Int, error) {
454454
callOpts := &bind.CallOpts{
455-
// Pending: true, // Seeing errors on even simnet that say "backend does not support pending state"
456455
From: c.acct,
457456
Context: ctx,
458457
}
@@ -947,14 +946,6 @@ func (c *tokenContractorV1) tokenAddress() common.Address {
947946
var _ contractor = (*tokenContractorV1)(nil)
948947
var _ tokenContractor = (*tokenContractorV1)(nil)
949948

950-
// readOnlyCallOpts is the CallOpts used for read-only contract method calls.
951-
func readOnlyCallOpts(ctx context.Context) *bind.CallOpts {
952-
return &bind.CallOpts{
953-
Pending: true,
954-
Context: ctx,
955-
}
956-
}
957-
958949
func estimateGas(ctx context.Context, from, to common.Address, abi *abi.ABI, cb bind.ContractBackend, value *big.Int, method string, args ...interface{}) (uint64, error) {
959950
data, err := abi.Pack(method, args...)
960951
if err != nil {

client/asset/eth/eth.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,7 +1497,7 @@ func (w *TokenWallet) FundOrder(ord *asset.Order) (asset.Coins, []dex.Bytes, uin
14971497
g, err := w.initGasEstimate(int(ord.MaxSwapCount), contractVer,
14981498
ord.RedeemVersion, ord.RedeemAssetID)
14991499
if err != nil {
1500-
return nil, nil, 0, fmt.Errorf("error estimating swap gas: %vlaptop apart comic equip remove adult system tuna office discover toddler can keep fury aware amazing injury typical", err)
1500+
return nil, nil, 0, fmt.Errorf("error estimating swap gas: %v", err)
15011501
}
15021502

15031503
ethToLock := ord.MaxFeeRate * g.Swap * ord.MaxSwapCount
@@ -3446,7 +3446,7 @@ func parseSecretHashes(tx *types.Transaction, contractVer uint32, isInit bool) (
34463446
}
34473447
locators = make([][]byte, 0, len(inits))
34483448
for k := range inits {
3449-
copyK := k // TODO: Is this really necessary?
3449+
copyK := k
34503450
locators = append(locators, copyK[:])
34513451
}
34523452
} else {
@@ -4628,7 +4628,7 @@ func (w *assetWallet) loadContractors() error {
46284628
// withContractor runs the provided function with the versioned contractor.
46294629
func (w *assetWallet) withContractor(contractVer uint32, f func(contractor) error) error {
46304630
if contractVer == dexeth.ContractVersionERC20 {
4631-
// For ERC02 methods, use the most recent contractor version.
4631+
// For ERC20 methods, use the most recent contractor version.
46324632
var bestVer uint32
46334633
var bestContractor contractor
46344634
for ver, c := range w.contractors {

client/asset/eth/eth_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,8 @@ func (c *tContractor) vector(ctx context.Context, locator []byte) (*dexeth.SwapV
377377
return nil, errors.New("swap not in map")
378378
}
379379
v := &dexeth.SwapVector{
380-
From: swap.Participant,
381-
To: swap.Initiator,
380+
From: swap.Initiator,
381+
To: swap.Participant,
382382
Value: swap.Value,
383383
SecretHash: secretHash,
384384
LockTime: uint64(swap.LockTime.Unix()),
@@ -399,8 +399,8 @@ func (c *tContractor) statusAndVector(ctx context.Context, locator []byte) (*dex
399399
return nil, nil, errors.New("swap not in map")
400400
}
401401
v := &dexeth.SwapVector{
402-
From: swap.Participant,
403-
To: swap.Initiator,
402+
From: swap.Initiator,
403+
To: swap.Participant,
404404
Value: swap.Value,
405405
SecretHash: vector.SecretHash,
406406
LockTime: uint64(swap.LockTime.Unix()),

client/core/simnet_trade.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2135,7 +2135,7 @@ func (s *simulationTest) assertBalanceChanges(client *simulationClient, isRefund
21352135
if isRefund {
21362136
// NOTE: Gas price may be higher if the eth harness has
21372137
// had a lot of use. The minimum is the gas tip cap.
2138-
ethRefundFees := int64(dexeth.RefundGas(0 /*version*/)) * dexeth.MinGasTipCap
2138+
ethRefundFees := int64(dexeth.RefundGas(1 /*version*/)) * dexeth.MinGasTipCap
21392139

21402140
msgTx := wire.NewMsgTx(0)
21412141
prevOut := wire.NewOutPoint(&chainhash.Hash{}, 0)

dex/networks/erc20/contracts/ERC20SwapV0.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ contract ERC20Swap {
120120
}
121121

122122
// redeem redeems an array of swaps contract. It checks that the sender is
123-
// not a contract, and that the secret hash hashes to secretHash. The ERC20
123+
// not a contract, and that the secret hashes to secretHash. The ERC20
124124
// tokens are transferred from the contract to the sender.
125125
function redeem(Redemption[] calldata redemptions)
126126
public

dex/networks/erc20/contracts/ERC20SwapV1.sol

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ pragma solidity = 0.8.18;
1414
// When calling initiate, the necessary tokens for swaps are transferred to
1515
// the swap contract. At this point the funds belong to the contract, and
1616
// cannot be accessed by anyone else, not even the contract's deployer. The
17-
// initiator sets a secret hash, a blocktime the funds will be accessible should
18-
// they not be redeemed, and a participant who can redeem before or after the
19-
// locktime. The participant can redeem at any time after the initiation
20-
// transaction is mined if they have the secret that hashes to the secret hash.
21-
// Otherwise, the initiator can refund funds any time after the locktime.
17+
// initiator commits to the swap parameters, including a locktime after which
18+
// the funds will be accessible for refund should they not be redeemed. The
19+
// participant can redeem at any time after the initiation transaction is mined
20+
// if they have the secret that hashes to the secret hash. Otherwise, the
21+
// initiator can refund funds any time after the locktime.
2222
//
2323
// This contract has no limits on gas used for any transactions.
2424
//
@@ -30,7 +30,7 @@ contract ERC20Swap {
3030

3131
address public immutable token_address;
3232

33-
// Step is a type that hold's a contract's current step. Empty is the
33+
// Step is a type that hold's a contract's current step. Empty is the
3434
// uninitiated or null value.
3535
enum Step { Empty, Filled, Redeemed, Refunded }
3636

@@ -41,6 +41,7 @@ contract ERC20Swap {
4141
}
4242

4343
bytes32 constant RefundRecord = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;
44+
bytes32 constant RefundRecordHash = 0xAF9613760F72635FBDB44A5A0A63C39F12AF30F950A6EE5C971BE188E89C4051;
4445

4546
// swaps is a map of contract hashes to the "swap record". The swap record
4647
// has the following interpretation.
@@ -134,6 +135,7 @@ contract ERC20Swap {
134135

135136
require(v.value > 0, "0 val");
136137
require(v.refundTimestamp > 0, "0 refundTimestamp");
138+
require(v.secretHash != RefundRecordHash, "illegal secret hash (refund record hash)");
137139

138140
bytes32 k = contractKey(v);
139141
bytes32 record = swaps[k];
@@ -153,7 +155,7 @@ contract ERC20Swap {
153155
require(success && (data.length == 0 || abi.decode(data, (bool))), 'transfer from failed');
154156
}
155157

156-
// isRedeemable returns whether or not a swap identified by secretHash
158+
// isRedeemable returns whether or not a swap identified by vector
157159
// can be redeemed using secret.
158160
function isRedeemable(Vector calldata v)
159161
public
@@ -165,7 +167,7 @@ contract ERC20Swap {
165167
}
166168

167169
// redeem redeems a Vector. It checks that the sender is not a contract,
168-
// and that the secret hash hashes to secretHash. msg.value is tranfered
170+
// and that the secret hashes to secretHash. msg.value is tranfered
169171
// from ETHSwap to the sender.
170172
//
171173
// To prevent reentry attack, it is very important to check the state of the
@@ -223,14 +225,14 @@ contract ERC20Swap {
223225
(bytes32 k, bytes32 record, uint256 blockNum) = retrieveStatus(v);
224226

225227
// Is this swap initialized?
228+
// This check also guarantees that the swap has not already been
229+
// refunded i.e. record != RefundRecord, since RefundRecord is certainly
230+
// greater than block.number.
226231
require(blockNum > 0 && blockNum <= block.number, "swap not active");
227232

228233
// Is it already redeemed?
229234
require(!secretValidates(record, v.secretHash), "swap already redeemed");
230235

231-
// Is it already refunded?
232-
require(record != RefundRecord, "swap already refunded");
233-
234236
swaps[k] = RefundRecord;
235237

236238
bool success;

dex/networks/erc20/contracts/v1/BinRuntimeV1.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)