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) {
|
|
|
|
ctx := context.Background() // FIXME(@fyrchik): timeout context
|
|
|
|
endpoint := v.GetString(endpointFlag)
|
2021-10-18 12:20:04 +00:00
|
|
|
if endpoint == "" {
|
|
|
|
return nil, errors.New("missing endpoint")
|
|
|
|
}
|
2021-07-19 11:04:19 +00:00
|
|
|
c, err := client.New(ctx, endpoint, client.Options{})
|
|
|
|
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
|
|
|
|
}
|