Browse Source

Ethclient remove ERC777

feature/sql-semaphore1
laisolizq 3 years ago
committed by Eduard S
parent
commit
d4753c7e1b
18 changed files with 1566 additions and 2365 deletions
  1. +45
    -68
      eth/auction.go
  2. +8
    -8
      eth/auction_test.go
  3. +10
    -4
      eth/client.go
  4. +2
    -10
      eth/contracts/README.md
  5. +53
    -32
      eth/contracts/auction/HermezAuctionProtocol.go
  6. +0
    -19
      eth/contracts/erc20.sol
  7. +0
    -274
      eth/contracts/erc20/erc20.go
  8. +0
    -1696
      eth/contracts/erc777/ERC777.go
  9. +35
    -35
      eth/contracts/hermez/Hermez.go
  10. +1163
    -0
      eth/contracts/tokenHEZ/HEZ.go
  11. +2
    -23
      eth/contracts/withdrawdelayer/WithdrawalDelayer.go
  12. +2
    -2
      eth/ethereum.go
  13. +3
    -3
      eth/ethereum_test.go
  14. +98
    -0
      eth/helpers.go
  15. +25
    -13
      eth/main_test.go
  16. +53
    -84
      eth/rollup.go
  17. +56
    -83
      eth/rollup_test.go
  18. +11
    -11
      eth/wdelayer_test.go

+ 45
- 68
eth/auction.go

@ -2,7 +2,6 @@ package eth
import (
"context"
"encoding/binary"
"fmt"
"math/big"
"strings"
@ -15,8 +14,7 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
HermezAuctionProtocol "github.com/hermeznetwork/hermez-node/eth/contracts/auction"
ERC777 "github.com/hermeznetwork/hermez-node/eth/contracts/erc777"
"golang.org/x/crypto/sha3"
HEZ "github.com/hermeznetwork/hermez-node/eth/contracts/tokenHEZ"
)
// AuctionConstants are the constants of the Rollup Smart Contract
@ -237,8 +235,9 @@ type AuctionInterface interface {
AuctionGetSlotSet(slot int64) (*big.Int, error)
// Bidding
AuctionBid(slot int64, bidAmount *big.Int) (*types.Transaction, error)
AuctionMultiBid(startingSlot int64, endingSlot int64, slotSet [6]bool, maxBid, closedMinBid, budget *big.Int) (*types.Transaction, error)
AuctionBid(amount *big.Int, slot int64, bidAmount *big.Int, deadline *big.Int) (tx *types.Transaction, err error)
AuctionMultiBid(amount *big.Int, startingSlot, endingSlot int64, slotSets [6]bool,
maxBid, minBid, deadline *big.Int) (tx *types.Transaction, err error)
// Forge
AuctionCanForge(forger ethCommon.Address, blockNum int64) (bool, error)
@ -262,15 +261,15 @@ type AuctionInterface interface {
// AuctionClient is the implementation of the interface to the Auction Smart Contract in ethereum.
type AuctionClient struct {
client *EthereumClient
address ethCommon.Address
tokenAddress ethCommon.Address
auction *HermezAuctionProtocol.HermezAuctionProtocol
contractAbi abi.ABI
client *EthereumClient
address ethCommon.Address
tokenHEZ TokenConfig
auction *HermezAuctionProtocol.HermezAuctionProtocol
contractAbi abi.ABI
}
// NewAuctionClient creates a new AuctionClient. `tokenAddress` is the address of the HEZ tokens.
func NewAuctionClient(client *EthereumClient, address, tokenAddress ethCommon.Address) (*AuctionClient, error) {
func NewAuctionClient(client *EthereumClient, address ethCommon.Address, tokenHEZ TokenConfig) (*AuctionClient, error) {
contractAbi, err := abi.JSON(strings.NewReader(string(HermezAuctionProtocol.HermezAuctionProtocolABI)))
if err != nil {
return nil, err
@ -280,11 +279,11 @@ func NewAuctionClient(client *EthereumClient, address, tokenAddress ethCommon.Ad
return nil, err
}
return &AuctionClient{
client: client,
address: address,
tokenAddress: tokenAddress,
auction: auction,
contractAbi: contractAbi,
client: client,
address: address,
tokenHEZ: tokenHEZ,
auction: auction,
contractAbi: contractAbi,
}, nil
}
@ -558,78 +557,56 @@ func (c *AuctionClient) AuctionGetSlotNumber(blockNum int64) (slot int64, err er
}
// AuctionBid is the interface to call the smart contract function
func (c *AuctionClient) AuctionBid(slot int64, bidAmount *big.Int) (tx *types.Transaction, err error) {
func (c *AuctionClient) AuctionBid(amount *big.Int, slot int64, bidAmount *big.Int, deadline *big.Int) (tx *types.Transaction, err error) {
if tx, err = c.client.CallAuth(
0,
func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
tokens, err := ERC777.NewERC777(c.tokenAddress, ec)
tokenHEZcontract, err := HEZ.NewHEZ(c.tokenHEZ.Address, ec)
if err != nil {
return nil, err
}
bidFnSignature := []byte("bid(uint128,uint128)")
hash := sha3.NewLegacyKeccak256()
_, err = hash.Write(bidFnSignature)
if err != nil {
return nil, err
}
methodID := hash.Sum(nil)[:4]
slotBytes := make([]byte, 8)
binary.BigEndian.PutUint64(slotBytes, uint64(slot))
paddedSlot := ethCommon.LeftPadBytes(slotBytes, 32)
paddedAmount := ethCommon.LeftPadBytes(bidAmount.Bytes(), 32)
var userData []byte
userData = append(userData, methodID...)
userData = append(userData, paddedSlot...)
userData = append(userData, paddedAmount...)
return tokens.Send(auth, c.address, bidAmount, userData)
owner := c.client.account.Address
spender := c.address
nonce, err := tokenHEZcontract.Nonces(nil, owner)
tokenname := c.tokenHEZ.Name
tokenAddr := c.tokenHEZ.Address
chainid, _ := c.client.client.ChainID(context.Background())
digest, _ := createPermitDigest(tokenAddr, owner, spender, chainid, amount, nonce, deadline, tokenname)
signature, _ := c.client.ks.SignHash(*c.client.account, digest)
permit := createPermit(owner, spender, amount, deadline, digest, signature)
_slot := big.NewInt(slot)
return c.auction.ProcessBid(auth, amount, _slot, bidAmount, permit)
},
); err != nil {
return nil, fmt.Errorf("Failed bid: %w", err)
}
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) (tx *types.Transaction, err error) {
func (c *AuctionClient) AuctionMultiBid(amount *big.Int, startingSlot, endingSlot int64, slotSets [6]bool,
maxBid, minBid, deadline *big.Int) (tx *types.Transaction, err error) {
if tx, err = c.client.CallAuth(
1000000, //nolint:gomnd
func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
tokens, err := ERC777.NewERC777(c.tokenAddress, ec)
tokenHEZcontract, err := HEZ.NewHEZ(c.tokenHEZ.Address, ec)
if err != nil {
return nil, err
}
multiBidFnSignature := []byte("multiBid(uint128,uint128,bool[6],uint128,uint128)")
hash := sha3.NewLegacyKeccak256()
_, err = hash.Write(multiBidFnSignature)
if err != nil {
return nil, err
}
methodID := hash.Sum(nil)[:4]
startingSlotBytes := make([]byte, 8)
binary.BigEndian.PutUint64(startingSlotBytes, uint64(startingSlot))
paddedStartingSlot := ethCommon.LeftPadBytes(startingSlotBytes, 32)
endingSlotBytes := make([]byte, 8)
binary.BigEndian.PutUint64(endingSlotBytes, uint64(endingSlot))
paddedEndingSlot := ethCommon.LeftPadBytes(endingSlotBytes, 32)
paddedMinBid := ethCommon.LeftPadBytes(closedMinBid.Bytes(), 32)
paddedMaxBid := ethCommon.LeftPadBytes(maxBid.Bytes(), 32)
var userData []byte
userData = append(userData, methodID...)
userData = append(userData, paddedStartingSlot...)
userData = append(userData, paddedEndingSlot...)
for i := 0; i < len(slotSet); i++ {
if slotSet[i] {
paddedSlotSet := ethCommon.LeftPadBytes([]byte{1}, 32)
userData = append(userData, paddedSlotSet...)
} else {
paddedSlotSet := ethCommon.LeftPadBytes([]byte{0}, 32)
userData = append(userData, paddedSlotSet...)
}
}
userData = append(userData, paddedMaxBid...)
userData = append(userData, paddedMinBid...)
return tokens.Send(auth, c.address, budget, userData)
owner := c.client.account.Address
spender := c.address
nonce, err := tokenHEZcontract.Nonces(nil, owner)
tokenname := c.tokenHEZ.Name
tokenAddr := c.tokenHEZ.Address
chainid, _ := c.client.client.ChainID(context.Background())
digest, _ := createPermitDigest(tokenAddr, owner, spender, chainid, amount, nonce, deadline, tokenname)
signature, _ := c.client.ks.SignHash(*c.client.account, digest)
permit := createPermit(owner, spender, amount, deadline, digest, signature)
_startingSlot := big.NewInt(startingSlot)
_endingSlot := big.NewInt(endingSlot)
return c.auction.ProcessMultiBid(auth, amount, _startingSlot, _endingSlot, slotSets, maxBid, minBid, permit)
},
); err != nil {
return nil, fmt.Errorf("Failed multibid: %w", err)

+ 8
- 8
eth/auction_test.go

@ -19,7 +19,7 @@ var allocationRatioConst [3]uint16 = [3]uint16{4000, 4000, 2000}
var auctionClientTest *AuctionClient
//var genesisBlock = 93
var genesisBlock = 100
var genesisBlock = 97
var minBidStr = "10000000000000000000"
var URL = "http://localhost:3000"
@ -270,8 +270,10 @@ func TestAuctionBid(t *testing.T) {
require.Nil(t, err)
bidAmount := new(big.Int)
bidAmount.SetString("12000000000000000000", 10)
amount := new(big.Int)
amount.SetString("12000000000000000000", 10)
bidderAddress := governanceAddressConst
_, err = auctionClientTest.AuctionBid(currentSlot+4, bidAmount)
_, err = auctionClientTest.AuctionBid(amount, currentSlot+4, bidAmount, deadline)
require.Nil(t, err)
currentBlockNum, _ := auctionClientTest.client.EthCurrentBlock()
auctionEvents, _, _ := auctionClientTest.AuctionEventsByBlock(currentBlockNum)
@ -309,7 +311,7 @@ func TestAuctionMultiBid(t *testing.T) {
budget := new(big.Int)
budget.SetString("45200000000000000000", 10)
bidderAddress := governanceAddressConst
_, err = auctionClientTest.AuctionMultiBid(currentSlot+4, currentSlot+10, slotSet, maxBid, minBid, budget)
_, err = auctionClientTest.AuctionMultiBid(budget, currentSlot+4, currentSlot+10, slotSet, maxBid, minBid, deadline)
require.Nil(t, err)
currentBlockNum, _ := auctionClientTest.client.EthCurrentBlock()
auctionEvents, _, _ := auctionClientTest.AuctionEventsByBlock(currentBlockNum)
@ -346,17 +348,15 @@ func TestAuctionClaimHEZ(t *testing.T) {
}
func TestAuctionForge(t *testing.T) {
auctionClientTestHermez, err := NewAuctionClient(ethereumClientHermez, auctionTestAddressConst, tokenHEZAddressConst)
auctionClientTestHermez, err := NewAuctionClient(ethereumClientHermez, auctionTestAddressConst, tokenHEZ)
require.Nil(t, err)
slotConst := 4
blockNum := int64(int(BLOCKSPERSLOT)*slotConst + genesisBlock)
currentBlockNum, _ := auctionClientTest.client.EthCurrentBlock()
currentBlockNum, _ := auctionClientTestHermez.client.EthCurrentBlock()
blocksToAdd := blockNum - currentBlockNum
addBlocks(blocksToAdd, ethClientDialURL)
currentBlockNum, _ = auctionClientTest.client.EthCurrentBlock()
currentBlockNum, _ = auctionClientTestHermez.client.EthCurrentBlock()
assert.Equal(t, currentBlockNum, blockNum)
_, err = auctionClientTestHermez.AuctionForge(bootCoordinatorAddressConst)
require.Contains(t, err.Error(), "Can't forge")
_, err = auctionClientTestHermez.AuctionForge(governanceAddressConst)
require.Nil(t, err)
}

+ 10
- 4
eth/client.go

@ -30,6 +30,12 @@ type Client struct {
RollupClient
}
// TokenConfig is used to define the information about token
type TokenConfig struct {
Address ethCommon.Address
Name string
}
// RollupConfig is the configuration for the Rollup smart contract interface
type RollupConfig struct {
Address ethCommon.Address
@ -37,8 +43,8 @@ type RollupConfig struct {
// AuctionConfig is the configuration for the Auction smart contract interface
type AuctionConfig struct {
Address ethCommon.Address
TokenHEZAddress ethCommon.Address
Address ethCommon.Address
TokenHEZ TokenConfig
}
// ClientConfig is the configuration of the Client
@ -51,11 +57,11 @@ type ClientConfig struct {
// NewClient creates a new Client to interact with Ethereum and the Hermez smart contracts.
func NewClient(client *ethclient.Client, account *accounts.Account, ks *ethKeystore.KeyStore, cfg *ClientConfig) (*Client, error) {
ethereumClient := NewEthereumClient(client, account, ks, &cfg.Ethereum)
auctionClient, err := NewAuctionClient(ethereumClient, cfg.Auction.Address, cfg.Auction.TokenHEZAddress)
auctionClient, err := NewAuctionClient(ethereumClient, cfg.Auction.Address, cfg.Auction.TokenHEZ)
if err != nil {
return nil, err
}
rollupCient, err := NewRollupClient(ethereumClient, cfg.Rollup.Address, cfg.Auction.TokenHEZAddress)
rollupCient, err := NewRollupClient(ethereumClient, cfg.Rollup.Address, cfg.Auction.TokenHEZ)
if err != nil {
return nil, err
}

+ 2
- 10
eth/contracts/README.md

@ -6,20 +6,12 @@ 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
abigen --abi=ERC777.abi --bin=ERC777.bin --pkg=ERC777 --out=ERC777.go
abigen --abi=HEZ.abi --bin=HEZ.bin --pkg=HEZ --out=HEZ.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
Specifically they have been processed in the commit with hash: `60e03e981f1ce607c27d405952bfc98de376f0c5`
Specifically they have been processed in the commit with hash: `7574ba47fd3d7dab2653a22f57b15c69280350dc`
> abigen version 1.9.21
---
ERC20 go code was generated with the following command:
```
abigen --sol erc20.sol --pkg erc20 --out erc20/erc20.go
```
Versions:
```

+ 53
- 32
eth/contracts/auction/HermezAuctionProtocol.go
File diff suppressed because it is too large
View File


+ 0
- 19
eth/contracts/erc20.sol

@ -1,19 +0,0 @@
pragma solidity ^0.7.0;
contract ERC20 {
string public constant name = "";
string public constant symbol = "";
uint8 public constant decimals = 0;
/*
function totalSupply() public constant returns (uint);
function balanceOf(address tokenOwner) public constant returns (uint balance);
function allowance(address tokenOwner, address spender) public constant returns (uint remaining);
function transfer(address to, uint tokens) public returns (bool success);
function approve(address spender, uint tokens) public returns (bool success);
function transferFrom(address from, address to, uint tokens) public returns (bool success);
event Transfer(address indexed from, address indexed to, uint tokens);
event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
*/
}

+ 0
- 274
eth/contracts/erc20/erc20.go

@ -1,274 +0,0 @@
// Code generated - DO NOT EDIT.
// This file is a generated binding and any manual changes will be lost.
package erc20
import (
"math/big"
"strings"
ethereum "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/event"
)
// Reference imports to suppress errors if they are not otherwise used.
var (
_ = big.NewInt
_ = strings.NewReader
_ = ethereum.NotFound
_ = bind.Bind
_ = common.Big1
_ = types.BloomLookup
_ = event.NewSubscription
)
// ERC20ABI is the input ABI used to generate the binding from.
const ERC20ABI = "[{\"inputs\":[],\"name\":\"decimals\",\"outputs\":[{\"internalType\":\"uint8\",\"name\":\"\",\"type\":\"uint8\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"name\",\"outputs\":[{\"internalType\":\"string\",\"name\":\"\",\"type\":\"string\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"symbol\",\"outputs\":[{\"internalType\":\"string\",\"name\":\"\",\"type\":\"string\"}],\"stateMutability\":\"view\",\"type\":\"function\"}]"
// ERC20FuncSigs maps the 4-byte function signature to its string representation.
var ERC20FuncSigs = map[string]string{
"313ce567": "decimals()",
"06fdde03": "name()",
"95d89b41": "symbol()",
}
// ERC20Bin is the compiled bytecode used for deploying new contracts.
var ERC20Bin = "0x608060405234801561001057600080fd5b50610123806100206000396000f3fe6080604052348015600f57600080fd5b5060043610603c5760003560e01c806306fdde03146041578063313ce5671460b957806395d89b41146041575b600080fd5b604760d5565b6040805160208082528351818301528351919283929083019185019080838360005b83811015607f5781810151838201526020016069565b50505050905090810190601f16801560ab5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b60bf60e8565b6040805160ff9092168252519081900360200190f35b6040518060200160405280600081525081565b60008156fea26469706673582212209717f9f3c7b4f090e7741b44c5cb9425a26b593410462c0f4c2c0c0879db648d64736f6c63430007010033"
// DeployERC20 deploys a new Ethereum contract, binding an instance of ERC20 to it.
func DeployERC20(auth *bind.TransactOpts, backend bind.ContractBackend) (common.Address, *types.Transaction, *ERC20, error) {
parsed, err := abi.JSON(strings.NewReader(ERC20ABI))
if err != nil {
return common.Address{}, nil, nil, err
}
address, tx, contract, err := bind.DeployContract(auth, parsed, common.FromHex(ERC20Bin), backend)
if err != nil {
return common.Address{}, nil, nil, err
}
return address, tx, &ERC20{ERC20Caller: ERC20Caller{contract: contract}, ERC20Transactor: ERC20Transactor{contract: contract}, ERC20Filterer: ERC20Filterer{contract: contract}}, nil
}
// ERC20 is an auto generated Go binding around an Ethereum contract.
type ERC20 struct {
ERC20Caller // Read-only binding to the contract
ERC20Transactor // Write-only binding to the contract
ERC20Filterer // Log filterer for contract events
}
// ERC20Caller is an auto generated read-only Go binding around an Ethereum contract.
type ERC20Caller struct {
contract *bind.BoundContract // Generic contract wrapper for the low level calls
}
// ERC20Transactor is an auto generated write-only Go binding around an Ethereum contract.
type ERC20Transactor struct {
contract *bind.BoundContract // Generic contract wrapper for the low level calls
}
// ERC20Filterer is an auto generated log filtering Go binding around an Ethereum contract events.
type ERC20Filterer struct {
contract *bind.BoundContract // Generic contract wrapper for the low level calls
}
// ERC20Session is an auto generated Go binding around an Ethereum contract,
// with pre-set call and transact options.
type ERC20Session struct {
Contract *ERC20 // Generic contract binding to set the session for
CallOpts bind.CallOpts // Call options to use throughout this session
TransactOpts bind.TransactOpts // Transaction auth options to use throughout this session
}
// ERC20CallerSession is an auto generated read-only Go binding around an Ethereum contract,
// with pre-set call options.
type ERC20CallerSession struct {
Contract *ERC20Caller // Generic contract caller binding to set the session for
CallOpts bind.CallOpts // Call options to use throughout this session
}
// ERC20TransactorSession is an auto generated write-only Go binding around an Ethereum contract,
// with pre-set transact options.
type ERC20TransactorSession struct {
Contract *ERC20Transactor // Generic contract transactor binding to set the session for
TransactOpts bind.TransactOpts // Transaction auth options to use throughout this session
}
// ERC20Raw is an auto generated low-level Go binding around an Ethereum contract.
type ERC20Raw struct {
Contract *ERC20 // Generic contract binding to access the raw methods on
}
// ERC20CallerRaw is an auto generated low-level read-only Go binding around an Ethereum contract.
type ERC20CallerRaw struct {
Contract *ERC20Caller // Generic read-only contract binding to access the raw methods on
}
// ERC20TransactorRaw is an auto generated low-level write-only Go binding around an Ethereum contract.
type ERC20TransactorRaw struct {
Contract *ERC20Transactor // Generic write-only contract binding to access the raw methods on
}
// NewERC20 creates a new instance of ERC20, bound to a specific deployed contract.
func NewERC20(address common.Address, backend bind.ContractBackend) (*ERC20, error) {
contract, err := bindERC20(address, backend, backend, backend)
if err != nil {
return nil, err
}
return &ERC20{ERC20Caller: ERC20Caller{contract: contract}, ERC20Transactor: ERC20Transactor{contract: contract}, ERC20Filterer: ERC20Filterer{contract: contract}}, nil
}
// NewERC20Caller creates a new read-only instance of ERC20, bound to a specific deployed contract.
func NewERC20Caller(address common.Address, caller bind.ContractCaller) (*ERC20Caller, error) {
contract, err := bindERC20(address, caller, nil, nil)
if err != nil {
return nil, err
}
return &ERC20Caller{contract: contract}, nil
}
// NewERC20Transactor creates a new write-only instance of ERC20, bound to a specific deployed contract.
func NewERC20Transactor(address common.Address, transactor bind.ContractTransactor) (*ERC20Transactor, error) {
contract, err := bindERC20(address, nil, transactor, nil)
if err != nil {
return nil, err
}
return &ERC20Transactor{contract: contract}, nil
}
// NewERC20Filterer creates a new log filterer instance of ERC20, bound to a specific deployed contract.
func NewERC20Filterer(address common.Address, filterer bind.ContractFilterer) (*ERC20Filterer, error) {
contract, err := bindERC20(address, nil, nil, filterer)
if err != nil {
return nil, err
}
return &ERC20Filterer{contract: contract}, nil
}
// bindERC20 binds a generic wrapper to an already deployed contract.
func bindERC20(address common.Address, caller bind.ContractCaller, transactor bind.ContractTransactor, filterer bind.ContractFilterer) (*bind.BoundContract, error) {
parsed, err := abi.JSON(strings.NewReader(ERC20ABI))
if err != nil {
return nil, err
}
return bind.NewBoundContract(address, parsed, caller, transactor, filterer), nil
}
// Call invokes the (constant) contract method with params as input values and
// sets the output to result. The result type might be a single field for simple
// returns, a slice of interfaces for anonymous returns and a struct for named
// returns.
func (_ERC20 *ERC20Raw) Call(opts *bind.CallOpts, result interface{}, method string, params ...interface{}) error {
return _ERC20.Contract.ERC20Caller.contract.Call(opts, result, method, params...)
}
// Transfer initiates a plain transaction to move funds to the contract, calling
// its default method if one is available.
func (_ERC20 *ERC20Raw) Transfer(opts *bind.TransactOpts) (*types.Transaction, error) {
return _ERC20.Contract.ERC20Transactor.contract.Transfer(opts)
}
// Transact invokes the (paid) contract method with params as input values.
func (_ERC20 *ERC20Raw) Transact(opts *bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, error) {
return _ERC20.Contract.ERC20Transactor.contract.Transact(opts, method, params...)
}
// Call invokes the (constant) contract method with params as input values and
// sets the output to result. The result type might be a single field for simple
// returns, a slice of interfaces for anonymous returns and a struct for named
// returns.
func (_ERC20 *ERC20CallerRaw) Call(opts *bind.CallOpts, result interface{}, method string, params ...interface{}) error {
return _ERC20.Contract.contract.Call(opts, result, method, params...)
}
// Transfer initiates a plain transaction to move funds to the contract, calling
// its default method if one is available.
func (_ERC20 *ERC20TransactorRaw) Transfer(opts *bind.TransactOpts) (*types.Transaction, error) {
return _ERC20.Contract.contract.Transfer(opts)
}
// Transact invokes the (paid) contract method with params as input values.
func (_ERC20 *ERC20TransactorRaw) Transact(opts *bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, error) {
return _ERC20.Contract.contract.Transact(opts, method, params...)
}
// Decimals is a free data retrieval call binding the contract method 0x313ce567.
//
// Solidity: function decimals() view returns(uint8)
func (_ERC20 *ERC20Caller) Decimals(opts *bind.CallOpts) (uint8, error) {
var (
ret0 = new(uint8)
)
out := ret0
err := _ERC20.contract.Call(opts, out, "decimals")
return *ret0, err
}
// Decimals is a free data retrieval call binding the contract method 0x313ce567.
//
// Solidity: function decimals() view returns(uint8)
func (_ERC20 *ERC20Session) Decimals() (uint8, error) {
return _ERC20.Contract.Decimals(&_ERC20.CallOpts)
}
// Decimals is a free data retrieval call binding the contract method 0x313ce567.
//
// Solidity: function decimals() view returns(uint8)
func (_ERC20 *ERC20CallerSession) Decimals() (uint8, error) {
return _ERC20.Contract.Decimals(&_ERC20.CallOpts)
}
// Name is a free data retrieval call binding the contract method 0x06fdde03.
//
// Solidity: function name() view returns(string)
func (_ERC20 *ERC20Caller) Name(opts *bind.CallOpts) (string, error) {
var (
ret0 = new(string)
)
out := ret0
err := _ERC20.contract.Call(opts, out, "name")
return *ret0, err
}
// Name is a free data retrieval call binding the contract method 0x06fdde03.
//
// Solidity: function name() view returns(string)
func (_ERC20 *ERC20Session) Name() (string, error) {
return _ERC20.Contract.Name(&_ERC20.CallOpts)
}
// Name is a free data retrieval call binding the contract method 0x06fdde03.
//
// Solidity: function name() view returns(string)
func (_ERC20 *ERC20CallerSession) Name() (string, error) {
return _ERC20.Contract.Name(&_ERC20.CallOpts)
}
// Symbol is a free data retrieval call binding the contract method 0x95d89b41.
//
// Solidity: function symbol() view returns(string)
func (_ERC20 *ERC20Caller) Symbol(opts *bind.CallOpts) (string, error) {
var (
ret0 = new(string)
)
out := ret0
err := _ERC20.contract.Call(opts, out, "symbol")
return *ret0, err
}
// Symbol is a free data retrieval call binding the contract method 0x95d89b41.
//
// Solidity: function symbol() view returns(string)
func (_ERC20 *ERC20Session) Symbol() (string, error) {
return _ERC20.Contract.Symbol(&_ERC20.CallOpts)
}
// Symbol is a free data retrieval call binding the contract method 0x95d89b41.
//
// Solidity: function symbol() view returns(string)
func (_ERC20 *ERC20CallerSession) Symbol() (string, error) {
return _ERC20.Contract.Symbol(&_ERC20.CallOpts)
}

+ 0
- 1696
eth/contracts/erc777/ERC777.go
File diff suppressed because it is too large
View File


+ 35
- 35
eth/contracts/hermez/Hermez.go
File diff suppressed because it is too large
View File


+ 1163
- 0
eth/contracts/tokenHEZ/HEZ.go
File diff suppressed because it is too large
View File


+ 2
- 23
eth/contracts/withdrawdelayer/WithdrawalDelayer.go
File diff suppressed because it is too large
View File


+ 2
- 2
eth/ethereum.go

@ -13,7 +13,7 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/hermeznetwork/hermez-node/common"
"github.com/hermeznetwork/hermez-node/eth/contracts/erc20"
HEZ "github.com/hermeznetwork/hermez-node/eth/contracts/tokenHEZ"
"github.com/hermeznetwork/hermez-node/log"
)
@ -277,7 +277,7 @@ func (c *EthereumClient) EthBlockByNumber(ctx context.Context, number int64) (*c
// EthERC20Consts returns the constants defined for a particular ERC20 Token instance.
func (c *EthereumClient) EthERC20Consts(tokenAddress ethCommon.Address) (*ERC20Consts, error) {
instance, err := erc20.NewERC20(tokenAddress, c.client)
instance, err := HEZ.NewHEZ(tokenAddress, c.client)
if err != nil {
return nil, err
}

+ 3
- 3
eth/ethereum_test.go

@ -13,9 +13,9 @@ func TestEthERC20(t *testing.T) {
require.Nil(t, err)
client := NewEthereumClient(ethClient, accountAux, ks, nil)
consts, err := client.EthERC20Consts(tokenERC20AddressConst)
consts, err := client.EthERC20Consts(tokenHEZAddressConst)
require.Nil(t, err)
assert.Equal(t, "ERC20_0", consts.Name)
assert.Equal(t, "20_0", consts.Symbol)
assert.Equal(t, "Hermez Network Token", consts.Name)
assert.Equal(t, "HEZ", consts.Symbol)
assert.Equal(t, uint64(18), consts.Decimals)
}

+ 98
- 0
eth/helpers.go

@ -1,10 +1,15 @@
package eth
import (
"encoding/hex"
"fmt"
"math/big"
"net/http"
"strconv"
"strings"
ethCommon "github.com/ethereum/go-ethereum/common"
"golang.org/x/crypto/sha3"
)
func addBlock(url string) {
@ -61,3 +66,96 @@ func addTime(seconds float64, url string) {
}
}()
}
func createPermitDigest(tokenAddr, owner, spender ethCommon.Address, chainID, value, nonce, deadline *big.Int, tokenName string) ([]byte, error) {
abiPermit := []byte("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)")
hashPermit := sha3.NewLegacyKeccak256()
hashPermit.Write(abiPermit)
abiEIP712Domain := []byte("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)")
hashEIP712Domain := sha3.NewLegacyKeccak256()
hashEIP712Domain.Write(abiEIP712Domain)
var encodeBytes []byte
paddedHash := ethCommon.LeftPadBytes(hashEIP712Domain.Sum(nil), 32)
hashName := sha3.NewLegacyKeccak256()
hashName.Write([]byte(tokenName))
paddedName := ethCommon.LeftPadBytes(hashName.Sum(nil), 32)
hashVersion := sha3.NewLegacyKeccak256()
hashVersion.Write([]byte("1"))
paddedX := ethCommon.LeftPadBytes(hashVersion.Sum(nil), 32)
paddedChainID := ethCommon.LeftPadBytes(chainID.Bytes(), 32)
paddedAddr := ethCommon.LeftPadBytes(tokenAddr.Bytes(), 32)
encodeBytes = append(encodeBytes, paddedHash...)
encodeBytes = append(encodeBytes, paddedName...)
encodeBytes = append(encodeBytes, paddedX...)
encodeBytes = append(encodeBytes, paddedChainID...)
encodeBytes = append(encodeBytes, paddedAddr...)
_domainSeparator := sha3.NewLegacyKeccak256()
_domainSeparator.Write(encodeBytes)
var bytes1 []byte
paddedHashPermit := ethCommon.LeftPadBytes(hashPermit.Sum(nil), 32)
paddedOwner := ethCommon.LeftPadBytes(owner.Bytes(), 32)
paddedSpender := ethCommon.LeftPadBytes(spender.Bytes(), 32)
paddedValue := ethCommon.LeftPadBytes(value.Bytes(), 32)
paddedNonce := ethCommon.LeftPadBytes(nonce.Bytes(), 32)
paddedDeadline := ethCommon.LeftPadBytes(deadline.Bytes(), 32)
bytes1 = append(bytes1, paddedHashPermit...)
bytes1 = append(bytes1, paddedOwner...)
bytes1 = append(bytes1, paddedSpender...)
bytes1 = append(bytes1, paddedValue...)
bytes1 = append(bytes1, paddedNonce...)
bytes1 = append(bytes1, paddedDeadline...)
hashBytes1 := sha3.NewLegacyKeccak256()
hashBytes1.Write(bytes1)
var bytes2 []byte
byte19, err := hex.DecodeString("19")
if err != nil {
return nil, err
}
byte01, err := hex.DecodeString("01")
if err != nil {
return nil, err
}
paddedY := ethCommon.LeftPadBytes(byte19, 1)
paddedZ := ethCommon.LeftPadBytes(byte01, 1)
paddedDomainSeparator := ethCommon.LeftPadBytes(_domainSeparator.Sum(nil), 32)
paddedHashBytes1 := ethCommon.LeftPadBytes(hashBytes1.Sum(nil), 32)
bytes2 = append(bytes2, paddedY...)
bytes2 = append(bytes2, paddedZ...)
bytes2 = append(bytes2, paddedDomainSeparator...)
bytes2 = append(bytes2, paddedHashBytes1...)
hashBytes2 := sha3.NewLegacyKeccak256()
hashBytes2.Write(bytes2)
return hashBytes2.Sum(nil), nil
}
func createPermit(owner, spender ethCommon.Address, amount, deadline *big.Int, digest, signature []byte) []byte {
r := signature[0:32]
s := signature[32:64]
v := signature[64] + byte(27)
ABIpermit := []byte("permit(address,address,uint256,uint256,uint8,bytes32,bytes32)")
hash := sha3.NewLegacyKeccak256()
hash.Write(ABIpermit)
methodID := hash.Sum(nil)[:4]
var permit []byte
paddedOwner := ethCommon.LeftPadBytes(owner.Bytes(), 32)
paddedSpender := ethCommon.LeftPadBytes(spender.Bytes(), 32)
paddedAmount := ethCommon.LeftPadBytes(amount.Bytes(), 32)
paddedDeadline := ethCommon.LeftPadBytes(deadline.Bytes(), 32)
paddedV := ethCommon.LeftPadBytes([]byte{v}, 32)
permit = append(permit, methodID...)
permit = append(permit, paddedOwner...)
permit = append(permit, paddedSpender...)
permit = append(permit, paddedAmount...)
permit = append(permit, paddedDeadline...)
permit = append(permit, paddedV...)
permit = append(permit, r...)
permit = append(permit, s...)
return permit
}

+ 25
- 13
eth/main_test.go

@ -2,6 +2,7 @@ package eth
import (
"io/ioutil"
"math/big"
"os"
"testing"
@ -22,27 +23,25 @@ var governancePrivateKey = os.Getenv("GOV_PK")
var ethClientDialURL = os.Getenv("ETHCLIENT_DIAL_URL")*/
var ethClientDialURL = "http://localhost:8545"
var password = "pass"
var deadline, _ = new(big.Int).SetString("ffffffffffffffffffffffffffffffff", 16)
// Smart Contract Addresses
var (
auctionAddressStr = "0x500D1d6A4c7D8Ae28240b47c8FCde034D827fD5e"
auctionAddressStr = "0x038B86d9d8FAFdd0a02ebd1A476432877b0107C8"
auctionAddressConst = ethCommon.HexToAddress(auctionAddressStr)
auctionTestAddressStr = "0x1d80315fac6aBd3EfeEbE97dEc44461ba7556160"
auctionTestAddressStr = "0xEcc0a6dbC0bb4D51E4F84A315a9e5B0438cAD4f0"
auctionTestAddressConst = ethCommon.HexToAddress(auctionTestAddressStr)
donationAddressStr = "0x6c365935CA8710200C7595F0a72EB6023A7706Cd"
donationAddressConst = ethCommon.HexToAddress(donationAddressStr)
bootCoordinatorAddressStr = "0xc783df8a850f42e7f7e57013759c285caa701eb6"
bootCoordinatorAddressConst = ethCommon.HexToAddress(bootCoordinatorAddressStr)
tokenERC777AddressStr = "0xf784709d2317D872237C4bC22f867d1BAe2913AB" //nolint:gosec
tokenERC777AddressConst = ethCommon.HexToAddress(tokenERC777AddressStr)
tokenERC20AddressStr = "0x3619DbE27d7c1e7E91aA738697Ae7Bc5FC3eACA5" //nolint:gosec
tokenERC20AddressConst = ethCommon.HexToAddress(tokenERC20AddressStr)
tokenHEZAddressConst = tokenERC777AddressConst
hermezRollupAddressStr = "0xEcc0a6dbC0bb4D51E4F84A315a9e5B0438cAD4f0"
tokenHEZAddressStr = "0xf4e77E5Da47AC3125140c470c71cBca77B5c638c" //nolint:gosec
tokenHEZAddressConst = ethCommon.HexToAddress(tokenHEZAddressStr)
hermezRollupAddressStr = "0xD6C850aeBFDC46D7F4c207e445cC0d6B0919BDBe"
hermezRollupAddressConst = ethCommon.HexToAddress(hermezRollupAddressStr)
wdelayerAddressStr = "0xD6C850aeBFDC46D7F4c207e445cC0d6B0919BDBe"
wdelayerAddressStr = "0x500D1d6A4c7D8Ae28240b47c8FCde034D827fD5e"
wdelayerAddressConst = ethCommon.HexToAddress(wdelayerAddressStr)
wdelayerTestAddressStr = "0x52d3b94181f8654db2530b0fEe1B19173f519C52"
wdelayerTestAddressStr = "0x1d80315fac6aBd3EfeEbE97dEc44461ba7556160"
wdelayerTestAddressConst = ethCommon.HexToAddress(wdelayerTestAddressStr)
safetyAddressStr = "0xE5904695748fe4A84b40b3fc79De2277660BD1D3"
safetyAddressConst = ethCommon.HexToAddress(safetyAddressStr)
@ -70,17 +69,27 @@ var (
auxAddressStr = "0x3d91185a02774C70287F6c74Dd26d13DFB58ff16"
auxAddressConst = ethCommon.HexToAddress(auxAddressStr)
aux2AddressSK = "28d1bfbbafe9d1d4f5a11c3c16ab6bf9084de48d99fbac4058bdfa3c80b29087"
aux2AddressStr = "0x532792b73c0c6e7565912e7039c59986f7e1dd1f"
aux2AddressConst = ethCommon.HexToAddress(aux2AddressStr)
hermezRollupTestSK = "28d1bfbbafe9d1d4f5a11c3c16ab6bf9084de48d99fbac4058bdfa3c80b29088"
hermezRollupTestAddressStr = "0xEa960515F8b4C237730F028cBAcF0a28E7F45dE0"
hermezRollupAddressTestConst = ethCommon.HexToAddress(hermezRollupTestAddressStr)
)
var tokenHEZ = TokenConfig{
Address: tokenHEZAddressConst,
Name: "Hermez Network Token",
}
var (
accountGov *accounts.Account
accountKep *accounts.Account
accountWhite *accounts.Account
accountGovDAO *accounts.Account
accountAux *accounts.Account
accountAux2 *accounts.Account
accountHermez *accounts.Account
ks *keystore.KeyStore
ethClient *ethclient.Client
@ -88,6 +97,7 @@ var (
ethereumClientKep *EthereumClient
ethereumClientGovDAO *EthereumClient
ethereumClientAux *EthereumClient
ethereumClientAux2 *EthereumClient
ethereumClientHermez *EthereumClient
)
@ -128,6 +138,7 @@ func TestMain(m *testing.M) {
accountWhite = addKey(ks, whiteHackGroupAddressSK)
accountGovDAO = addKey(ks, hermezGovernanceDAOAddressSK)
accountAux = addKey(ks, auxAddressSK)
accountAux2 = addKey(ks, aux2AddressSK)
accountHermez = addKey(ks, hermezRollupTestSK)
ethClient, err = ethclient.Dial(ethClientDialURL)
@ -138,15 +149,15 @@ func TestMain(m *testing.M) {
// Controllable Governance Address
ethereumClientGov := NewEthereumClient(ethClient, accountGov, ks, nil)
auctionClient, err = NewAuctionClient(ethereumClientGov, auctionAddressConst, tokenHEZAddressConst)
auctionClient, err = NewAuctionClient(ethereumClientGov, auctionAddressConst, tokenHEZ)
if err != nil {
panic(err)
}
auctionClientTest, err = NewAuctionClient(ethereumClientGov, auctionTestAddressConst, tokenHEZAddressConst)
auctionClientTest, err = NewAuctionClient(ethereumClientGov, auctionTestAddressConst, tokenHEZ)
if err != nil {
panic(err)
}
rollupClient, err = NewRollupClient(ethereumClientGov, hermezRollupAddressConst, tokenHEZAddressConst)
rollupClient, err = NewRollupClient(ethereumClientGov, hermezRollupAddressConst, tokenHEZ)
if err != nil {
panic(err)
}
@ -163,6 +174,7 @@ func TestMain(m *testing.M) {
ethereumClientWhite = NewEthereumClient(ethClient, accountWhite, ks, nil)
ethereumClientGovDAO = NewEthereumClient(ethClient, accountGovDAO, ks, nil)
ethereumClientAux = NewEthereumClient(ethClient, accountAux, ks, nil)
ethereumClientAux2 = NewEthereumClient(ethClient, accountAux2, ks, nil)
ethereumClientHermez = NewEthereumClient(ethClient, accountHermez, ks, nil)
exitVal = m.Run()

+ 53
- 84
eth/rollup.go

@ -14,11 +14,10 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/hermeznetwork/hermez-node/common"
ERC777 "github.com/hermeznetwork/hermez-node/eth/contracts/erc777"
Hermez "github.com/hermeznetwork/hermez-node/eth/contracts/hermez"
HEZ "github.com/hermeznetwork/hermez-node/eth/contracts/tokenHEZ"
"github.com/hermeznetwork/hermez-node/log"
"github.com/iden3/go-iden3-crypto/babyjub"
"golang.org/x/crypto/sha3"
)
const (
@ -259,13 +258,13 @@ type RollupInterface interface {
// Public Functions
RollupForgeBatch(*RollupForgeBatchArgs) (*types.Transaction, error)
RollupAddToken(tokenAddress ethCommon.Address, feeAddToken *big.Int) (*types.Transaction, error)
RollupAddToken(tokenAddress ethCommon.Address, feeAddToken, deadline *big.Int) (*types.Transaction, error)
RollupWithdrawMerkleProof(babyPubKey *babyjub.PublicKey, tokenID uint32, numExitRoot, idx int64, amount *big.Int, siblings []*big.Int, instantWithdraw bool) (*types.Transaction, error)
RollupWithdrawCircuit(proofA, proofC [2]*big.Int, proofB [2][2]*big.Int, tokenID uint32, numExitRoot, idx int64, amount *big.Int, instantWithdraw bool) (*types.Transaction, error)
RollupL1UserTxERC20ETH(fromBJJ *babyjub.PublicKey, fromIdx int64, loadAmount *big.Int, amount *big.Int, tokenID uint32, toIdx int64) (*types.Transaction, error)
RollupL1UserTxERC777(fromBJJ *babyjub.PublicKey, fromIdx int64, loadAmount *big.Int, amount *big.Int, tokenID uint32, toIdx int64) (*types.Transaction, error)
RollupL1UserTxERC20Permit(fromBJJ *babyjub.PublicKey, fromIdx int64, loadAmount *big.Int, amount *big.Int, tokenID uint32, toIdx int64, deadline *big.Int) (tx *types.Transaction, err error)
// Governance Public Functions
RollupUpdateForgeL1L2BatchTimeout(newForgeL1L2BatchTimeout int64) (*types.Transaction, error)
@ -289,15 +288,15 @@ 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
tokenHEZAddress ethCommon.Address
hermez *Hermez.Hermez
contractAbi abi.ABI
client *EthereumClient
address ethCommon.Address
tokenHEZ TokenConfig
hermez *Hermez.Hermez
contractAbi abi.ABI
}
// NewRollupClient creates a new RollupClient
func NewRollupClient(client *EthereumClient, address ethCommon.Address, tokenHEZAddress ethCommon.Address) (*RollupClient, error) {
func NewRollupClient(client *EthereumClient, address ethCommon.Address, tokenHEZ TokenConfig) (*RollupClient, error) {
contractAbi, err := abi.JSON(strings.NewReader(string(Hermez.HermezABI)))
if err != nil {
return nil, err
@ -307,11 +306,11 @@ func NewRollupClient(client *EthereumClient, address ethCommon.Address, tokenHEZ
return nil, err
}
return &RollupClient{
client: client,
address: address,
tokenHEZAddress: tokenHEZAddress,
hermez: hermez,
contractAbi: contractAbi,
client: client,
address: address,
tokenHEZ: tokenHEZ,
hermez: hermez,
contractAbi: contractAbi,
}, nil
}
@ -372,26 +371,25 @@ func (c *RollupClient) RollupForgeBatch(args *RollupForgeBatchArgs) (tx *types.T
// RollupAddToken is the interface to call the smart contract function.
// `feeAddToken` is the amount of HEZ tokens that will be paid to add the
// token. `feeAddToken` must match the public value of the smart contract.
func (c *RollupClient) RollupAddToken(tokenAddress ethCommon.Address, feeAddToken *big.Int) (tx *types.Transaction, err error) {
func (c *RollupClient) RollupAddToken(tokenAddress ethCommon.Address, feeAddToken, deadline *big.Int) (tx *types.Transaction, err error) {
if tx, err = c.client.CallAuth(
0,
func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
tokens, err := ERC777.NewERC777(c.tokenHEZAddress, ec)
tokenHEZcontract, err := HEZ.NewHEZ(c.tokenHEZ.Address, ec)
if err != nil {
return nil, err
}
addTokenFnSignature := []byte("addToken(address)")
hash := sha3.NewLegacyKeccak256()
_, err = hash.Write(addTokenFnSignature)
if err != nil {
return nil, err
}
methodID := hash.Sum(nil)[:4]
var data []byte
data = append(data, methodID...)
paddedAddress := ethCommon.LeftPadBytes(tokenAddress.Bytes(), 32)
data = append(data, paddedAddress[:]...)
return tokens.Send(auth, c.address, feeAddToken, data)
owner := c.client.account.Address
spender := c.address
nonce, err := tokenHEZcontract.Nonces(nil, owner)
tokenname := c.tokenHEZ.Name
tokenAddr := c.tokenHEZ.Address
chainid, _ := c.client.client.ChainID(context.Background())
digest, _ := createPermitDigest(tokenAddr, owner, spender, chainid, feeAddToken, nonce, deadline, tokenname)
signature, _ := c.client.ks.SignHash(*c.client.account, digest)
permit := createPermit(owner, spender, feeAddToken, deadline, digest, signature)
return c.hermez.AddToken(auth, tokenAddress, permit)
},
); err != nil {
return nil, fmt.Errorf("Failed add Token %w", err)
@ -404,16 +402,12 @@ func (c *RollupClient) RollupWithdrawMerkleProof(fromBJJ *babyjub.PublicKey, tok
if tx, err = c.client.CallAuth(
0,
func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
hermez, err := Hermez.NewHermez(c.address, ec)
if err != nil {
return nil, err
}
pkCompL := fromBJJ.Compress()
pkCompB := common.SwapEndianness(pkCompL[:])
babyPubKey := new(big.Int).SetBytes(pkCompB)
numExitRootB := big.NewInt(numExitRoot)
idxBig := big.NewInt(idx)
return hermez.WithdrawMerkleProof(auth, tokenID, amount, babyPubKey, numExitRootB, siblings, idxBig, instantWithdraw)
return c.hermez.WithdrawMerkleProof(auth, tokenID, amount, babyPubKey, numExitRootB, siblings, idxBig, instantWithdraw)
},
); err != nil {
return nil, fmt.Errorf("Failed update WithdrawMerkleProof: %w", err)
@ -432,10 +426,6 @@ func (c *RollupClient) RollupL1UserTxERC20ETH(fromBJJ *babyjub.PublicKey, fromId
if tx, err = c.client.CallAuth(
0,
func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
hermez, err := Hermez.NewHermez(c.address, ec)
if err != nil {
return nil, err
}
pkCompL := fromBJJ.Compress()
pkCompB := common.SwapEndianness(pkCompL[:])
babyPubKey := new(big.Int).SetBytes(pkCompB)
@ -453,7 +443,8 @@ func (c *RollupClient) RollupL1UserTxERC20ETH(fromBJJ *babyjub.PublicKey, fromId
if tokenID == 0 {
auth.Value = loadAmount
}
return hermez.AddL1Transaction(auth, babyPubKey, fromIdxBig, uint16(loadAmountF), uint16(amountF), tokenIDBig, toIdxBig)
var permit []byte
return c.hermez.AddL1Transaction(auth, babyPubKey, fromIdxBig, uint16(loadAmountF), uint16(amountF), tokenIDBig, toIdxBig, permit)
},
); err != nil {
return nil, fmt.Errorf("Failed add L1 Tx ERC20/ETH: %w", err)
@ -461,59 +452,45 @@ func (c *RollupClient) RollupL1UserTxERC20ETH(fromBJJ *babyjub.PublicKey, fromId
return tx, nil
}
// RollupL1UserTxERC777 is the interface to call the smart contract function
func (c *RollupClient) RollupL1UserTxERC777(fromBJJ *babyjub.PublicKey, fromIdx int64, loadAmount *big.Int, amount *big.Int, tokenID uint32, toIdx int64) (tx *types.Transaction, err error) {
// RollupL1UserTxERC20Permit is the interface to call the smart contract function
func (c *RollupClient) RollupL1UserTxERC20Permit(fromBJJ *babyjub.PublicKey, fromIdx int64, loadAmount *big.Int, amount *big.Int, tokenID uint32, toIdx int64, deadline *big.Int) (tx *types.Transaction, err error) {
if tx, err = c.client.CallAuth(
0,
func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
tokens, err := ERC777.NewERC777(c.tokenHEZAddress, ec)
if err != nil {
return nil, err
}
addL1TxFnSignature := []byte("addL1Transaction(uint256,uint48,uint16,uint16,uint32,uint48)")
hash := sha3.NewLegacyKeccak256()
_, err = hash.Write(addL1TxFnSignature)
if err != nil {
return nil, err
}
methodID := hash.Sum(nil)[:4]
pkCompL := fromBJJ.Compress()
pkCompB := common.SwapEndianness(pkCompL[:])
paddedAddress := ethCommon.LeftPadBytes(pkCompB, 32)
fromIdxB, err := common.Idx(fromIdx).Bytes()
if err != nil {
return nil, err
}
paddedFromIdx := ethCommon.LeftPadBytes(fromIdxB[:], 32)
babyPubKey := new(big.Int).SetBytes(pkCompB)
fromIdxBig := big.NewInt(fromIdx)
toIdxBig := big.NewInt(toIdx)
tokenIDBig := uint32(tokenID)
loadAmountF, err := common.NewFloat16(loadAmount)
if err != nil {
return nil, err
}
paddedLoadAmount := ethCommon.LeftPadBytes(loadAmountF.Bytes(), 32)
amountF, err := common.NewFloat16(amount)
if err != nil {
return nil, err
}
paddedAmount := ethCommon.LeftPadBytes(amountF.Bytes(), 32)
tokenIDB := common.TokenID(tokenID).Bytes()
paddedTokenID := ethCommon.LeftPadBytes(tokenIDB, 32)
toIdxB, err := common.Idx(toIdx).Bytes()
if tokenID == 0 {
auth.Value = loadAmount
}
tokenHEZcontract, err := HEZ.NewHEZ(c.tokenHEZ.Address, ec)
if err != nil {
return nil, err
}
paddedToIdx := ethCommon.LeftPadBytes(toIdxB[:], 32)
var data []byte
data = append(data, methodID...)
data = append(data, paddedAddress[:]...)
data = append(data, paddedFromIdx[:]...)
data = append(data, paddedLoadAmount[:]...)
data = append(data, paddedAmount[:]...)
data = append(data, paddedTokenID[:]...)
data = append(data, paddedToIdx[:]...)
return tokens.Send(auth, c.address, loadAmount, data)
owner := c.client.account.Address
spender := c.address
nonce, err := tokenHEZcontract.Nonces(nil, owner)
tokenname := c.tokenHEZ.Name
tokenAddr := c.tokenHEZ.Address
chainid, _ := c.client.client.ChainID(context.Background())
digest, _ := createPermitDigest(tokenAddr, owner, spender, chainid, amount, nonce, deadline, tokenname)
signature, _ := c.client.ks.SignHash(*c.client.account, digest)
permit := createPermit(owner, spender, amount, deadline, digest, signature)
return c.hermez.AddL1Transaction(auth, babyPubKey, fromIdxBig, uint16(loadAmountF), uint16(amountF), tokenIDBig, toIdxBig, permit)
},
); err != nil {
return nil, fmt.Errorf("Failed add L1 Tx ERC777: %w", err)
return nil, fmt.Errorf("Failed add L1 Tx ERC20Permit: %w", err)
}
return tx, nil
}
@ -539,11 +516,7 @@ func (c *RollupClient) RollupUpdateForgeL1L2BatchTimeout(newForgeL1L2BatchTimeou
if tx, err = c.client.CallAuth(
0,
func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
hermez, err := Hermez.NewHermez(c.address, ec)
if err != nil {
return nil, err
}
return hermez.UpdateForgeL1L2BatchTimeout(auth, uint8(newForgeL1L2BatchTimeout))
return c.hermez.UpdateForgeL1L2BatchTimeout(auth, uint8(newForgeL1L2BatchTimeout))
},
); err != nil {
return nil, fmt.Errorf("Failed update ForgeL1L2BatchTimeout: %w", err)
@ -556,11 +529,7 @@ func (c *RollupClient) RollupUpdateFeeAddToken(newFeeAddToken *big.Int) (tx *typ
if tx, err = c.client.CallAuth(
0,
func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
hermez, err := Hermez.NewHermez(c.address, ec)
if err != nil {
return nil, err
}
return hermez.UpdateFeeAddToken(auth, newFeeAddToken)
return c.hermez.UpdateFeeAddToken(auth, newFeeAddToken)
},
); err != nil {
return nil, fmt.Errorf("Failed update FeeAddToken: %w", err)

+ 56
- 83
eth/rollup_test.go

@ -26,7 +26,7 @@ var maxTx = int64(512)
var nLevels = int64(32)
var tokenIDERC777 uint32
var tokenIDERC20 uint32
var tokenHEZID uint32
type keys struct {
BJJSecretKey *babyjub.PrivateKey
@ -56,7 +56,7 @@ func TestRollupConstants(t *testing.T) {
require.Nil(t, err)
assert.Equal(t, absoluteMaxL1L2BatchTimeout, rollupConstants.AbsoluteMaxL1L2BatchTimeout)
assert.Equal(t, auctionAddressConst, rollupConstants.HermezAuctionContract)
assert.Equal(t, tokenERC777AddressConst, rollupConstants.TokenHEZ)
assert.Equal(t, tokenHEZAddressConst, rollupConstants.TokenHEZ)
assert.Equal(t, maxTx, rollupConstants.Verifiers[0].MaxTx)
assert.Equal(t, nLevels, rollupConstants.Verifiers[0].NLevels)
assert.Equal(t, governanceAddressConst, rollupConstants.HermezGovernanceDAOAddress)
@ -72,29 +72,17 @@ func TestRollupRegisterTokensCount(t *testing.T) {
func TestAddToken(t *testing.T) {
feeAddToken := big.NewInt(10)
// Addtoken ERC20
// Addtoken ERC20Permit
registerTokensCount, err := rollupClient.RollupRegisterTokensCount()
require.Nil(t, err)
_, err = rollupClient.RollupAddToken(tokenERC20AddressConst, feeAddToken)
_, err = rollupClient.RollupAddToken(tokenHEZAddressConst, feeAddToken, deadline)
require.Nil(t, err)
currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
assert.Equal(t, tokenERC20AddressConst, rollupEvents.AddToken[0].TokenAddress)
assert.Equal(t, tokenHEZAddressConst, rollupEvents.AddToken[0].TokenAddress)
assert.Equal(t, registerTokensCount, common.TokenID(rollupEvents.AddToken[0].TokenID).BigInt())
tokenIDERC20 = rollupEvents.AddToken[0].TokenID
// Addtoken ERC777
registerTokensCount, err = rollupClient.RollupRegisterTokensCount()
require.Nil(t, err)
_, err = rollupClient.RollupAddToken(tokenERC777AddressConst, feeAddToken)
require.Nil(t, err)
currentBlockNum, _ = rollupClient.client.EthCurrentBlock()
rollupEvents, _, _ = rollupClient.RollupEventsByBlock(currentBlockNum)
assert.Equal(t, tokenERC777AddressConst, rollupEvents.AddToken[0].TokenAddress)
assert.Equal(t, registerTokensCount, common.TokenID(rollupEvents.AddToken[0].TokenID).BigInt())
tokenIDERC777 = rollupEvents.AddToken[0].TokenID
tokenHEZID = rollupEvents.AddToken[0].TokenID
}
func TestRollupForgeBatch(t *testing.T) {
@ -113,7 +101,7 @@ func TestRollupForgeBatch(t *testing.T) {
minBid.SetString("11000000000000000000", 10)
budget := new(big.Int)
budget.SetString("45200000000000000000", 10)
_, err = auctionClient.AuctionMultiBid(currentSlot+4, currentSlot+10, slotSet, maxBid, minBid, budget)
_, err = auctionClient.AuctionMultiBid(budget, currentSlot+4, currentSlot+10, slotSet, maxBid, minBid, deadline)
require.Nil(t, err)
// Add Blocks
@ -124,21 +112,6 @@ func TestRollupForgeBatch(t *testing.T) {
// Forge
args := new(RollupForgeBatchArgs)
// feeIdxCoordinatorBytes, err := hex.DecodeString("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
// require.Nil(t, err)
// lenFeeIdxCoordinatorBytes := int(4)
// numFeeIdxCoordinator := len(feeIdxCoordinatorBytes) / lenFeeIdxCoordinatorBytes
// for i := 0; i < numFeeIdxCoordinator; i++ {
// var paddedFeeIdx [6]byte
// if lenFeeIdxCoordinatorBytes < common.IdxBytesLen {
// copy(paddedFeeIdx[6-lenFeeIdxCoordinatorBytes:], feeIdxCoordinatorBytes[i*lenFeeIdxCoordinatorBytes:(i+1)*lenFeeIdxCoordinatorBytes])
// } else {
// copy(paddedFeeIdx[:], feeIdxCoordinatorBytes[i*lenFeeIdxCoordinatorBytes:(i+1)*lenFeeIdxCoordinatorBytes])
// }
// FeeIdxCoordinator, err := common.IdxFromBytes(paddedFeeIdx[:])
// require.Nil(t, err)
// args.FeeIdxCoordinator = append(args.FeeIdxCoordinator, FeeIdxCoordinator)
// }
args.FeeIdxCoordinator = []common.Idx{} // When encoded, 64 times the 0 idx means that no idx to collect fees is specified.
l1CoordinatorBytes, err := hex.DecodeString("1c660323607bb113e586183609964a333d07ebe4bef3be82ec13af453bae9590bd7711cdb6abf42f176eadfbe5506fbef5e092e5543733f91b0061d9a7747fa10694a915a6470fa230de387b51e6f4db0b09787867778687b55197ad6d6a86eac000000001")
require.Nil(t, err)
@ -226,7 +199,7 @@ func TestRollupUpdateFeeAddToken(t *testing.T) {
}
func TestRollupL1UserTxETHCreateAccountDeposit(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
require.Nil(t, err)
key := genKeysBjj(2)
fromIdxInt64 := int64(0)
@ -255,7 +228,7 @@ func TestRollupL1UserTxETHCreateAccountDeposit(t *testing.T) {
}
func TestRollupL1UserTxERC20CreateAccountDeposit(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
rollupClientAux2, err := NewRollupClient(ethereumClientAux2, hermezRollupAddressConst, tokenHEZ)
require.Nil(t, err)
key := genKeysBjj(1)
fromIdxInt64 := int64(0)
@ -265,11 +238,11 @@ func TestRollupL1UserTxERC20CreateAccountDeposit(t *testing.T) {
FromIdx: common.Idx(fromIdxInt64),
ToIdx: common.Idx(toIdxInt64),
LoadAmount: big.NewInt(10),
TokenID: common.TokenID(tokenIDERC20),
TokenID: common.TokenID(tokenHEZID),
Amount: big.NewInt(0),
}
_, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC20, toIdxInt64)
_, err = rollupClientAux2.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenHEZID, toIdxInt64)
require.Nil(t, err)
currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
@ -279,11 +252,11 @@ func TestRollupL1UserTxERC20CreateAccountDeposit(t *testing.T) {
assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
assert.Equal(t, rollupClientAux2.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
}
func TestRollupL1UserTxERC777CreateAccountDeposit(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
func TestRollupL1UserTxERC20PermitCreateAccountDeposit(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
require.Nil(t, err)
key := genKeysBjj(3)
fromIdxInt64 := int64(0)
@ -297,7 +270,7 @@ func TestRollupL1UserTxERC777CreateAccountDeposit(t *testing.T) {
Amount: big.NewInt(0),
}
_, err = rollupClientAux.RollupL1UserTxERC777(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64)
_, err = rollupClientAux.RollupL1UserTxERC20Permit(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64, deadline)
require.Nil(t, err)
currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
@ -311,7 +284,7 @@ func TestRollupL1UserTxERC777CreateAccountDeposit(t *testing.T) {
}
func TestRollupL1UserTxETHDeposit(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
require.Nil(t, err)
key := genKeysBjj(2)
fromIdxInt64 := int64(0)
@ -340,7 +313,7 @@ func TestRollupL1UserTxETHDeposit(t *testing.T) {
}
func TestRollupL1UserTxERC20Deposit(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
rollupClientAux2, err := NewRollupClient(ethereumClientAux2, hermezRollupAddressConst, tokenHEZ)
require.Nil(t, err)
key := genKeysBjj(1)
fromIdxInt64 := int64(0)
@ -350,11 +323,11 @@ func TestRollupL1UserTxERC20Deposit(t *testing.T) {
FromIdx: common.Idx(fromIdxInt64),
ToIdx: common.Idx(toIdxInt64),
LoadAmount: big.NewInt(10),
TokenID: common.TokenID(tokenIDERC20),
TokenID: common.TokenID(tokenHEZID),
Amount: big.NewInt(0),
}
_, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC20, toIdxInt64)
_, err = rollupClientAux2.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenHEZID, toIdxInt64)
require.Nil(t, err)
currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
@ -364,11 +337,11 @@ func TestRollupL1UserTxERC20Deposit(t *testing.T) {
assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
assert.Equal(t, rollupClientAux2.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
}
func TestRollupL1UserTxERC777Deposit(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
func TestRollupL1UserTxERC20PermitDeposit(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
require.Nil(t, err)
key := genKeysBjj(3)
fromIdxInt64 := int64(0)
@ -382,7 +355,7 @@ func TestRollupL1UserTxERC777Deposit(t *testing.T) {
Amount: big.NewInt(0),
}
_, err = rollupClientAux.RollupL1UserTxERC777(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64)
_, err = rollupClientAux.RollupL1UserTxERC20Permit(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64, deadline)
require.Nil(t, err)
currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
@ -396,7 +369,7 @@ func TestRollupL1UserTxERC777Deposit(t *testing.T) {
}
func TestRollupL1UserTxETHDepositTransfer(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
require.Nil(t, err)
key := genKeysBjj(2)
fromIdxInt64 := int64(0)
@ -425,7 +398,7 @@ func TestRollupL1UserTxETHDepositTransfer(t *testing.T) {
}
func TestRollupL1UserTxERC20DepositTransfer(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
rollupClientAux2, err := NewRollupClient(ethereumClientAux2, hermezRollupAddressConst, tokenHEZ)
require.Nil(t, err)
key := genKeysBjj(1)
fromIdxInt64 := int64(0)
@ -435,11 +408,11 @@ func TestRollupL1UserTxERC20DepositTransfer(t *testing.T) {
FromIdx: common.Idx(fromIdxInt64),
ToIdx: common.Idx(toIdxInt64),
LoadAmount: big.NewInt(10),
TokenID: common.TokenID(tokenIDERC20),
TokenID: common.TokenID(tokenHEZID),
Amount: big.NewInt(0),
}
_, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC20, toIdxInt64)
_, err = rollupClientAux2.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenHEZID, toIdxInt64)
require.Nil(t, err)
currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
@ -449,11 +422,11 @@ func TestRollupL1UserTxERC20DepositTransfer(t *testing.T) {
assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
assert.Equal(t, rollupClientAux2.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
}
func TestRollupL1UserTxERC777DepositTransfer(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
func TestRollupL1UserTxERC20PermitDepositTransfer(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
require.Nil(t, err)
key := genKeysBjj(3)
fromIdxInt64 := int64(0)
@ -467,7 +440,7 @@ func TestRollupL1UserTxERC777DepositTransfer(t *testing.T) {
Amount: big.NewInt(0),
}
_, err = rollupClientAux.RollupL1UserTxERC777(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64)
_, err = rollupClientAux.RollupL1UserTxERC20Permit(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64, deadline)
require.Nil(t, err)
currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
@ -481,7 +454,7 @@ func TestRollupL1UserTxERC777DepositTransfer(t *testing.T) {
}
func TestRollupL1UserTxETHCreateAccountDepositTransfer(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
require.Nil(t, err)
key := genKeysBjj(2)
fromIdxInt64 := int64(0)
@ -510,7 +483,7 @@ func TestRollupL1UserTxETHCreateAccountDepositTransfer(t *testing.T) {
}
func TestRollupL1UserTxERC20CreateAccountDepositTransfer(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
rollupClientAux2, err := NewRollupClient(ethereumClientAux2, hermezRollupAddressConst, tokenHEZ)
require.Nil(t, err)
key := genKeysBjj(1)
fromIdxInt64 := int64(0)
@ -520,11 +493,11 @@ func TestRollupL1UserTxERC20CreateAccountDepositTransfer(t *testing.T) {
FromIdx: common.Idx(fromIdxInt64),
ToIdx: common.Idx(toIdxInt64),
LoadAmount: big.NewInt(10),
TokenID: common.TokenID(tokenIDERC20),
TokenID: common.TokenID(tokenHEZID),
Amount: big.NewInt(0),
}
_, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC20, toIdxInt64)
_, err = rollupClientAux2.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenHEZID, toIdxInt64)
require.Nil(t, err)
currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
@ -534,11 +507,11 @@ func TestRollupL1UserTxERC20CreateAccountDepositTransfer(t *testing.T) {
assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
assert.Equal(t, rollupClientAux2.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
}
func TestRollupL1UserTxERC777CreateAccountDepositTransfer(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
func TestRollupL1UserTxERC20PermitCreateAccountDepositTransfer(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
require.Nil(t, err)
key := genKeysBjj(3)
fromIdxInt64 := int64(0)
@ -552,7 +525,7 @@ func TestRollupL1UserTxERC777CreateAccountDepositTransfer(t *testing.T) {
Amount: big.NewInt(0),
}
_, err = rollupClientAux.RollupL1UserTxERC777(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64)
_, err = rollupClientAux.RollupL1UserTxERC20Permit(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64, deadline)
require.Nil(t, err)
currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
@ -566,7 +539,7 @@ func TestRollupL1UserTxERC777CreateAccountDepositTransfer(t *testing.T) {
}
func TestRollupL1UserTxETHForceTransfer(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
require.Nil(t, err)
key := genKeysBjj(2)
fromIdxInt64 := int64(0)
@ -595,7 +568,7 @@ func TestRollupL1UserTxETHForceTransfer(t *testing.T) {
}
func TestRollupL1UserTxERC20ForceTransfer(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
rollupClientAux2, err := NewRollupClient(ethereumClientAux2, hermezRollupAddressConst, tokenHEZ)
require.Nil(t, err)
key := genKeysBjj(1)
fromIdxInt64 := int64(0)
@ -605,11 +578,11 @@ func TestRollupL1UserTxERC20ForceTransfer(t *testing.T) {
FromIdx: common.Idx(fromIdxInt64),
ToIdx: common.Idx(toIdxInt64),
LoadAmount: big.NewInt(10),
TokenID: common.TokenID(tokenIDERC20),
TokenID: common.TokenID(tokenHEZID),
Amount: big.NewInt(0),
}
_, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC20, toIdxInt64)
_, err = rollupClientAux2.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenHEZID, toIdxInt64)
require.Nil(t, err)
currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
@ -619,11 +592,11 @@ func TestRollupL1UserTxERC20ForceTransfer(t *testing.T) {
assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
assert.Equal(t, rollupClientAux2.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
}
func TestRollupL1UserTxERC777ForceTransfer(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
func TestRollupL1UserTxERC20PermitForceTransfer(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
require.Nil(t, err)
key := genKeysBjj(3)
fromIdxInt64 := int64(0)
@ -637,7 +610,7 @@ func TestRollupL1UserTxERC777ForceTransfer(t *testing.T) {
Amount: big.NewInt(0),
}
_, err = rollupClientAux.RollupL1UserTxERC777(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64)
_, err = rollupClientAux.RollupL1UserTxERC20Permit(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64, deadline)
require.Nil(t, err)
currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
@ -651,7 +624,7 @@ func TestRollupL1UserTxERC777ForceTransfer(t *testing.T) {
}
func TestRollupL1UserTxETHForceExit(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
require.Nil(t, err)
key := genKeysBjj(2)
fromIdxInt64 := int64(0)
@ -680,7 +653,7 @@ func TestRollupL1UserTxETHForceExit(t *testing.T) {
}
func TestRollupL1UserTxERC20ForceExit(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
rollupClientAux2, err := NewRollupClient(ethereumClientAux2, hermezRollupAddressConst, tokenHEZ)
require.Nil(t, err)
key := genKeysBjj(1)
fromIdxInt64 := int64(0)
@ -690,11 +663,11 @@ func TestRollupL1UserTxERC20ForceExit(t *testing.T) {
FromIdx: common.Idx(fromIdxInt64),
ToIdx: common.Idx(toIdxInt64),
LoadAmount: big.NewInt(10),
TokenID: common.TokenID(tokenIDERC20),
TokenID: common.TokenID(tokenHEZID),
Amount: big.NewInt(0),
}
_, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC20, toIdxInt64)
_, err = rollupClientAux2.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenHEZID, toIdxInt64)
require.Nil(t, err)
currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
@ -704,11 +677,11 @@ func TestRollupL1UserTxERC20ForceExit(t *testing.T) {
assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
assert.Equal(t, rollupClientAux2.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
}
func TestRollupL1UserTxERC777ForceExit(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
func TestRollupL1UserTxERC20PermitForceExit(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
require.Nil(t, err)
key := genKeysBjj(3)
fromIdxInt64 := int64(0)
@ -724,7 +697,7 @@ func TestRollupL1UserTxERC777ForceExit(t *testing.T) {
Amount: big.NewInt(0),
}
_, err = rollupClientAux.RollupL1UserTxERC777(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64)
_, err = rollupClientAux.RollupL1UserTxERC20Permit(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64, deadline)
require.Nil(t, err)
currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
@ -783,7 +756,7 @@ func TestRollupForgeBatch2(t *testing.T) {
}
func TestRollupWithdrawMerkleProof(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
require.Nil(t, err)
var pkComp babyjub.PublicKeyComp

+ 11
- 11
eth/wdelayer_test.go

@ -117,19 +117,19 @@ func TestWDelayerDeposit(t *testing.T) {
amount.SetString("1100000000000000000", 10)
wdelayerClientHermez, err := NewWDelayerClient(ethereumClientHermez, wdelayerTestAddressConst)
require.Nil(t, err)
_, err = wdelayerClientHermez.WDelayerDeposit(auxAddressConst, tokenERC20AddressConst, amount)
_, err = wdelayerClientHermez.WDelayerDeposit(auxAddressConst, tokenHEZAddressConst, amount)
require.Nil(t, err)
currentBlockNum, _ := wdelayerClientTest.client.EthCurrentBlock()
wdelayerEvents, _, _ := wdelayerClientTest.WDelayerEventsByBlock(currentBlockNum)
assert.Equal(t, amount, wdelayerEvents.Deposit[0].Amount)
assert.Equal(t, auxAddressConst, wdelayerEvents.Deposit[0].Owner)
assert.Equal(t, tokenERC20AddressConst, wdelayerEvents.Deposit[0].Token)
assert.Equal(t, tokenHEZAddressConst, wdelayerEvents.Deposit[0].Token)
}
func TestWDelayerDepositInfo(t *testing.T) {
amount := new(big.Int)
amount.SetString("1100000000000000000", 10)
state, err := wdelayerClientTest.WDelayerDepositInfo(auxAddressConst, tokenERC20AddressConst)
state, err := wdelayerClientTest.WDelayerDepositInfo(auxAddressConst, tokenHEZAddressConst)
require.Nil(t, err)
assert.Equal(t, state.Amount, amount)
}
@ -137,16 +137,16 @@ func TestWDelayerDepositInfo(t *testing.T) {
func TestWDelayerWithdrawal(t *testing.T) {
amount := new(big.Int)
amount.SetString("1100000000000000000", 10)
_, err := wdelayerClientTest.WDelayerWithdrawal(auxAddressConst, tokenERC20AddressConst)
_, err := wdelayerClientTest.WDelayerWithdrawal(auxAddressConst, tokenHEZAddressConst)
require.Contains(t, err.Error(), "Withdrawal not allowed yet")
addBlocks(newWithdrawalDelay.Int64(), ethClientDialURL)
_, err = wdelayerClientTest.WDelayerWithdrawal(auxAddressConst, tokenERC20AddressConst)
_, err = wdelayerClientTest.WDelayerWithdrawal(auxAddressConst, tokenHEZAddressConst)
require.Nil(t, err)
currentBlockNum, _ := wdelayerClientTest.client.EthCurrentBlock()
wdelayerEvents, _, _ := wdelayerClientTest.WDelayerEventsByBlock(currentBlockNum)
assert.Equal(t, amount, wdelayerEvents.Withdraw[0].Amount)
assert.Equal(t, auxAddressConst, wdelayerEvents.Withdraw[0].Owner)
assert.Equal(t, tokenERC20AddressConst, wdelayerEvents.Withdraw[0].Token)
assert.Equal(t, tokenHEZAddressConst, wdelayerEvents.Withdraw[0].Token)
}
func TestWDelayerSecondDeposit(t *testing.T) {
@ -154,13 +154,13 @@ func TestWDelayerSecondDeposit(t *testing.T) {
amount.SetString("1100000000000000000", 10)
wdelayerClientHermez, err := NewWDelayerClient(ethereumClientHermez, wdelayerTestAddressConst)
require.Nil(t, err)
_, err = wdelayerClientHermez.WDelayerDeposit(auxAddressConst, tokenERC20AddressConst, amount)
_, err = wdelayerClientHermez.WDelayerDeposit(auxAddressConst, tokenHEZAddressConst, amount)
require.Nil(t, err)
currentBlockNum, _ := wdelayerClientTest.client.EthCurrentBlock()
wdelayerEvents, _, _ := wdelayerClientTest.WDelayerEventsByBlock(currentBlockNum)
assert.Equal(t, amount, wdelayerEvents.Deposit[0].Amount)
assert.Equal(t, auxAddressConst, wdelayerEvents.Deposit[0].Owner)
assert.Equal(t, tokenERC20AddressConst, wdelayerEvents.Deposit[0].Token)
assert.Equal(t, tokenHEZAddressConst, wdelayerEvents.Deposit[0].Token)
}
func TestWDelayerEnableEmergencyMode(t *testing.T) {
@ -192,15 +192,15 @@ func TestWDelayerEscapeHatchWithdrawal(t *testing.T) {
amount.SetString("10000000000000000", 10)
wdelayerClientWhite, err := NewWDelayerClient(ethereumClientWhite, wdelayerTestAddressConst)
require.Nil(t, err)
_, err = wdelayerClientWhite.WDelayerEscapeHatchWithdrawal(governanceAddressConst, tokenERC20AddressConst, amount)
_, err = wdelayerClientWhite.WDelayerEscapeHatchWithdrawal(governanceAddressConst, tokenHEZAddressConst, amount)
require.Contains(t, err.Error(), "NO MAX_EMERGENCY_MODE_TIME")
seconds := maxEmergencyModeTime.Seconds()
addTime(seconds, ethClientDialURL)
_, err = wdelayerClientWhite.WDelayerEscapeHatchWithdrawal(governanceAddressConst, tokenERC20AddressConst, amount)
_, err = wdelayerClientWhite.WDelayerEscapeHatchWithdrawal(governanceAddressConst, tokenHEZAddressConst, amount)
require.Nil(t, err)
currentBlockNum, _ := wdelayerClientTest.client.EthCurrentBlock()
wdelayerEvents, _, _ := wdelayerClientTest.WDelayerEventsByBlock(currentBlockNum)
assert.Equal(t, tokenERC20AddressConst, wdelayerEvents.EscapeHatchWithdrawal[0].Token)
assert.Equal(t, tokenHEZAddressConst, wdelayerEvents.EscapeHatchWithdrawal[0].Token)
assert.Equal(t, governanceAddressConst, wdelayerEvents.EscapeHatchWithdrawal[0].To)
assert.Equal(t, whiteHackGroupAddressConst, wdelayerEvents.EscapeHatchWithdrawal[0].Who)
assert.Equal(t, amount, wdelayerEvents.EscapeHatchWithdrawal[0].Amount)

Loading…
Cancel
Save