Skip to content

Commit

Permalink
networks/eth: Move generated files to swap.
Browse files Browse the repository at this point in the history
In order to avoid possible naming conflicts with generated files, move
them to their own package.
  • Loading branch information
JoeGruffins committed Nov 30, 2021
1 parent b66b579 commit 2ae68af
Show file tree
Hide file tree
Showing 15 changed files with 91 additions and 71 deletions.
31 changes: 16 additions & 15 deletions client/asset/eth/contractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"decred.org/dcrdex/dex"
"decred.org/dcrdex/dex/encode"
dexeth "decred.org/dcrdex/dex/networks/eth"
"decred.org/dcrdex/dex/networks/eth/swap"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
Expand All @@ -43,17 +44,17 @@ type contractor interface {
type contractorConstructor func(net dex.Network, addr common.Address, ec *ethclient.Client) (contractor, error)

type contractV0 interface {
Initiate(opts *bind.TransactOpts, initiations []dexeth.ETHSwapInitiation) (*types.Transaction, error)
Redeem(opts *bind.TransactOpts, redemptions []dexeth.ETHSwapRedemption) (*types.Transaction, error)
Swap(opts *bind.CallOpts, secretHash [32]byte) (dexeth.ETHSwapSwap, error)
Initiate(opts *bind.TransactOpts, initiations []swap.ETHSwapInitiation) (*types.Transaction, error)
Redeem(opts *bind.TransactOpts, redemptions []swap.ETHSwapRedemption) (*types.Transaction, error)
Swap(opts *bind.CallOpts, secretHash [32]byte) (swap.ETHSwapSwap, error)
Refund(opts *bind.TransactOpts, secretHash [32]byte) (*types.Transaction, error)
IsRedeemable(opts *bind.CallOpts, secretHash [32]byte, secret [32]byte) (bool, error)
}

// contractorV0 is the contractor for contract version 0.
// Redeem and Refund methods of ETHSwap already have suitable return types.
// Redeem and Refund methods of swap.ETHSwap already have suitable return types.
type contractorV0 struct {
contractV0 // *dexeth.ETHSwap
contractV0 // *swap.ETHSwap
abi abi.ABI
ec *ethclient.Client
contractAddr common.Address
Expand All @@ -65,11 +66,11 @@ func newV0contractor(net dex.Network, acctAddr common.Address, ec *ethclient.Cli
if !exists || contractAddr == (common.Address{}) {
return nil, fmt.Errorf("no contract address for version 0, net %s", net)
}
c, err := dexeth.NewETHSwap(contractAddr, ec)
c, err := swap.NewETHSwap(contractAddr, ec)
if err != nil {
return nil, err
}
parsedABI, err := abi.JSON(strings.NewReader(dexeth.ETHSwapABI))
parsedABI, err := abi.JSON(strings.NewReader(swap.ETHSwapABI))
if err != nil {
return nil, err
}
Expand All @@ -83,7 +84,7 @@ func newV0contractor(net dex.Network, acctAddr common.Address, ec *ethclient.Cli
}

func (c *contractorV0) initiate(txOpts *bind.TransactOpts, contracts []*asset.Contract) (*types.Transaction, error) {
inits := make([]dexeth.ETHSwapInitiation, 0, len(contracts))
inits := make([]swap.ETHSwapInitiation, 0, len(contracts))
secrets := make(map[[32]byte]bool, len(contracts))

for _, contract := range contracts {
Expand All @@ -105,7 +106,7 @@ func (c *contractorV0) initiate(txOpts *bind.TransactOpts, contracts []*asset.Co
return nil, fmt.Errorf("%q is not an address", contract.Address)
}

inits = append(inits, dexeth.ETHSwapInitiation{
inits = append(inits, swap.ETHSwapInitiation{
RefundTimestamp: big.NewInt(int64(contract.LockTime)),
SecretHash: secretHash,
Participant: common.HexToAddress(contract.Address),
Expand All @@ -116,7 +117,7 @@ func (c *contractorV0) initiate(txOpts *bind.TransactOpts, contracts []*asset.Co
}

func (c *contractorV0) redeem(txOpts *bind.TransactOpts, redemptions []*asset.Redemption) (*types.Transaction, error) {
redemps := make([]dexeth.ETHSwapRedemption, 0, len(redemptions))
redemps := make([]swap.ETHSwapRedemption, 0, len(redemptions))
for _, r := range redemptions {
secretB, secretHashB := r.Secret, r.Spends.SecretHash
if len(secretB) != 32 || len(secretHashB) != 32 {
Expand All @@ -125,7 +126,7 @@ func (c *contractorV0) redeem(txOpts *bind.TransactOpts, redemptions []*asset.Re
var secret, secretHash [32]byte
copy(secret[:], secretB)
copy(secretHash[:], secretHashB)
redemps = append(redemps, dexeth.ETHSwapRedemption{
redemps = append(redemps, swap.ETHSwapRedemption{
Secret: secret,
SecretHash: secretHash,
})
Expand Down Expand Up @@ -164,9 +165,9 @@ func (c *contractorV0) isRedeemable(secretHash, secret [32]byte) (bool, error) {
}

func (c *contractorV0) estimateRedeemGas(ctx context.Context, secrets [][32]byte) (uint64, error) {
redemps := make([]dexeth.ETHSwapRedemption, 0, len(secrets))
redemps := make([]swap.ETHSwapRedemption, 0, len(secrets))
for _, secret := range secrets {
redemps = append(redemps, dexeth.ETHSwapRedemption{
redemps = append(redemps, swap.ETHSwapRedemption{
Secret: secret,
SecretHash: sha256.Sum256(secret[:]),
})
Expand Down Expand Up @@ -197,11 +198,11 @@ func (c *contractorV0) estimateRefundGas(ctx context.Context, secretHash [32]byt
}

func (c *contractorV0) estimateInitGas(ctx context.Context, n int) (uint64, error) {
initiations := make([]dexeth.ETHSwapInitiation, 0, n)
initiations := make([]swap.ETHSwapInitiation, 0, n)
for j := 0; j < n; j++ {
var secretHash [32]byte
copy(secretHash[:], encode.RandomBytes(32))
initiations = append(initiations, dexeth.ETHSwapInitiation{
initiations = append(initiations, swap.ETHSwapInitiation{
RefundTimestamp: big.NewInt(1),
SecretHash: secretHash,
Participant: c.acctAddr,
Expand Down
10 changes: 5 additions & 5 deletions client/asset/eth/contractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"decred.org/dcrdex/client/asset"
"decred.org/dcrdex/dex/encode"
dexeth "decred.org/dcrdex/dex/networks/eth"
"decred.org/dcrdex/dex/networks/eth/swap"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/core/types"
)
Expand All @@ -18,16 +18,16 @@ type tContractV0 struct {
swapErr error
}

func (c tContractV0) Initiate(opts *bind.TransactOpts, initiations []dexeth.ETHSwapInitiation) (*types.Transaction, error) {
func (c tContractV0) Initiate(opts *bind.TransactOpts, initiations []swap.ETHSwapInitiation) (*types.Transaction, error) {
return nil, c.initErr
}

func (c tContractV0) Redeem(opts *bind.TransactOpts, redemptions []dexeth.ETHSwapRedemption) (*types.Transaction, error) {
func (c tContractV0) Redeem(opts *bind.TransactOpts, redemptions []swap.ETHSwapRedemption) (*types.Transaction, error) {
return nil, c.redeemErr
}

func (c tContractV0) Swap(opts *bind.CallOpts, secretHash [32]byte) (dexeth.ETHSwapSwap, error) {
return dexeth.ETHSwapSwap{
func (c tContractV0) Swap(opts *bind.CallOpts, secretHash [32]byte) (swap.ETHSwapSwap, error) {
return swap.ETHSwapSwap{
InitBlockNumber: new(big.Int),
RefundBlockTimestamp: new(big.Int),
Value: new(big.Int),
Expand Down
3 changes: 2 additions & 1 deletion client/asset/eth/nodeclient_harness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
"decred.org/dcrdex/dex"
"decred.org/dcrdex/dex/encode"
dexeth "decred.org/dcrdex/dex/networks/eth"
"decred.org/dcrdex/dex/networks/eth/swap"
"decred.org/dcrdex/internal/eth/reentryattack"
"github.com/davecgh/go-spew/spew"
"github.com/ethereum/go-ethereum/accounts"
Expand Down Expand Up @@ -1294,7 +1295,7 @@ func testGetCodeAt(t *testing.T) {
if err != nil {
t.Fatalf("Failed to get bytecode: %v", err)
}
c, err := hex.DecodeString(dexeth.ETHSwapRuntimeBin)
c, err := hex.DecodeString(swap.ETHSwapRuntimeBin)
if err != nil {
t.Fatalf("Error decoding")
}
Expand Down
File renamed without changes.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2ae68af

Please sign in to comment.