2022-07-21 19:39:53 +00:00
|
|
|
package rpcclient
|
2021-03-16 19:55:17 +00:00
|
|
|
|
2021-03-22 09:13:08 +00:00
|
|
|
// Various non-policy things from native contracts.
|
2021-03-16 19:55:17 +00:00
|
|
|
|
|
|
|
import (
|
2022-08-09 12:18:16 +00:00
|
|
|
"crypto/elliptic"
|
2021-03-24 10:12:33 +00:00
|
|
|
"errors"
|
2021-03-16 19:55:17 +00:00
|
|
|
"fmt"
|
|
|
|
|
2022-07-06 13:55:51 +00:00
|
|
|
"github.com/google/uuid"
|
2022-08-01 12:27:36 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config"
|
2021-03-16 19:55:17 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
|
2021-03-22 17:34:27 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native/noderoles"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2022-07-22 16:09:29 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neorpc/result"
|
2022-07-21 19:39:53 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/nns"
|
2022-08-09 12:18:16 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap"
|
2021-05-14 16:38:04 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2022-08-09 12:18:16 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2021-03-16 19:55:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetOraclePrice invokes `getPrice` method on a native Oracle contract.
|
2022-08-16 15:59:33 +00:00
|
|
|
//
|
|
|
|
// Deprecated: please use oracle subpackage.
|
2021-03-16 19:55:17 +00:00
|
|
|
func (c *Client) GetOraclePrice() (int64, error) {
|
2022-03-01 10:17:37 +00:00
|
|
|
oracleHash, err := c.GetNativeContractHash(nativenames.Oracle)
|
2021-03-16 19:55:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("failed to get native Oracle hash: %w", err)
|
|
|
|
}
|
|
|
|
return c.invokeNativeGetMethod(oracleHash, "getPrice")
|
|
|
|
}
|
2021-03-16 19:59:04 +00:00
|
|
|
|
2021-05-14 16:38:04 +00:00
|
|
|
// GetNNSPrice invokes `getPrice` method on a NeoNameService contract with the specified hash.
|
2022-08-26 16:45:37 +00:00
|
|
|
//
|
|
|
|
// Deprecated: please use nns subpackage. This method will be removed in future versions.
|
2021-05-14 16:38:04 +00:00
|
|
|
func (c *Client) GetNNSPrice(nnsHash util.Uint160) (int64, error) {
|
2021-03-16 19:59:04 +00:00
|
|
|
return c.invokeNativeGetMethod(nnsHash, "getPrice")
|
|
|
|
}
|
2021-03-16 20:03:51 +00:00
|
|
|
|
|
|
|
// GetGasPerBlock invokes `getGasPerBlock` method on a native NEO contract.
|
2022-08-17 18:55:30 +00:00
|
|
|
//
|
|
|
|
// Deprecated: please use neo subpackage. This method will be removed in future releases.
|
2021-03-16 20:03:51 +00:00
|
|
|
func (c *Client) GetGasPerBlock() (int64, error) {
|
2021-07-21 09:19:55 +00:00
|
|
|
return c.getFromNEO("getGasPerBlock")
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCandidateRegisterPrice invokes `getRegisterPrice` method on native NEO contract.
|
2022-08-17 18:55:30 +00:00
|
|
|
//
|
|
|
|
// Deprecated: please use neo subpackage. This method will be removed in future releases.
|
2021-07-21 09:19:55 +00:00
|
|
|
func (c *Client) GetCandidateRegisterPrice() (int64, error) {
|
|
|
|
return c.getFromNEO("getRegisterPrice")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) getFromNEO(meth string) (int64, error) {
|
2021-03-16 20:03:51 +00:00
|
|
|
neoHash, err := c.GetNativeContractHash(nativenames.Neo)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("failed to get native NEO hash: %w", err)
|
|
|
|
}
|
2021-07-21 09:19:55 +00:00
|
|
|
return c.invokeNativeGetMethod(neoHash, meth)
|
2021-03-16 20:03:51 +00:00
|
|
|
}
|
2021-03-22 17:34:27 +00:00
|
|
|
|
|
|
|
// GetDesignatedByRole invokes `getDesignatedByRole` method on a native RoleManagement contract.
|
2022-08-15 13:07:23 +00:00
|
|
|
//
|
|
|
|
// Deprecated: please use rolemgmt package.
|
2021-03-22 17:34:27 +00:00
|
|
|
func (c *Client) GetDesignatedByRole(role noderoles.Role, index uint32) (keys.PublicKeys, error) {
|
|
|
|
rmHash, err := c.GetNativeContractHash(nativenames.Designation)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get native RoleManagement hash: %w", err)
|
|
|
|
}
|
2022-08-09 12:18:16 +00:00
|
|
|
arr, err := unwrap.Array(c.reader.Call(rmHash, "getDesignatedByRole", int64(role), index))
|
2021-03-22 17:34:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-09 12:18:16 +00:00
|
|
|
pks := make(keys.PublicKeys, len(arr))
|
|
|
|
for i, item := range arr {
|
|
|
|
val, err := item.TryBytes()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid array element #%d: %s", i, item.Type())
|
|
|
|
}
|
|
|
|
pks[i], err = keys.NewPublicKeyFromBytes(val, elliptic.P256())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pks, nil
|
2021-03-22 17:34:27 +00:00
|
|
|
}
|
2021-03-24 10:12:33 +00:00
|
|
|
|
2021-05-14 16:38:04 +00:00
|
|
|
// NNSResolve invokes `resolve` method on a NameService contract with the specified hash.
|
2022-08-26 16:45:37 +00:00
|
|
|
//
|
|
|
|
// Deprecated: please use nns subpackage. This method will be removed in future versions.
|
2021-05-17 18:10:09 +00:00
|
|
|
func (c *Client) NNSResolve(nnsHash util.Uint160, name string, typ nns.RecordType) (string, error) {
|
|
|
|
if typ == nns.CNAME {
|
2021-03-24 10:12:33 +00:00
|
|
|
return "", errors.New("can't resolve CNAME record type")
|
|
|
|
}
|
2022-08-09 12:18:16 +00:00
|
|
|
return unwrap.UTF8String(c.reader.Call(nnsHash, "resolve", name, int64(typ)))
|
2021-03-24 10:12:33 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 16:38:04 +00:00
|
|
|
// NNSIsAvailable invokes `isAvailable` method on a NeoNameService contract with the specified hash.
|
2022-08-26 16:45:37 +00:00
|
|
|
//
|
|
|
|
// Deprecated: please use nns subpackage. This method will be removed in future versions.
|
2021-05-14 16:38:04 +00:00
|
|
|
func (c *Client) NNSIsAvailable(nnsHash util.Uint160, name string) (bool, error) {
|
2022-08-09 12:18:16 +00:00
|
|
|
return unwrap.Bool(c.reader.Call(nnsHash, "isAvailable", name))
|
2021-03-24 10:12:33 +00:00
|
|
|
}
|
2021-09-09 19:52:27 +00:00
|
|
|
|
2022-07-06 13:55:51 +00:00
|
|
|
// NNSGetAllRecords returns iterator over records for a given name from NNS service.
|
|
|
|
// First return value is the session ID, the second one is Iterator itself, the
|
|
|
|
// third one is an error. Use TraverseIterator method to traverse iterator values or
|
|
|
|
// TerminateSession to terminate opened iterator session. See TraverseIterator and
|
|
|
|
// TerminateSession documentation for more details.
|
2022-08-26 16:45:37 +00:00
|
|
|
//
|
|
|
|
// Deprecated: please use nns subpackage. This method will be removed in future versions.
|
2022-07-06 13:55:51 +00:00
|
|
|
func (c *Client) NNSGetAllRecords(nnsHash util.Uint160, name string) (uuid.UUID, result.Iterator, error) {
|
2022-08-09 12:18:16 +00:00
|
|
|
return unwrap.SessionIterator(c.reader.Call(nnsHash, "getAllRecords", name))
|
2022-07-06 13:55:51 +00:00
|
|
|
}
|
|
|
|
|
2022-07-06 15:15:17 +00:00
|
|
|
// NNSUnpackedGetAllRecords returns a set of records for a given name from NNS service
|
|
|
|
// (config.DefaultMaxIteratorResultItems at max). It differs from NNSGetAllRecords in
|
|
|
|
// that no iterator session is used to retrieve values from iterator. Instead, unpacking
|
|
|
|
// VM script is created and invoked via `invokescript` JSON-RPC call.
|
2022-08-26 16:45:37 +00:00
|
|
|
//
|
|
|
|
// Deprecated: please use nns subpackage. This method will be removed in future versions.
|
2022-07-06 13:55:51 +00:00
|
|
|
func (c *Client) NNSUnpackedGetAllRecords(nnsHash util.Uint160, name string) ([]nns.RecordState, error) {
|
2022-08-09 12:18:16 +00:00
|
|
|
arr, err := unwrap.Array(c.reader.CallAndExpandIterator(nnsHash, "getAllRecords", config.DefaultMaxIteratorResultItems, name))
|
2021-09-09 19:52:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-09 12:18:16 +00:00
|
|
|
res := make([]nns.RecordState, len(arr))
|
|
|
|
for i := range arr {
|
|
|
|
rs, ok := arr[i].Value().([]stackitem.Item)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("failed to decode RecordState from stackitem #%d: not a struct", i)
|
|
|
|
}
|
|
|
|
if len(rs) != 3 {
|
|
|
|
return nil, fmt.Errorf("failed to decode RecordState from stackitem #%d: wrong number of elements", i)
|
|
|
|
}
|
|
|
|
name, err := rs[0].TryBytes()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to decode RecordState from stackitem #%d: %w", i, err)
|
|
|
|
}
|
|
|
|
typ, err := rs[1].TryInteger()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to decode RecordState from stackitem #%d: %w", i, err)
|
|
|
|
}
|
|
|
|
data, err := rs[2].TryBytes()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to decode RecordState from stackitem #%d: %w", i, err)
|
|
|
|
}
|
|
|
|
u64Typ := typ.Uint64()
|
|
|
|
if !typ.IsUint64() || u64Typ > 255 {
|
|
|
|
return nil, fmt.Errorf("failed to decode RecordState from stackitem #%d: bad type", i)
|
|
|
|
}
|
|
|
|
res[i] = nns.RecordState{
|
|
|
|
Name: string(name),
|
|
|
|
Type: nns.RecordType(u64Typ),
|
|
|
|
Data: string(data),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res, nil
|
2021-09-09 19:52:27 +00:00
|
|
|
}
|
2022-03-01 10:10:54 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetNotaryServiceFeePerKey returns a reward per notary request key for the designated
|
2022-03-01 10:10:54 +00:00
|
|
|
// notary nodes. It doesn't cache the result.
|
2022-08-23 19:00:23 +00:00
|
|
|
//
|
|
|
|
// Deprecated: please use the Notary contract wrapper from the notary subpackage. This
|
|
|
|
// method will be removed in future versions.
|
2022-03-01 10:10:54 +00:00
|
|
|
func (c *Client) GetNotaryServiceFeePerKey() (int64, error) {
|
|
|
|
notaryHash, err := c.GetNativeContractHash(nativenames.Notary)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("failed to get native Notary hash: %w", err)
|
|
|
|
}
|
|
|
|
return c.invokeNativeGetMethod(notaryHash, "getNotaryServiceFeePerKey")
|
|
|
|
}
|