Skip to content

Commit

Permalink
remove erc20 bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
ralph-pichler committed Mar 3, 2021
1 parent 7efc2a6 commit 5399c7d
Show file tree
Hide file tree
Showing 9 changed files with 278 additions and 143 deletions.
1 change: 0 additions & 1 deletion pkg/node/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ func InitChequebookService(
overlayEthAddress,
chequeSigner,
chequebook.NewSimpleSwapBindings,
chequebook.NewERC20Bindings,
)
if err != nil {
return nil, fmt.Errorf("chequebook init: %w", err)
Expand Down
12 changes: 0 additions & 12 deletions pkg/settlement/swap/chequebook/bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,3 @@ type SimpleSwapBindingFunc = func(common.Address, bind.ContractBackend) (SimpleS
func NewSimpleSwapBindings(address common.Address, backend bind.ContractBackend) (SimpleSwapBinding, error) {
return simpleswapfactory.NewERC20SimpleSwap(address, backend)
}

// ERC20Binding is the interface for the generated go bindings for ERC20
type ERC20Binding interface {
BalanceOf(*bind.CallOpts, common.Address) (*big.Int, error)
}

type ERC20BindingFunc = func(common.Address, bind.ContractBackend) (ERC20Binding, error)

// NewERC20Bindings generates the default go bindings
func NewERC20Bindings(address common.Address, backend bind.ContractBackend) (ERC20Binding, error) {
return simpleswapfactory.NewERC20(address, backend)
}
39 changes: 6 additions & 33 deletions pkg/settlement/swap/chequebook/chequebook.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethersphere/bee/pkg/settlement/swap/erc20"
"github.com/ethersphere/bee/pkg/settlement/swap/transaction"
"github.com/ethersphere/bee/pkg/storage"
"github.com/ethersphere/sw3-bindings/v3/simpleswapfactory"
Expand All @@ -36,7 +37,6 @@ var (
chequebookABI = transaction.ParseABIUnchecked(simpleswapfactory.ERC20SimpleSwapABI)
chequeCashedEventType = chequebookABI.Events["ChequeCashed"]
chequeBouncedEventType = chequebookABI.Events["ChequeBounced"]
erc20ABI = transaction.ParseABIUnchecked(simpleswapfactory.ERC20ABI)
)

// Service is the main interface for interacting with the nodes chequebook.
Expand Down Expand Up @@ -70,34 +70,27 @@ type service struct {
chequebookInstance SimpleSwapBinding
ownerAddress common.Address

erc20Address common.Address
erc20Instance ERC20Binding
erc20Service erc20.Service

store storage.StateStorer
chequeSigner ChequeSigner
totalIssuedReserved *big.Int
}

// New creates a new chequebook service for the provided chequebook contract.
func New(backend transaction.Backend, transactionService transaction.Service, address, erc20Address, ownerAddress common.Address, store storage.StateStorer, chequeSigner ChequeSigner, simpleSwapBindingFunc SimpleSwapBindingFunc, erc20BindingFunc ERC20BindingFunc) (Service, error) {
func New(backend transaction.Backend, transactionService transaction.Service, address, ownerAddress common.Address, store storage.StateStorer, chequeSigner ChequeSigner, erc20Service erc20.Service, simpleSwapBindingFunc SimpleSwapBindingFunc) (Service, error) {
chequebookInstance, err := simpleSwapBindingFunc(address, backend)
if err != nil {
return nil, err
}

erc20Instance, err := erc20BindingFunc(erc20Address, backend)
if err != nil {
return nil, err
}

return &service{
backend: backend,
transactionService: transactionService,
address: address,
chequebookInstance: chequebookInstance,
ownerAddress: ownerAddress,
erc20Address: erc20Address,
erc20Instance: erc20Instance,
erc20Service: erc20Service,
store: store,
chequeSigner: chequeSigner,
totalIssuedReserved: big.NewInt(0),
Expand All @@ -111,9 +104,7 @@ func (s *service) Address() common.Address {

// Deposit starts depositing erc20 token into the chequebook. This returns once the transactions has been broadcast.
func (s *service) Deposit(ctx context.Context, amount *big.Int) (hash common.Hash, err error) {
balance, err := s.erc20Instance.BalanceOf(&bind.CallOpts{
Context: ctx,
}, s.ownerAddress)
balance, err := s.erc20Service.BalanceOf(ctx, s.ownerAddress)
if err != nil {
return common.Hash{}, err
}
Expand All @@ -123,25 +114,7 @@ func (s *service) Deposit(ctx context.Context, amount *big.Int) (hash common.Has
return common.Hash{}, ErrInsufficientFunds
}

callData, err := erc20ABI.Pack("transfer", s.address, amount)
if err != nil {
return common.Hash{}, err
}

request := &transaction.TxRequest{
To: &s.erc20Address,
Data: callData,
GasPrice: nil,
GasLimit: 0,
Value: big.NewInt(0),
}

txHash, err := s.transactionService.Send(ctx, request)
if err != nil {
return common.Hash{}, err
}

return txHash, nil
return s.erc20Service.Transfer(ctx, s.address, amount)
}

// Balance returns the token balance of the chequebook.
Expand Down
Loading

0 comments on commit 5399c7d

Please sign in to comment.