github.com/mavryk-network/mvgo@v1.19.9/internal/compose/alpha/task/common.go (about)

     1  // Copyright (c) 2023 Blockwatch Data Inc.
     2  // Author: alex@blockwatch.cc, abdul@blockwatch.cc
     3  
     4  package task
     5  
     6  import (
     7  	"github.com/mavryk-network/mvgo/internal/compose"
     8  	"github.com/mavryk-network/mvgo/internal/compose/alpha"
     9  	"github.com/mavryk-network/mvgo/mavryk"
    10  
    11  	"github.com/pkg/errors"
    12  )
    13  
    14  type BaseTask struct {
    15  	Source mavryk.Address
    16  	Key    mavryk.PrivateKey
    17  }
    18  
    19  func (t *BaseTask) parse(ctx compose.Context, task alpha.Task) (err error) {
    20  	if task.Source != "" {
    21  		if t.Source, err = ctx.ResolveAddress(task.Source); err != nil {
    22  			err = errors.Wrap(err, "source")
    23  			return
    24  		}
    25  		if t.Key, err = ctx.ResolvePrivateKey(task.Source); err != nil {
    26  			err = errors.Wrap(err, "key")
    27  			return
    28  		}
    29  	} else {
    30  		t.Source = ctx.BaseAccount.Address
    31  		t.Key = ctx.BaseAccount.PrivateKey
    32  	}
    33  	return
    34  }
    35  
    36  type TargetTask struct {
    37  	BaseTask
    38  	Destination mavryk.Address
    39  }
    40  
    41  func (t *TargetTask) parse(ctx compose.Context, task alpha.Task) (err error) {
    42  	if err = t.BaseTask.parse(ctx, task); err != nil {
    43  		return
    44  	}
    45  	if t.Destination, err = ctx.ResolveAddress(task.Destination); err != nil {
    46  		err = errors.Wrap(err, "destination")
    47  		return
    48  	}
    49  	return
    50  }