2021-07-19 11:04:19 +00:00
|
|
|
package morph
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-10-18 12:20:04 +00:00
|
|
|
"errors"
|
2021-11-30 11:45:40 +00:00
|
|
|
"time"
|
2021-07-19 11:04:19 +00:00
|
|
|
|
2021-11-30 11:45:40 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2021-07-19 11:04:19 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpc/client"
|
2021-11-30 11:45:40 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/spf13/cobra"
|
2021-07-19 11:04:19 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
2021-11-30 11:45:40 +00:00
|
|
|
type clientContext struct {
|
|
|
|
Client *client.Client
|
|
|
|
Hashes []util.Uint256
|
|
|
|
WaitDuration time.Duration
|
|
|
|
PollInterval time.Duration
|
|
|
|
}
|
|
|
|
|
2021-07-19 11:04:19 +00:00
|
|
|
func getN3Client(v *viper.Viper) (*client.Client, error) {
|
2021-12-15 18:05:21 +00:00
|
|
|
// number of opened connections
|
|
|
|
// by neo-go client per one host
|
2022-02-07 13:58:56 +00:00
|
|
|
const (
|
|
|
|
maxConnsPerHost = 10
|
|
|
|
requestTimeout = time.Second * 10
|
|
|
|
)
|
2021-12-15 18:05:21 +00:00
|
|
|
|
2022-02-07 13:58:56 +00:00
|
|
|
ctx := context.Background()
|
2021-07-19 11:04:19 +00:00
|
|
|
endpoint := v.GetString(endpointFlag)
|
2021-10-18 12:20:04 +00:00
|
|
|
if endpoint == "" {
|
|
|
|
return nil, errors.New("missing endpoint")
|
|
|
|
}
|
2022-02-07 13:58:56 +00:00
|
|
|
c, err := client.New(ctx, endpoint, client.Options{
|
|
|
|
MaxConnsPerHost: maxConnsPerHost,
|
|
|
|
RequestTimeout: requestTimeout,
|
|
|
|
})
|
2021-07-19 11:04:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := c.Init(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
2021-11-30 11:45:40 +00:00
|
|
|
|
2021-11-30 11:57:58 +00:00
|
|
|
func defaultClientContext(c *client.Client) *clientContext {
|
|
|
|
return &clientContext{
|
|
|
|
Client: c,
|
|
|
|
WaitDuration: time.Second * 30,
|
|
|
|
PollInterval: time.Second,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-30 11:45:40 +00:00
|
|
|
func (c *clientContext) sendTx(tx *transaction.Transaction, cmd *cobra.Command, await bool) error {
|
|
|
|
h, err := c.Client.SendRawTransaction(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Hashes = append(c.Hashes, h)
|
|
|
|
|
|
|
|
if await {
|
|
|
|
return c.awaitTx(cmd)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|