Commit 70770e1d authored by Ralph Pichler's avatar Ralph Pichler Committed by GitHub

Swap ux improvements (#761)

parent 6b731c7f
...@@ -182,11 +182,11 @@ func NewBee(addr string, swarmAddress swarm.Address, publicKey ecdsa.PublicKey, ...@@ -182,11 +182,11 @@ func NewBee(addr string, swarmAddress swarm.Address, publicKey ecdsa.PublicKey,
var found bool var found bool
factoryAddress, found = chequebook.DiscoverFactoryAddress(chainID.Int64()) factoryAddress, found = chequebook.DiscoverFactoryAddress(chainID.Int64())
if !found { if !found {
return nil, errors.New("no known factory address") return nil, errors.New("no known factory address for this network")
} }
logger.Infof("using default factory address for chain id %d: %x", chainID, factoryAddress) logger.Infof("using default factory address for chain id %d: %x", chainID, factoryAddress)
} else if !common.IsHexAddress(o.SwapFactoryAddress) { } else if !common.IsHexAddress(o.SwapFactoryAddress) {
return nil, errors.New("invalid factory address") return nil, errors.New("malformed factory address")
} else { } else {
factoryAddress = common.HexToAddress(o.SwapFactoryAddress) factoryAddress = common.HexToAddress(o.SwapFactoryAddress)
logger.Infof("using custom factory address: %x", factoryAddress) logger.Infof("using custom factory address: %x", factoryAddress)
......
...@@ -163,7 +163,8 @@ func (m *chequeSignerMock) Sign(cheque *chequebook.Cheque) ([]byte, error) { ...@@ -163,7 +163,8 @@ func (m *chequeSignerMock) Sign(cheque *chequebook.Cheque) ([]byte, error) {
type factoryMock struct { type factoryMock struct {
erc20Address func(ctx context.Context) (common.Address, error) erc20Address func(ctx context.Context) (common.Address, error)
deploy func(ctx context.Context, issuer common.Address, defaultHardDepositTimeoutDuration *big.Int) (common.Address, error) deploy func(ctx context.Context, issuer common.Address, defaultHardDepositTimeoutDuration *big.Int) (common.Hash, error)
waitDeployed func(ctx context.Context, txHash common.Hash) (common.Address, error)
verifyBytecode func(ctx context.Context) error verifyBytecode func(ctx context.Context) error
verifyChequebook func(ctx context.Context, chequebook common.Address) error verifyChequebook func(ctx context.Context, chequebook common.Address) error
} }
...@@ -173,11 +174,14 @@ func (m *factoryMock) ERC20Address(ctx context.Context) (common.Address, error) ...@@ -173,11 +174,14 @@ func (m *factoryMock) ERC20Address(ctx context.Context) (common.Address, error)
return m.erc20Address(ctx) return m.erc20Address(ctx)
} }
// Deploy deploys a new chequebook and returns once confirmed. func (m *factoryMock) Deploy(ctx context.Context, issuer common.Address, defaultHardDepositTimeoutDuration *big.Int) (common.Hash, error) {
func (m *factoryMock) Deploy(ctx context.Context, issuer common.Address, defaultHardDepositTimeoutDuration *big.Int) (common.Address, error) {
return m.deploy(ctx, issuer, defaultHardDepositTimeoutDuration) return m.deploy(ctx, issuer, defaultHardDepositTimeoutDuration)
} }
func (m *factoryMock) WaitDeployed(ctx context.Context, txHash common.Hash) (common.Address, error) {
return m.waitDeployed(ctx, txHash)
}
// VerifyBytecode checks that the factory is valid. // VerifyBytecode checks that the factory is valid.
func (m *factoryMock) VerifyBytecode(ctx context.Context) error { func (m *factoryMock) VerifyBytecode(ctx context.Context) error {
return m.verifyBytecode(ctx) return m.verifyBytecode(ctx)
......
...@@ -27,8 +27,10 @@ var ( ...@@ -27,8 +27,10 @@ var (
type Factory interface { type Factory interface {
// ERC20Address returns the token for which this factory deploys chequebooks. // ERC20Address returns the token for which this factory deploys chequebooks.
ERC20Address(ctx context.Context) (common.Address, error) ERC20Address(ctx context.Context) (common.Address, error)
// Deploy deploys a new chequebook and returns once confirmed. // Deploy deploys a new chequebook and returns once the transaction has been submitted.
Deploy(ctx context.Context, issuer common.Address, defaultHardDepositTimeoutDuration *big.Int) (common.Address, error) Deploy(ctx context.Context, issuer common.Address, defaultHardDepositTimeoutDuration *big.Int) (common.Hash, error)
// WaitDeployed waits for the deployment transaction to confirm and returns the chequebook address
WaitDeployed(ctx context.Context, txHash common.Hash) (common.Address, error)
// VerifyBytecode checks that the factory is valid. // VerifyBytecode checks that the factory is valid.
VerifyBytecode(ctx context.Context) error VerifyBytecode(ctx context.Context) error
// VerifyChequebook checks that the supplied chequebook has been deployed by this factory. // VerifyChequebook checks that the supplied chequebook has been deployed by this factory.
...@@ -65,11 +67,11 @@ func NewFactory(backend Backend, transactionService TransactionService, address ...@@ -65,11 +67,11 @@ func NewFactory(backend Backend, transactionService TransactionService, address
}, nil }, nil
} }
// Deploy deploys a new chequebook and returns once confirmed. // Deploy deploys a new chequebook and returns once the transaction has been submitted.
func (c *factory) Deploy(ctx context.Context, issuer common.Address, defaultHardDepositTimeoutDuration *big.Int) (common.Address, error) { func (c *factory) Deploy(ctx context.Context, issuer common.Address, defaultHardDepositTimeoutDuration *big.Int) (common.Hash, error) {
callData, err := c.ABI.Pack("deploySimpleSwap", issuer, big.NewInt(0).Set(defaultHardDepositTimeoutDuration)) callData, err := c.ABI.Pack("deploySimpleSwap", issuer, big.NewInt(0).Set(defaultHardDepositTimeoutDuration))
if err != nil { if err != nil {
return common.Address{}, err return common.Hash{}, err
} }
request := &TxRequest{ request := &TxRequest{
...@@ -82,9 +84,14 @@ func (c *factory) Deploy(ctx context.Context, issuer common.Address, defaultHard ...@@ -82,9 +84,14 @@ func (c *factory) Deploy(ctx context.Context, issuer common.Address, defaultHard
txHash, err := c.transactionService.Send(ctx, request) txHash, err := c.transactionService.Send(ctx, request)
if err != nil { if err != nil {
return common.Address{}, err return common.Hash{}, err
} }
return txHash, nil
}
// WaitDeployed waits for the deployment transaction to confirm and returns the chequebook address
func (c *factory) WaitDeployed(ctx context.Context, txHash common.Hash) (common.Address, error) {
receipt, err := c.transactionService.WaitForReceipt(ctx, txHash) receipt, err := c.transactionService.WaitForReceipt(ctx, txHash)
if err != nil { if err != nil {
return common.Address{}, err return common.Address{}, err
......
...@@ -225,7 +225,16 @@ func TestFactoryDeploy(t *testing.T) { ...@@ -225,7 +225,16 @@ func TestFactoryDeploy(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
chequebookAddress, err := factory.Deploy(context.Background(), issuerAddress, defaultTimeout) txHash, err := factory.Deploy(context.Background(), issuerAddress, defaultTimeout)
if err != nil {
t.Fatal(err)
}
if txHash != deployTransactionHash {
t.Fatalf("returning wrong transaction hash. wanted %x, got %x", deployTransactionHash, txHash)
}
chequebookAddress, err := factory.WaitDeployed(context.Background(), txHash)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
...@@ -237,23 +246,12 @@ func TestFactoryDeploy(t *testing.T) { ...@@ -237,23 +246,12 @@ func TestFactoryDeploy(t *testing.T) {
func TestFactoryDeployReverted(t *testing.T) { func TestFactoryDeployReverted(t *testing.T) {
factoryAddress := common.HexToAddress("0xabcd") factoryAddress := common.HexToAddress("0xabcd")
issuerAddress := common.HexToAddress("0xefff")
defaultTimeout := big.NewInt(1)
deployTransactionHash := common.HexToHash("0xffff") deployTransactionHash := common.HexToHash("0xffff")
factory, err := newTestFactory( factory, err := newTestFactory(
t, t,
factoryAddress, factoryAddress,
&backendMock{}, &backendMock{},
&transactionServiceMock{ &transactionServiceMock{
send: func(ctx context.Context, request *chequebook.TxRequest) (txHash common.Hash, err error) {
if request.To != factoryAddress {
t.Fatalf("sending to wrong address. wanted %x, got %x", factoryAddress, request.To)
}
if request.Value.Cmp(big.NewInt(0)) != 0 {
t.Fatal("trying to send ether")
}
return deployTransactionHash, nil
},
waitForReceipt: func(ctx context.Context, txHash common.Hash) (receipt *types.Receipt, err error) { waitForReceipt: func(ctx context.Context, txHash common.Hash) (receipt *types.Receipt, err error) {
if txHash != deployTransactionHash { if txHash != deployTransactionHash {
t.Fatalf("waiting for wrong transaction. wanted %x, got %x", deployTransactionHash, txHash) t.Fatalf("waiting for wrong transaction. wanted %x, got %x", deployTransactionHash, txHash)
...@@ -268,7 +266,7 @@ func TestFactoryDeployReverted(t *testing.T) { ...@@ -268,7 +266,7 @@ func TestFactoryDeployReverted(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
_, err = factory.Deploy(context.Background(), issuerAddress, defaultTimeout) _, err = factory.WaitDeployed(context.Background(), deployTransactionHash)
if err == nil { if err == nil {
t.Fatal("returned failed chequebook deployment") t.Fatal("returned failed chequebook deployment")
} }
......
...@@ -6,7 +6,7 @@ package chequebook ...@@ -6,7 +6,7 @@ package chequebook
import ( import (
"context" "context"
"errors" "fmt"
"math/big" "math/big"
"github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/accounts/abi/bind"
...@@ -41,6 +41,8 @@ func Init( ...@@ -41,6 +41,8 @@ func Init(
return nil, err return nil, err
} }
logger.Info("no chequebook found, deploying new one.")
var chequebookAddress common.Address var chequebookAddress common.Address
err = stateStore.Get(chequebookKey, &chequebookAddress) err = stateStore.Get(chequebookKey, &chequebookAddress)
if err != nil { if err != nil {
...@@ -62,14 +64,19 @@ func Init( ...@@ -62,14 +64,19 @@ func Init(
} }
if balance.Cmp(big.NewInt(int64(swapInitialDeposit))) < 0 { if balance.Cmp(big.NewInt(int64(swapInitialDeposit))) < 0 {
return nil, errors.New("insufficient token for initial deposit") return nil, fmt.Errorf("insufficient token for initial deposit. Please make sure there is sufficient eth and bzz available on %x", overlayEthAddress)
} }
} }
// if we don't yet have a chequebook, deploy a new one // if we don't yet have a chequebook, deploy a new one
logger.Info("deploying new chequebook") txHash, err := chequebookFactory.Deploy(ctx, overlayEthAddress, big.NewInt(0))
if err != nil {
return nil, err
}
logger.Infof("deploying new chequebook in transaction %x", txHash)
chequebookAddress, err = chequebookFactory.Deploy(ctx, overlayEthAddress, big.NewInt(0)) chequebookAddress, err = chequebookFactory.WaitDeployed(ctx, txHash)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -88,19 +95,19 @@ func Init( ...@@ -88,19 +95,19 @@ func Init(
} }
if swapInitialDeposit != 0 { if swapInitialDeposit != 0 {
logger.Info("depositing into new chequebook") logger.Infof("depositing %d token into new chequebook", swapInitialDeposit)
depositHash, err := chequebookService.Deposit(ctx, big.NewInt(int64(swapInitialDeposit))) depositHash, err := chequebookService.Deposit(ctx, big.NewInt(int64(swapInitialDeposit)))
if err != nil { if err != nil {
return nil, err return nil, err
} }
logger.Infof("sent deposit transaction %x", depositHash)
err = chequebookService.WaitForDeposit(ctx, depositHash) err = chequebookService.WaitForDeposit(ctx, depositHash)
if err != nil { if err != nil {
return nil, err return nil, err
} }
logger.Infof("deposited to chequebook %x in transaction %x", chequebookAddress, depositHash) logger.Info("successfully deposited to chequebook")
} }
} else { } else {
chequebookService, err = New(swapBackend, transactionService, chequebookAddress, erc20Address, overlayEthAddress, stateStore, chequeSigner, simpleSwapBindingFunc, erc20BindingFunc) chequebookService, err = New(swapBackend, transactionService, chequebookAddress, erc20Address, overlayEthAddress, stateStore, chequeSigner, simpleSwapBindingFunc, erc20BindingFunc)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment