2020-07-24 13:54:03 +00:00
|
|
|
package client
|
2020-07-10 14:17:51 +00:00
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
2020-10-28 12:59:40 +00:00
|
|
|
"math/big"
|
2020-07-10 14:17:51 +00:00
|
|
|
|
2022-08-29 15:32:54 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neorpc/result"
|
2020-08-27 10:54:26 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-07-10 14:17:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
Use these function to parse stack parameters obtained from `TestInvoke`
|
|
|
|
function to native go types. You should know upfront return types of invoked
|
|
|
|
method.
|
|
|
|
*/
|
|
|
|
|
2020-08-27 10:54:26 +00:00
|
|
|
// BoolFromStackItem receives boolean value from the value of a smart contract parameter.
|
|
|
|
func BoolFromStackItem(param stackitem.Item) (bool, error) {
|
|
|
|
switch param.Type() {
|
|
|
|
case stackitem.BooleanT, stackitem.IntegerT, stackitem.ByteArrayT:
|
2020-09-01 11:12:34 +00:00
|
|
|
return param.TryBool()
|
2020-08-27 10:54:26 +00:00
|
|
|
default:
|
2021-05-18 08:12:51 +00:00
|
|
|
return false, fmt.Errorf("chain/client: %s is not a bool type", param.Type())
|
2020-08-27 10:54:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IntFromStackItem receives numerical value from the value of a smart contract parameter.
|
|
|
|
func IntFromStackItem(param stackitem.Item) (int64, error) {
|
|
|
|
switch param.Type() {
|
|
|
|
case stackitem.IntegerT, stackitem.ByteArrayT:
|
|
|
|
i, err := param.TryInteger()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return i.Int64(), nil
|
|
|
|
default:
|
2021-05-18 08:12:51 +00:00
|
|
|
return 0, fmt.Errorf("chain/client: %s is not an integer type", param.Type())
|
2020-08-27 10:54:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 12:59:40 +00:00
|
|
|
// BigIntFromStackItem receives numerical value from the value of a smart contract parameter.
|
|
|
|
func BigIntFromStackItem(param stackitem.Item) (*big.Int, error) {
|
|
|
|
return param.TryInteger()
|
|
|
|
}
|
|
|
|
|
2020-08-27 10:54:26 +00:00
|
|
|
// BytesFromStackItem receives binary value from the value of a smart contract parameter.
|
|
|
|
func BytesFromStackItem(param stackitem.Item) ([]byte, error) {
|
2020-09-02 16:24:46 +00:00
|
|
|
switch param.Type() {
|
|
|
|
case stackitem.BufferT, stackitem.ByteArrayT:
|
|
|
|
return param.TryBytes()
|
2021-04-15 13:23:12 +00:00
|
|
|
case stackitem.IntegerT:
|
|
|
|
n, err := param.TryInteger()
|
|
|
|
if err != nil {
|
2024-12-12 12:06:20 +00:00
|
|
|
return nil, fmt.Errorf("parse integer bytes: %w", err)
|
2021-04-15 13:23:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return n.Bytes(), nil
|
2020-09-02 16:24:46 +00:00
|
|
|
case stackitem.AnyT:
|
|
|
|
if param.Value() == nil {
|
2020-08-27 10:54:26 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2020-09-02 16:24:46 +00:00
|
|
|
fallthrough
|
|
|
|
default:
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("chain/client: %s is not a byte array type", param.Type())
|
2020-08-27 10:54:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ArrayFromStackItem returns the slice contract parameters from passed parameter.
|
|
|
|
//
|
2024-07-12 14:57:11 +00:00
|
|
|
// If passed parameter carries boolean false value, returns (nil, nil).
|
2020-08-27 10:54:26 +00:00
|
|
|
func ArrayFromStackItem(param stackitem.Item) ([]stackitem.Item, error) {
|
|
|
|
switch param.Type() {
|
|
|
|
case stackitem.AnyT:
|
|
|
|
return nil, nil
|
2020-08-31 15:17:31 +00:00
|
|
|
case stackitem.ArrayT, stackitem.StructT:
|
2020-08-27 10:54:26 +00:00
|
|
|
items, ok := param.Value().([]stackitem.Item)
|
|
|
|
if !ok {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("chain/client: can't convert %T to parameter slice", param.Value())
|
2020-08-27 10:54:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return items, nil
|
|
|
|
default:
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("chain/client: %s is not an array type", param.Type())
|
2020-08-27 10:54:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// StringFromStackItem receives string value from the value of a smart contract parameter.
|
|
|
|
func StringFromStackItem(param stackitem.Item) (string, error) {
|
|
|
|
if param.Type() != stackitem.ByteArrayT {
|
2021-05-18 08:12:51 +00:00
|
|
|
return "", fmt.Errorf("chain/client: %s is not an string type", param.Type())
|
2020-08-27 10:54:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return stackitem.ToString(param)
|
|
|
|
}
|
2022-08-29 15:32:54 +00:00
|
|
|
|
|
|
|
func addFeeCheckerModifier(add int64) func(r *result.Invoke, t *transaction.Transaction) error {
|
|
|
|
return func(r *result.Invoke, t *transaction.Transaction) error {
|
|
|
|
if r.State != HaltState {
|
2023-01-10 13:07:47 +00:00
|
|
|
return wrapFrostFSError(¬HaltStateError{state: r.State, exception: r.FaultException})
|
2022-08-29 15:32:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.SystemFee += add
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|