github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/cli/smartcontract/testdata/verifyrpc/verify.go (about)

     1  // Code generated by neo-go contract generate-rpcwrapper --manifest <file.json> --out <file.go> [--hash <hash>] [--config <config>]; DO NOT EDIT.
     2  
     3  // Package verify contains RPC wrappers for verify contract.
     4  package verify
     5  
     6  import (
     7  	"errors"
     8  	"fmt"
     9  	"github.com/nspcc-dev/neo-go/pkg/core/transaction"
    10  	"github.com/nspcc-dev/neo-go/pkg/neorpc/result"
    11  	"github.com/nspcc-dev/neo-go/pkg/smartcontract"
    12  	"github.com/nspcc-dev/neo-go/pkg/util"
    13  	"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
    14  )
    15  
    16  // Hash contains contract hash.
    17  var Hash = util.Uint160{0x33, 0x22, 0x11, 0x0, 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x0}
    18  
    19  // HelloWorldEvent represents "Hello world!" event emitted by the contract.
    20  type HelloWorldEvent struct {
    21  	Args []any
    22  }
    23  
    24  // Actor is used by Contract to call state-changing methods.
    25  type Actor interface {
    26  	MakeCall(contract util.Uint160, method string, params ...any) (*transaction.Transaction, error)
    27  	MakeRun(script []byte) (*transaction.Transaction, error)
    28  	MakeUnsignedCall(contract util.Uint160, method string, attrs []transaction.Attribute, params ...any) (*transaction.Transaction, error)
    29  	MakeUnsignedRun(script []byte, attrs []transaction.Attribute) (*transaction.Transaction, error)
    30  	SendCall(contract util.Uint160, method string, params ...any) (util.Uint256, uint32, error)
    31  	SendRun(script []byte) (util.Uint256, uint32, error)
    32  }
    33  
    34  // Contract implements all contract methods.
    35  type Contract struct {
    36  	actor Actor
    37  	hash  util.Uint160
    38  }
    39  
    40  // New creates an instance of Contract using Hash and the given Actor.
    41  func New(actor Actor) *Contract {
    42  	var hash = Hash
    43  	return &Contract{actor, hash}
    44  }
    45  
    46  func (c *Contract) scriptForVerify() ([]byte, error) {
    47  	return smartcontract.CreateCallWithAssertScript(c.hash, "verify")
    48  }
    49  
    50  // Verify creates a transaction invoking `verify` method of the contract.
    51  // This transaction is signed and immediately sent to the network.
    52  // The values returned are its hash, ValidUntilBlock value and error if any.
    53  func (c *Contract) Verify() (util.Uint256, uint32, error) {
    54  	script, err := c.scriptForVerify()
    55  	if err != nil {
    56  		return util.Uint256{}, 0, err
    57  	}
    58  	return c.actor.SendRun(script)
    59  }
    60  
    61  // VerifyTransaction creates a transaction invoking `verify` method of the contract.
    62  // This transaction is signed, but not sent to the network, instead it's
    63  // returned to the caller.
    64  func (c *Contract) VerifyTransaction() (*transaction.Transaction, error) {
    65  	script, err := c.scriptForVerify()
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  	return c.actor.MakeRun(script)
    70  }
    71  
    72  // VerifyUnsigned creates a transaction invoking `verify` method of the contract.
    73  // This transaction is not signed, it's simply returned to the caller.
    74  // Any fields of it that do not affect fees can be changed (ValidUntilBlock,
    75  // Nonce), fee values (NetworkFee, SystemFee) can be increased as well.
    76  func (c *Contract) VerifyUnsigned() (*transaction.Transaction, error) {
    77  	script, err := c.scriptForVerify()
    78  	if err != nil {
    79  		return nil, err
    80  	}
    81  	return c.actor.MakeUnsignedRun(script, nil)
    82  }
    83  
    84  // HelloWorldEventsFromApplicationLog retrieves a set of all emitted events
    85  // with "Hello world!" name from the provided [result.ApplicationLog].
    86  func HelloWorldEventsFromApplicationLog(log *result.ApplicationLog) ([]*HelloWorldEvent, error) {
    87  	if log == nil {
    88  		return nil, errors.New("nil application log")
    89  	}
    90  
    91  	var res []*HelloWorldEvent
    92  	for i, ex := range log.Executions {
    93  		for j, e := range ex.Events {
    94  			if e.Name != "Hello world!" {
    95  				continue
    96  			}
    97  			event := new(HelloWorldEvent)
    98  			err := event.FromStackItem(e.Item)
    99  			if err != nil {
   100  				return nil, fmt.Errorf("failed to deserialize HelloWorldEvent from stackitem (execution #%d, event #%d): %w", i, j, err)
   101  			}
   102  			res = append(res, event)
   103  		}
   104  	}
   105  
   106  	return res, nil
   107  }
   108  
   109  // FromStackItem converts provided [stackitem.Array] to HelloWorldEvent or
   110  // returns an error if it's not possible to do to so.
   111  func (e *HelloWorldEvent) FromStackItem(item *stackitem.Array) error {
   112  	if item == nil {
   113  		return errors.New("nil item")
   114  	}
   115  	arr, ok := item.Value().([]stackitem.Item)
   116  	if !ok {
   117  		return errors.New("not an array")
   118  	}
   119  	if len(arr) != 1 {
   120  		return errors.New("wrong number of structure elements")
   121  	}
   122  
   123  	var (
   124  		index = -1
   125  		err   error
   126  	)
   127  	index++
   128  	e.Args, err = func(item stackitem.Item) ([]any, error) {
   129  		arr, ok := item.Value().([]stackitem.Item)
   130  		if !ok {
   131  			return nil, errors.New("not an array")
   132  		}
   133  		res := make([]any, len(arr))
   134  		for i := range res {
   135  			res[i], err = arr[i].Value(), error(nil)
   136  			if err != nil {
   137  				return nil, fmt.Errorf("item %d: %w", i, err)
   138  			}
   139  		}
   140  		return res, nil
   141  	}(arr[index])
   142  	if err != nil {
   143  		return fmt.Errorf("field Args: %w", err)
   144  	}
   145  
   146  	return nil
   147  }