Skip to content

Commit

Permalink
feat: alternative withdrawal address
Browse files Browse the repository at this point in the history
  • Loading branch information
notanatol committed Mar 7, 2024
1 parent d3d6685 commit 12dff05
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 34 deletions.
2 changes: 2 additions & 0 deletions cmd/bee/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ const (
optionNameStateStoreCacheCapacity = "statestore-cache-capacity"
optionNameTargetNeighborhood = "target-neighborhood"
optionNameNeighborhoodSuggester = "neighborhood-suggester"
optionNameWithdrawalAddress = "withdrawal-address"
)

// nolint:gochecknoinits
Expand Down Expand Up @@ -304,6 +305,7 @@ func (c *command) setAllFlags(cmd *cobra.Command) {
cmd.Flags().Uint64(optionNameStateStoreCacheCapacity, 100_000, "lru memory caching capacity in number of statestore entries")
cmd.Flags().String(optionNameTargetNeighborhood, "", "neighborhood to target in binary format (ex: 111111001) for mining the initial overlay")
cmd.Flags().String(optionNameNeighborhoodSuggester, "https://api.swarmscan.io/v1/network/neighborhoods/suggestion", "suggester for target neighborhood")
cmd.Flags().String(optionNameWithdrawalAddress, "", "Withdrawal target address")
}

func newLogger(cmd *cobra.Command, verbosity string) (log.Logger, error) {
Expand Down
2 changes: 2 additions & 0 deletions cmd/bee/cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"strings"

"github.com/ethereum/go-ethereum/common"
"github.com/ethersphere/bee/pkg/node"
"github.com/ethersphere/bee/pkg/settlement/swap/erc20"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -91,6 +92,7 @@ func (c *command) initDeployCmd() error {
chainID,
swapBackend,
overlayEthAddress,
common.Address{}, // not required for deploy
transactionService,
chequebookFactory,
swapInitialDeposit,
Expand Down
1 change: 1 addition & 0 deletions cmd/bee/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ func buildBeeNode(ctx context.Context, c *command, cmd *cobra.Command, logger lo
StatestoreCacheCapacity: c.config.GetUint64(optionNameStateStoreCacheCapacity),
TargetNeighborhood: c.config.GetString(optionNameTargetNeighborhood),
NeighborhoodSuggester: c.config.GetString(optionNameNeighborhoodSuggester),
WithdrawalAddress: c.config.GetString(optionNameWithdrawalAddress),
})

return b, err
Expand Down
2 changes: 2 additions & 0 deletions pkg/node/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func InitChequebookService(
chainID int64,
backend transaction.Backend,
overlayEthAddress common.Address,
withdrawalAddress common.Address,
transactionService transaction.Service,
chequebookFactory chequebook.Factory,
initialDeposit string,
Expand Down Expand Up @@ -162,6 +163,7 @@ func InitChequebookService(
backend,
chainID,
overlayEthAddress,
withdrawalAddress,
chequeSigner,
erc20Service,
)
Expand Down
5 changes: 5 additions & 0 deletions pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ type Options struct {
StatestoreCacheCapacity uint64
TargetNeighborhood string
NeighborhoodSuggester string
WithdrawalAddress string
}

const (
Expand Down Expand Up @@ -315,6 +316,7 @@ func NewBee(
var (
chainBackend transaction.Backend
overlayEthAddress common.Address
withdrawalAddress common.Address
chainID int64
transactionService transaction.Service
transactionMonitor transaction.Monitor
Expand Down Expand Up @@ -525,6 +527,8 @@ func NewBee(

erc20Service = erc20.New(transactionService, erc20Address)

withdrawalAddress = common.HexToAddress(o.WithdrawalAddress)

if o.ChequebookEnable && chainEnabled {
chequebookService, err = InitChequebookService(
ctx,
Expand All @@ -534,6 +538,7 @@ func NewBee(
chainID,
chainBackend,
overlayEthAddress,
withdrawalAddress,
transactionService,
chequebookFactory,
o.SwapInitialDeposit,
Expand Down
16 changes: 13 additions & 3 deletions pkg/settlement/swap/chequebook/chequebook.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ type service struct {
lock sync.Mutex
transactionService transaction.Service

address common.Address
address common.Address
withdrawalAddress common.Address

contract *chequebookContract
ownerAddress common.Address

Expand All @@ -81,12 +83,13 @@ type service struct {
}

// New creates a new chequebook service for the provided chequebook contract.
func New(transactionService transaction.Service, address, ownerAddress common.Address, store storage.StateStorer, chequeSigner ChequeSigner, erc20Service erc20.Service) (Service, error) {
func New(transactionService transaction.Service, address, ownerAddress, withdrawalAddress common.Address, store storage.StateStorer, chequeSigner ChequeSigner, erc20Service erc20.Service) (Service, error) {
return &service{
transactionService: transactionService,
address: address,
contract: newChequebookContract(address, transactionService),
ownerAddress: ownerAddress,
withdrawalAddress: withdrawalAddress,
erc20Service: erc20Service,
store: store,
chequeSigner: chequeSigner,
Expand Down Expand Up @@ -326,8 +329,15 @@ func (s *service) Withdraw(ctx context.Context, amount *big.Int) (hash common.Ha
return common.Hash{}, err
}

var zeroAddress common.Address

address := s.address
if s.withdrawalAddress != zeroAddress {
address = s.withdrawalAddress
}

request := &transaction.TxRequest{
To: &s.address,
To: &address,
Data: callData,
GasPrice: sctx.GetGasPrice(ctx),
GasLimit: 95000,
Expand Down
106 changes: 77 additions & 29 deletions pkg/settlement/swap/chequebook/chequebook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func TestChequebookAddress(t *testing.T) {
transactionmock.New(),
address,
ownerAdress,
common.Address{},
nil,
&chequeSignerMock{},
erc20mock.New(),
Expand All @@ -54,6 +55,7 @@ func TestChequebookBalance(t *testing.T) {
),
address,
ownerAdress,
common.Address{},
nil,
&chequeSignerMock{},
erc20mock.New(),
Expand Down Expand Up @@ -84,6 +86,7 @@ func TestChequebookDeposit(t *testing.T) {
transactionmock.New(),
address,
ownerAdress,
common.Address{},
nil,
&chequeSignerMock{},
erc20mock.New(
Expand Down Expand Up @@ -137,6 +140,7 @@ func TestChequebookWaitForDeposit(t *testing.T) {
),
address,
ownerAdress,
common.Address{},
nil,
&chequeSignerMock{},
erc20mock.New(),
Expand Down Expand Up @@ -170,6 +174,7 @@ func TestChequebookWaitForDepositReverted(t *testing.T) {
),
address,
ownerAdress,
common.Address{},
nil,
&chequeSignerMock{},
erc20mock.New(),
Expand Down Expand Up @@ -213,6 +218,7 @@ func TestChequebookIssue(t *testing.T) {
),
address,
ownerAdress,
common.Address{},
store,
chequeSigner,
erc20mock.New(),
Expand Down Expand Up @@ -355,6 +361,7 @@ func TestChequebookIssueErrorSend(t *testing.T) {
transactionmock.New(),
address,
ownerAdress,
common.Address{},
store,
chequeSigner,
erc20mock.New(),
Expand Down Expand Up @@ -399,6 +406,7 @@ func TestChequebookIssueOutOfFunds(t *testing.T) {
),
address,
ownerAdress,
common.Address{},
store,
&chequeSignerMock{},
erc20mock.New(),
Expand All @@ -425,38 +433,77 @@ func TestChequebookIssueOutOfFunds(t *testing.T) {
func TestChequebookWithdraw(t *testing.T) {
t.Parallel()

address := common.HexToAddress("0xabcd")
ownerAdress := common.HexToAddress("0xfff")
balance := big.NewInt(30)
withdrawAmount := big.NewInt(20)
txHash := common.HexToHash("0xdddd")
store := storemock.NewStateStore()
chequebookService, err := chequebook.New(
transactionmock.New(
transactionmock.WithABICallSequence(
transactionmock.ABICall(&chequebookABI, address, balance.FillBytes(make([]byte, 32)), "balance"),
transactionmock.ABICall(&chequebookABI, address, big.NewInt(0).FillBytes(make([]byte, 32)), "totalPaidOut"),
t.Run("target withdrawal address is owner", func(t *testing.T) {
address := common.HexToAddress("0xabcd")
ownerAdress := common.HexToAddress("0xfff")
balance := big.NewInt(30)
withdrawAmount := big.NewInt(20)
txHash := common.HexToHash("0xdddd")
store := storemock.NewStateStore()
chequebookService, err := chequebook.New(
transactionmock.New(
transactionmock.WithABICallSequence(
transactionmock.ABICall(&chequebookABI, address, balance.FillBytes(make([]byte, 32)), "balance"),
transactionmock.ABICall(&chequebookABI, address, big.NewInt(0).FillBytes(make([]byte, 32)), "totalPaidOut"),
),
transactionmock.WithABISend(&chequebookABI, txHash, address, big.NewInt(0), "withdraw", withdrawAmount),
),
transactionmock.WithABISend(&chequebookABI, txHash, address, big.NewInt(0), "withdraw", withdrawAmount),
),
address,
ownerAdress,
store,
&chequeSignerMock{},
erc20mock.New(),
)
if err != nil {
t.Fatal(err)
}
address,
ownerAdress,
common.Address{},
store,
&chequeSignerMock{},
erc20mock.New(),
)
if err != nil {
t.Fatal(err)
}

returnedTxHash, err := chequebookService.Withdraw(context.Background(), withdrawAmount)
if err != nil {
t.Fatal(err)
}
returnedTxHash, err := chequebookService.Withdraw(context.Background(), withdrawAmount)
if err != nil {
t.Fatal(err)
}

if txHash != returnedTxHash {
t.Fatalf("returned wrong transaction hash. wanted %v, got %v", txHash, returnedTxHash)
}
if txHash != returnedTxHash {
t.Fatalf("returned wrong transaction hash. wanted %v, got %v", txHash, returnedTxHash)
}
})
t.Run("target withdrawal address provided", func(t *testing.T) {
address := common.HexToAddress("0xabcd")
withdrawalAddress := common.HexToAddress("0xabef")
ownerAdress := common.HexToAddress("0xfff")
balance := big.NewInt(30)
withdrawAmount := big.NewInt(20)
txHash := common.HexToHash("0xdddd")
store := storemock.NewStateStore()
chequebookService, err := chequebook.New(
transactionmock.New(
transactionmock.WithABICallSequence(
transactionmock.ABICall(&chequebookABI, address, balance.FillBytes(make([]byte, 32)), "balance"),
transactionmock.ABICall(&chequebookABI, address, big.NewInt(0).FillBytes(make([]byte, 32)), "totalPaidOut"),
),
transactionmock.WithABISend(&chequebookABI, txHash, withdrawalAddress, big.NewInt(0), "withdraw", withdrawAmount),
),
address,
ownerAdress,
withdrawalAddress,
store,
&chequeSignerMock{},
erc20mock.New(),
)
if err != nil {
t.Fatal(err)
}

returnedTxHash, err := chequebookService.Withdraw(context.Background(), withdrawAmount)
if err != nil {
t.Fatal(err)
}

if txHash != returnedTxHash {
t.Fatalf("returned wrong transaction hash. wanted %v, got %v", txHash, returnedTxHash)
}
})
}

func TestChequebookWithdrawInsufficientFunds(t *testing.T) {
Expand All @@ -477,6 +524,7 @@ func TestChequebookWithdrawInsufficientFunds(t *testing.T) {
),
address,
ownerAdress,
common.Address{},
store,
&chequeSignerMock{},
erc20mock.New(),
Expand Down
5 changes: 3 additions & 2 deletions pkg/settlement/swap/chequebook/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func Init(
swapBackend transaction.Backend,
chainId int64,
overlayEthAddress common.Address,
withdrawalAddress common.Address,
chequeSigner ChequeSigner,
erc20Service erc20.Service,
) (chequebookService Service, err error) {
Expand Down Expand Up @@ -187,7 +188,7 @@ func Init(
return nil, err
}

chequebookService, err = New(transactionService, chequebookAddress, overlayEthAddress, stateStore, chequeSigner, erc20Service)
chequebookService, err = New(transactionService, chequebookAddress, overlayEthAddress, withdrawalAddress, stateStore, chequeSigner, erc20Service)
if err != nil {
return nil, err
}
Expand All @@ -208,7 +209,7 @@ func Init(
logger.Info("successfully deposited to chequebook")
}
} else {
chequebookService, err = New(transactionService, chequebookAddress, overlayEthAddress, stateStore, chequeSigner, erc20Service)
chequebookService, err = New(transactionService, chequebookAddress, overlayEthAddress, withdrawalAddress, stateStore, chequeSigner, erc20Service)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 12dff05

Please sign in to comment.