github.com/amazechain/amc@v0.1.3/accounts/abi/error.go (about)

     1  // Copyright 2023 The AmazeChain Authors
     2  // This file is part of the AmazeChain library.
     3  //
     4  // The AmazeChain library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The AmazeChain library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package abi
    18  
    19  import (
    20  	"bytes"
    21  	"errors"
    22  	"fmt"
    23  	"github.com/amazechain/amc/common/crypto"
    24  	"github.com/amazechain/amc/common/types"
    25  	"strings"
    26  )
    27  
    28  type Error struct {
    29  	Name   string
    30  	Inputs Arguments
    31  	str    string
    32  
    33  	// Sig contains the string signature according to the ABI spec.
    34  	// e.g.	 error foo(uint32 a, int b) = "foo(uint32,int256)"
    35  	// Please note that "int" is substitute for its canonical representation "int256"
    36  	Sig string
    37  
    38  	// ID returns the canonical representation of the error's signature used by the
    39  	// abi definition to identify event names and types.
    40  	ID types.Hash
    41  }
    42  
    43  func NewError(name string, inputs Arguments) Error {
    44  	// sanitize inputs to remove inputs without names
    45  	// and precompute string and sig representation.
    46  	names := make([]string, len(inputs))
    47  	typess := make([]string, len(inputs))
    48  	for i, input := range inputs {
    49  		if input.Name == "" {
    50  			inputs[i] = Argument{
    51  				Name:    fmt.Sprintf("arg%d", i),
    52  				Indexed: input.Indexed,
    53  				Type:    input.Type,
    54  			}
    55  		} else {
    56  			inputs[i] = input
    57  		}
    58  		// string representation
    59  		names[i] = fmt.Sprintf("%v %v", input.Type, inputs[i].Name)
    60  		if input.Indexed {
    61  			names[i] = fmt.Sprintf("%v indexed %v", input.Type, inputs[i].Name)
    62  		}
    63  		// sig representation
    64  		typess[i] = input.Type.String()
    65  	}
    66  
    67  	str := fmt.Sprintf("error %v(%v)", name, strings.Join(names, ", "))
    68  	sig := fmt.Sprintf("%v(%v)", name, strings.Join(typess, ","))
    69  	id := types.BytesToHash(crypto.Keccak256([]byte(sig)))
    70  
    71  	return Error{
    72  		Name:   name,
    73  		Inputs: inputs,
    74  		str:    str,
    75  		Sig:    sig,
    76  		ID:     id,
    77  	}
    78  }
    79  
    80  func (e *Error) String() string {
    81  	return e.str
    82  }
    83  
    84  func (e *Error) Unpack(data []byte) (interface{}, error) {
    85  	if len(data) < 4 {
    86  		return "", errors.New("invalid data for unpacking")
    87  	}
    88  	if !bytes.Equal(data[:4], e.ID[:4]) {
    89  		return "", errors.New("invalid data for unpacking")
    90  	}
    91  	return e.Inputs.Unpack(data[4:])
    92  }