diff --git a/cli/node/cfg.example.toml b/cli/node/cfg.example.toml index 591f346..b99d97a 100644 --- a/cli/node/cfg.example.toml +++ b/cli/node/cfg.example.toml @@ -23,6 +23,7 @@ SyncLoopInterval = "1s" Rollup = "0xEcc0a6dbC0bb4D51E4F84A315a9e5B0438cAD4f0" Auction = "0x500D1d6A4c7D8Ae28240b47c8FCde034D827fD5e" TokenHEZ = "0xf784709d2317D872237C4bC22f867d1BAe2913AB" +TokenHEZName = "Hermez Network Token" [EthClient] CallGasLimit = 300000 diff --git a/config/config.go b/config/config.go index f9846fa..37aa927 100644 --- a/config/config.go +++ b/config/config.go @@ -68,9 +68,10 @@ type Node struct { SyncLoopInterval Duration `validate:"required"` } `validate:"required"` SmartContracts struct { - Rollup ethCommon.Address `validate:"required"` - Auction ethCommon.Address `validate:"required"` - TokenHEZ ethCommon.Address `validate:"required"` + Rollup ethCommon.Address `validate:"required"` + Auction ethCommon.Address `validate:"required"` + TokenHEZ ethCommon.Address `validate:"required"` + TokenHEZName string `validate:"required"` } `validate:"required"` EthClient struct { CallGasLimit uint64 `validate:"required"` diff --git a/coordinator/coordinator_test.go b/coordinator/coordinator_test.go index cdd7980..fd4dbb8 100644 --- a/coordinator/coordinator_test.go +++ b/coordinator/coordinator_test.go @@ -178,9 +178,9 @@ func TestCoordinator(t *testing.T) { // Bid for slot 2 and 4 _, err := ethClient.AuctionSetCoordinator(forger, "https://foo.bar") require.Nil(t, err) - _, err = ethClient.AuctionBid(2, big.NewInt(9999)) + _, err = ethClient.AuctionBidSimple(2, big.NewInt(9999)) require.Nil(t, err) - _, err = ethClient.AuctionBid(4, big.NewInt(9999)) + _, err = ethClient.AuctionBidSimple(4, big.NewInt(9999)) require.Nil(t, err) c := NewCoordinator(conf, hdb, txsel, bb, serverProofs, ethClient) diff --git a/eth/README.md b/eth/README.md index 0e18b86..f9b071a 100644 --- a/eth/README.md +++ b/eth/README.md @@ -12,23 +12,28 @@ While the prepared deployment is not found to master, branch in repository must Now, install the dependencies: -`npm i` +``` +cd contracts/ +yarn install +``` Go to where the deployment scripts for the test are found: -`cd scripts/ethclient-test-deployment` +`cd scripts/ethclient-deployment/` -Now, a bash script has to be run to do the deployment: -`./test-deployment` +Now, a bash script (which uses gnome-terminal) has to be run to do the deployment: +`./test-deploy.sh` This bash file follows these steps: - `npx builder node`: a local blockchain to do our tests - `npx buidler run --network localhost test-deployment.js`: run the deployment on the local blockchain +Alternatively you can run the two previous commands manually in different terminals. An output file necessary for the next step is obtained: `deploy-output`. -> The files that find in `/eth/contracts` must be obtained from the same contract that we deploy in this step +> The files that appear in `hermez-node/eth/contracts` must be generated from the same contract that we deploy in this step + ## Ethclient Test Different environment variables are necessary to run this test. @@ -46,9 +51,9 @@ WDELAYER="0x500D1d6A4c7D8Ae28240b47c8FCde034D827fD5e" WDELAYER_TEST="0x1d80315fac6aBd3EfeEbE97dEc44461ba7556160" ``` -> An example is found in `/etc/.env.example` +> An example is found in `hermez-node/eth/.env.example` -And then run test: +And then run test from `hermez-node/eth/`: `INTEGRATION=1 go test` diff --git a/eth/auction.go b/eth/auction.go index b8f2428..732b91e 100644 --- a/eth/auction.go +++ b/eth/auction.go @@ -263,13 +263,14 @@ type AuctionInterface interface { type AuctionClient struct { client *EthereumClient address ethCommon.Address - tokenHEZ TokenConfig + tokenHEZCfg TokenConfig auction *HermezAuctionProtocol.HermezAuctionProtocol + tokenHEZ *HEZ.HEZ contractAbi abi.ABI } // NewAuctionClient creates a new AuctionClient. `tokenAddress` is the address of the HEZ tokens. -func NewAuctionClient(client *EthereumClient, address ethCommon.Address, tokenHEZ TokenConfig) (*AuctionClient, error) { +func NewAuctionClient(client *EthereumClient, address ethCommon.Address, tokenHEZCfg TokenConfig) (*AuctionClient, error) { contractAbi, err := abi.JSON(strings.NewReader(string(HermezAuctionProtocol.HermezAuctionProtocolABI))) if err != nil { return nil, err @@ -278,11 +279,16 @@ func NewAuctionClient(client *EthereumClient, address ethCommon.Address, tokenHE if err != nil { return nil, err } + tokenHEZ, err := HEZ.NewHEZ(tokenHEZCfg.Address, client.Client()) + if err != nil { + return nil, err + } return &AuctionClient{ client: client, address: address, - tokenHEZ: tokenHEZ, + tokenHEZCfg: tokenHEZCfg, auction: auction, + tokenHEZ: tokenHEZ, contractAbi: contractAbi, }, nil } @@ -561,17 +567,16 @@ func (c *AuctionClient) AuctionBid(amount *big.Int, slot int64, bidAmount *big.I if tx, err = c.client.CallAuth( 0, func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) { - tokenHEZcontract, err := HEZ.NewHEZ(c.tokenHEZ.Address, ec) + owner := c.client.account.Address + spender := c.address + nonce, err := c.tokenHEZ.Nonces(nil, owner) if err != nil { return nil, err } - 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) + tokenName := c.tokenHEZCfg.Name + tokenAddr := c.tokenHEZCfg.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) @@ -581,7 +586,6 @@ func (c *AuctionClient) AuctionBid(amount *big.Int, slot int64, bidAmount *big.I return nil, fmt.Errorf("Failed bid: %w", err) } return tx, nil - } // AuctionMultiBid is the interface to call the smart contract function @@ -590,18 +594,17 @@ func (c *AuctionClient) AuctionMultiBid(amount *big.Int, startingSlot, endingSlo if tx, err = c.client.CallAuth( 1000000, //nolint:gomnd func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) { - tokenHEZcontract, err := HEZ.NewHEZ(c.tokenHEZ.Address, ec) + owner := c.client.account.Address + spender := c.address + nonce, err := c.tokenHEZ.Nonces(nil, owner) if err != nil { return nil, err } - 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()) + tokenName := c.tokenHEZCfg.Name + tokenAddr := c.tokenHEZCfg.Address + chainid, _ := c.client.Client().ChainID(context.Background()) - digest, _ := createPermitDigest(tokenAddr, owner, spender, chainid, amount, nonce, deadline, tokenname) + 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) diff --git a/eth/auction_test.go b/eth/auction_test.go index 4fe21da..a3efb0f 100644 --- a/eth/auction_test.go +++ b/eth/auction_test.go @@ -13,7 +13,7 @@ const openAuctionSlotsConst = uint16(4320) const closedAuctionSlotsConst = uint16(2) const outbiddingConst = uint16(1000) const currentSlotConst = 0 -const BLOCKSPERSLOT = uint8(40) +const blocksPerSlot = uint8(40) const minBidStr = "10000000000000000000" const URL = "http://localhost:3000" @@ -33,7 +33,7 @@ func TestAuctionConstants(t *testing.T) { auctionConstants, err := auctionClientTest.AuctionConstants() require.Nil(t, err) - assert.Equal(t, auctionConstants.BlocksPerSlot, BLOCKSPERSLOT) + assert.Equal(t, auctionConstants.BlocksPerSlot, blocksPerSlot) assert.Equal(t, auctionConstants.GenesisBlockNum, genesisBlock) assert.Equal(t, auctionConstants.HermezRollup, hermezRollupAddressTestConst) assert.Equal(t, auctionConstants.InitialMinimalBidding, INITMINBID) @@ -277,7 +277,7 @@ func TestAuctionBid(t *testing.T) { func TestAuctionGetSlotNumber(t *testing.T) { slotConst := 4 - blockNum := int(BLOCKSPERSLOT)*slotConst + int(genesisBlock) + blockNum := int(blocksPerSlot)*slotConst + int(genesisBlock) slot, err := auctionClientTest.AuctionGetSlotNumber(int64(blockNum)) require.Nil(t, err) @@ -286,7 +286,7 @@ func TestAuctionGetSlotNumber(t *testing.T) { func TestAuctionCanForge(t *testing.T) { slotConst := 4 - blockNum := int(BLOCKSPERSLOT)*slotConst + int(genesisBlock) + blockNum := int(blocksPerSlot)*slotConst + int(genesisBlock) canForge, err := auctionClientTest.AuctionCanForge(governanceAddressConst, int64(blockNum)) require.Nil(t, err) @@ -344,7 +344,7 @@ func TestAuctionForge(t *testing.T) { auctionClientTestHermez, err := NewAuctionClient(ethereumClientHermez, auctionTestAddressConst, tokenHEZ) require.Nil(t, err) slotConst := 4 - blockNum := int64(int(BLOCKSPERSLOT)*slotConst + int(genesisBlock)) + blockNum := int64(int(blocksPerSlot)*slotConst + int(genesisBlock)) currentBlockNum, _ := auctionClientTestHermez.client.EthCurrentBlock() blocksToAdd := blockNum - currentBlockNum addBlocks(blocksToAdd, ethClientDialURL) diff --git a/eth/contracts/README.md b/eth/contracts/README.md index c00da75..bc0f430 100644 --- a/eth/contracts/README.md +++ b/eth/contracts/README.md @@ -11,7 +11,7 @@ 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 Branch: `RemoveERC777` -Specifically they have been processed in the commit with hash: `7574ba47fd3d7dab2653a22f57b15c69280350dc` +Specifically they have been processed in the commit with hash: `2a1cfccfba6770c1077ecea983d2c743dc4a1e93` Versions: ``` diff --git a/eth/ethereum.go b/eth/ethereum.go index 78e2583..fec0f04 100644 --- a/eth/ethereum.go +++ b/eth/ethereum.go @@ -277,6 +277,8 @@ 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) { + // We use the HEZ token smart contract interfacehere because it's an + // ERC20, which allows us to access the standard ERC20 constants. instance, err := HEZ.NewHEZ(tokenAddress, c.client) if err != nil { return nil, err diff --git a/eth/helpers.go b/eth/helpers.go index fc9e9a8..169f5cb 100644 --- a/eth/helpers.go +++ b/eth/helpers.go @@ -1,7 +1,6 @@ package eth import ( - "encoding/hex" "fmt" "math/big" "net/http" @@ -68,19 +67,21 @@ func addTime(seconds float64, url string) { } func createPermitDigest(tokenAddr, owner, spender ethCommon.Address, chainID, value, nonce, deadline *big.Int, tokenName string) ([]byte, error) { + // NOTE: We ignore hash.Write errors because we are writing to a memory + // buffer and don't expect any errors to occur. abiPermit := []byte("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)") hashPermit := sha3.NewLegacyKeccak256() - hashPermit.Write(abiPermit) + hashPermit.Write(abiPermit) //nolint:errcheck,gosec abiEIP712Domain := []byte("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)") hashEIP712Domain := sha3.NewLegacyKeccak256() - hashEIP712Domain.Write(abiEIP712Domain) + hashEIP712Domain.Write(abiEIP712Domain) //nolint:errcheck,gosec var encodeBytes []byte paddedHash := ethCommon.LeftPadBytes(hashEIP712Domain.Sum(nil), 32) hashName := sha3.NewLegacyKeccak256() - hashName.Write([]byte(tokenName)) + hashName.Write([]byte(tokenName)) //nolint:errcheck,gosec paddedName := ethCommon.LeftPadBytes(hashName.Sum(nil), 32) hashVersion := sha3.NewLegacyKeccak256() - hashVersion.Write([]byte("1")) + hashVersion.Write([]byte("1")) //nolint:errcheck,gosec paddedX := ethCommon.LeftPadBytes(hashVersion.Sum(nil), 32) paddedChainID := ethCommon.LeftPadBytes(chainID.Bytes(), 32) paddedAddr := ethCommon.LeftPadBytes(tokenAddr.Bytes(), 32) @@ -90,7 +91,7 @@ func createPermitDigest(tokenAddr, owner, spender ethCommon.Address, chainID, va encodeBytes = append(encodeBytes, paddedChainID...) encodeBytes = append(encodeBytes, paddedAddr...) _domainSeparator := sha3.NewLegacyKeccak256() - _domainSeparator.Write(encodeBytes) + _domainSeparator.Write(encodeBytes) //nolint:errcheck,gosec var bytes1 []byte paddedHashPermit := ethCommon.LeftPadBytes(hashPermit.Sum(nil), 32) @@ -106,19 +107,11 @@ func createPermitDigest(tokenAddr, owner, spender ethCommon.Address, chainID, va bytes1 = append(bytes1, paddedNonce...) bytes1 = append(bytes1, paddedDeadline...) hashBytes1 := sha3.NewLegacyKeccak256() - hashBytes1.Write(bytes1) + hashBytes1.Write(bytes1) //nolint:errcheck,gosec 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) + paddedY := ethCommon.LeftPadBytes([]byte{0x19}, 1) + paddedZ := ethCommon.LeftPadBytes([]byte{0x01}, 1) paddedDomainSeparator := ethCommon.LeftPadBytes(_domainSeparator.Sum(nil), 32) paddedHashBytes1 := ethCommon.LeftPadBytes(hashBytes1.Sum(nil), 32) bytes2 = append(bytes2, paddedY...) @@ -126,7 +119,7 @@ func createPermitDigest(tokenAddr, owner, spender ethCommon.Address, chainID, va bytes2 = append(bytes2, paddedDomainSeparator...) bytes2 = append(bytes2, paddedHashBytes1...) hashBytes2 := sha3.NewLegacyKeccak256() - hashBytes2.Write(bytes2) + hashBytes2.Write(bytes2) //nolint:errcheck,gosec return hashBytes2.Sum(nil), nil } @@ -134,11 +127,11 @@ func createPermitDigest(tokenAddr, owner, spender ethCommon.Address, chainID, va 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) + v := signature[64] + byte(27) //nolint:gomnd ABIpermit := []byte("permit(address,address,uint256,uint256,uint8,bytes32,bytes32)") hash := sha3.NewLegacyKeccak256() - hash.Write(ABIpermit) + hash.Write(ABIpermit) //nolint:errcheck,gosec methodID := hash.Sum(nil)[:4] var permit []byte diff --git a/eth/main_test.go b/eth/main_test.go index 96ba7b5..81eb4a1 100644 --- a/eth/main_test.go +++ b/eth/main_test.go @@ -16,25 +16,25 @@ import ( "github.com/joho/godotenv" ) -var ethClientDialURLConst = "http://localhost:8545" -var passwordConst = "pass" -var deadlineConst, _ = new(big.Int).SetString("ffffffffffffffffffffffffffffffff", 16) - var errEnvVar = fmt.Errorf("Some environment variable is missing") +var ( + ethClientDialURL = "http://localhost:8545" + password = "pass" + deadline, _ = new(big.Int).SetString("ffffffffffffffffffffffffffffffff", 16) +) + // Smart Contract Addresses var ( - password string - ethClientDialURL string - deadline *big.Int - genesisBlock int64 - auctionAddressConst ethCommon.Address - auctionTestAddressConst ethCommon.Address - tokenHEZAddressConst ethCommon.Address - hermezRollupAddressConst ethCommon.Address - wdelayerAddressConst ethCommon.Address - wdelayerTestAddressConst ethCommon.Address - tokenHEZ TokenConfig + genesisBlock int64 + auctionAddressConst ethCommon.Address + auctionTestAddressConst ethCommon.Address + tokenHEZAddressConst ethCommon.Address + hermezRollupAddressConst ethCommon.Address + wdelayerAddressConst ethCommon.Address + wdelayerTestAddressConst ethCommon.Address + tokenHEZ TokenConfig + donationAddressStr = "0x6c365935CA8710200C7595F0a72EB6023A7706Cd" donationAddressConst = ethCommon.HexToAddress(donationAddressStr) bootCoordinatorAddressStr = "0xc783df8a850f42e7f7e57013759c285caa701eb6" @@ -65,9 +65,9 @@ var ( auxAddressStr = "0x3d91185a02774C70287F6c74Dd26d13DFB58ff16" auxAddressConst = ethCommon.HexToAddress(auxAddressStr) - aux2AddressSK = "28d1bfbbafe9d1d4f5a11c3c16ab6bf9084de48d99fbac4058bdfa3c80b29087" - aux2AddressStr = "0x532792b73c0c6e7565912e7039c59986f7e1dd1f" - aux2AddressConst = ethCommon.HexToAddress(aux2AddressStr) + aux2AddressSK = "28d1bfbbafe9d1d4f5a11c3c16ab6bf9084de48d99fbac4058bdfa3c80b29087" + // aux2AddressStr = "0x532792b73c0c6e7565912e7039c59986f7e1dd1f" + // aux2AddressConst = ethCommon.HexToAddress(aux2AddressStr) hermezRollupTestSK = "28d1bfbbafe9d1d4f5a11c3c16ab6bf9084de48d99fbac4058bdfa3c80b29088" hermezRollupTestAddressStr = "0xEa960515F8b4C237730F028cBAcF0a28E7F45dE0" @@ -111,7 +111,7 @@ func addKey(ks *keystore.KeyStore, skHex string) *accounts.Account { func getEnvVariables() { err := godotenv.Load() if err != nil { - fmt.Println("Variables loaded from command") + fmt.Println("Variables loaded from environment") } else { fmt.Println("Variables loaded from .env file") } @@ -130,9 +130,6 @@ func getEnvVariables() { panic(errEnvVar) } - ethClientDialURL = ethClientDialURLConst - password = passwordConst - deadline = deadlineConst auctionAddressConst = ethCommon.HexToAddress(auctionAddressStr) auctionTestAddressConst = ethCommon.HexToAddress(auctionTestAddressStr) tokenHEZAddressConst = ethCommon.HexToAddress(tokenHEZAddressStr) diff --git a/eth/rollup.go b/eth/rollup.go index aa2f748..cf62a9f 100644 --- a/eth/rollup.go +++ b/eth/rollup.go @@ -290,13 +290,14 @@ type RollupInterface interface { type RollupClient struct { client *EthereumClient address ethCommon.Address - tokenHEZ TokenConfig + tokenHEZCfg TokenConfig hermez *Hermez.Hermez + tokenHEZ *HEZ.HEZ contractAbi abi.ABI } // NewRollupClient creates a new RollupClient -func NewRollupClient(client *EthereumClient, address ethCommon.Address, tokenHEZ TokenConfig) (*RollupClient, error) { +func NewRollupClient(client *EthereumClient, address ethCommon.Address, tokenHEZCfg TokenConfig) (*RollupClient, error) { contractAbi, err := abi.JSON(strings.NewReader(string(Hermez.HermezABI))) if err != nil { return nil, err @@ -305,11 +306,16 @@ func NewRollupClient(client *EthereumClient, address ethCommon.Address, tokenHEZ if err != nil { return nil, err } + tokenHEZ, err := HEZ.NewHEZ(tokenHEZCfg.Address, client.Client()) + if err != nil { + return nil, err + } return &RollupClient{ client: client, address: address, - tokenHEZ: tokenHEZ, + tokenHEZCfg: tokenHEZCfg, hermez: hermez, + tokenHEZ: tokenHEZ, contractAbi: contractAbi, }, nil } @@ -375,17 +381,16 @@ func (c *RollupClient) RollupAddToken(tokenAddress ethCommon.Address, feeAddToke if tx, err = c.client.CallAuth( 0, func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) { - tokenHEZcontract, err := HEZ.NewHEZ(c.tokenHEZ.Address, ec) + owner := c.client.account.Address + spender := c.address + nonce, err := c.tokenHEZ.Nonces(nil, owner) if err != nil { return nil, err } - 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) + tokenName := c.tokenHEZCfg.Name + tokenAddr := c.tokenHEZCfg.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) @@ -431,7 +436,6 @@ func (c *RollupClient) RollupL1UserTxERC20ETH(fromBJJ *babyjub.PublicKey, fromId 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 @@ -444,7 +448,8 @@ func (c *RollupClient) RollupL1UserTxERC20ETH(fromBJJ *babyjub.PublicKey, fromId auth.Value = loadAmount } var permit []byte - return c.hermez.AddL1Transaction(auth, babyPubKey, fromIdxBig, uint16(loadAmountF), uint16(amountF), tokenIDBig, toIdxBig, permit) + return c.hermez.AddL1Transaction(auth, babyPubKey, fromIdxBig, uint16(loadAmountF), + uint16(amountF), tokenID, toIdxBig, permit) }, ); err != nil { return nil, fmt.Errorf("Failed add L1 Tx ERC20/ETH: %w", err) @@ -462,7 +467,6 @@ func (c *RollupClient) RollupL1UserTxERC20Permit(fromBJJ *babyjub.PublicKey, fro 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 @@ -474,20 +478,20 @@ func (c *RollupClient) RollupL1UserTxERC20Permit(fromBJJ *babyjub.PublicKey, fro if tokenID == 0 { auth.Value = loadAmount } - tokenHEZcontract, err := HEZ.NewHEZ(c.tokenHEZ.Address, ec) + owner := c.client.account.Address + spender := c.address + nonce, err := c.tokenHEZ.Nonces(nil, owner) if err != nil { return nil, err } - 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) + tokenName := c.tokenHEZCfg.Name + tokenAddr := c.tokenHEZCfg.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) + return c.hermez.AddL1Transaction(auth, babyPubKey, fromIdxBig, uint16(loadAmountF), + uint16(amountF), tokenID, toIdxBig, permit) }, ); err != nil { return nil, fmt.Errorf("Failed add L1 Tx ERC20Permit: %w", err) diff --git a/eth/rollup_test.go b/eth/rollup_test.go index af7b1a3..9e9078c 100644 --- a/eth/rollup_test.go +++ b/eth/rollup_test.go @@ -105,7 +105,7 @@ func TestRollupForgeBatch(t *testing.T) { require.Nil(t, err) // Add Blocks - blockNum := int64(int(BLOCKSPERSLOT)*int(currentSlot+4) + int(genesisBlock)) + blockNum := int64(int(blocksPerSlot)*int(currentSlot+4) + int(genesisBlock)) currentBlockNum, _ := auctionClient.client.EthCurrentBlock() blocksToAdd := blockNum - currentBlockNum addBlocks(blocksToAdd, ethClientDialURL) diff --git a/eth/wdelayer_test.go b/eth/wdelayer_test.go index 079f4a4..addd8ff 100644 --- a/eth/wdelayer_test.go +++ b/eth/wdelayer_test.go @@ -138,7 +138,7 @@ func TestWDelayerWithdrawal(t *testing.T) { amount := new(big.Int) amount.SetString("1100000000000000000", 10) _, err := wdelayerClientTest.WDelayerWithdrawal(auxAddressConst, tokenHEZAddressConst) - require.Contains(t, err.Error(), "Withdrawal not allowed yet") + require.Contains(t, err.Error(), "WITHDRAWAL_NOT_ALLOWED") addBlocks(newWithdrawalDelay.Int64(), ethClientDialURL) _, err = wdelayerClientTest.WDelayerWithdrawal(auxAddressConst, tokenHEZAddressConst) require.Nil(t, err) @@ -193,7 +193,7 @@ func TestWDelayerEscapeHatchWithdrawal(t *testing.T) { wdelayerClientWhite, err := NewWDelayerClient(ethereumClientWhite, wdelayerTestAddressConst) require.Nil(t, err) _, err = wdelayerClientWhite.WDelayerEscapeHatchWithdrawal(governanceAddressConst, tokenHEZAddressConst, amount) - require.Contains(t, err.Error(), "NO MAX_EMERGENCY_MODE_TIME") + require.Contains(t, err.Error(), "NO_MAX_EMERGENCY_MODE_TIME") seconds := maxEmergencyModeTime.Seconds() addTime(seconds, ethClientDialURL) _, err = wdelayerClientWhite.WDelayerEscapeHatchWithdrawal(governanceAddressConst, tokenHEZAddressConst, amount) diff --git a/node/node.go b/node/node.go index e675528..2c3e823 100644 --- a/node/node.go +++ b/node/node.go @@ -96,8 +96,11 @@ func NewNode(mode Mode, cfg *config.Node, coordCfg *config.Coordinator) (*Node, Address: cfg.SmartContracts.Rollup, }, Auction: eth.AuctionConfig{ - Address: cfg.SmartContracts.Auction, - TokenHEZAddress: cfg.SmartContracts.TokenHEZ, + Address: cfg.SmartContracts.Auction, + TokenHEZ: eth.TokenConfig{ + Address: cfg.SmartContracts.TokenHEZ, + Name: cfg.SmartContracts.TokenHEZName, + }, }, }) if err != nil { diff --git a/synchronizer/synchronizer_test.go b/synchronizer/synchronizer_test.go index 353af78..da51a0a 100644 --- a/synchronizer/synchronizer_test.go +++ b/synchronizer/synchronizer_test.go @@ -146,7 +146,7 @@ func TestSync(t *testing.T) { // Add tokens to ethereum, and to rollup for _, token := range tokens { client.CtlAddERC20(token.Addr, token.Consts) - _, err := client.RollupAddToken(token.Addr, clientSetup.RollupVariables.FeeAddToken) + _, err := client.RollupAddTokenSimple(token.Addr, clientSetup.RollupVariables.FeeAddToken) require.Nil(t, err) } diff --git a/test/ethclient.go b/test/ethclient.go index 4865e32..a9b630d 100644 --- a/test/ethclient.go +++ b/test/ethclient.go @@ -621,6 +621,12 @@ var errTODO = fmt.Errorf("TODO: Not implemented yet") // }) // } +// RollupL1UserTxERC20Permit is the interface to call the smart contract function +func (c *Client) RollupL1UserTxERC20Permit(fromBJJ *babyjub.PublicKey, fromIdx int64, loadAmount *big.Int, amount *big.Int, tokenID uint32, toIdx int64, deadline *big.Int) (tx *types.Transaction, err error) { + log.Error("TODO") + return nil, errTODO +} + // RollupL1UserTxERC20ETH sends an L1UserTx to the Rollup. func (c *Client) RollupL1UserTxERC20ETH( fromBJJ *babyjub.PublicKey, @@ -683,10 +689,10 @@ func (c *Client) RollupL1UserTxERC20ETH( } // RollupL1UserTxERC777 is the interface to call the smart contract function -func (c *Client) RollupL1UserTxERC777(fromBJJ *babyjub.PublicKey, fromIdx int64, loadAmount *big.Int, amount *big.Int, tokenID uint32, toIdx int64) (*types.Transaction, error) { - log.Error("TODO") - return nil, errTODO -} +// func (c *Client) RollupL1UserTxERC777(fromBJJ *babyjub.PublicKey, fromIdx int64, loadAmount *big.Int, amount *big.Int, tokenID uint32, toIdx int64) (*types.Transaction, error) { +// log.Error("TODO") +// return nil, errTODO +// } // RollupRegisterTokensCount is the interface to call the smart contract function func (c *Client) RollupRegisterTokensCount() (*big.Int, error) { @@ -790,8 +796,15 @@ func (c *Client) addBatch(args *eth.RollupForgeBatchArgs) (*types.Transaction, e return ethTx, nil } +// RollupAddTokenSimple is a wrapper around RollupAddToken that automatically +// sets `deadlie`. +func (c *Client) RollupAddTokenSimple(tokenAddress ethCommon.Address, feeAddToken *big.Int) (tx *types.Transaction, err error) { + return c.RollupAddToken(tokenAddress, feeAddToken, big.NewInt(9999)) //nolint:gomnd +} + // RollupAddToken is the interface to call the smart contract function -func (c *Client) RollupAddToken(tokenAddress ethCommon.Address, feeAddToken *big.Int) (tx *types.Transaction, err error) { +func (c *Client) RollupAddToken(tokenAddress ethCommon.Address, feeAddToken *big.Int, + deadline *big.Int) (tx *types.Transaction, err error) { c.rw.Lock() defer c.rw.Unlock() cpy := c.nextBlock().copy() @@ -1190,8 +1203,15 @@ func (c *Client) AuctionGetSlotSet(slot int64) (*big.Int, error) { // return errTODO // } -// AuctionBid is the interface to call the smart contract function -func (c *Client) AuctionBid(slot int64, bidAmount *big.Int) (tx *types.Transaction, err error) { +// AuctionBidSimple is a wrapper around AuctionBid that automatically sets `amount` and `deadline`. +func (c *Client) AuctionBidSimple(slot int64, bidAmount *big.Int) (tx *types.Transaction, err error) { + return c.AuctionBid(bidAmount, slot, bidAmount, big.NewInt(99999)) //nolint:gomnd +} + +// AuctionBid is the interface to call the smart contract function. This +// implementation behaves as if any address has infinite tokens. +func (c *Client) AuctionBid(amount *big.Int, slot int64, bidAmount *big.Int, + deadline *big.Int) (tx *types.Transaction, err error) { c.rw.Lock() defer c.rw.Unlock() cpy := c.nextBlock().copy() @@ -1242,8 +1262,10 @@ func (c *Client) AuctionBid(slot int64, bidAmount *big.Int) (tx *types.Transacti return a.addTransaction(newTransaction("bid", data{slot, bidAmount, *c.addr})), nil } -// AuctionMultiBid is the interface to call the smart contract function -func (c *Client) AuctionMultiBid(startingSlot int64, endingSlot int64, slotSet [6]bool, maxBid, closedMinBid, budget *big.Int) (tx *types.Transaction, err error) { +// AuctionMultiBid is the interface to call the smart contract function. This +// implementation behaves as if any address has infinite tokens. +func (c *Client) AuctionMultiBid(amount *big.Int, startingSlot int64, endingSlot int64, slotSet [6]bool, + maxBid, closedMinBid, deadline *big.Int) (tx *types.Transaction, err error) { c.rw.Lock() defer c.rw.Unlock() cpy := c.nextBlock().copy() diff --git a/test/ethclient_test.go b/test/ethclient_test.go index a8d9398..da191e6 100644 --- a/test/ethclient_test.go +++ b/test/ethclient_test.go @@ -94,34 +94,34 @@ func TestClientAuction(t *testing.T) { // Check several cases in which bid doesn't succed, and also do 2 successful bids. - _, err := c.AuctionBid(0, big.NewInt(1)) + _, err := c.AuctionBidSimple(0, big.NewInt(1)) assert.Equal(t, errBidClosed, err) - _, err = c.AuctionBid(4322, big.NewInt(1)) + _, err = c.AuctionBidSimple(4322, big.NewInt(1)) assert.Equal(t, errBidNotOpen, err) // 101 % 6 = 5; defaultSlotSetBid[5] = 1500; 1500 + 10% = 1650 - _, err = c.AuctionBid(101, big.NewInt(1650)) + _, err = c.AuctionBidSimple(101, big.NewInt(1650)) assert.Equal(t, errCoordNotReg, err) _, err = c.AuctionSetCoordinator(addrForge, "https://foo.bar") assert.Nil(t, err) - _, err = c.AuctionBid(3, big.NewInt(1)) + _, err = c.AuctionBidSimple(3, big.NewInt(1)) assert.Equal(t, errBidBelowMin, err) - _, err = c.AuctionBid(3, big.NewInt(1650)) + _, err = c.AuctionBidSimple(3, big.NewInt(1650)) assert.Nil(t, err) c.CtlSetAddr(addrBidder2) _, err = c.AuctionSetCoordinator(addrForge2, "https://foo2.bar") assert.Nil(t, err) - _, err = c.AuctionBid(3, big.NewInt(16)) + _, err = c.AuctionBidSimple(3, big.NewInt(16)) assert.Equal(t, errBidBelowMin, err) // 1650 + 10% = 1815 - _, err = c.AuctionBid(3, big.NewInt(1815)) + _, err = c.AuctionBidSimple(3, big.NewInt(1815)) assert.Nil(t, err) c.CtlMineBlock() @@ -143,7 +143,7 @@ func TestClientRollup(t *testing.T) { // Add a token - tx, err := c.RollupAddToken(token1Addr, clientSetup.RollupVariables.FeeAddToken) + tx, err := c.RollupAddTokenSimple(token1Addr, clientSetup.RollupVariables.FeeAddToken) require.Nil(t, err) assert.NotNil(t, tx)