smartcontract: make CreateCallAndUnwrapIteratorScript accept Go types

Parameter is for the RPC client, all other CreateXXXScript functions deal with
regular types.
This commit is contained in:
Roman Khimov 2022-07-29 11:11:18 +03:00
parent 848d68fba8
commit a8a2f2ed5a
2 changed files with 11 additions and 13 deletions

View file

@ -116,7 +116,14 @@ func (c *Client) InvokeAndPackIteratorResults(contract util.Uint160, operation s
if len(maxIteratorResultItems) != 0 { if len(maxIteratorResultItems) != 0 {
max = maxIteratorResultItems[0] max = maxIteratorResultItems[0]
} }
bytes, err := smartcontract.CreateCallAndUnwrapIteratorScript(contract, operation, params, max) values, err := smartcontract.ExpandParameterToEmitable(smartcontract.Parameter{
Type: smartcontract.ArrayType,
Value: params,
})
if err != nil {
return nil, fmt.Errorf("expanding params to emitable: %w", err)
}
bytes, err := smartcontract.CreateCallAndUnwrapIteratorScript(contract, operation, max, values.([]interface{})...)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to create iterator unwrapper script: %w", err) return nil, fmt.Errorf("failed to create iterator unwrapper script: %w", err)
} }

View file

@ -18,20 +18,11 @@ import (
// processed this way (and this number can't exceed VM limits), the result of the // processed this way (and this number can't exceed VM limits), the result of the
// script is an array containing extracted value elements. This script can be useful // script is an array containing extracted value elements. This script can be useful
// for interactions with RPC server that have iterator sessions disabled. // for interactions with RPC server that have iterator sessions disabled.
func CreateCallAndUnwrapIteratorScript(contract util.Uint160, operation string, params []Parameter, maxIteratorResultItems int) ([]byte, error) { func CreateCallAndUnwrapIteratorScript(contract util.Uint160, operation string, maxIteratorResultItems int, params ...interface{}) ([]byte, error) {
script := io.NewBufBinWriter() script := io.NewBufBinWriter()
emit.Int(script.BinWriter, int64(maxIteratorResultItems)) emit.Int(script.BinWriter, int64(maxIteratorResultItems))
// Pack arguments for System.Contract.Call. emit.AppCall(script.BinWriter, contract, operation, callflag.All, params...) // The System.Contract.Call itself, it will push Iterator on estack.
arr, err := ExpandParameterToEmitable(Parameter{ emit.Opcodes(script.BinWriter, opcode.NEWARRAY0) // Push new empty array to estack. This array will store iterator's elements.
Type: ArrayType,
Value: params,
})
if err != nil {
return nil, fmt.Errorf("expanding params to emitable: %w", err)
}
emit.Array(script.BinWriter, arr.([]interface{})...)
emit.AppCallNoArgs(script.BinWriter, contract, operation, callflag.All) // The System.Contract.Call itself, it will push Iterator on estack.
emit.Opcodes(script.BinWriter, opcode.NEWARRAY0) // Push new empty array to estack. This array will store iterator's elements.
// Start the iterator traversal cycle. // Start the iterator traversal cycle.
iteratorTraverseCycleStartOffset := script.Len() iteratorTraverseCycleStartOffset := script.Len()