fix payer key

This commit is contained in:
Julien Cassis 2020-11-26 17:15:40 -05:00
parent 8d95c0fd72
commit b893a1c31a
2 changed files with 26 additions and 22 deletions

View File

@ -26,25 +26,25 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var tokenRegisterCmd = &cobra.Command{ var tokenRegistryRegisterCmd = &cobra.Command{
Use: "token register {token} {name} {symbol} {logo}", Use: "register {token} {name} {symbol} {logo}",
Short: "register meta data for a token", Short: "register meta data for a token",
Args: cobra.ExactArgs(5), Args: cobra.ExactArgs(4),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
vault := mustGetWallet() vault := mustGetWallet()
client := getClient() client := getClient()
tokenAddress, err := solana.PublicKeyFromBase58(args[1]) tokenAddress, err := solana.PublicKeyFromBase58(args[0])
errorCheck("invalid token address", err) errorCheck("invalid token address", err)
logo, err := tokenregistry.LogoFromString(args[2]) logo, err := tokenregistry.LogoFromString(args[1])
errorCheck("invalid logo", err) errorCheck("invalid logo", err)
name, err := tokenregistry.NameFromString(args[3]) name, err := tokenregistry.NameFromString(args[2])
errorCheck("invalid name", err) errorCheck("invalid name", err)
symbol, err := tokenregistry.SymbolFromString(args[4]) symbol, err := tokenregistry.SymbolFromString(args[3])
errorCheck("invalid symbol", err) errorCheck("invalid symbol", err)
pkeyStr := viper.GetString("token-regiser-cmd-registrar") pkeyStr := viper.GetString("token-registry-register-cmd-registrar")
if pkeyStr == "" { if pkeyStr == "" {
fmt.Errorf("unable to continue without a specified registrar") fmt.Errorf("unable to continue without a specified registrar")
} }
@ -62,6 +62,8 @@ var tokenRegisterCmd = &cobra.Command{
return fmt.Errorf("registrar key must be present in the vault to register a token") return fmt.Errorf("registrar key must be present in the vault to register a token")
} }
fmt.Println(registrarPubKey.String())
tokenMetaAccount := solana.NewAccount() tokenMetaAccount := solana.NewAccount()
lamport, err := client.GetMinimumBalanceForRentExemption(context.Background(), tokenregistry.TOKEN_META_SIZE) lamport, err := client.GetMinimumBalanceForRentExemption(context.Background(), tokenregistry.TOKEN_META_SIZE)
@ -72,7 +74,9 @@ var tokenRegisterCmd = &cobra.Command{
createAccountInstruction := system.NewCreateAccountInstruction(uint64(lamport), tokenregistry.TOKEN_META_SIZE, tokenRegistryProgramID, registrarPubKey, tokenMetaAccount.PublicKey()) createAccountInstruction := system.NewCreateAccountInstruction(uint64(lamport), tokenregistry.TOKEN_META_SIZE, tokenRegistryProgramID, registrarPubKey, tokenMetaAccount.PublicKey())
registerTokenInstruction := tokenregistry.NewRegisterTokenInstruction(logo, name, symbol, tokenMetaAccount.PublicKey(), registrarPubKey, tokenAddress) registerTokenInstruction := tokenregistry.NewRegisterTokenInstruction(logo, name, symbol, tokenMetaAccount.PublicKey(), registrarPubKey, tokenAddress)
trx, err := solana.TransactionWithInstructions([]solana.TransactionInstruction{createAccountInstruction, registerTokenInstruction}, nil) trx, err := solana.TransactionWithInstructions([]solana.TransactionInstruction{createAccountInstruction, registerTokenInstruction}, &solana.Options{
Payer: registrarPubKey,
})
errorCheck("unable to craft transaction", err) errorCheck("unable to craft transaction", err)
_, err = trx.Sign(func(key solana.PublicKey) *solana.PrivateKey { _, err = trx.Sign(func(key solana.PublicKey) *solana.PrivateKey {
@ -94,6 +98,6 @@ var tokenRegisterCmd = &cobra.Command{
} }
func init() { func init() {
tokenCmd.AddCommand(tokenRegisterCmd) tokenRegistryCmd.AddCommand(tokenRegistryRegisterCmd)
tokenRegisterCmd.PersistentFlags().String("registrar", "9hFtYBYmBJCVguRYs9pBTWKYAFoKfjYR7zBPpEkVsmD", "The public key that will register the token") tokenRegistryRegisterCmd.PersistentFlags().String("registrar", "9hFtYBYmBJCVguRYs9pBTWKYAFoKfjYR7zBPpEkVsmD", "The public key that will register the token")
} }

View File

@ -17,7 +17,7 @@ type TransactionInstruction interface {
} }
type Options struct { type Options struct {
payer *PublicKey Payer PublicKey
} }
func TransactionWithInstructions(instructions []TransactionInstruction, opt *Options) (*Transaction, error) { func TransactionWithInstructions(instructions []TransactionInstruction, opt *Options) (*Transaction, error) {
@ -25,22 +25,22 @@ func TransactionWithInstructions(instructions []TransactionInstruction, opt *Opt
return nil, fmt.Errorf("requires at-least one instruction to create a transaction") return nil, fmt.Errorf("requires at-least one instruction to create a transaction")
} }
var feePayer PublicKey
if opt == nil { if opt == nil {
opt = &Options{} found := false
}
feePayer := opt.payer
if feePayer == nil {
for _, act := range instructions[0].Accounts() { for _, act := range instructions[0].Accounts() {
if act.IsSigner { if act.IsSigner {
feePayer = &act.PublicKey feePayer = act.PublicKey
found = true
break break
} }
} }
} if !found {
if feePayer == nil {
return nil, fmt.Errorf("cannot determine fee payer. You can ether pass the fee payer vai the 'TransactionWithInstructions' option parameter or it fallback to the first instruction's first signer") return nil, fmt.Errorf("cannot determine fee payer. You can ether pass the fee payer vai the 'TransactionWithInstructions' option parameter or it fallback to the first instruction's first signer")
} }
} else {
feePayer = opt.Payer
}
programIDs := map[string]bool{} programIDs := map[string]bool{}
accounts := []*AccountMeta{} accounts := []*AccountMeta{}
@ -80,7 +80,7 @@ func TransactionWithInstructions(instructions []TransactionInstruction, opt *Opt
// Move fee payer to the front // Move fee payer to the front
feePayerIndex := -1 feePayerIndex := -1
for idx, acc := range uniqAccounts { for idx, acc := range uniqAccounts {
if acc.PublicKey.Equals(*feePayer) { if acc.PublicKey.Equals(feePayer) {
feePayerIndex = idx feePayerIndex = idx
} }
} }