package xmrmaker

import (
	"bytes"
	"context"

	"github.com/noot/atomic-swap/net/message"
	"github.com/noot/atomic-swap/protocol/backend"
	"github.com/noot/atomic-swap/swapfactory"

	ethcommon "github.com/ethereum/go-ethereum/common"
)

// swapFactoryRuntimeBin is generated by using the geth `evm` binary or with solc --runtime-bin
//nolint:lll
var swapFactoryRuntimeBin = "[encoded data, 6756 characters]"

func checkContractCode(ctx context.Context, b backend.Backend, contractAddr ethcommon.Address) error {
	code, err := b.CodeAt(ctx, contractAddr, nil)
	if err != nil {
		return err
	}

	expectedCode := ethcommon.FromHex(swapFactoryRuntimeBin)
	if !bytes.Contains(expectedCode, code) {
		return errInvalidSwapContract
	}

	return nil
}

func convertContractSwap(msg *message.ContractSwap) swapfactory.SwapFactorySwap {
	return swapfactory.SwapFactorySwap{
		Owner:        msg.Owner,
		Claimer:      msg.Claimer,
		PubKeyClaim:  msg.PubKeyClaim,
		PubKeyRefund: msg.PubKeyRefund,
		Timeout0:     msg.Timeout0,
		Timeout1:     msg.Timeout1,
		Value:        msg.Value,
		Nonce:        msg.Nonce,
	}
}
