Skip to content

Commit cd4866c

Browse files
committed
checkpoint. redid server. tests passing. no v1 tests on server yet.
1 parent fe3fb28 commit cd4866c

17 files changed

Lines changed: 904 additions & 587 deletions

File tree

client/asset/eth/contractor.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -585,8 +585,7 @@ func (c *tokenContractorV0) tokenAddress() common.Address {
585585

586586
type contractorV1 struct {
587587
contractV1
588-
abi *abi.ABI
589-
// net dex.Network
588+
abi *abi.ABI
590589
contractAddr common.Address
591590
acctAddr common.Address
592591
cb bind.ContractBackend
@@ -598,15 +597,13 @@ type contractorV1 struct {
598597
var _ contractor = (*contractorV1)(nil)
599598

600599
func newV1Contractor(net dex.Network, contractAddr, acctAddr common.Address, cb bind.ContractBackend) (contractor, error) {
601-
602600
c, err := swapv1.NewETHSwap(contractAddr, cb)
603601
if err != nil {
604602
return nil, err
605603
}
606604
return &contractorV1{
607-
contractV1: c,
608-
abi: dexeth.ABIs[1],
609-
// net: net,
605+
contractV1: c,
606+
abi: dexeth.ABIs[1],
610607
contractAddr: contractAddr,
611608
acctAddr: acctAddr,
612609
cb: cb,
@@ -698,7 +695,7 @@ func (c *contractorV1) redeem(txOpts *bind.TransactOpts, redeems []*asset.Redemp
698695

699696
// Not checking version from DecodeLocator because it was already
700697
// audited and incorrect version locator would err below anyway.
701-
_, locator, err := dexeth.DecodeLocator(r.Spends.Contract)
698+
_, locator, err := dexeth.DecodeContractData(r.Spends.Contract)
702699
if err != nil {
703700
return nil, fmt.Errorf("error parsing locator redeem: %w", err)
704701
}

client/asset/eth/eth.go

Lines changed: 25 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,6 @@ const (
9696
// TODO: Find a way to ask the host about their config set max fee and
9797
// gas values.
9898
maxTxFeeGwei = 1_000_000_000
99-
100-
contractVersionERC20 = ^uint32(0)
101-
contractVersionUnknown = contractVersionERC20 - 1
10299
)
103100

104101
var (
@@ -549,14 +546,7 @@ func privKeyFromSeed(seed []byte) (pk []byte, zero func(), err error) {
549546
// contractVersion converts a server version to a contract version. It applies
550547
// to both tokens and eth right now, but that may not always be the case.
551548
func contractVersion(serverVer uint32) uint32 {
552-
switch serverVer {
553-
case 0:
554-
return 0
555-
case 1:
556-
return 1
557-
default:
558-
return contractVersionUnknown
559-
}
549+
return dexeth.ProtocolVersion(serverVer).ContractVersion()
560550
}
561551

562552
func CreateEVMWallet(chainID int64, createWalletParams *asset.CreateWalletParams, compat *CompatibilityData, skipConnect bool) error {
@@ -1326,9 +1316,10 @@ func (w *baseWallet) MaxFundingFees(_ uint32, _ uint64, _ map[string]string) uin
13261316

13271317
// SingleLotSwapRefundFees returns the fees for a swap transaction for a single lot.
13281318
func (w *assetWallet) SingleLotSwapRefundFees(serverVer uint32, feeSuggestion uint64, _ bool) (swapFees uint64, refundFees uint64, err error) {
1329-
g := w.gases(contractVersion(serverVer))
1319+
contractVer := contractVersion(serverVer)
1320+
g := w.gases(contractVer)
13301321
if g == nil {
1331-
return 0, 0, fmt.Errorf("no gases known for %d version %d", w.assetID, serverVer)
1322+
return 0, 0, fmt.Errorf("no gases known for %d, contract version %d", w.assetID, contractVer)
13321323
}
13331324
return g.Swap * feeSuggestion, g.Refund * feeSuggestion, nil
13341325
}
@@ -2155,7 +2146,7 @@ func (w *assetWallet) Redeem(form *asset.RedeemForm, feeWallet *assetWallet, non
21552146
// from redemption.Spends.Contract. Even for scriptable UTXO assets, the
21562147
// redeem script in this Contract field is redundant with the SecretHash
21572148
// field as ExtractSwapDetails can be applied to extract the hash.
2158-
ver, locator, err := dexeth.DecodeLocator(redemption.Spends.Contract)
2149+
ver, locator, err := dexeth.DecodeContractData(redemption.Spends.Contract)
21592150
if err != nil {
21602151
return fail(fmt.Errorf("Redeem: invalid versioned swap contract data: %w", err))
21612152
}
@@ -2310,7 +2301,7 @@ func recoverPubkey(msgHash, sig []byte) ([]byte, error) {
23102301
// tokenBalance checks the token balance of the account handled by the wallet.
23112302
func (w *assetWallet) tokenBalance() (bal *big.Int, err error) {
23122303
// We don't care about the version.
2313-
return bal, w.withTokenContractor(w.assetID, contractVersionERC20, func(c tokenContractor) error {
2304+
return bal, w.withTokenContractor(w.assetID, dexeth.ContractVersionERC20, func(c tokenContractor) error {
23142305
bal, err = c.balance(w.ctx)
23152306
return err
23162307
})
@@ -2319,7 +2310,7 @@ func (w *assetWallet) tokenBalance() (bal *big.Int, err error) {
23192310
// tokenAllowance checks the amount of tokens that the swap contract is approved
23202311
// to spend on behalf of the account handled by the wallet.
23212312
func (w *assetWallet) tokenAllowance() (allowance *big.Int, err error) {
2322-
return allowance, w.withTokenContractor(w.assetID, contractVersionERC20, func(c tokenContractor) error {
2313+
return allowance, w.withTokenContractor(w.assetID, dexeth.ContractVersionERC20, func(c tokenContractor) error {
23232314
allowance, err = c.allowance(w.ctx)
23242315
return err
23252316
})
@@ -2685,7 +2676,7 @@ func (w *assetWallet) AuditContract(coinID, contract, serializedTx dex.Bytes, re
26852676
return nil, fmt.Errorf("AuditContract: coin id != txHash - coin id: %x, txHash: %s", coinID, tx.Hash())
26862677
}
26872678

2688-
version, locator, err := dexeth.DecodeLocator(contract)
2679+
version, locator, err := dexeth.DecodeContractData(contract)
26892680
if err != nil {
26902681
return nil, fmt.Errorf("AuditContract: failed to decode contract data: %w", err)
26912682
}
@@ -2727,19 +2718,8 @@ func (w *assetWallet) AuditContract(coinID, contract, serializedTx dex.Bytes, re
27272718
if !ok {
27282719
return nil, errors.New("AuditContract: tx does not initiate secret hash")
27292720
}
2730-
// Check vector equivalence. Secret hash equivalence is implied by the
2731-
// vectors presence in the map returned from ParseInitiateData.
2732-
if vec.Value != txVec.Value {
2733-
return nil, errors.New("tx data value doesn't match reported locator data")
2734-
}
2735-
if vec.To != txVec.To {
2736-
return nil, errors.New("tx to address doesn't match reported locator data")
2737-
}
2738-
if vec.From != txVec.From {
2739-
return nil, errors.New("tx from address doesn't match reported locator data")
2740-
}
2741-
if vec.LockTime != txVec.LockTime {
2742-
return nil, errors.New("tx lock time doesn't match reported locator data")
2721+
if !dexeth.CompareVectors(vec, txVec) {
2722+
return nil, fmt.Errorf("tx vector doesn't match expectation. %+v != %+v", txVec, vec)
27432723
}
27442724
val = vec.Value
27452725
participant = vec.To.String()
@@ -2788,7 +2768,7 @@ func (w *assetWallet) LockTimeExpired(ctx context.Context, lockTime time.Time) (
27882768
// ContractLockTimeExpired returns true if the specified contract's locktime has
27892769
// expired, making it possible to issue a Refund.
27902770
func (w *assetWallet) ContractLockTimeExpired(ctx context.Context, contract dex.Bytes) (bool, time.Time, error) {
2791-
contractVer, locator, err := dexeth.DecodeLocator(contract)
2771+
contractVer, locator, err := dexeth.DecodeContractData(contract)
27922772
if err != nil {
27932773
return false, time.Time{}, err
27942774
}
@@ -2855,7 +2835,7 @@ func (w *assetWallet) FindRedemption(ctx context.Context, _, contract dex.Bytes)
28552835
// contract, so we are basically doing the next best thing here.
28562836
const coinIDTmpl = coinIDTakerFoundMakerRedemption + "%s"
28572837

2858-
contractVer, locator, err := dexeth.DecodeLocator(contract)
2838+
contractVer, locator, err := dexeth.DecodeContractData(contract)
28592839
if err != nil {
28602840
return nil, nil, err
28612841
}
@@ -2934,7 +2914,7 @@ func (w *assetWallet) findSecret(locator []byte, contractVer uint32) ([]byte, st
29342914
// Refund refunds a contract. This can only be used after the time lock has
29352915
// expired.
29362916
func (w *assetWallet) Refund(_, contract dex.Bytes, feeRate uint64) (dex.Bytes, error) {
2937-
contractVer, locator, err := dexeth.DecodeLocator(contract)
2917+
contractVer, locator, err := dexeth.DecodeContractData(contract)
29382918
if err != nil {
29392919
return nil, fmt.Errorf("Refund: failed to decode contract: %w", err)
29402920
}
@@ -3032,7 +3012,7 @@ func (w *ETHWallet) EstimateRegistrationTxFee(feeRate uint64) uint64 {
30323012
// EstimateRegistrationTxFee returns an estimate for the tx fee needed to
30333013
// pay the registration fee using the provided feeRate.
30343014
func (w *TokenWallet) EstimateRegistrationTxFee(feeRate uint64) uint64 {
3035-
g := w.gases(contractVersionERC20)
3015+
g := w.gases(dexeth.ContractVersionERC20)
30363016
if g == nil {
30373017
w.log.Errorf("no gas table")
30383018
return math.MaxUint64
@@ -3107,7 +3087,7 @@ func (w *TokenWallet) canSend(value uint64, verifyBalance, isPreEstimate bool) (
31073087
return 0, nil, fmt.Errorf("error getting max fee rate: %w", err)
31083088
}
31093089

3110-
g := w.gases(contractVersionERC20)
3090+
g := w.gases(dexeth.ContractVersionERC20)
31113091
if g == nil {
31123092
return 0, nil, fmt.Errorf("gas table not found")
31133093
}
@@ -3199,7 +3179,7 @@ func (w *ETHWallet) RestorationInfo(seed []byte) ([]*asset.WalletRestoration, er
31993179
// SwapConfirmations gets the number of confirmations and the spend status
32003180
// for the specified swap.
32013181
func (w *assetWallet) SwapConfirmations(ctx context.Context, coinID dex.Bytes, contract dex.Bytes, _ time.Time) (confs uint32, spent bool, err error) {
3202-
contractVer, secretHash, err := dexeth.DecodeLocator(contract)
3182+
contractVer, secretHash, err := dexeth.DecodeContractData(contract)
32033183
if err != nil {
32043184
return 0, false, err
32053185
}
@@ -3384,7 +3364,7 @@ func (eth *assetWallet) DynamicRedemptionFeesPaid(ctx context.Context, coinID, c
33843364
// secret hashes.
33853365
func (eth *baseWallet) swapOrRedemptionFeesPaid(ctx context.Context, coinID, contractData dex.Bytes,
33863366
isInit bool) (fee uint64, secretHashes [][]byte, err error) {
3387-
contractVer, locator, err := dexeth.DecodeLocator(contractData)
3367+
contractVer, locator, err := dexeth.DecodeContractData(contractData)
33883368
if err != nil {
33893369
return 0, nil, err
33903370
}
@@ -4001,7 +3981,7 @@ func (w *assetWallet) checkUnconfirmedRedemption(locator []byte, contractVer uin
40013981
// entire redemption batch, a new transaction containing only the swap we are
40023982
// searching for will be created.
40033983
func (w *assetWallet) confirmRedemptionWithoutMonitoredTx(txHash common.Hash, redemption *asset.Redemption, feeWallet *assetWallet) (*asset.ConfirmRedemptionStatus, error) {
4004-
contractVer, locator, err := dexeth.DecodeLocator(redemption.Spends.Contract)
3984+
contractVer, locator, err := dexeth.DecodeContractData(redemption.Spends.Contract)
40053985
if err != nil {
40063986
return nil, fmt.Errorf("failed to decode contract data: %w", err)
40073987
}
@@ -4094,7 +4074,7 @@ func (w *assetWallet) confirmRedemption(coinID dex.Bytes, redemption *asset.Rede
40944074
txHash = monitoredTxHash
40954075
}
40964076

4097-
contractVer, locator, err := dexeth.DecodeLocator(redemption.Spends.Contract)
4077+
contractVer, locator, err := dexeth.DecodeContractData(redemption.Spends.Contract)
40984078
if err != nil {
40994079
return nil, fmt.Errorf("failed to decode contract data: %w", err)
41004080
}
@@ -4495,7 +4475,7 @@ func (w *ETHWallet) sendToAddr(addr common.Address, amt uint64, maxFeeRate *big.
44954475
func (w *TokenWallet) sendToAddr(addr common.Address, amt uint64, maxFeeRate *big.Int) (tx *types.Transaction, err error) {
44964476
w.baseWallet.nonceSendMtx.Lock()
44974477
defer w.baseWallet.nonceSendMtx.Unlock()
4498-
g := w.gases(contractVersionERC20)
4478+
g := w.gases(dexeth.ContractVersionERC20)
44994479
if g == nil {
45004480
return nil, fmt.Errorf("no gas table")
45014481
}
@@ -4504,7 +4484,7 @@ func (w *TokenWallet) sendToAddr(addr common.Address, amt uint64, maxFeeRate *bi
45044484
if err != nil {
45054485
return nil, err
45064486
}
4507-
return tx, w.withTokenContractor(w.assetID, contractVersionERC20, func(c tokenContractor) error {
4487+
return tx, w.withTokenContractor(w.assetID, dexeth.ContractVersionERC20, func(c tokenContractor) error {
45084488
tx, err = c.transfer(txOpts, addr, w.evmify(amt))
45094489
if err != nil {
45104490
c.voidUnusedNonce()
@@ -4627,7 +4607,7 @@ func (w *assetWallet) loadContractors() error {
46274607

46284608
// withContractor runs the provided function with the versioned contractor.
46294609
func (w *assetWallet) withContractor(contractVer uint32, f func(contractor) error) error {
4630-
if contractVer == contractVersionERC20 {
4610+
if contractVer == dexeth.ContractVersionERC20 {
46314611
// For ERC02 methods, use the most recent contractor version.
46324612
var bestVer uint32
46334613
var bestContractor contractor
@@ -4660,16 +4640,17 @@ func (w *assetWallet) withTokenContractor(assetID, ver uint32, f func(tokenContr
46604640
// estimateApproveGas estimates the gas required for a transaction approving a
46614641
// spender for an ERC20 contract.
46624642
func (w *assetWallet) estimateApproveGas(newGas *big.Int) (gas uint64, err error) {
4663-
return gas, w.withTokenContractor(w.assetID, contractVersionERC20, func(c tokenContractor) error {
4643+
return gas, w.withTokenContractor(w.assetID, dexeth.ContractVersionERC20, func(c tokenContractor) error {
46644644
gas, err = c.estimateApproveGas(w.ctx, newGas)
46654645
return err
46664646
})
46674647
}
46684648

46694649
// estimateTransferGas estimates the gas needed for a token transfer call to an
46704650
// ERC20 contract.
4651+
// TODO: Delete this and contractor methods. Unused.
46714652
func (w *assetWallet) estimateTransferGas(val uint64) (gas uint64, err error) {
4672-
return gas, w.withTokenContractor(w.assetID, contractVersionERC20, func(c tokenContractor) error {
4653+
return gas, w.withTokenContractor(w.assetID, dexeth.ContractVersionERC20, func(c tokenContractor) error {
46734654
gas, err = c.estimateTransferGas(w.ctx, w.evmify(val))
46744655
return err
46754656
})

client/asset/eth/eth_test.go

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2471,7 +2471,7 @@ func testSwap(t *testing.T, assetID uint32) {
24712471
testName, receipt.Coin().Value(), contract.Value)
24722472
}
24732473
contractData := receipt.Contract()
2474-
contractVer, locator, err := dexeth.DecodeLocator(contractData)
2474+
contractVer, locator, err := dexeth.DecodeContractData(contractData)
24752475
if err != nil {
24762476
t.Fatalf("failed to decode contract data: %v", err)
24772477
}
@@ -3047,7 +3047,7 @@ func testRedeem(t *testing.T, assetID uint32) {
30473047
// Check that value of output coin is as axpected
30483048
var totalSwapValue uint64
30493049
for _, redemption := range test.form.Redemptions {
3050-
_, locator, err := dexeth.DecodeLocator(redemption.Spends.Contract)
3050+
_, locator, err := dexeth.DecodeContractData(redemption.Spends.Contract)
30513051
if err != nil {
30523052
t.Fatalf("DecodeLocator: %v", err)
30533053
}
@@ -3302,15 +3302,6 @@ func TestMaxOrder(t *testing.T) {
33023302
}
33033303
}
33043304

3305-
func overMaxWei() *big.Int {
3306-
maxInt := ^uint64(0)
3307-
maxWei := new(big.Int).SetUint64(maxInt)
3308-
gweiFactorBig := big.NewInt(dexeth.GweiFactor)
3309-
maxWei.Mul(maxWei, gweiFactorBig)
3310-
overMaxWei := new(big.Int).Set(maxWei)
3311-
return overMaxWei.Add(overMaxWei, gweiFactorBig)
3312-
}
3313-
33143305
func packInitiateDataV0(initiations []*dexeth.Initiation) ([]byte, error) {
33153306
abiInitiations := make([]swapv0.ETHSwapInitiation, 0, len(initiations))
33163307
for _, init := range initiations {
@@ -3500,7 +3491,7 @@ func testAuditContract(t *testing.T, assetID uint32) {
35003491
t.Fatalf(`"%v": expected contract %x != actual %x`, test.name, test.contract, auditInfo.Contract)
35013492
}
35023493

3503-
_, expectedSecretHash, err := dexeth.DecodeLocator(test.contract)
3494+
_, expectedSecretHash, err := dexeth.DecodeContractData(test.contract)
35043495
if err != nil {
35053496
t.Fatalf(`"%v": failed to decode versioned bytes: %v`, test.name, err)
35063497
}
@@ -4087,24 +4078,14 @@ func testRefundReserves(t *testing.T, assetID uint32) {
40874078
node.swapMap = map[[32]byte]*dexeth.SwapState{secretHash: {}}
40884079

40894080
feeWallet := eth
4090-
gasesV0 := dexeth.VersionedGases[0]
4091-
gasesV1 := &dexeth.Gases{Refund: 1e6}
4081+
gasesV0 := eth.versionedGases[0]
4082+
gasesV1 := eth.versionedGases[1]
40924083
assetV0 := *tETHV0
40934084
assetV1 := *tETHV0
4094-
if assetID == BipID {
4095-
eth.versionedGases[1] = gasesV1
4096-
} else {
4085+
if assetID != BipID {
40974086
feeWallet = node.tokenParent
40984087
assetV0 = *tTokenV0
40994088
assetV1 = *tTokenV0
4100-
tokenContracts := dexeth.Tokens[simnetTokenID].NetTokens[dex.Simnet].SwapContracts
4101-
gasesV0 = &tokenGasesV0
4102-
tc := *tokenContracts[0]
4103-
tc.Gas = *gasesV1
4104-
tokenContracts[1] = &tc
4105-
defer delete(tokenContracts, 1)
4106-
eth.versionedGases[0] = gasesV0
4107-
eth.versionedGases[1] = gasesV1
41084089
node.tokenContractor.bal = dexeth.GweiToWei(1e9)
41094090
}
41104091

@@ -4185,9 +4166,9 @@ func testRedemptionReserves(t *testing.T, assetID uint32) {
41854166
var secretHash [32]byte
41864167
node.tContractor.swapMap[secretHash] = &dexeth.SwapState{}
41874168

4188-
gasesV1 := &dexeth.Gases{Redeem: 1e6, RedeemAdd: 85e5}
4169+
gasesV0 := eth.versionedGases[0]
4170+
gasesV1 := eth.versionedGases[1]
41894171
eth.versionedGases[1] = gasesV1
4190-
gasesV0 := dexeth.VersionedGases[0]
41914172
assetV0 := *tETHV0
41924173
assetV1 := *tETHV0
41934174
feeWallet := eth
@@ -4196,12 +4177,6 @@ func testRedemptionReserves(t *testing.T, assetID uint32) {
41964177
feeWallet = node.tokenParent
41974178
assetV0 = *tTokenV0
41984179
assetV1 = *tTokenV0
4199-
tokenContracts := dexeth.Tokens[simnetTokenID].NetTokens[dex.Simnet].SwapContracts
4200-
gasesV0 = &tokenGasesV0
4201-
tc := *tokenContracts[0]
4202-
tc.Gas = *gasesV1
4203-
tokenContracts[1] = &tc
4204-
defer delete(tokenContracts, 1)
42054180
}
42064181

42074182
assetV0.MaxFeeRate = 45

client/asset/eth/nodeclient.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ import (
3030
)
3131

3232
const (
33-
contractVersionNewest = ^uint32(0)
34-
approveGas = 4e5
33+
approveGas = 4e5
3534
)
3635

3736
var (
@@ -410,9 +409,10 @@ func newTxOpts(ctx context.Context, from common.Address, val, maxGas uint64, max
410409
}
411410

412411
func gases(contractVer uint32, versionedGases map[uint32]*dexeth.Gases) *dexeth.Gases {
413-
if contractVer != contractVersionNewest {
412+
if contractVer != dexeth.ContractVersionERC20 {
414413
return versionedGases[contractVer]
415414
}
415+
416416
var bestVer uint32
417417
var bestGases *dexeth.Gases
418418
for ver, gases := range versionedGases {

0 commit comments

Comments
 (0)