Skip to content

Commit

Permalink
remove use of bindings for factory
Browse files Browse the repository at this point in the history
  • Loading branch information
ralph-pichler committed Mar 3, 2021
1 parent bacba66 commit 28828d4
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 151 deletions.
10 changes: 2 additions & 8 deletions pkg/node/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,11 @@ func InitChequebookFactory(
logger.Infof("using custom factory address: %x", addr)
}

chequebookFactory, err := chequebook.NewFactory(
return chequebook.NewFactory(
backend,
transactionService,
addr,
chequebook.NewSimpleSwapFactoryBindingFunc,
)
if err != nil {
return nil, fmt.Errorf("new factory: %w", err)
}

return chequebookFactory, nil
), nil
}

// InitChequebookService will initialize the chequebook service with the given
Expand Down
13 changes: 0 additions & 13 deletions pkg/settlement/swap/chequebook/bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,3 @@ type ERC20BindingFunc = func(common.Address, bind.ContractBackend) (ERC20Binding
func NewERC20Bindings(address common.Address, backend bind.ContractBackend) (ERC20Binding, error) {
return simpleswapfactory.NewERC20(address, backend)
}

// SimpleSwapFactoryBinding is the interface for the generated go bindings for SimpleSwapFactory
type SimpleSwapFactoryBinding interface {
DeployedContracts(*bind.CallOpts, common.Address) (bool, error)
ERC20Address(*bind.CallOpts) (common.Address, error)
}

type SimpleSwapFactoryBindingFunc = func(common.Address, bind.ContractBackend) (SimpleSwapFactoryBinding, error)

// NewSimpleSwapFactoryBindingFunc generates the default go bindings
func NewSimpleSwapFactoryBindingFunc(address common.Address, backend bind.ContractBackend) (SimpleSwapFactoryBinding, error) {
return simpleswapfactory.NewSimpleSwapFactory(address, backend)
}
12 changes: 0 additions & 12 deletions pkg/settlement/swap/chequebook/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,6 @@ import (
"github.com/ethersphere/bee/pkg/settlement/swap/chequebook"
)

type simpleSwapFactoryBindingMock struct {
erc20Address func(*bind.CallOpts) (common.Address, error)
deployedContracts func(*bind.CallOpts, common.Address) (bool, error)
}

func (m *simpleSwapFactoryBindingMock) DeployedContracts(o *bind.CallOpts, a common.Address) (bool, error) {
return m.deployedContracts(o, a)
}
func (m *simpleSwapFactoryBindingMock) ERC20Address(o *bind.CallOpts) (common.Address, error) {
return m.erc20Address(o)
}

type simpleSwapBindingMock struct {
balance func(*bind.CallOpts) (*big.Int, error)
issuer func(*bind.CallOpts) (common.Address, error)
Expand Down
51 changes: 34 additions & 17 deletions pkg/settlement/swap/chequebook/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"fmt"
"math/big"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethersphere/bee/pkg/settlement/swap/transaction"
"github.com/ethersphere/sw3-bindings/v3/simpleswapfactory"
Expand Down Expand Up @@ -43,27 +43,19 @@ type factory struct {
backend transaction.Backend
transactionService transaction.Service
address common.Address

instance SimpleSwapFactoryBinding
}

type simpleSwapDeployedEvent struct {
ContractAddress common.Address
}

// NewFactory creates a new factory service for the provided factory contract.
func NewFactory(backend transaction.Backend, transactionService transaction.Service, address common.Address, simpleSwapFactoryBindingFunc SimpleSwapFactoryBindingFunc) (Factory, error) {
instance, err := simpleSwapFactoryBindingFunc(address, backend)
if err != nil {
return nil, err
}

func NewFactory(backend transaction.Backend, transactionService transaction.Service, address common.Address) Factory {
return &factory{
backend: backend,
transactionService: transactionService,
address: address,
instance: instance,
}, nil
}
}

// Deploy deploys a new chequebook and returns once the transaction has been submitted.
Expand Down Expand Up @@ -121,12 +113,25 @@ func (c *factory) VerifyBytecode(ctx context.Context) (err error) {

// VerifyChequebook checks that the supplied chequebook has been deployed by this factory.
func (c *factory) VerifyChequebook(ctx context.Context, chequebook common.Address) error {
deployed, err := c.instance.DeployedContracts(&bind.CallOpts{
Context: ctx,
}, chequebook)
callData, err := factoryABI.Pack("deployedContracts", chequebook)
if err != nil {
return err
}

output, err := c.transactionService.Call(ctx, &transaction.TxRequest{
To: &c.address,
Data: callData,
})
if err != nil {
return err
}

results, err := factoryABI.Unpack("deployedContracts", output)
if err != nil {
return err
}

deployed := *abi.ConvertType(results[0], new(bool)).(*bool)
if !deployed {
return ErrNotDeployedByFactory
}
Expand All @@ -135,13 +140,25 @@ func (c *factory) VerifyChequebook(ctx context.Context, chequebook common.Addres

// ERC20Address returns the token for which this factory deploys chequebooks.
func (c *factory) ERC20Address(ctx context.Context) (common.Address, error) {
erc20Address, err := c.instance.ERC20Address(&bind.CallOpts{
Context: ctx,
callData, err := factoryABI.Pack("ERC20Address")
if err != nil {
return common.Address{}, err
}

output, err := c.transactionService.Call(ctx, &transaction.TxRequest{
To: &c.address,
Data: callData,
})
if err != nil {
return common.Address{}, err
}
return erc20Address, nil

results, err := factoryABI.Unpack("ERC20Address", output)
if err != nil {
return common.Address{}, err
}

return *abi.ConvertType(results[0], new(common.Address)).(*common.Address), nil
}

// DiscoverFactoryAddress returns the canonical factory for this chainID
Expand Down
Loading

0 comments on commit 28828d4

Please sign in to comment.