Commit 4220008c authored by Ralph Pichler's avatar Ralph Pichler Committed by GitHub

swap without bindings (#1471)

parent bfc0e675
...@@ -134,7 +134,6 @@ func InitChequebookService( ...@@ -134,7 +134,6 @@ func InitChequebookService(
chainID, chainID,
overlayEthAddress, overlayEthAddress,
chequeSigner, chequeSigner,
chequebook.NewSimpleSwapBindings,
) )
if err != nil { if err != nil {
return nil, fmt.Errorf("chequebook init: %w", err) return nil, fmt.Errorf("chequebook init: %w", err)
...@@ -153,11 +152,10 @@ func initChequeStoreCashout( ...@@ -153,11 +152,10 @@ func initChequeStoreCashout(
) (chequebook.ChequeStore, chequebook.CashoutService) { ) (chequebook.ChequeStore, chequebook.CashoutService) {
chequeStore := chequebook.NewChequeStore( chequeStore := chequebook.NewChequeStore(
stateStore, stateStore,
swapBackend,
chequebookFactory, chequebookFactory,
chainID, chainID,
overlayEthAddress, overlayEthAddress,
chequebook.NewSimpleSwapBindings, transactionService,
chequebook.RecoverCheque, chequebook.RecoverCheque,
) )
......
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package chequebook
import (
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethersphere/sw3-bindings/v3/simpleswapfactory"
)
// SimpleSwapBinding is the interface for the generated go bindings for ERC20SimpleSwap
type SimpleSwapBinding interface {
Balance(*bind.CallOpts) (*big.Int, error)
Issuer(*bind.CallOpts) (common.Address, error)
TotalPaidOut(*bind.CallOpts) (*big.Int, error)
PaidOut(*bind.CallOpts, common.Address) (*big.Int, error)
}
type SimpleSwapBindingFunc = func(common.Address, bind.ContractBackend) (SimpleSwapBinding, error)
// NewSimpleSwapBindings generates the default go bindings
func NewSimpleSwapBindings(address common.Address, backend bind.ContractBackend) (SimpleSwapBinding, error) {
return simpleswapfactory.NewERC20SimpleSwap(address, backend)
}
...@@ -12,7 +12,6 @@ import ( ...@@ -12,7 +12,6 @@ import (
"strings" "strings"
"sync" "sync"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethersphere/bee/pkg/settlement/swap/erc20" "github.com/ethersphere/bee/pkg/settlement/swap/erc20"
"github.com/ethersphere/bee/pkg/settlement/swap/transaction" "github.com/ethersphere/bee/pkg/settlement/swap/transaction"
...@@ -63,11 +62,10 @@ type Service interface { ...@@ -63,11 +62,10 @@ type Service interface {
type service struct { type service struct {
lock sync.Mutex lock sync.Mutex
backend transaction.Backend
transactionService transaction.Service transactionService transaction.Service
address common.Address address common.Address
chequebookInstance SimpleSwapBinding contract *chequebookContract
ownerAddress common.Address ownerAddress common.Address
erc20Service erc20.Service erc20Service erc20.Service
...@@ -78,17 +76,11 @@ type service struct { ...@@ -78,17 +76,11 @@ type service struct {
} }
// New creates a new chequebook service for the provided chequebook contract. // New creates a new chequebook service for the provided chequebook contract.
func New(backend transaction.Backend, transactionService transaction.Service, address, ownerAddress common.Address, store storage.StateStorer, chequeSigner ChequeSigner, erc20Service erc20.Service, simpleSwapBindingFunc SimpleSwapBindingFunc) (Service, error) { func New(transactionService transaction.Service, address, ownerAddress common.Address, store storage.StateStorer, chequeSigner ChequeSigner, erc20Service erc20.Service) (Service, error) {
chequebookInstance, err := simpleSwapBindingFunc(address, backend)
if err != nil {
return nil, err
}
return &service{ return &service{
backend: backend,
transactionService: transactionService, transactionService: transactionService,
address: address, address: address,
chequebookInstance: chequebookInstance, contract: newChequebookContract(address, transactionService),
ownerAddress: ownerAddress, ownerAddress: ownerAddress,
erc20Service: erc20Service, erc20Service: erc20Service,
store: store, store: store,
...@@ -119,9 +111,7 @@ func (s *service) Deposit(ctx context.Context, amount *big.Int) (hash common.Has ...@@ -119,9 +111,7 @@ func (s *service) Deposit(ctx context.Context, amount *big.Int) (hash common.Has
// Balance returns the token balance of the chequebook. // Balance returns the token balance of the chequebook.
func (s *service) Balance(ctx context.Context) (*big.Int, error) { func (s *service) Balance(ctx context.Context) (*big.Int, error) {
return s.chequebookInstance.Balance(&bind.CallOpts{ return s.contract.Balance(ctx)
Context: ctx,
})
} }
// AvailableBalance returns the token balance of the chequebook which is not yet used for uncashed cheques. // AvailableBalance returns the token balance of the chequebook which is not yet used for uncashed cheques.
...@@ -136,9 +126,7 @@ func (s *service) AvailableBalance(ctx context.Context) (*big.Int, error) { ...@@ -136,9 +126,7 @@ func (s *service) AvailableBalance(ctx context.Context) (*big.Int, error) {
return nil, err return nil, err
} }
totalPaidOut, err := s.chequebookInstance.TotalPaidOut(&bind.CallOpts{ totalPaidOut, err := s.contract.TotalPaidOut(ctx)
Context: ctx,
})
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
...@@ -11,63 +11,25 @@ import ( ...@@ -11,63 +11,25 @@ import (
"math/big" "math/big"
"testing" "testing"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethersphere/bee/pkg/settlement/swap/chequebook" "github.com/ethersphere/bee/pkg/settlement/swap/chequebook"
"github.com/ethersphere/bee/pkg/settlement/swap/erc20"
erc20mock "github.com/ethersphere/bee/pkg/settlement/swap/erc20/mock" erc20mock "github.com/ethersphere/bee/pkg/settlement/swap/erc20/mock"
"github.com/ethersphere/bee/pkg/settlement/swap/transaction" "github.com/ethersphere/bee/pkg/settlement/swap/transaction"
"github.com/ethersphere/bee/pkg/settlement/swap/transaction/backendmock"
transactionmock "github.com/ethersphere/bee/pkg/settlement/swap/transaction/mock" transactionmock "github.com/ethersphere/bee/pkg/settlement/swap/transaction/mock"
storemock "github.com/ethersphere/bee/pkg/statestore/mock" storemock "github.com/ethersphere/bee/pkg/statestore/mock"
"github.com/ethersphere/bee/pkg/storage"
) )
func newTestChequebook(
t *testing.T,
backend transaction.Backend,
transactionService transaction.Service,
address,
ownerAdress common.Address,
store storage.StateStorer,
chequeSigner chequebook.ChequeSigner,
erc20 erc20.Service,
simpleSwapBinding chequebook.SimpleSwapBinding,
) (chequebook.Service, error) {
return chequebook.New(
backend,
transactionService,
address,
ownerAdress,
store,
chequeSigner,
erc20,
func(addr common.Address, b bind.ContractBackend) (chequebook.SimpleSwapBinding, error) {
if addr != address {
t.Fatalf("initialised binding with wrong address. wanted %x, got %x", address, addr)
}
if b != backend {
t.Fatal("initialised binding with wrong backend")
}
return simpleSwapBinding, nil
},
)
}
func TestChequebookAddress(t *testing.T) { func TestChequebookAddress(t *testing.T) {
address := common.HexToAddress("0xabcd") address := common.HexToAddress("0xabcd")
ownerAdress := common.HexToAddress("0xfff") ownerAdress := common.HexToAddress("0xfff")
chequebookService, err := newTestChequebook( chequebookService, err := chequebook.New(
t,
backendmock.New(),
transactionmock.New(), transactionmock.New(),
address, address,
ownerAdress, ownerAdress,
nil, nil,
&chequeSignerMock{}, &chequeSignerMock{},
erc20mock.New(), erc20mock.New(),
&simpleSwapBindingMock{},
) )
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
...@@ -82,20 +44,15 @@ func TestChequebookBalance(t *testing.T) { ...@@ -82,20 +44,15 @@ func TestChequebookBalance(t *testing.T) {
address := common.HexToAddress("0xabcd") address := common.HexToAddress("0xabcd")
ownerAdress := common.HexToAddress("0xfff") ownerAdress := common.HexToAddress("0xfff")
balance := big.NewInt(10) balance := big.NewInt(10)
chequebookService, err := newTestChequebook( chequebookService, err := chequebook.New(
t, transactionmock.New(
backendmock.New(), transactionmock.WithABICall(&chequebookABI, balance.FillBytes(make([]byte, 32)), "balance"),
transactionmock.New(), ),
address, address,
ownerAdress, ownerAdress,
nil, nil,
&chequeSignerMock{}, &chequeSignerMock{},
erc20mock.New(), erc20mock.New(),
&simpleSwapBindingMock{
balance: func(*bind.CallOpts) (*big.Int, error) {
return balance, nil
},
},
) )
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
...@@ -117,9 +74,7 @@ func TestChequebookDeposit(t *testing.T) { ...@@ -117,9 +74,7 @@ func TestChequebookDeposit(t *testing.T) {
balance := big.NewInt(30) balance := big.NewInt(30)
depositAmount := big.NewInt(20) depositAmount := big.NewInt(20)
txHash := common.HexToHash("0xdddd") txHash := common.HexToHash("0xdddd")
chequebookService, err := newTestChequebook( chequebookService, err := chequebook.New(
t,
backendmock.New(),
transactionmock.New(), transactionmock.New(),
address, address,
ownerAdress, ownerAdress,
...@@ -142,7 +97,6 @@ func TestChequebookDeposit(t *testing.T) { ...@@ -142,7 +97,6 @@ func TestChequebookDeposit(t *testing.T) {
return txHash, nil return txHash, nil
}), }),
), ),
&simpleSwapBindingMock{},
) )
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
...@@ -162,9 +116,7 @@ func TestChequebookWaitForDeposit(t *testing.T) { ...@@ -162,9 +116,7 @@ func TestChequebookWaitForDeposit(t *testing.T) {
address := common.HexToAddress("0xabcd") address := common.HexToAddress("0xabcd")
ownerAdress := common.HexToAddress("0xfff") ownerAdress := common.HexToAddress("0xfff")
txHash := common.HexToHash("0xdddd") txHash := common.HexToHash("0xdddd")
chequebookService, err := newTestChequebook( chequebookService, err := chequebook.New(
t,
backendmock.New(),
transactionmock.New( transactionmock.New(
transactionmock.WithWaitForReceiptFunc(func(ctx context.Context, tx common.Hash) (*types.Receipt, error) { transactionmock.WithWaitForReceiptFunc(func(ctx context.Context, tx common.Hash) (*types.Receipt, error) {
if tx != txHash { if tx != txHash {
...@@ -180,7 +132,6 @@ func TestChequebookWaitForDeposit(t *testing.T) { ...@@ -180,7 +132,6 @@ func TestChequebookWaitForDeposit(t *testing.T) {
nil, nil,
&chequeSignerMock{}, &chequeSignerMock{},
erc20mock.New(), erc20mock.New(),
&simpleSwapBindingMock{},
) )
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
...@@ -196,9 +147,7 @@ func TestChequebookWaitForDepositReverted(t *testing.T) { ...@@ -196,9 +147,7 @@ func TestChequebookWaitForDepositReverted(t *testing.T) {
address := common.HexToAddress("0xabcd") address := common.HexToAddress("0xabcd")
ownerAdress := common.HexToAddress("0xfff") ownerAdress := common.HexToAddress("0xfff")
txHash := common.HexToHash("0xdddd") txHash := common.HexToHash("0xdddd")
chequebookService, err := newTestChequebook( chequebookService, err := chequebook.New(
t,
backendmock.New(),
transactionmock.New( transactionmock.New(
transactionmock.WithWaitForReceiptFunc(func(ctx context.Context, tx common.Hash) (*types.Receipt, error) { transactionmock.WithWaitForReceiptFunc(func(ctx context.Context, tx common.Hash) (*types.Receipt, error) {
if tx != txHash { if tx != txHash {
...@@ -214,7 +163,6 @@ func TestChequebookWaitForDepositReverted(t *testing.T) { ...@@ -214,7 +163,6 @@ func TestChequebookWaitForDepositReverted(t *testing.T) {
nil, nil,
&chequeSignerMock{}, &chequeSignerMock{},
erc20mock.New(), erc20mock.New(),
&simpleSwapBindingMock{},
) )
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
...@@ -240,23 +188,22 @@ func TestChequebookIssue(t *testing.T) { ...@@ -240,23 +188,22 @@ func TestChequebookIssue(t *testing.T) {
sig := common.Hex2Bytes("0xffff") sig := common.Hex2Bytes("0xffff")
chequeSigner := &chequeSignerMock{} chequeSigner := &chequeSignerMock{}
chequebookService, err := newTestChequebook( chequebookService, err := chequebook.New(
t, transactionmock.New(
backendmock.New(), transactionmock.WithABICallSequence(
transactionmock.New(), transactionmock.ABICall(&chequebookABI, big.NewInt(100).FillBytes(make([]byte, 32)), "balance"),
transactionmock.ABICall(&chequebookABI, big.NewInt(0).FillBytes(make([]byte, 32)), "totalPaidOut"),
transactionmock.ABICall(&chequebookABI, big.NewInt(100).FillBytes(make([]byte, 32)), "balance"),
transactionmock.ABICall(&chequebookABI, big.NewInt(0).FillBytes(make([]byte, 32)), "totalPaidOut"),
transactionmock.ABICall(&chequebookABI, big.NewInt(100).FillBytes(make([]byte, 32)), "balance"),
transactionmock.ABICall(&chequebookABI, big.NewInt(0).FillBytes(make([]byte, 32)), "totalPaidOut"),
),
),
address, address,
ownerAdress, ownerAdress,
store, store,
chequeSigner, chequeSigner,
erc20mock.New(), erc20mock.New(),
&simpleSwapBindingMock{
balance: func(*bind.CallOpts) (*big.Int, error) {
return big.NewInt(100), nil
},
totalPaidOut: func(*bind.CallOpts) (*big.Int, error) {
return big.NewInt(0), nil
},
},
) )
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
...@@ -390,23 +337,13 @@ func TestChequebookIssueErrorSend(t *testing.T) { ...@@ -390,23 +337,13 @@ func TestChequebookIssueErrorSend(t *testing.T) {
sig := common.Hex2Bytes("0xffff") sig := common.Hex2Bytes("0xffff")
chequeSigner := &chequeSignerMock{} chequeSigner := &chequeSignerMock{}
chequebookService, err := newTestChequebook( chequebookService, err := chequebook.New(
t,
backendmock.New(),
transactionmock.New(), transactionmock.New(),
address, address,
ownerAdress, ownerAdress,
store, store,
chequeSigner, chequeSigner,
erc20mock.New(), erc20mock.New(),
&simpleSwapBindingMock{
balance: func(*bind.CallOpts) (*big.Int, error) {
return amount, nil
},
totalPaidOut: func(*bind.CallOpts) (*big.Int, error) {
return big.NewInt(0), nil
},
},
) )
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
...@@ -437,23 +374,18 @@ func TestChequebookIssueOutOfFunds(t *testing.T) { ...@@ -437,23 +374,18 @@ func TestChequebookIssueOutOfFunds(t *testing.T) {
store := storemock.NewStateStore() store := storemock.NewStateStore()
amount := big.NewInt(20) amount := big.NewInt(20)
chequebookService, err := newTestChequebook( chequebookService, err := chequebook.New(
t, transactionmock.New(
backendmock.New(), transactionmock.WithABICallSequence(
transactionmock.New(), transactionmock.ABICall(&chequebookABI, big.NewInt(0).FillBytes(make([]byte, 32)), "balance"),
transactionmock.ABICall(&chequebookABI, big.NewInt(0).FillBytes(make([]byte, 32)), "totalPaidOut"),
),
),
address, address,
ownerAdress, ownerAdress,
store, store,
&chequeSignerMock{}, &chequeSignerMock{},
erc20mock.New(), erc20mock.New(),
&simpleSwapBindingMock{
balance: func(*bind.CallOpts) (*big.Int, error) {
return big.NewInt(0), nil
},
totalPaidOut: func(*bind.CallOpts) (*big.Int, error) {
return big.NewInt(0), nil
},
},
) )
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
...@@ -481,33 +413,19 @@ func TestChequebookWithdraw(t *testing.T) { ...@@ -481,33 +413,19 @@ func TestChequebookWithdraw(t *testing.T) {
withdrawAmount := big.NewInt(20) withdrawAmount := big.NewInt(20)
txHash := common.HexToHash("0xdddd") txHash := common.HexToHash("0xdddd")
store := storemock.NewStateStore() store := storemock.NewStateStore()
chequebookService, err := newTestChequebook( chequebookService, err := chequebook.New(
t,
backendmock.New(),
transactionmock.New( transactionmock.New(
transactionmock.WithSendFunc(func(c context.Context, request *transaction.TxRequest) (common.Hash, error) { transactionmock.WithABICallSequence(
if request.To != nil && *request.To != address { transactionmock.ABICall(&chequebookABI, balance.FillBytes(make([]byte, 32)), "balance"),
t.Fatalf("sending to wrong contract. wanted %x, got %x", address, request.To) transactionmock.ABICall(&chequebookABI, big.NewInt(0).FillBytes(make([]byte, 32)), "totalPaidOut"),
} ),
if request.Value.Cmp(big.NewInt(0)) != 0 { transactionmock.WithABISend(&chequebookABI, txHash, address, big.NewInt(0), "withdraw", withdrawAmount),
t.Fatal("sending ether to token contract")
}
return txHash, nil
}),
), ),
address, address,
ownerAdress, ownerAdress,
store, store,
&chequeSignerMock{}, &chequeSignerMock{},
erc20mock.New(), erc20mock.New(),
&simpleSwapBindingMock{
balance: func(*bind.CallOpts) (*big.Int, error) {
return balance, nil
},
totalPaidOut: func(*bind.CallOpts) (*big.Int, error) {
return big.NewInt(0), nil
},
},
) )
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
...@@ -526,40 +444,22 @@ func TestChequebookWithdraw(t *testing.T) { ...@@ -526,40 +444,22 @@ func TestChequebookWithdraw(t *testing.T) {
func TestChequebookWithdrawInsufficientFunds(t *testing.T) { func TestChequebookWithdrawInsufficientFunds(t *testing.T) {
address := common.HexToAddress("0xabcd") address := common.HexToAddress("0xabcd")
ownerAdress := common.HexToAddress("0xfff") ownerAdress := common.HexToAddress("0xfff")
balance := big.NewInt(30)
withdrawAmount := big.NewInt(20) withdrawAmount := big.NewInt(20)
txHash := common.HexToHash("0xdddd") txHash := common.HexToHash("0xdddd")
store := storemock.NewStateStore() store := storemock.NewStateStore()
chequebookService, err := newTestChequebook( chequebookService, err := chequebook.New(
t,
backendmock.New(),
transactionmock.New( transactionmock.New(
transactionmock.WithSendFunc(func(c context.Context, request *transaction.TxRequest) (common.Hash, error) { transactionmock.WithABISend(&chequebookABI, txHash, address, big.NewInt(0), "withdraw", withdrawAmount),
if request.To != nil && *request.To != address { transactionmock.WithABICallSequence(
t.Fatalf("sending to wrong contract. wanted %x, got %x", address, request.To) transactionmock.ABICall(&chequebookABI, big.NewInt(0).FillBytes(make([]byte, 32)), "balance"),
} transactionmock.ABICall(&chequebookABI, big.NewInt(0).FillBytes(make([]byte, 32)), "totalPaidOut"),
if request.Value.Cmp(big.NewInt(0)) != 0 { ),
t.Fatal("sending ether to token contract")
}
return txHash, nil
}),
transactionmock.WithCallFunc(func(ctx context.Context, request *transaction.TxRequest) (result []byte, err error) {
return balance.FillBytes(make([]byte, 32)), nil
}),
), ),
address, address,
ownerAdress, ownerAdress,
store, store,
&chequeSignerMock{}, &chequeSignerMock{},
erc20mock.New(), erc20mock.New(),
&simpleSwapBindingMock{
balance: func(*bind.CallOpts) (*big.Int, error) {
return big.NewInt(0), nil
},
totalPaidOut: func(*bind.CallOpts) (*big.Int, error) {
return big.NewInt(0), nil
},
},
) )
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
......
...@@ -12,7 +12,6 @@ import ( ...@@ -12,7 +12,6 @@ import (
"strings" "strings"
"sync" "sync"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethersphere/bee/pkg/crypto" "github.com/ethersphere/bee/pkg/crypto"
"github.com/ethersphere/bee/pkg/settlement/swap/transaction" "github.com/ethersphere/bee/pkg/settlement/swap/transaction"
...@@ -48,8 +47,7 @@ type chequeStore struct { ...@@ -48,8 +47,7 @@ type chequeStore struct {
store storage.StateStorer store storage.StateStorer
factory Factory factory Factory
chaindID int64 chaindID int64
simpleSwapBindingFunc SimpleSwapBindingFunc transactionService transaction.Service
backend transaction.Backend
beneficiary common.Address // the beneficiary we expect in cheques sent to us beneficiary common.Address // the beneficiary we expect in cheques sent to us
recoverChequeFunc RecoverChequeFunc recoverChequeFunc RecoverChequeFunc
} }
...@@ -59,18 +57,16 @@ type RecoverChequeFunc func(cheque *SignedCheque, chainID int64) (common.Address ...@@ -59,18 +57,16 @@ type RecoverChequeFunc func(cheque *SignedCheque, chainID int64) (common.Address
// NewChequeStore creates new ChequeStore // NewChequeStore creates new ChequeStore
func NewChequeStore( func NewChequeStore(
store storage.StateStorer, store storage.StateStorer,
backend transaction.Backend,
factory Factory, factory Factory,
chainID int64, chainID int64,
beneficiary common.Address, beneficiary common.Address,
simpleSwapBindingFunc SimpleSwapBindingFunc, transactionService transaction.Service,
recoverChequeFunc RecoverChequeFunc) ChequeStore { recoverChequeFunc RecoverChequeFunc) ChequeStore {
return &chequeStore{ return &chequeStore{
store: store, store: store,
factory: factory, factory: factory,
backend: backend,
chaindID: chainID, chaindID: chainID,
simpleSwapBindingFunc: simpleSwapBindingFunc, transactionService: transactionService,
beneficiary: beneficiary, beneficiary: beneficiary,
recoverChequeFunc: recoverChequeFunc, recoverChequeFunc: recoverChequeFunc,
} }
...@@ -135,16 +131,10 @@ func (s *chequeStore) ReceiveCheque(ctx context.Context, cheque *SignedCheque) ( ...@@ -135,16 +131,10 @@ func (s *chequeStore) ReceiveCheque(ctx context.Context, cheque *SignedCheque) (
} }
// blockchain calls below // blockchain calls below
contract := newChequebookContract(cheque.Chequebook, s.transactionService)
binding, err := s.simpleSwapBindingFunc(cheque.Chequebook, s.backend)
if err != nil {
return nil, err
}
// this does not change for the same chequebook // this does not change for the same chequebook
expectedIssuer, err := binding.Issuer(&bind.CallOpts{ expectedIssuer, err := contract.Issuer(ctx)
Context: ctx,
})
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -161,16 +151,12 @@ func (s *chequeStore) ReceiveCheque(ctx context.Context, cheque *SignedCheque) ( ...@@ -161,16 +151,12 @@ func (s *chequeStore) ReceiveCheque(ctx context.Context, cheque *SignedCheque) (
// basic liquidity check // basic liquidity check
// could be omitted as it is not particularly useful // could be omitted as it is not particularly useful
balance, err := binding.Balance(&bind.CallOpts{ balance, err := contract.Balance(ctx)
Context: ctx,
})
if err != nil { if err != nil {
return nil, err return nil, err
} }
alreadyPaidOut, err := binding.PaidOut(&bind.CallOpts{ alreadyPaidOut, err := contract.PaidOut(ctx, s.beneficiary)
Context: ctx,
}, s.beneficiary)
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
...@@ -10,10 +10,9 @@ import ( ...@@ -10,10 +10,9 @@ import (
"math/big" "math/big"
"testing" "testing"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethersphere/bee/pkg/settlement/swap/chequebook" "github.com/ethersphere/bee/pkg/settlement/swap/chequebook"
"github.com/ethersphere/bee/pkg/settlement/swap/transaction/backendmock" transactionmock "github.com/ethersphere/bee/pkg/settlement/swap/transaction/mock"
storemock "github.com/ethersphere/bee/pkg/statestore/mock" storemock "github.com/ethersphere/bee/pkg/statestore/mock"
) )
...@@ -49,29 +48,19 @@ func TestReceiveCheque(t *testing.T) { ...@@ -49,29 +48,19 @@ func TestReceiveCheque(t *testing.T) {
chequestore := chequebook.NewChequeStore( chequestore := chequebook.NewChequeStore(
store, store,
backendmock.New(),
factory, factory,
chainID, chainID,
beneficiary, beneficiary,
func(address common.Address, b bind.ContractBackend) (chequebook.SimpleSwapBinding, error) { transactionmock.New(
if address != chequebookAddress { transactionmock.WithABICallSequence(
t.Fatalf("binding to wrong chequebook. wanted %x, got %x", chequebookAddress, address) transactionmock.ABICall(&chequebookABI, issuer.Hash().Bytes(), "issuer"),
} transactionmock.ABICall(&chequebookABI, cumulativePayout2.FillBytes(make([]byte, 32)), "balance"),
return &simpleSwapBindingMock{ transactionmock.ABICall(&chequebookABI, big.NewInt(0).FillBytes(make([]byte, 32)), "paidOut", beneficiary),
issuer: func(*bind.CallOpts) (common.Address, error) { transactionmock.ABICall(&chequebookABI, issuer.Hash().Bytes(), "issuer"),
return issuer, nil transactionmock.ABICall(&chequebookABI, cumulativePayout2.FillBytes(make([]byte, 32)), "balance"),
}, transactionmock.ABICall(&chequebookABI, big.NewInt(0).FillBytes(make([]byte, 32)), "paidOut", beneficiary),
balance: func(*bind.CallOpts) (*big.Int, error) { ),
return cumulativePayout2, nil ),
},
paidOut: func(o *bind.CallOpts, b common.Address) (*big.Int, error) {
if b != beneficiary {
t.Fatalf("checking for wrong beneficiary. wanted %x, got %x", beneficiary, b)
}
return big.NewInt(0), nil
},
}, nil
},
func(c *chequebook.SignedCheque, cid int64) (common.Address, error) { func(c *chequebook.SignedCheque, cid int64) (common.Address, error) {
if cid != chainID { if cid != chainID {
t.Fatalf("recovery with wrong chain id. wanted %d, got %d", chainID, cid) t.Fatalf("recovery with wrong chain id. wanted %d, got %d", chainID, cid)
...@@ -149,12 +138,12 @@ func TestReceiveChequeInvalidBeneficiary(t *testing.T) { ...@@ -149,12 +138,12 @@ func TestReceiveChequeInvalidBeneficiary(t *testing.T) {
chequestore := chequebook.NewChequeStore( chequestore := chequebook.NewChequeStore(
store, store,
backendmock.New(),
&factoryMock{}, &factoryMock{},
chainID, chainID,
beneficiary, beneficiary,
transactionmock.New(),
nil, nil,
nil) )
_, err := chequestore.ReceiveCheque(context.Background(), cheque) _, err := chequestore.ReceiveCheque(context.Background(), cheque)
if err == nil { if err == nil {
...@@ -177,7 +166,6 @@ func TestReceiveChequeInvalidAmount(t *testing.T) { ...@@ -177,7 +166,6 @@ func TestReceiveChequeInvalidAmount(t *testing.T) {
chequestore := chequebook.NewChequeStore( chequestore := chequebook.NewChequeStore(
store, store,
backendmock.New(),
&factoryMock{ &factoryMock{
verifyChequebook: func(ctx context.Context, address common.Address) error { verifyChequebook: func(ctx context.Context, address common.Address) error {
return nil return nil
...@@ -185,19 +173,13 @@ func TestReceiveChequeInvalidAmount(t *testing.T) { ...@@ -185,19 +173,13 @@ func TestReceiveChequeInvalidAmount(t *testing.T) {
}, },
chainID, chainID,
beneficiary, beneficiary,
func(address common.Address, b bind.ContractBackend) (chequebook.SimpleSwapBinding, error) { transactionmock.New(
return &simpleSwapBindingMock{ transactionmock.WithABICallSequence(
issuer: func(*bind.CallOpts) (common.Address, error) { transactionmock.ABICall(&chequebookABI, issuer.Hash().Bytes(), "issuer"),
return issuer, nil transactionmock.ABICall(&chequebookABI, cumulativePayout.FillBytes(make([]byte, 32)), "balance"),
}, transactionmock.ABICall(&chequebookABI, big.NewInt(0).FillBytes(make([]byte, 32)), "paidOut", beneficiary),
balance: func(*bind.CallOpts) (*big.Int, error) { ),
return cumulativePayout, nil ),
},
paidOut: func(o *bind.CallOpts, b common.Address) (*big.Int, error) {
return big.NewInt(0), nil
},
}, nil
},
func(c *chequebook.SignedCheque, cid int64) (common.Address, error) { func(c *chequebook.SignedCheque, cid int64) (common.Address, error) {
return issuer, nil return issuer, nil
}) })
...@@ -241,7 +223,6 @@ func TestReceiveChequeInvalidChequebook(t *testing.T) { ...@@ -241,7 +223,6 @@ func TestReceiveChequeInvalidChequebook(t *testing.T) {
chequestore := chequebook.NewChequeStore( chequestore := chequebook.NewChequeStore(
store, store,
backendmock.New(),
&factoryMock{ &factoryMock{
verifyChequebook: func(ctx context.Context, address common.Address) error { verifyChequebook: func(ctx context.Context, address common.Address) error {
return chequebook.ErrNotDeployedByFactory return chequebook.ErrNotDeployedByFactory
...@@ -249,19 +230,12 @@ func TestReceiveChequeInvalidChequebook(t *testing.T) { ...@@ -249,19 +230,12 @@ func TestReceiveChequeInvalidChequebook(t *testing.T) {
}, },
chainID, chainID,
beneficiary, beneficiary,
func(address common.Address, b bind.ContractBackend) (chequebook.SimpleSwapBinding, error) { transactionmock.New(
return &simpleSwapBindingMock{ transactionmock.WithABICallSequence(
issuer: func(*bind.CallOpts) (common.Address, error) { transactionmock.ABICall(&chequebookABI, issuer.Bytes(), "issuer"),
return issuer, nil transactionmock.ABICall(&chequebookABI, cumulativePayout.FillBytes(make([]byte, 32)), "balance"),
}, ),
balance: func(*bind.CallOpts) (*big.Int, error) { ),
return cumulativePayout, nil
},
paidOut: func(o *bind.CallOpts, b common.Address) (*big.Int, error) {
return big.NewInt(0), nil
},
}, nil
},
func(c *chequebook.SignedCheque, cid int64) (common.Address, error) { func(c *chequebook.SignedCheque, cid int64) (common.Address, error) {
return issuer, nil return issuer, nil
}) })
...@@ -290,7 +264,6 @@ func TestReceiveChequeInvalidSignature(t *testing.T) { ...@@ -290,7 +264,6 @@ func TestReceiveChequeInvalidSignature(t *testing.T) {
chequestore := chequebook.NewChequeStore( chequestore := chequebook.NewChequeStore(
store, store,
backendmock.New(),
&factoryMock{ &factoryMock{
verifyChequebook: func(ctx context.Context, address common.Address) error { verifyChequebook: func(ctx context.Context, address common.Address) error {
return nil return nil
...@@ -298,16 +271,11 @@ func TestReceiveChequeInvalidSignature(t *testing.T) { ...@@ -298,16 +271,11 @@ func TestReceiveChequeInvalidSignature(t *testing.T) {
}, },
chainID, chainID,
beneficiary, beneficiary,
func(address common.Address, b bind.ContractBackend) (chequebook.SimpleSwapBinding, error) { transactionmock.New(
return &simpleSwapBindingMock{ transactionmock.WithABICallSequence(
issuer: func(*bind.CallOpts) (common.Address, error) { transactionmock.ABICall(&chequebookABI, issuer.Hash().Bytes(), "issuer"),
return issuer, nil ),
}, ),
balance: func(*bind.CallOpts) (*big.Int, error) {
return cumulativePayout, nil
},
}, nil
},
func(c *chequebook.SignedCheque, cid int64) (common.Address, error) { func(c *chequebook.SignedCheque, cid int64) (common.Address, error) {
return common.Address{}, nil return common.Address{}, nil
}) })
...@@ -336,7 +304,6 @@ func TestReceiveChequeInsufficientBalance(t *testing.T) { ...@@ -336,7 +304,6 @@ func TestReceiveChequeInsufficientBalance(t *testing.T) {
chequestore := chequebook.NewChequeStore( chequestore := chequebook.NewChequeStore(
store, store,
backendmock.New(),
&factoryMock{ &factoryMock{
verifyChequebook: func(ctx context.Context, address common.Address) error { verifyChequebook: func(ctx context.Context, address common.Address) error {
return nil return nil
...@@ -344,19 +311,13 @@ func TestReceiveChequeInsufficientBalance(t *testing.T) { ...@@ -344,19 +311,13 @@ func TestReceiveChequeInsufficientBalance(t *testing.T) {
}, },
chainID, chainID,
beneficiary, beneficiary,
func(address common.Address, b bind.ContractBackend) (chequebook.SimpleSwapBinding, error) { transactionmock.New(
return &simpleSwapBindingMock{ transactionmock.WithABICallSequence(
issuer: func(*bind.CallOpts) (common.Address, error) { transactionmock.ABICall(&chequebookABI, issuer.Hash().Bytes(), "issuer"),
return issuer, nil transactionmock.ABICall(&chequebookABI, new(big.Int).Sub(cumulativePayout, big.NewInt(1)).FillBytes(make([]byte, 32)), "balance"),
}, transactionmock.ABICall(&chequebookABI, big.NewInt(0).FillBytes(make([]byte, 32)), "paidOut", beneficiary),
balance: func(*bind.CallOpts) (*big.Int, error) { ),
return big.NewInt(0).Sub(cumulativePayout, big.NewInt(1)), nil ),
},
paidOut: func(o *bind.CallOpts, b common.Address) (*big.Int, error) {
return big.NewInt(0), nil
},
}, nil
},
func(c *chequebook.SignedCheque, cid int64) (common.Address, error) { func(c *chequebook.SignedCheque, cid int64) (common.Address, error) {
return issuer, nil return issuer, nil
}) })
...@@ -385,7 +346,6 @@ func TestReceiveChequeSufficientBalancePaidOut(t *testing.T) { ...@@ -385,7 +346,6 @@ func TestReceiveChequeSufficientBalancePaidOut(t *testing.T) {
chequestore := chequebook.NewChequeStore( chequestore := chequebook.NewChequeStore(
store, store,
backendmock.New(),
&factoryMock{ &factoryMock{
verifyChequebook: func(ctx context.Context, address common.Address) error { verifyChequebook: func(ctx context.Context, address common.Address) error {
return nil return nil
...@@ -393,19 +353,13 @@ func TestReceiveChequeSufficientBalancePaidOut(t *testing.T) { ...@@ -393,19 +353,13 @@ func TestReceiveChequeSufficientBalancePaidOut(t *testing.T) {
}, },
chainID, chainID,
beneficiary, beneficiary,
func(address common.Address, b bind.ContractBackend) (chequebook.SimpleSwapBinding, error) { transactionmock.New(
return &simpleSwapBindingMock{ transactionmock.WithABICallSequence(
issuer: func(*bind.CallOpts) (common.Address, error) { transactionmock.ABICall(&chequebookABI, issuer.Hash().Bytes(), "issuer"),
return issuer, nil transactionmock.ABICall(&chequebookABI, new(big.Int).Sub(cumulativePayout, big.NewInt(100)).FillBytes(make([]byte, 32)), "balance"),
}, transactionmock.ABICall(&chequebookABI, big.NewInt(0).FillBytes(make([]byte, 32)), "paidOut", beneficiary),
balance: func(*bind.CallOpts) (*big.Int, error) { ),
return big.NewInt(0).Sub(cumulativePayout, big.NewInt(100)), nil ),
},
paidOut: func(o *bind.CallOpts, b common.Address) (*big.Int, error) {
return big.NewInt(100), nil
},
}, nil
},
func(c *chequebook.SignedCheque, cid int64) (common.Address, error) { func(c *chequebook.SignedCheque, cid int64) (common.Address, error) {
return issuer, nil return issuer, nil
}) })
......
...@@ -8,34 +8,10 @@ import ( ...@@ -8,34 +8,10 @@ import (
"context" "context"
"math/big" "math/big"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethersphere/bee/pkg/settlement/swap/chequebook" "github.com/ethersphere/bee/pkg/settlement/swap/chequebook"
) )
type simpleSwapBindingMock struct {
balance func(*bind.CallOpts) (*big.Int, error)
issuer func(*bind.CallOpts) (common.Address, error)
totalPaidOut func(o *bind.CallOpts) (*big.Int, error)
paidOut func(*bind.CallOpts, common.Address) (*big.Int, error)
}
func (m *simpleSwapBindingMock) Balance(o *bind.CallOpts) (*big.Int, error) {
return m.balance(o)
}
func (m *simpleSwapBindingMock) Issuer(o *bind.CallOpts) (common.Address, error) {
return m.issuer(o)
}
func (m *simpleSwapBindingMock) TotalPaidOut(o *bind.CallOpts) (*big.Int, error) {
return m.totalPaidOut(o)
}
func (m *simpleSwapBindingMock) PaidOut(o *bind.CallOpts, c common.Address) (*big.Int, error) {
return m.paidOut(o, c)
}
type chequeSignerMock struct { type chequeSignerMock struct {
sign func(cheque *chequebook.Cheque) ([]byte, error) sign func(cheque *chequebook.Cheque) ([]byte, error)
} }
......
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package chequebook
import (
"context"
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethersphere/bee/pkg/settlement/swap/transaction"
)
type chequebookContract struct {
address common.Address
transactionService transaction.Service
}
func newChequebookContract(address common.Address, transactionService transaction.Service) *chequebookContract {
return &chequebookContract{
address: address,
transactionService: transactionService,
}
}
func (c *chequebookContract) Issuer(ctx context.Context) (common.Address, error) {
callData, err := chequebookABI.Pack("issuer")
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
}
results, err := chequebookABI.Unpack("issuer", output)
if err != nil {
return common.Address{}, err
}
return *abi.ConvertType(results[0], new(common.Address)).(*common.Address), nil
}
// Balance returns the token balance of the chequebook.
func (c *chequebookContract) Balance(ctx context.Context) (*big.Int, error) {
callData, err := chequebookABI.Pack("balance")
if err != nil {
return nil, err
}
output, err := c.transactionService.Call(ctx, &transaction.TxRequest{
To: &c.address,
Data: callData,
})
if err != nil {
return nil, err
}
results, err := chequebookABI.Unpack("balance", output)
if err != nil {
return nil, err
}
return abi.ConvertType(results[0], new(big.Int)).(*big.Int), nil
}
func (c *chequebookContract) PaidOut(ctx context.Context, address common.Address) (*big.Int, error) {
callData, err := chequebookABI.Pack("paidOut", address)
if err != nil {
return nil, err
}
output, err := c.transactionService.Call(ctx, &transaction.TxRequest{
To: &c.address,
Data: callData,
})
if err != nil {
return nil, err
}
results, err := chequebookABI.Unpack("paidOut", output)
if err != nil {
return nil, err
}
return abi.ConvertType(results[0], new(big.Int)).(*big.Int), nil
}
func (c *chequebookContract) TotalPaidOut(ctx context.Context) (*big.Int, error) {
callData, err := chequebookABI.Pack("totalPaidOut")
if err != nil {
return nil, err
}
output, err := c.transactionService.Call(ctx, &transaction.TxRequest{
To: &c.address,
Data: callData,
})
if err != nil {
return nil, err
}
results, err := chequebookABI.Unpack("totalPaidOut", output)
if err != nil {
return nil, err
}
return abi.ConvertType(results[0], new(big.Int)).(*big.Int), nil
}
...@@ -102,7 +102,6 @@ func Init( ...@@ -102,7 +102,6 @@ func Init(
chainId int64, chainId int64,
overlayEthAddress common.Address, overlayEthAddress common.Address,
chequeSigner ChequeSigner, chequeSigner ChequeSigner,
simpleSwapBindingFunc SimpleSwapBindingFunc,
) (chequebookService Service, err error) { ) (chequebookService Service, err error) {
// verify that the supplied factory is valid // verify that the supplied factory is valid
err = chequebookFactory.VerifyBytecode(ctx) err = chequebookFactory.VerifyBytecode(ctx)
...@@ -167,7 +166,7 @@ func Init( ...@@ -167,7 +166,7 @@ func Init(
return nil, err return nil, err
} }
chequebookService, err = New(swapBackend, transactionService, chequebookAddress, overlayEthAddress, stateStore, chequeSigner, erc20Service, simpleSwapBindingFunc) chequebookService, err = New(transactionService, chequebookAddress, overlayEthAddress, stateStore, chequeSigner, erc20Service)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -188,7 +187,7 @@ func Init( ...@@ -188,7 +187,7 @@ func Init(
logger.Info("successfully deposited to chequebook") logger.Info("successfully deposited to chequebook")
} }
} else { } else {
chequebookService, err = New(swapBackend, transactionService, chequebookAddress, overlayEthAddress, stateStore, chequeSigner, erc20Service, simpleSwapBindingFunc) chequebookService, err = New(transactionService, chequebookAddress, overlayEthAddress, stateStore, chequeSigner, erc20Service)
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
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