Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpcbinding: expand struct generator with ToSCParameter #3804

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions cli/smartcontract/testdata/rpcbindings/nft-nd/rpcbindings.out
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ func itemToNftRoyaltyRecipientShare(item stackitem.Item, err error) (*NftRoyalty
// Ensure *NftRoyaltyRecipientShare is a proper [stackitem.Convertible].
var _ = stackitem.Convertible(&NftRoyaltyRecipientShare{})

// Ensure *NftRoyaltyRecipientShare is a proper [smartcontract.Convertible].
var _ = smartcontract.Convertible(&NftRoyaltyRecipientShare{})

// FromStackItem retrieves fields of NftRoyaltyRecipientShare from the given
// [stackitem.Item] or returns an error if it's not possible to do to so.
// It implements [stackitem.Convertible] interface.
Expand Down Expand Up @@ -278,3 +281,31 @@ func (res *NftRoyaltyRecipientShare) ToStackItem() (stackitem.Item, error) {

return stackitem.NewStruct(items), nil
}

// ToSCParameter creates [smartcontract.Parameter] representing NftRoyaltyRecipientShare.
// It implements [smartcontract.Convertible] interface so that NftRoyaltyRecipientShare
// could be used with invokers.
func (res *NftRoyaltyRecipientShare) ToSCParameter() (smartcontract.Parameter, error) {
if res == nil {
return smartcontract.Parameter{Type: smartcontract.AnyType}, nil
}

var (
err error
prm smartcontract.Parameter
prms = make([]smartcontract.Parameter, 0, 2)
)
prm, err = smartcontract.NewParameterFromValue(res.Address)
if err != nil {
return smartcontract.Parameter{}, fmt.Errorf("field Address: %w", err)
}
prms = append(prms, prm)

prm, err = smartcontract.NewParameterFromValue(res.Share)
if err != nil {
return smartcontract.Parameter{}, fmt.Errorf("field Share: %w", err)
}
prms = append(prms, prm)

return smartcontract.Parameter{Type: smartcontract.ArrayType, Value: prms}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ func itemToNftRoyaltyRecipientShare(item stackitem.Item, err error) (*NftRoyalty
// Ensure *NftRoyaltyRecipientShare is a proper [stackitem.Convertible].
var _ = stackitem.Convertible(&NftRoyaltyRecipientShare{})

// Ensure *NftRoyaltyRecipientShare is a proper [smartcontract.Convertible].
var _ = smartcontract.Convertible(&NftRoyaltyRecipientShare{})

// FromStackItem retrieves fields of NftRoyaltyRecipientShare from the given
// [stackitem.Item] or returns an error if it's not possible to do to so.
// It implements [stackitem.Convertible] interface.
Expand Down Expand Up @@ -273,3 +276,31 @@ func (res *NftRoyaltyRecipientShare) ToStackItem() (stackitem.Item, error) {

return stackitem.NewStruct(items), nil
}

// ToSCParameter creates [smartcontract.Parameter] representing NftRoyaltyRecipientShare.
// It implements [smartcontract.Convertible] interface so that NftRoyaltyRecipientShare
// could be used with invokers.
func (res *NftRoyaltyRecipientShare) ToSCParameter() (smartcontract.Parameter, error) {
if res == nil {
return smartcontract.Parameter{Type: smartcontract.AnyType}, nil
}

var (
err error
prm smartcontract.Parameter
prms = make([]smartcontract.Parameter, 0, 2)
)
prm, err = smartcontract.NewParameterFromValue(res.Address)
if err != nil {
return smartcontract.Parameter{}, fmt.Errorf("field Address: %w", err)
}
prms = append(prms, prm)

prm, err = smartcontract.NewParameterFromValue(res.Share)
if err != nil {
return smartcontract.Parameter{}, fmt.Errorf("field Share: %w", err)
}
prms = append(prms, prm)

return smartcontract.Parameter{Type: smartcontract.ArrayType, Value: prms}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/neorpc/result"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"math/big"
Expand Down Expand Up @@ -203,6 +204,9 @@ func itemToCrazyStruct(item stackitem.Item, err error) (*CrazyStruct, error) {
// Ensure *CrazyStruct is a proper [stackitem.Convertible].
var _ = stackitem.Convertible(&CrazyStruct{})

// Ensure *CrazyStruct is a proper [smartcontract.Convertible].
var _ = smartcontract.Convertible(&CrazyStruct{})

// FromStackItem retrieves fields of CrazyStruct from the given
// [stackitem.Item] or returns an error if it's not possible to do to so.
// It implements [stackitem.Convertible] interface.
Expand Down Expand Up @@ -261,6 +265,34 @@ func (res *CrazyStruct) ToStackItem() (stackitem.Item, error) {
return stackitem.NewStruct(items), nil
}

// ToSCParameter creates [smartcontract.Parameter] representing CrazyStruct.
// It implements [smartcontract.Convertible] interface so that CrazyStruct
// could be used with invokers.
func (res *CrazyStruct) ToSCParameter() (smartcontract.Parameter, error) {
if res == nil {
return smartcontract.Parameter{Type: smartcontract.AnyType}, nil
}

var (
err error
prm smartcontract.Parameter
prms = make([]smartcontract.Parameter, 0, 2)
)
prm, err = smartcontract.NewParameterFromValue(res.I)
if err != nil {
return smartcontract.Parameter{}, fmt.Errorf("field I: %w", err)
}
prms = append(prms, prm)

prm, err = smartcontract.NewParameterFromValue(res.B)
if err != nil {
return smartcontract.Parameter{}, fmt.Errorf("field B: %w", err)
}
prms = append(prms, prm)

return smartcontract.Parameter{Type: smartcontract.ArrayType, Value: prms}, nil
}

// itemToSimpleStruct converts stack item into *SimpleStruct.
// NULL item is returned as nil pointer without error.
func itemToSimpleStruct(item stackitem.Item, err error) (*SimpleStruct, error) {
Expand All @@ -279,6 +311,9 @@ func itemToSimpleStruct(item stackitem.Item, err error) (*SimpleStruct, error) {
// Ensure *SimpleStruct is a proper [stackitem.Convertible].
var _ = stackitem.Convertible(&SimpleStruct{})

// Ensure *SimpleStruct is a proper [smartcontract.Convertible].
var _ = smartcontract.Convertible(&SimpleStruct{})

// FromStackItem retrieves fields of SimpleStruct from the given
// [stackitem.Item] or returns an error if it's not possible to do to so.
// It implements [stackitem.Convertible] interface.
Expand Down Expand Up @@ -325,6 +360,28 @@ func (res *SimpleStruct) ToStackItem() (stackitem.Item, error) {
return stackitem.NewStruct(items), nil
}

// ToSCParameter creates [smartcontract.Parameter] representing SimpleStruct.
// It implements [smartcontract.Convertible] interface so that SimpleStruct
// could be used with invokers.
func (res *SimpleStruct) ToSCParameter() (smartcontract.Parameter, error) {
if res == nil {
return smartcontract.Parameter{Type: smartcontract.AnyType}, nil
}

var (
err error
prm smartcontract.Parameter
prms = make([]smartcontract.Parameter, 0, 1)
)
prm, err = smartcontract.NewParameterFromValue(res.I)
if err != nil {
return smartcontract.Parameter{}, fmt.Errorf("field I: %w", err)
}
prms = append(prms, prm)

return smartcontract.Parameter{Type: smartcontract.ArrayType, Value: prms}, nil
}

// ComplicatedNameEventsFromApplicationLog retrieves a set of all emitted events
// with "! complicated name %$#" name from the provided [result.ApplicationLog].
func ComplicatedNameEventsFromApplicationLog(log *result.ApplicationLog) ([]*ComplicatedNameEvent, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/neorpc/result"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"math/big"
Expand Down Expand Up @@ -203,6 +204,9 @@ func itemToUnnamed(item stackitem.Item, err error) (*Unnamed, error) {
// Ensure *Unnamed is a proper [stackitem.Convertible].
var _ = stackitem.Convertible(&Unnamed{})

// Ensure *Unnamed is a proper [smartcontract.Convertible].
var _ = smartcontract.Convertible(&Unnamed{})

// FromStackItem retrieves fields of Unnamed from the given
// [stackitem.Item] or returns an error if it's not possible to do to so.
// It implements [stackitem.Convertible] interface.
Expand Down Expand Up @@ -261,6 +265,34 @@ func (res *Unnamed) ToStackItem() (stackitem.Item, error) {
return stackitem.NewStruct(items), nil
}

// ToSCParameter creates [smartcontract.Parameter] representing Unnamed.
// It implements [smartcontract.Convertible] interface so that Unnamed
// could be used with invokers.
func (res *Unnamed) ToSCParameter() (smartcontract.Parameter, error) {
if res == nil {
return smartcontract.Parameter{Type: smartcontract.AnyType}, nil
}

var (
err error
prm smartcontract.Parameter
prms = make([]smartcontract.Parameter, 0, 2)
)
prm, err = smartcontract.NewParameterFromValue(res.I)
if err != nil {
return smartcontract.Parameter{}, fmt.Errorf("field I: %w", err)
}
prms = append(prms, prm)

prm, err = smartcontract.NewParameterFromValue(res.B)
if err != nil {
return smartcontract.Parameter{}, fmt.Errorf("field B: %w", err)
}
prms = append(prms, prm)

return smartcontract.Parameter{Type: smartcontract.ArrayType, Value: prms}, nil
}

// itemToUnnamedX converts stack item into *UnnamedX.
// NULL item is returned as nil pointer without error.
func itemToUnnamedX(item stackitem.Item, err error) (*UnnamedX, error) {
Expand All @@ -279,6 +311,9 @@ func itemToUnnamedX(item stackitem.Item, err error) (*UnnamedX, error) {
// Ensure *UnnamedX is a proper [stackitem.Convertible].
var _ = stackitem.Convertible(&UnnamedX{})

// Ensure *UnnamedX is a proper [smartcontract.Convertible].
var _ = smartcontract.Convertible(&UnnamedX{})

// FromStackItem retrieves fields of UnnamedX from the given
// [stackitem.Item] or returns an error if it's not possible to do to so.
// It implements [stackitem.Convertible] interface.
Expand Down Expand Up @@ -325,6 +360,28 @@ func (res *UnnamedX) ToStackItem() (stackitem.Item, error) {
return stackitem.NewStruct(items), nil
}

// ToSCParameter creates [smartcontract.Parameter] representing UnnamedX.
// It implements [smartcontract.Convertible] interface so that UnnamedX
// could be used with invokers.
func (res *UnnamedX) ToSCParameter() (smartcontract.Parameter, error) {
if res == nil {
return smartcontract.Parameter{Type: smartcontract.AnyType}, nil
}

var (
err error
prm smartcontract.Parameter
prms = make([]smartcontract.Parameter, 0, 1)
)
prm, err = smartcontract.NewParameterFromValue(res.I)
if err != nil {
return smartcontract.Parameter{}, fmt.Errorf("field I: %w", err)
}
prms = append(prms, prm)

return smartcontract.Parameter{Type: smartcontract.ArrayType, Value: prms}, nil
}

// ComplicatedNameEventsFromApplicationLog retrieves a set of all emitted events
// with "! complicated name %$#" name from the provided [result.ApplicationLog].
func ComplicatedNameEventsFromApplicationLog(log *result.ApplicationLog) ([]*ComplicatedNameEvent, error) {
Expand Down
Loading
Loading