2022-07-21 19:39:53 +00:00
|
|
|
package rpcclient
|
2021-03-23 19:50:15 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2022-07-06 15:15:17 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config"
|
2022-06-15 18:23:29 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2022-07-22 16:09:29 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neorpc/result"
|
2022-06-15 18:23:29 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2021-03-23 19:04:34 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2021-03-23 19:50:15 +00:00
|
|
|
)
|
|
|
|
|
2022-06-15 18:23:29 +00:00
|
|
|
// InvokeAndPackIteratorResults creates a script containing System.Contract.Call
|
|
|
|
// of the specified contract with the specified arguments. It assumes that the
|
|
|
|
// specified operation will return iterator. The script traverses the resulting
|
|
|
|
// iterator, packs all its values into array and pushes the resulting array on
|
|
|
|
// stack. Constructed script is invoked via `invokescript` JSON-RPC API using
|
|
|
|
// the provided signers. The result of the script invocation contains single array
|
|
|
|
// stackitem on stack if invocation HALTed. InvokeAndPackIteratorResults can be
|
|
|
|
// used to interact with JSON-RPC server where iterator sessions are disabled to
|
2022-07-06 15:15:17 +00:00
|
|
|
// retrieve iterator values via single `invokescript` JSON-RPC call. It returns
|
|
|
|
// maxIteratorResultItems items at max which is set to
|
|
|
|
// config.DefaultMaxIteratorResultItems by default.
|
2022-08-01 12:27:36 +00:00
|
|
|
//
|
|
|
|
// Deprecated: please use more convenient and powerful invoker.Invoker interface with
|
|
|
|
// CallAndExpandIterator method. This method will be removed in future versions.
|
2022-07-06 15:15:17 +00:00
|
|
|
func (c *Client) InvokeAndPackIteratorResults(contract util.Uint160, operation string, params []smartcontract.Parameter, signers []transaction.Signer, maxIteratorResultItems ...int) (*result.Invoke, error) {
|
|
|
|
max := config.DefaultMaxIteratorResultItems
|
|
|
|
if len(maxIteratorResultItems) != 0 {
|
|
|
|
max = maxIteratorResultItems[0]
|
|
|
|
}
|
2022-07-29 08:11:18 +00:00
|
|
|
values, err := smartcontract.ExpandParameterToEmitable(smartcontract.Parameter{
|
|
|
|
Type: smartcontract.ArrayType,
|
|
|
|
Value: params,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("expanding params to emitable: %w", err)
|
|
|
|
}
|
2023-04-03 10:34:24 +00:00
|
|
|
bytes, err := smartcontract.CreateCallAndUnwrapIteratorScript(contract, operation, max, values.([]any)...)
|
2022-06-15 18:23:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create iterator unwrapper script: %w", err)
|
|
|
|
}
|
|
|
|
return c.InvokeScript(bytes, signers)
|
|
|
|
}
|