Update common.L1Tx parsers, add test checks

This commit is contained in:
arnaucube
2020-09-15 12:12:06 +02:00
parent 1de2ec9937
commit 92fa8aa439
3 changed files with 69 additions and 58 deletions

View File

@@ -2,12 +2,16 @@ package common
import (
"encoding/binary"
"fmt"
"math/big"
"time"
ethCommon "github.com/ethereum/go-ethereum/common"
)
// tokenIDBytesLen defines the length of the TokenID byte array representation
const tokenIDBytesLen = 4
// Token is a struct that represents an Ethereum token that is supported in Hermez network
type Token struct {
TokenID TokenID `meddler:"token_id"`
@@ -41,3 +45,12 @@ func (t TokenID) Bytes() []byte {
func (t TokenID) BigInt() *big.Int {
return big.NewInt(int64(t))
}
// TokenIDFromBytes returns TokenID from a byte array
func TokenIDFromBytes(b []byte) (TokenID, error) {
if len(b) != tokenIDBytesLen {
return 0, fmt.Errorf("can not parse TokenID, bytes len %d, expected 4", len(b))
}
tid := binary.LittleEndian.Uint32(b[:4])
return TokenID(tid), nil
}