Add contracts & ethclient auction

This commit is contained in:
laisolizq
2020-09-09 13:10:03 +02:00
parent bcd93fee36
commit d3ee6e443c
6 changed files with 8126 additions and 10 deletions

View File

@@ -5,6 +5,8 @@ import (
ethCommon "github.com/ethereum/go-ethereum/common" ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
HermezAuctionProtocol "github.com/hermeznetwork/hermez-node/eth/contracts/auction"
) )
// AuctionConstants are the constants of the Rollup Smart Contract // AuctionConstants are the constants of the Rollup Smart Contract
@@ -252,6 +254,16 @@ type AuctionInterface interface {
// AuctionClient is the implementation of the interface to the Auction Smart Contract in ethereum. // AuctionClient is the implementation of the interface to the Auction Smart Contract in ethereum.
type AuctionClient struct { type AuctionClient struct {
client *EthereumClient
address ethCommon.Address
}
// NewAuctionClient creates a new AuctionClient
func NewAuctionClient(client *EthereumClient, address ethCommon.Address) *AuctionClient {
return &AuctionClient{
client: client,
address: address,
}
} }
// AuctionSetSlotDeadline is the interface to call the smart contract function // AuctionSetSlotDeadline is the interface to call the smart contract function
@@ -261,7 +273,18 @@ func (c *AuctionClient) AuctionSetSlotDeadline(newDeadline uint8) (*types.Transa
// AuctionGetSlotDeadline is the interface to call the smart contract function // AuctionGetSlotDeadline is the interface to call the smart contract function
func (c *AuctionClient) AuctionGetSlotDeadline() (uint8, error) { func (c *AuctionClient) AuctionGetSlotDeadline() (uint8, error) {
return 0, errTODO var slotDeadline uint8
if err := c.client.Call(func(ec *ethclient.Client) error {
auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
if err != nil {
return err
}
slotDeadline, err = auction.GetSlotDeadline(nil)
return err
}); err != nil {
return 0, err
}
return slotDeadline, nil
} }
// AuctionSetOpenAuctionSlots is the interface to call the smart contract function // AuctionSetOpenAuctionSlots is the interface to call the smart contract function
@@ -271,7 +294,18 @@ func (c *AuctionClient) AuctionSetOpenAuctionSlots(newOpenAuctionSlots uint16) (
// AuctionGetOpenAuctionSlots is the interface to call the smart contract function // AuctionGetOpenAuctionSlots is the interface to call the smart contract function
func (c *AuctionClient) AuctionGetOpenAuctionSlots() (uint16, error) { func (c *AuctionClient) AuctionGetOpenAuctionSlots() (uint16, error) {
return 0, errTODO var openAuctionSlots uint16
if err := c.client.Call(func(ec *ethclient.Client) error {
auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
if err != nil {
return err
}
openAuctionSlots, err = auction.GetOpenAuctionSlots(nil)
return err
}); err != nil {
return 0, err
}
return openAuctionSlots, nil
} }
// AuctionSetClosedAuctionSlots is the interface to call the smart contract function // AuctionSetClosedAuctionSlots is the interface to call the smart contract function
@@ -281,7 +315,18 @@ func (c *AuctionClient) AuctionSetClosedAuctionSlots(newClosedAuctionSlots uint1
// AuctionGetClosedAuctionSlots is the interface to call the smart contract function // AuctionGetClosedAuctionSlots is the interface to call the smart contract function
func (c *AuctionClient) AuctionGetClosedAuctionSlots() (uint16, error) { func (c *AuctionClient) AuctionGetClosedAuctionSlots() (uint16, error) {
return 0, errTODO var closedAuctionSlots uint16
if err := c.client.Call(func(ec *ethclient.Client) error {
auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
if err != nil {
return err
}
closedAuctionSlots, err = auction.GetClosedAuctionSlots(nil)
return err
}); err != nil {
return 0, err
}
return closedAuctionSlots, nil
} }
// AuctionSetOutbidding is the interface to call the smart contract function // AuctionSetOutbidding is the interface to call the smart contract function
@@ -291,7 +336,18 @@ func (c *AuctionClient) AuctionSetOutbidding(newOutbidding uint8) (*types.Transa
// AuctionGetOutbidding is the interface to call the smart contract function // AuctionGetOutbidding is the interface to call the smart contract function
func (c *AuctionClient) AuctionGetOutbidding() (uint8, error) { func (c *AuctionClient) AuctionGetOutbidding() (uint8, error) {
return 0, errTODO var outbidding uint8
if err := c.client.Call(func(ec *ethclient.Client) error {
auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
if err != nil {
return err
}
outbidding, err = auction.GetOutbidding(nil)
return err
}); err != nil {
return 0, err
}
return outbidding, nil
} }
// AuctionSetAllocationRatio is the interface to call the smart contract function // AuctionSetAllocationRatio is the interface to call the smart contract function
@@ -301,7 +357,18 @@ func (c *AuctionClient) AuctionSetAllocationRatio(newAllocationRatio [3]uint8) (
// AuctionGetAllocationRatio is the interface to call the smart contract function // AuctionGetAllocationRatio is the interface to call the smart contract function
func (c *AuctionClient) AuctionGetAllocationRatio() ([3]uint8, error) { func (c *AuctionClient) AuctionGetAllocationRatio() ([3]uint8, error) {
return [3]uint8{}, errTODO var allocationRation [3]uint8
if err := c.client.Call(func(ec *ethclient.Client) error {
auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
if err != nil {
return err
}
allocationRation, err = auction.GetAllocationRatio(nil)
return err
}); err != nil {
return [3]uint8{}, err
}
return allocationRation, nil
} }
// AuctionSetDonationAddress is the interface to call the smart contract function // AuctionSetDonationAddress is the interface to call the smart contract function
@@ -311,7 +378,18 @@ func (c *AuctionClient) AuctionSetDonationAddress(newDonationAddress ethCommon.A
// AuctionGetDonationAddress is the interface to call the smart contract function // AuctionGetDonationAddress is the interface to call the smart contract function
func (c *AuctionClient) AuctionGetDonationAddress() (*ethCommon.Address, error) { func (c *AuctionClient) AuctionGetDonationAddress() (*ethCommon.Address, error) {
return nil, errTODO var donationAddress ethCommon.Address
if err := c.client.Call(func(ec *ethclient.Client) error {
auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
if err != nil {
return err
}
donationAddress, err = auction.GetDonationAddress(nil)
return err
}); err != nil {
return nil, err
}
return &donationAddress, nil
} }
// AuctionSetBootCoordinator is the interface to call the smart contract function // AuctionSetBootCoordinator is the interface to call the smart contract function
@@ -321,7 +399,18 @@ func (c *AuctionClient) AuctionSetBootCoordinator(newBootCoordinator ethCommon.A
// AuctionGetBootCoordinator is the interface to call the smart contract function // AuctionGetBootCoordinator is the interface to call the smart contract function
func (c *AuctionClient) AuctionGetBootCoordinator() (*ethCommon.Address, error) { func (c *AuctionClient) AuctionGetBootCoordinator() (*ethCommon.Address, error) {
return nil, errTODO var bootCoordinator ethCommon.Address
if err := c.client.Call(func(ec *ethclient.Client) error {
auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
if err != nil {
return err
}
bootCoordinator, err = auction.GetBootCoordinator(nil)
return err
}); err != nil {
return nil, err
}
return &bootCoordinator, nil
} }
// AuctionChangeEpochMinBid is the interface to call the smart contract function // AuctionChangeEpochMinBid is the interface to call the smart contract function
@@ -346,17 +435,52 @@ func (c *AuctionClient) AuctionUpdateCoordinatorInfo(forgerAddress ethCommon.Add
// AuctionGetCurrentSlotNumber is the interface to call the smart contract function // AuctionGetCurrentSlotNumber is the interface to call the smart contract function
func (c *AuctionClient) AuctionGetCurrentSlotNumber() (int64, error) { func (c *AuctionClient) AuctionGetCurrentSlotNumber() (int64, error) {
return 0, errTODO var _currentSlotNumber *big.Int
if err := c.client.Call(func(ec *ethclient.Client) error {
auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
if err != nil {
return err
}
_currentSlotNumber, err = auction.GetCurrentSlotNumber(nil)
return err
}); err != nil {
return 0, err
}
currentSlotNumber := _currentSlotNumber.Int64()
return currentSlotNumber, nil
} }
// AuctionGetMinBidBySlot is the interface to call the smart contract function // AuctionGetMinBidBySlot is the interface to call the smart contract function
func (c *AuctionClient) AuctionGetMinBidBySlot(slot int64) (*big.Int, error) { func (c *AuctionClient) AuctionGetMinBidBySlot(slot int64) (*big.Int, error) {
return nil, errTODO var minBid *big.Int
if err := c.client.Call(func(ec *ethclient.Client) error {
auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
if err != nil {
return err
}
slotToSend := big.NewInt(slot)
minBid, err = auction.GetMinBidBySlot(nil, slotToSend)
return err
}); err != nil {
return big.NewInt(0), err
}
return minBid, nil
} }
// AuctionGetMinBidEpoch is the interface to call the smart contract function // AuctionGetMinBidEpoch is the interface to call the smart contract function
func (c *AuctionClient) AuctionGetMinBidEpoch(epoch uint8) (*big.Int, error) { func (c *AuctionClient) AuctionGetMinBidEpoch(epoch uint8) (*big.Int, error) {
return nil, errTODO var minBidEpoch *big.Int
if err := c.client.Call(func(ec *ethclient.Client) error {
auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
if err != nil {
return err
}
minBidEpoch, err = auction.GetMinBidEpoch(nil, epoch)
return err
}); err != nil {
return big.NewInt(0), err
}
return minBidEpoch, nil
} }
// AuctionTokensReceived is the interface to call the smart contract function // AuctionTokensReceived is the interface to call the smart contract function

108
eth/auction_test.go Normal file
View File

@@ -0,0 +1,108 @@
package eth
import (
"os"
"testing"
"github.com/ethereum/go-ethereum/common"
"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 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 integration = os.Getenv("INTEGRATION")
var ehtClientDialURL = os.Getenv("ETHCLIENT_DIAL_URL")
var auctionAddressStr = os.Getenv("AUCTION_ADDRESS")
func TestNewAction(t *testing.T) {
if integration != "" {
// Init eth client
ethClient, err := ethclient.Dial(ehtClientDialURL)
require.Nil(t, err)
ethereumClient := NewEthereumClient(ethClient, nil, nil, nil)
auctionAddress := common.HexToAddress(auctionAddressStr)
auctionClient = NewAuctionClient(ethereumClient, auctionAddress)
}
}
func TestAuctionGetSlotDeadline(t *testing.T) {
if auctionClient != nil {
slotDeadline, err := auctionClient.AuctionGetSlotDeadline()
require.Nil(t, err)
assert.Equal(t, slotDeadlineConst, slotDeadline)
}
}
func TestAuctionGetOpenAuctionSlots(t *testing.T) {
if auctionClient != nil {
openAuctionSlots, err := auctionClient.AuctionGetOpenAuctionSlots()
require.Nil(t, err)
openAuctionSlotsInt := int(openAuctionSlots)
assert.Equal(t, openAuctionSlotsConst, openAuctionSlotsInt)
}
}
func TestAuctionGetClosedAuctionSlots(t *testing.T) {
if auctionClient != nil {
closedAuctionSlots, err := auctionClient.AuctionGetClosedAuctionSlots()
require.Nil(t, err)
closedAuctionSlotsInt := int(closedAuctionSlots)
assert.Equal(t, closedAuctionSlotsConst, closedAuctionSlotsInt)
}
}
func TestAuctionGetOutbidding(t *testing.T) {
if auctionClient != nil {
outbidding, err := auctionClient.AuctionGetOutbidding()
require.Nil(t, err)
outbiddingInt := int(outbidding)
assert.Equal(t, outbiddingConst, outbiddingInt)
}
}
func TestAuctionGetAllocationRatio(t *testing.T) {
if auctionClient != nil {
allocationRatio, err := auctionClient.AuctionGetAllocationRatio()
require.Nil(t, err)
assert.Equal(t, allocationRatioConst, allocationRatio)
}
}
func TestAuctionGetDonationAddress(t *testing.T) {
if auctionClient != nil {
donationAddress, err := auctionClient.AuctionGetDonationAddress()
require.Nil(t, err)
donationAddressConst := common.HexToAddress(donationAddressConstStr)
assert.Equal(t, &donationAddressConst, donationAddress)
}
}
func TestAuctionGetBootCoordinator(t *testing.T) {
if auctionClient != nil {
bootCoordinator, err := auctionClient.AuctionGetBootCoordinator()
require.Nil(t, err)
bootCoordinatorConst := common.HexToAddress(bootCoordinatorConstStr)
assert.Equal(t, &bootCoordinatorConst, bootCoordinator)
}
}
func TestAuctionGetCurrentSlotNumber(t *testing.T) {
if auctionClient != nil {
currentSlot, err := auctionClient.AuctionGetCurrentSlotNumber()
require.Nil(t, err)
currentSlotInt := int(currentSlot)
assert.Equal(t, currentSlotConst, currentSlotInt)
}
}

14
eth/contracts/README.md Normal file
View File

@@ -0,0 +1,14 @@
## Contracts
The go code of the contracts has been generated with the following command:
```
abigen --abi=WithdrawalDelayer.abi --bin=WithdrawalDelayer.bin --pkg=WithdrawalDelayer --out=WithdrawalDelayer.go
abigen --abi=Hermez.abi --bin=Hermez.bin --pkg=Hermez --out=Hermez.go
abigen --abi=HermezAuctionProtocol.abi --bin=HermezAuctionProtocol.bin --pkg=HermezAuctionProtocol --out=HermezAuctionProtocol.go
```
You must compile the contracts to get the `.bin` and `.abi` files. The contracts used are in the repo: https://github.com/hermeznetwork/contracts-circuits
Specifically they have been processed in the commit with hash: `0ba124d60e192afda99ef4703aa13b8fef4a392a`
> abigen version 1.9.21

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long