diff --git a/eth/auction.go b/eth/auction.go index bea1343..6e1b94f 100644 --- a/eth/auction.go +++ b/eth/auction.go @@ -1,8 +1,10 @@ package eth import ( + "fmt" "math/big" + "github.com/ethereum/go-ethereum/accounts/abi/bind" ethCommon "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethclient" @@ -278,8 +280,21 @@ func NewAuctionClient(client *EthereumClient, address ethCommon.Address) *Auctio // AuctionSetSlotDeadline is the interface to call the smart contract function func (c *AuctionClient) AuctionSetSlotDeadline(newDeadline uint8) (*types.Transaction, error) { - log.Error("TODO") - return nil, errTODO + var tx *types.Transaction + var err error + if tx, err = c.client.CallAuth( + 1000000, + func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) { + auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec) + if err != nil { + return nil, err + } + return auction.SetSlotDeadline(auth, newDeadline) + }, + ); err != nil { + return nil, fmt.Errorf("Failed setting slotDeadline: %w", err) + } + return tx, nil } // AuctionGetSlotDeadline is the interface to call the smart contract function @@ -300,8 +315,21 @@ func (c *AuctionClient) AuctionGetSlotDeadline() (uint8, error) { // AuctionSetOpenAuctionSlots is the interface to call the smart contract function func (c *AuctionClient) AuctionSetOpenAuctionSlots(newOpenAuctionSlots uint16) (*types.Transaction, error) { - log.Error("TODO") - return nil, errTODO + var tx *types.Transaction + var err error + if tx, err = c.client.CallAuth( + 1000000, + func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) { + auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec) + if err != nil { + return nil, err + } + return auction.SetOpenAuctionSlots(auth, newOpenAuctionSlots) + }, + ); err != nil { + return nil, fmt.Errorf("Failed setting openAuctionSlots: %w", err) + } + return tx, nil } // AuctionGetOpenAuctionSlots is the interface to call the smart contract function @@ -322,8 +350,21 @@ func (c *AuctionClient) AuctionGetOpenAuctionSlots() (uint16, error) { // AuctionSetClosedAuctionSlots is the interface to call the smart contract function func (c *AuctionClient) AuctionSetClosedAuctionSlots(newClosedAuctionSlots uint16) (*types.Transaction, error) { - log.Error("TODO") - return nil, errTODO + var tx *types.Transaction + var err error + if tx, err = c.client.CallAuth( + 1000000, + func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) { + auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec) + if err != nil { + return nil, err + } + return auction.SetClosedAuctionSlots(auth, newClosedAuctionSlots) + }, + ); err != nil { + return nil, fmt.Errorf("Failed setting closedAuctionSlots: %w", err) + } + return tx, nil } // AuctionGetClosedAuctionSlots is the interface to call the smart contract function @@ -343,9 +384,22 @@ func (c *AuctionClient) AuctionGetClosedAuctionSlots() (uint16, error) { } // AuctionSetOutbidding is the interface to call the smart contract function -func (c *AuctionClient) AuctionSetOutbidding(newOutbidding uint16) (*types.Transaction, error) { - log.Error("TODO") - return nil, errTODO +func (c *AuctionClient) AuctionSetOutbidding(newOutbidding uint8) (*types.Transaction, error) { + var tx *types.Transaction + var err error + if tx, err = c.client.CallAuth( + 1000000, + func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) { + auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec) + if err != nil { + return nil, err + } + return auction.SetOutbidding(auth, newOutbidding) + }, + ); err != nil { + return nil, fmt.Errorf("Failed setting setOutbidding: %w", err) + } + return tx, nil } // AuctionGetOutbidding is the interface to call the smart contract function @@ -368,9 +422,22 @@ func (c *AuctionClient) AuctionGetOutbidding() (uint16, error) { } // AuctionSetAllocationRatio is the interface to call the smart contract function -func (c *AuctionClient) AuctionSetAllocationRatio(newAllocationRatio [3]uint16) (*types.Transaction, error) { - log.Error("TODO") - return nil, errTODO +func (c *AuctionClient) AuctionSetAllocationRatio(newAllocationRatio [3]uint8) (*types.Transaction, error) { + var tx *types.Transaction + var err error + if tx, err = c.client.CallAuth( + 1000000, + func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) { + auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec) + if err != nil { + return nil, err + } + return auction.SetAllocationRatio(auth, newAllocationRatio) + }, + ); err != nil { + return nil, fmt.Errorf("Failed setting allocationRatio: %w", err) + } + return tx, nil } // AuctionGetAllocationRatio is the interface to call the smart contract function @@ -394,8 +461,21 @@ func (c *AuctionClient) AuctionGetAllocationRatio() ([3]uint16, error) { // AuctionSetDonationAddress is the interface to call the smart contract function func (c *AuctionClient) AuctionSetDonationAddress(newDonationAddress ethCommon.Address) (*types.Transaction, error) { - log.Error("TODO") - return nil, errTODO + var tx *types.Transaction + var err error + if tx, err = c.client.CallAuth( + 1000000, + func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) { + auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec) + if err != nil { + return nil, err + } + return auction.SetDonationAddress(auth, newDonationAddress) + }, + ); err != nil { + return nil, fmt.Errorf("Failed setting donationAddress: %w", err) + } + return tx, nil } // AuctionGetDonationAddress is the interface to call the smart contract function @@ -416,8 +496,21 @@ func (c *AuctionClient) AuctionGetDonationAddress() (*ethCommon.Address, error) // AuctionSetBootCoordinator is the interface to call the smart contract function func (c *AuctionClient) AuctionSetBootCoordinator(newBootCoordinator ethCommon.Address) (*types.Transaction, error) { - log.Error("TODO") - return nil, errTODO + var tx *types.Transaction + var err error + if tx, err = c.client.CallAuth( + 1000000, + func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) { + auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec) + if err != nil { + return nil, err + } + return auction.SetBootCoordinator(auth, newBootCoordinator) + }, + ); err != nil { + return nil, fmt.Errorf("Failed setting bootCoordinator: %w", err) + } + return tx, nil } // AuctionGetBootCoordinator is the interface to call the smart contract function @@ -436,28 +529,81 @@ func (c *AuctionClient) AuctionGetBootCoordinator() (*ethCommon.Address, error) return &bootCoordinator, nil } -// AuctionChangeDefaultSlotSetBid is the interface to call the smart contract function -func (c *AuctionClient) AuctionChangeDefaultSlotSetBid(slotSet int64, newInitialMinBid *big.Int) (*types.Transaction, error) { - log.Error("TODO") - return nil, errTODO +// AuctionChangeEpochMinBid is the interface to call the smart contract function +func (c *AuctionClient) AuctionChangeEpochMinBid(slotEpoch int64, newInitialMinBid *big.Int) (*types.Transaction, error) { + var tx *types.Transaction + var err error + if tx, err = c.client.CallAuth( + 1000000, + func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) { + auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec) + if err != nil { + return nil, err + } + slotEpochToSend := big.NewInt(slotEpoch) + fmt.Println(slotEpochToSend) + fmt.Println(newInitialMinBid) + return auction.ChangeEpochMinBid(auth, slotEpochToSend, newInitialMinBid) + }, + ); err != nil { + return nil, fmt.Errorf("Failed changing epoch minBid: %w", err) + } + fmt.Println(tx) + return tx, nil } // AuctionRegisterCoordinator is the interface to call the smart contract function func (c *AuctionClient) AuctionRegisterCoordinator(forgerAddress ethCommon.Address, URL string) (*types.Transaction, error) { - log.Error("TODO") - return nil, errTODO + var tx *types.Transaction + var err error + if tx, err = c.client.CallAuth( + 1000000, + func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) { + auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec) + if err != nil { + return nil, err + } + return auction.RegisterCoordinator(auth, forgerAddress, URL) + }, + ); err != nil { + return nil, fmt.Errorf("Failed register coordinator: %w", err) + } + return tx, nil } // AuctionIsRegisteredCoordinator is the interface to call the smart contract function func (c *AuctionClient) AuctionIsRegisteredCoordinator(forgerAddress ethCommon.Address) (bool, error) { - log.Error("TODO") - return false, errTODO + var registered bool + if err := c.client.Call(func(ec *ethclient.Client) error { + auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec) + if err != nil { + return err + } + registered, err = auction.IsRegisteredCoordinator(nil, forgerAddress) + return err + }); err != nil { + return false, err + } + return registered, nil } // AuctionUpdateCoordinatorInfo is the interface to call the smart contract function func (c *AuctionClient) AuctionUpdateCoordinatorInfo(forgerAddress ethCommon.Address, newWithdrawAddress ethCommon.Address, newURL string) (*types.Transaction, error) { - log.Error("TODO") - return nil, errTODO + var tx *types.Transaction + var err error + if tx, err = c.client.CallAuth( + 1000000, + func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) { + auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec) + if err != nil { + return nil, err + } + return auction.UpdateCoordinatorInfo(auth, forgerAddress, newWithdrawAddress, newURL) + }, + ); err != nil { + return nil, fmt.Errorf("Failed update coordinator info: %w", err) + } + return tx, nil } // AuctionGetCurrentSlotNumber is the interface to call the smart contract function diff --git a/eth/auction_test.go b/eth/auction_test.go index 5ffa952..792b56b 100644 --- a/eth/auction_test.go +++ b/eth/auction_test.go @@ -4,36 +4,65 @@ import ( "os" "testing" + "github.com/ethereum/go-ethereum/accounts/keystore" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethclient" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) const slotDeadlineConst = uint8(20) -const openAuctionSlotsConst = 4320 -const closedAuctionSlotsConst = 2 -const outbiddingConst = 10 +const openAuctionSlotsConst = uint16(4320) +const closedAuctionSlotsConst = uint16(2) +const outbiddingConst = uint8(10) const currentSlotConst = 0 var allocationRatioConst [3]uint8 = [3]uint8{40, 40, 20} var auctionClient *AuctionClient -var donationAddressConstStr = os.Getenv("DONATION_ADDRESS") -var bootCoordinatorConstStr = os.Getenv("BOOT_COORDINATOR_ADDRESS") +/*var donationAddressStr = os.Getenv("DONATION_ADDRESS") +var bootCoordinatorStr = os.Getenv("BOOT_COORDINATOR_ADDRESS") var integration = os.Getenv("INTEGRATION") var ehtClientDialURL = os.Getenv("ETHCLIENT_DIAL_URL") -var auctionAddressStr = os.Getenv("AUCTION_ADDRESS") +var auctionAddressStr = os.Getenv("AUCTION_ADDRESS")*/ + +var donationAddressStr = "0x6c365935CA8710200C7595F0a72EB6023A7706Cd" +var bootCoordinatorStr = "0xc783df8a850f42e7f7e57013759c285caa701eb6" +var integration = os.Getenv("INTEGRATION") +var ehtClientDialURL = "http://localhost:8545" +var auctionAddressStr = "0x3619DbE27d7c1e7E91aA738697Ae7Bc5FC3eACA5" +var ownerAddressStr = "0xc783df8a850f42e7F7e57013759C285caa701eB6" +var governanceAddressStr = "0xead9c93b79ae7c1591b1fb5323bd777e86e150d4" +var governancePrivateKey = "d49743deccbccc5dc7baa8e69e5be03298da8688a15dd202e20f15d5e0e9a9fb" +var minBidStr = "10000000000000000000" +var URL = "http://localhost:3000" +var newURL = "http://localhost:3002" + +var pathKs = "test/ks" +var password = "pass" func TestNewAction(t *testing.T) { - if integration != "" { - // Init eth client - ethClient, err := ethclient.Dial(ehtClientDialURL) + key, err := crypto.HexToECDSA(governancePrivateKey) + require.Nil(t, err) + ks := keystore.NewKeyStore(pathKs, keystore.StandardScryptN, keystore.StandardScryptP) + account, err := ks.ImportECDSA(key, password) + ks.Unlock(account, password) + // Init eth client + ethClient, err := ethclient.Dial(ehtClientDialURL) + require.Nil(t, err) + ethereumClient := NewEthereumClient(ethClient, &account, ks, nil) + auctionAddress := common.HexToAddress(auctionAddressStr) + auctionClient = NewAuctionClient(ethereumClient, auctionAddress) +} + +func TestAuctionGetCurrentSlotNumber(t *testing.T) { + if auctionClient != nil { + currentSlot, err := auctionClient.AuctionGetCurrentSlotNumber() require.Nil(t, err) - ethereumClient := NewEthereumClient(ethClient, nil, nil, nil) - auctionAddress := common.HexToAddress(auctionAddressStr) - auctionClient = NewAuctionClient(ethereumClient, auctionAddress) + currentSlotInt := int(currentSlot) + assert.Equal(t, currentSlotConst, currentSlotInt) } } @@ -45,12 +74,35 @@ func TestAuctionGetSlotDeadline(t *testing.T) { } } +func TestAuctionSetSlotDeadline(t *testing.T) { + newSlotDeadline := uint8(25) + if auctionClient != nil { + _, err := auctionClient.AuctionSetSlotDeadline(newSlotDeadline) + require.Nil(t, err) + slotDeadline, err := auctionClient.AuctionGetSlotDeadline() + assert.Equal(t, newSlotDeadline, slotDeadline) + _, err = auctionClient.AuctionSetSlotDeadline(slotDeadlineConst) + require.Nil(t, err) + } +} + func TestAuctionGetOpenAuctionSlots(t *testing.T) { if auctionClient != nil { openAuctionSlots, err := auctionClient.AuctionGetOpenAuctionSlots() require.Nil(t, err) - openAuctionSlotsInt := int(openAuctionSlots) - assert.Equal(t, openAuctionSlotsConst, openAuctionSlotsInt) + assert.Equal(t, openAuctionSlotsConst, openAuctionSlots) + } +} + +func TestAuctionSetOpenAuctionSlots(t *testing.T) { + newOpenAuctionSlots := uint16(4500) + if auctionClient != nil { + _, err := auctionClient.AuctionSetOpenAuctionSlots(newOpenAuctionSlots) + require.Nil(t, err) + openAuctionSlots, err := auctionClient.AuctionGetOpenAuctionSlots() + assert.Equal(t, newOpenAuctionSlots, openAuctionSlots) + _, err = auctionClient.AuctionSetOpenAuctionSlots(openAuctionSlotsConst) + require.Nil(t, err) } } @@ -58,8 +110,19 @@ func TestAuctionGetClosedAuctionSlots(t *testing.T) { if auctionClient != nil { closedAuctionSlots, err := auctionClient.AuctionGetClosedAuctionSlots() require.Nil(t, err) - closedAuctionSlotsInt := int(closedAuctionSlots) - assert.Equal(t, closedAuctionSlotsConst, closedAuctionSlotsInt) + assert.Equal(t, closedAuctionSlotsConst, closedAuctionSlots) + } +} + +func TestAuctionSetClosedAuctionSlots(t *testing.T) { + newClosedAuctionSlots := uint16(5) + if auctionClient != nil { + _, err := auctionClient.AuctionSetClosedAuctionSlots(newClosedAuctionSlots) + require.Nil(t, err) + closedAuctionSlots, err := auctionClient.AuctionGetClosedAuctionSlots() + assert.Equal(t, newClosedAuctionSlots, closedAuctionSlots) + _, err = auctionClient.AuctionSetClosedAuctionSlots(closedAuctionSlotsConst) + require.Nil(t, err) } } @@ -67,8 +130,19 @@ func TestAuctionGetOutbidding(t *testing.T) { if auctionClient != nil { outbidding, err := auctionClient.AuctionGetOutbidding() require.Nil(t, err) - outbiddingInt := int(outbidding) - assert.Equal(t, outbiddingConst, outbiddingInt) + assert.Equal(t, outbiddingConst, outbidding) + } +} + +func TestAuctionSetOutbidding(t *testing.T) { + newOutbidding := uint8(15) + if auctionClient != nil { + _, err := auctionClient.AuctionSetOutbidding(newOutbidding) + require.Nil(t, err) + outbidding, err := auctionClient.AuctionGetOutbidding() + assert.Equal(t, newOutbidding, outbidding) + _, err = auctionClient.AuctionSetOutbidding(outbiddingConst) + require.Nil(t, err) } } @@ -80,11 +154,23 @@ func TestAuctionGetAllocationRatio(t *testing.T) { } } +func TestAuctionSetAllocationRatio(t *testing.T) { + newAllocationRatio := [3]uint8{30, 30, 40} + if auctionClient != nil { + _, err := auctionClient.AuctionSetAllocationRatio(newAllocationRatio) + require.Nil(t, err) + allocationRatio, err := auctionClient.AuctionGetAllocationRatio() + assert.Equal(t, newAllocationRatio, allocationRatio) + _, err = auctionClient.AuctionSetAllocationRatio(allocationRatioConst) + require.Nil(t, err) + } +} + func TestAuctionGetDonationAddress(t *testing.T) { if auctionClient != nil { donationAddress, err := auctionClient.AuctionGetDonationAddress() require.Nil(t, err) - donationAddressConst := common.HexToAddress(donationAddressConstStr) + donationAddressConst := common.HexToAddress(donationAddressStr) assert.Equal(t, &donationAddressConst, donationAddress) } } @@ -93,16 +179,93 @@ func TestAuctionGetBootCoordinator(t *testing.T) { if auctionClient != nil { bootCoordinator, err := auctionClient.AuctionGetBootCoordinator() require.Nil(t, err) - bootCoordinatorConst := common.HexToAddress(bootCoordinatorConstStr) + bootCoordinatorConst := common.HexToAddress(bootCoordinatorStr) assert.Equal(t, &bootCoordinatorConst, bootCoordinator) } } -func TestAuctionGetCurrentSlotNumber(t *testing.T) { +func TestAuctionSetDonationAddress(t *testing.T) { + newDonationAddress := common.HexToAddress(governanceAddressStr) if auctionClient != nil { - currentSlot, err := auctionClient.AuctionGetCurrentSlotNumber() + _, err := auctionClient.AuctionSetDonationAddress(newDonationAddress) + require.Nil(t, err) + donationAddress, err := auctionClient.AuctionGetDonationAddress() + assert.Equal(t, &newDonationAddress, donationAddress) + donationAddressConst := common.HexToAddress(donationAddressStr) + _, err = auctionClient.AuctionSetDonationAddress(donationAddressConst) + require.Nil(t, err) + } +} + +func TestAuctionSetBootCoordinator(t *testing.T) { + newBootCoordinator := common.HexToAddress(governanceAddressStr) + if auctionClient != nil { + _, err := auctionClient.AuctionSetBootCoordinator(newBootCoordinator) + require.Nil(t, err) + bootCoordinator, err := auctionClient.AuctionGetBootCoordinator() + assert.Equal(t, &newBootCoordinator, bootCoordinator) + bootCoordinatorConst := common.HexToAddress(bootCoordinatorStr) + _, err = auctionClient.AuctionSetBootCoordinator(bootCoordinatorConst) + require.Nil(t, err) + } +} + +func TestAuctionGetMinBidEpoch(t *testing.T) { + epoch := uint8(3) + if auctionClient != nil { + minBid, err := auctionClient.AuctionGetMinBidEpoch(epoch) + require.Nil(t, err) + assert.Equal(t, minBid.String(), minBidStr) + } +} + +/* func TestAuctionChangeEpochMinBid(t *testing.T) { + slotEpoch := int64(3) + epoch := uint8(3) + newInitialMinBid := new(big.Int) + newInitialMinBid.SetString("20000000000000000000", 10) + if auctionClient != nil { + _, err := auctionClient.AuctionChangeEpochMinBid(slotEpoch, newInitialMinBid) + require.Nil(t, err) + minBid, err := auctionClient.AuctionGetMinBidEpoch(epoch) + require.Nil(t, err) + assert.Equal(t, minBid, newInitialMinBid) + newMinBid := new(big.Int) + newMinBid.SetString("10000000000000000000", 10) + _, err = auctionClient.AuctionChangeEpochMinBid(slotEpoch, newMinBid) + require.Nil(t, err) + } +} */ +func TestAuctionIsRegisteredCoordinator(t *testing.T) { + forgerAddress := common.HexToAddress(governanceAddressStr) + if auctionClient != nil { + registered, err := auctionClient.AuctionIsRegisteredCoordinator(forgerAddress) + require.Nil(t, err) + assert.Equal(t, registered, false) + } +} + +func TestAuctionRegisterCoordinator(t *testing.T) { + forgerAddress := common.HexToAddress(governanceAddressStr) + if auctionClient != nil { + _, err := auctionClient.AuctionRegisterCoordinator(forgerAddress, URL) + require.Nil(t, err) + } +} + +func TestAuctionIsRegisteredCoordinatorTrue(t *testing.T) { + forgerAddress := common.HexToAddress(governanceAddressStr) + if auctionClient != nil { + registered, err := auctionClient.AuctionIsRegisteredCoordinator(forgerAddress) + require.Nil(t, err) + assert.Equal(t, registered, true) + } +} + +func AuctionUpdateCoordinatorInfo(t *testing.T) { + forgerAddress := common.HexToAddress(governanceAddressStr) + if auctionClient != nil { + _, err := auctionClient.AuctionUpdateCoordinatorInfo(forgerAddress, forgerAddress, newURL) require.Nil(t, err) - currentSlotInt := int(currentSlot) - assert.Equal(t, currentSlotConst, currentSlotInt) } } diff --git a/eth/rollup.go b/eth/rollup.go index 48d671d..36f52d2 100644 --- a/eth/rollup.go +++ b/eth/rollup.go @@ -216,6 +216,16 @@ type RollupInterface interface { // RollupClient is the implementation of the interface to the Rollup Smart Contract in ethereum. type RollupClient struct { + client *EthereumClient + address ethCommon.Address +} + +// NewRollupClient creates a new RollupClient +func NewRollupClient(client *EthereumClient, address ethCommon.Address) *RollupClient { + return &RollupClient{ + client: client, + address: address, + } } // RollupForgeBatch is the interface to call the smart contract function