arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
Pass tokenAddress to AuctionClient constructor
Eduard S eduard@iden3.io
Mon, 21 Sep 2020 15:34:36 +0200
commit

1386f4b5e1727987447049338b970189136bf455

parent

729f37c912d5c3cd7cb96b6bbc41ed8913123586

5 files changed, 30 insertions(+), 27 deletions(-)

jump to
M eth/auction.go → eth/auction.go

@@ -243,8 +243,8 @@

// Bidding // AuctionTokensReceived(operator, from, to ethCommon.Address, amount *big.Int, // userData, operatorData []byte) error // Only called from another smart contract - AuctionBid(slot int64, bidAmount *big.Int, forger, tokenAddress ethCommon.Address) (*types.Transaction, error) - AuctionMultiBid(startingSlot int64, endingSlot int64, slotSet [6]bool, maxBid, closedMinBid, budget *big.Int, forger, tokenAddress ethCommon.Address) (*types.Transaction, error) + AuctionBid(slot int64, bidAmount *big.Int, forger ethCommon.Address) (*types.Transaction, error) + AuctionMultiBid(startingSlot int64, endingSlot int64, slotSet [6]bool, maxBid, closedMinBid, budget *big.Int, forger ethCommon.Address) (*types.Transaction, error) // Forge AuctionCanForge(forger ethCommon.Address, blockNum int64) (bool, error)

@@ -267,17 +267,19 @@ //

// AuctionClient is the implementation of the interface to the Auction Smart Contract in ethereum. type AuctionClient struct { - client *EthereumClient - address ethCommon.Address - gasLimit uint64 + client *EthereumClient + address ethCommon.Address + tokenAddress ethCommon.Address + gasLimit uint64 } -// NewAuctionClient creates a new AuctionClient -func NewAuctionClient(client *EthereumClient, address ethCommon.Address) *AuctionClient { +// NewAuctionClient creates a new AuctionClient. `tokenAddress` is the address of the HEZ tokens. +func NewAuctionClient(client *EthereumClient, address, tokenAddress ethCommon.Address) *AuctionClient { return &AuctionClient{ - client: client, - address: address, - gasLimit: 1000000, //nolint:gomnd + client: client, + address: address, + tokenAddress: tokenAddress, + gasLimit: 1000000, //nolint:gomnd } }

@@ -689,13 +691,13 @@ // return errTODO

// } // AuctionBid is the interface to call the smart contract function -func (c *AuctionClient) AuctionBid(slot int64, bidAmount *big.Int, forger, tokenAddress ethCommon.Address) (*types.Transaction, error) { +func (c *AuctionClient) AuctionBid(slot int64, bidAmount *big.Int, forger ethCommon.Address) (*types.Transaction, error) { var tx *types.Transaction var err error if tx, err = c.client.CallAuth( c.gasLimit, func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) { - tokens, err := ERC777.NewERC777(tokenAddress, ec) + tokens, err := ERC777.NewERC777(c.tokenAddress, ec) if err != nil { return nil, err }

@@ -725,13 +727,13 @@ return tx, nil

} // AuctionMultiBid is the interface to call the smart contract function -func (c *AuctionClient) AuctionMultiBid(startingSlot int64, endingSlot int64, slotSet [6]bool, maxBid, closedMinBid, budget *big.Int, forger, tokenAddress ethCommon.Address) (*types.Transaction, error) { +func (c *AuctionClient) AuctionMultiBid(startingSlot int64, endingSlot int64, slotSet [6]bool, maxBid, closedMinBid, budget *big.Int, forger ethCommon.Address) (*types.Transaction, error) { var tx *types.Transaction var err error if tx, err = c.client.CallAuth( c.gasLimit, func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) { - tokens, err := ERC777.NewERC777(tokenAddress, ec) + tokens, err := ERC777.NewERC777(c.tokenAddress, ec) if err != nil { return nil, err }
M eth/auction_test.go → eth/auction_test.go

@@ -68,8 +68,9 @@ ethClient, err := ethclient.Dial(ehtClientDialURL)

require.Nil(t, err) ethereumClient := NewEthereumClient(ethClient, &account, ks, nil) auctionAddress := common.HexToAddress(auctionAddressStr) + tokenAddress := common.HexToAddress(tokenHezStr) if integration != "" { - auctionClient = NewAuctionClient(ethereumClient, auctionAddress) + auctionClient = NewAuctionClient(ethereumClient, auctionAddress, tokenAddress) } }

@@ -351,7 +352,7 @@ require.Nil(t, err)

bidAmount := new(big.Int) bidAmount.SetString("11000000000000000000", 10) forgerAddress := common.HexToAddress(governanceAddressStr) - _, err = auctionClient.AuctionBid(currentSlot+4, bidAmount, forgerAddress, TOKENHEZ) + _, err = auctionClient.AuctionBid(currentSlot+4, bidAmount, forgerAddress) require.Nil(t, err) } }

@@ -368,7 +369,7 @@ minBid.SetString("11000000000000000000", 10)

budget := new(big.Int) budget.SetString("110000000000000000000", 10) forgerAddress := common.HexToAddress(governanceAddressStr) - _, err = auctionClient.AuctionMultiBid(currentSlot+5, currentSlot+10, slotSet, maxBid, minBid, budget, forgerAddress, TOKENHEZ) + _, err = auctionClient.AuctionMultiBid(currentSlot+5, currentSlot+10, slotSet, maxBid, minBid, budget, forgerAddress) require.Nil(t, err) } }
M go.mod → go.mod

@@ -22,5 +22,6 @@ github.com/urfave/cli v1.22.1

github.com/urfave/cli/v2 v2.2.0 go.uber.org/multierr v1.6.0 // indirect go.uber.org/zap v1.16.0 + golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 gopkg.in/go-playground/validator.v9 v9.29.1 )
M test/ethclient.go → test/ethclient.go

@@ -1068,7 +1068,7 @@ // return errTODO

// } // AuctionBid is the interface to call the smart contract function -func (c *Client) AuctionBid(slot int64, bidAmount *big.Int, forger, tokenAddress ethCommon.Address) (tx *types.Transaction, err error) { +func (c *Client) AuctionBid(slot int64, bidAmount *big.Int, forger ethCommon.Address) (tx *types.Transaction, err error) { c.rw.Lock() defer c.rw.Unlock() cpy := c.nextBlock().copy()

@@ -1117,7 +1117,7 @@ return c.newTransaction("bid", data{slot, bidAmount, forger}), nil

} // AuctionMultiBid is the interface to call the smart contract function -func (c *Client) AuctionMultiBid(startingSlot int64, endingSlot int64, slotSet [6]bool, maxBid, closedMinBid, budget *big.Int, forger, tokenAddress ethCommon.Address) (tx *types.Transaction, err error) { +func (c *Client) AuctionMultiBid(startingSlot int64, endingSlot int64, slotSet [6]bool, maxBid, closedMinBid, budget *big.Int, forger ethCommon.Address) (tx *types.Transaction, err error) { c.rw.Lock() defer c.rw.Unlock() cpy := c.nextBlock().copy()
M test/ethclient_test.go → test/ethclient_test.go

@@ -68,7 +68,6 @@ func TestClientAuction(t *testing.T) {

addrWithdraw := ethCommon.HexToAddress("0x6b175474e89094c44da98b954eedeac495271d0f") addrForge := ethCommon.HexToAddress("0xCfAA413eEb796f328620a3630Ae39124cabcEa92") addrForge2 := ethCommon.HexToAddress("0x1fCb4ac309428feCc61B1C8cA5823C15A5e1a800") - token1Addr := ethCommon.HexToAddress("0x6b175474e89094c44da98b954eedeac495271d0f") var timer timer clientSetup := NewClientSetupExample()

@@ -81,33 +80,33 @@ c := NewClient(true, &timer, addrWithdraw, clientSetup)

// Check several cases in which bid doesn't succed, and also do 2 successful bids. - _, err := c.AuctionBid(0, big.NewInt(1), addrForge, token1Addr) + _, err := c.AuctionBid(0, big.NewInt(1), addrForge) assert.Equal(t, errBidClosed, err) - _, err = c.AuctionBid(4322, big.NewInt(1), addrForge, token1Addr) + _, err = c.AuctionBid(4322, big.NewInt(1), addrForge) assert.Equal(t, errBidNotOpen, err) // 101 % 6 = 5; defaultSlotSetBid[5] = 1500; 1500 + 10% = 1650 - _, err = c.AuctionBid(101, big.NewInt(1650), addrForge, token1Addr) + _, err = c.AuctionBid(101, big.NewInt(1650), addrForge) assert.Equal(t, errCoordNotReg, err) _, err = c.AuctionRegisterCoordinator(addrForge, "https://foo.bar") assert.Nil(t, err) - _, err = c.AuctionBid(3, big.NewInt(1), addrForge, token1Addr) + _, err = c.AuctionBid(3, big.NewInt(1), addrForge) assert.Equal(t, errBidBelowMin, err) - _, err = c.AuctionBid(3, big.NewInt(1650), addrForge, token1Addr) + _, err = c.AuctionBid(3, big.NewInt(1650), addrForge) assert.Nil(t, err) _, err = c.AuctionRegisterCoordinator(addrForge2, "https://foo2.bar") assert.Nil(t, err) - _, err = c.AuctionBid(3, big.NewInt(16), addrForge2, token1Addr) + _, err = c.AuctionBid(3, big.NewInt(16), addrForge2) assert.Equal(t, errBidBelowMin, err) // 1650 + 10% = 1815 - _, err = c.AuctionBid(3, big.NewInt(1815), addrForge2, token1Addr) + _, err = c.AuctionBid(3, big.NewInt(1815), addrForge2) assert.Nil(t, err) c.CtlMineBlock()