github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/stellar/inflation.go (about)

     1  package stellar
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/keybase/client/go/libkb"
     7  	"github.com/keybase/client/go/protocol/stellar1"
     8  	"github.com/keybase/stellarnet"
     9  )
    10  
    11  func GetPredefinedInflationDestinations(mctx libkb.MetaContext) (ret []stellar1.PredefinedInflationDestination, err error) {
    12  	return getGlobal(mctx.G()).walletState.GetInflationDestinations(mctx.Ctx())
    13  }
    14  
    15  func SetInflationDestinationLocal(mctx libkb.MetaContext, arg stellar1.SetInflationDestinationLocalArg) (err error) {
    16  	defer mctx.Trace(
    17  		fmt.Sprintf("Stellar.SetInflationDestinationLocal(on=%s,to=%s)", arg.AccountID, arg.Destination),
    18  		&err)()
    19  
    20  	walletState := getGlobal(mctx.G()).walletState
    21  
    22  	// look up sender account
    23  	senderEntry, senderAccountBundle, err := LookupSender(mctx, arg.AccountID)
    24  	if err != nil {
    25  		return err
    26  	}
    27  	senderSeed := senderAccountBundle.Signers[0]
    28  	senderSeed2, err := stellarnet.NewSeedStr(senderSeed.SecureNoLogString())
    29  	if err != nil {
    30  		return err
    31  	}
    32  
    33  	destinationAddrStr, err := stellarnet.NewAddressStr(arg.Destination.String())
    34  	if err != nil {
    35  		return fmt.Errorf("Malformed destination account ID: %s", err)
    36  	}
    37  
    38  	sp, unlock := NewSeqnoProvider(mctx, walletState)
    39  	defer unlock()
    40  
    41  	tb, err := getTimeboundsForSending(mctx, walletState)
    42  	if err != nil {
    43  		return err
    44  	}
    45  
    46  	baseFee := walletState.BaseFee(mctx)
    47  
    48  	sig, err := stellarnet.SetInflationDestinationTransaction(senderSeed2, destinationAddrStr, sp, tb, baseFee)
    49  	if err != nil {
    50  		return err
    51  	}
    52  	err = walletState.SetInflationDestination(mctx.Ctx(), sig.Signed)
    53  	if err != nil {
    54  		return err
    55  	}
    56  
    57  	// force load this transaction before refreshing the wallet state
    58  	loader := DefaultLoader(mctx.G())
    59  	loader.LoadPaymentSync(mctx.Ctx(), stellar1.PaymentID(sig.TxHash))
    60  
    61  	err = walletState.Refresh(mctx, senderEntry.AccountID, "set inflation destination")
    62  	if err != nil {
    63  		mctx.Debug("SetInflationDestinationLocal ws.Refresh error: %s", err)
    64  	}
    65  
    66  	return nil
    67  }
    68  
    69  func GetInflationDestination(mctx libkb.MetaContext, accountID stellar1.AccountID) (res stellar1.InflationDestinationResultLocal, err error) {
    70  	defer mctx.Trace("Stellar.GetInflationDestination", &err)()
    71  
    72  	walletState := getGlobal(mctx.G()).walletState
    73  	details, err := walletState.Details(mctx.Ctx(), accountID)
    74  	if err != nil {
    75  		return res, err
    76  	}
    77  
    78  	dest := details.InflationDestination
    79  	if dest == nil {
    80  		// Inflation destination is not set on the account.
    81  		res.Destination = nil
    82  		return res, nil
    83  	}
    84  
    85  	res.Destination = dest
    86  	if dest.Eq(accountID) {
    87  		res.Self = true
    88  	} else {
    89  		destinations, err := GetPredefinedInflationDestinations(mctx)
    90  		if err != nil {
    91  			return res, err
    92  		}
    93  		for _, known := range destinations {
    94  			if dest.Eq(known.AccountID) {
    95  				res.KnownDestination = &known
    96  				break
    97  			}
    98  		}
    99  	}
   100  
   101  	return res, nil
   102  }