github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/stellar/options.go (about)

     1  package stellar
     2  
     3  import (
     4  	"sync"
     5  	"time"
     6  
     7  	"github.com/keybase/client/go/libkb"
     8  	"github.com/keybase/client/go/protocol/stellar1"
     9  	"github.com/keybase/client/go/stellar/remote"
    10  	"github.com/stellar/go/build"
    11  )
    12  
    13  // Options holds dynamic options for clients to use when preparing
    14  // stellar transactions.
    15  type Options struct {
    16  	network stellar1.NetworkOptions
    17  	mtime   time.Time
    18  	sync.Mutex
    19  }
    20  
    21  func NewOptions() *Options {
    22  	return &Options{}
    23  }
    24  
    25  // NetworkOptions returns stellar1.NetworkOptions that is less than 1 hour old.
    26  func (o *Options) NetworkOptions(mctx libkb.MetaContext, r remote.Remoter) stellar1.NetworkOptions {
    27  	o.Lock()
    28  	defer o.Unlock()
    29  
    30  	if time.Since(o.mtime) < 1*time.Hour {
    31  		return o.network
    32  	}
    33  
    34  	options, err := r.NetworkOptions(mctx.Ctx())
    35  	if err != nil {
    36  		mctx.Debug("error calling NetworkOptions: %s", err)
    37  	} else {
    38  		mctx.Debug("updating NetworkOptions: %+v", options)
    39  		o.network = options
    40  		o.mtime = time.Now()
    41  	}
    42  
    43  	return o.network
    44  }
    45  
    46  func (o *Options) BaseFee(mctx libkb.MetaContext, r remote.Remoter) uint64 {
    47  	options := o.NetworkOptions(mctx, r)
    48  	if options.BaseFee < build.DefaultBaseFee {
    49  		return build.DefaultBaseFee
    50  	}
    51  	return options.BaseFee
    52  }