Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

networks/eth: Move generated files to swap. #1309

Merged
merged 2 commits into from
Dec 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
swapv0 "decred.org/dcrdex/dex/networks/eth/contracts/v0"
"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 []swapv0.ETHSwapInitiation) (*types.Transaction, error)
Redeem(opts *bind.TransactOpts, redemptions []swapv0.ETHSwapRedemption) (*types.Transaction, error)
Swap(opts *bind.CallOpts, secretHash [32]byte) (swapv0.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 swapv0.ETHSwap already have suitable return types.
type contractorV0 struct {
contractV0 // *dexeth.ETHSwap
contractV0 // *swapv0.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 := swapv0.NewETHSwap(contractAddr, ec)
if err != nil {
return nil, err
}
parsedABI, err := abi.JSON(strings.NewReader(dexeth.ETHSwapABI))
parsedABI, err := abi.JSON(strings.NewReader(swapv0.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([]swapv0.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, swapv0.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([]swapv0.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, swapv0.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([]swapv0.ETHSwapRedemption, 0, len(secrets))
for _, secret := range secrets {
redemps = append(redemps, dexeth.ETHSwapRedemption{
redemps = append(redemps, swapv0.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([]swapv0.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, swapv0.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"
swapv0 "decred.org/dcrdex/dex/networks/eth/contracts/v0"
"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 []swapv0.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 []swapv0.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) (swapv0.ETHSwapSwap, error) {
return swapv0.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"
swapv0 "decred.org/dcrdex/dex/networks/eth/contracts/v0"
"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(swapv0.ETHSwapRuntimeBin)
if err != nil {
t.Fatalf("Error decoding")
}
Expand Down
31 changes: 17 additions & 14 deletions dex/networks/eth/contracts/updatecontract.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,35 @@ then
exit 1
fi

ETH_SWAP_VERSION=$1
SOLIDITY_FILE=ETHSwapV${ETH_SWAP_VERSION}.sol
if [ ! -f ./${SOLIDITY_FILE} ]
VERSION=$1
PKG_NAME=v${VERSION}
SOLIDITY_FILE=./ETHSwapV${VERSION}.sol
if [ ! -f ${SOLIDITY_FILE} ]
then
echo "${SOLIDITY_FILE} does not exist" >&2
exit 1
fi

solc --bin-runtime --optimize ${SOLIDITY_FILE} -o .
BYTECODE=$(<ETHSwap.bin-runtime)
mkdir temp

cat > "../BinRuntimeV${ETH_SWAP_VERSION}.go" <<EOF
solc --bin-runtime --optimize ${SOLIDITY_FILE} -o ./temp/
BYTECODE=$(<./temp/ETHSwap.bin-runtime)

cat > "./${PKG_NAME}/BinRuntimeV${VERSION}.go" <<EOF
// Code generated - DO NOT EDIT.
// This file is a generated binding and any manual changes will be lost.

package eth
package ${PKG_NAME}

const ETHSwapRuntimeBin = "${BYTECODE}"
EOF

rm ETHSwap.bin-runtime

abigen --sol ${SOLIDITY_FILE} --pkg eth --out ../contract.go
abigen --sol ${SOLIDITY_FILE} --pkg ${PKG_NAME} --out ./${PKG_NAME}/contract.go

solc --bin --optimize ${SOLIDITY_FILE} -o .
BYTECODE=$(<ETHSwap.bin)
sed -i.tmp "s/ETH_SWAP_V${ETH_SWAP_VERSION}=.*/ETH_SWAP_V${ETH_SWAP_VERSION}=\"${BYTECODE}\"/" ../../../testing/eth/harness.sh
solc --bin --optimize ${SOLIDITY_FILE} -o ./temp
BYTECODE=$(<./temp/ETHSwap.bin)
sed -i.tmp "s/ETH_SWAP_V${VERSION}=.*/ETH_SWAP_V${VERSION}=\"${BYTECODE}\"/" ../../../testing/eth/harness.sh
# mac needs a temp file specified above.
rm ../../../testing/eth/harness.sh.tmp
rm ETHSwap.bin

rm -fr temp

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

Loading