rpc/client: unify invoke* functions

This commit is contained in:
Roman Khimov 2020-07-02 15:53:39 +03:00
parent d8677844fd
commit cc95d559cc

View file

@ -350,35 +350,25 @@ func (c *Client) GetVersion() (*result.Version, error) {
// InvokeScript returns the result of the given script after running it true the VM. // InvokeScript returns the result of the given script after running it true the VM.
// NOTE: This is a test invoke and will not affect the blockchain. // NOTE: This is a test invoke and will not affect the blockchain.
func (c *Client) InvokeScript(script string, cosigners []transaction.Cosigner) (*result.Invoke, error) { func (c *Client) InvokeScript(script string, cosigners []transaction.Cosigner) (*result.Invoke, error) {
var ( var p = request.NewRawParams(script)
params request.RawParams return c.invokeSomething("invokescript", p, cosigners)
resp = &result.Invoke{}
)
if cosigners != nil {
params = request.NewRawParams(script, cosigners)
} else {
params = request.NewRawParams(script)
}
if err := c.performRequest("invokescript", params, resp); err != nil {
return nil, err
}
return resp, nil
} }
// InvokeFunction returns the results after calling the smart contract scripthash // InvokeFunction returns the results after calling the smart contract scripthash
// with the given operation and parameters. // with the given operation and parameters.
// NOTE: this is test invoke and will not affect the blockchain. // NOTE: this is test invoke and will not affect the blockchain.
func (c *Client) InvokeFunction(script, operation string, params []smartcontract.Parameter, cosigners []transaction.Cosigner) (*result.Invoke, error) { func (c *Client) InvokeFunction(script, operation string, params []smartcontract.Parameter, cosigners []transaction.Cosigner) (*result.Invoke, error) {
var ( var p = request.NewRawParams(script, operation, params)
p request.RawParams return c.invokeSomething("invokefunction", p, cosigners)
resp = &result.Invoke{} }
)
// invokeSomething is an inner wrapper for Invoke* functions
func (c *Client) invokeSomething(method string, p request.RawParams, cosigners []transaction.Cosigner) (*result.Invoke, error) {
var resp = new(result.Invoke)
if cosigners != nil { if cosigners != nil {
p = request.NewRawParams(script, operation, params, cosigners) p.Values = append(p.Values, cosigners)
} else {
p = request.NewRawParams(script, operation, params)
} }
if err := c.performRequest("invokefunction", p, resp); err != nil { if err := c.performRequest(method, p, resp); err != nil {
return nil, err return nil, err
} }
return resp, nil return resp, nil