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"
)
var tokenRegisterCmd = &cobra.Command{
Use: "token register {token} {name} {symbol} {logo}",
var tokenRegistryRegisterCmd = &cobra.Command{
Use: "register {token} {name} {symbol} {logo}",
Short: "register meta data for a token",
Args: cobra.ExactArgs(5),
Args: cobra.ExactArgs(4),
RunE: func(cmd *cobra.Command, args []string) error {
vault := mustGetWallet()
client := getClient()
tokenAddress, err := solana.PublicKeyFromBase58(args[1])
tokenAddress, err := solana.PublicKeyFromBase58(args[0])
errorCheck("invalid token address", err)
logo, err := tokenregistry.LogoFromString(args[2])
logo, err := tokenregistry.LogoFromString(args[1])
errorCheck("invalid logo", err)
name, err := tokenregistry.NameFromString(args[3])
name, err := tokenregistry.NameFromString(args[2])
errorCheck("invalid name", err)
symbol, err := tokenregistry.SymbolFromString(args[4])
symbol, err := tokenregistry.SymbolFromString(args[3])
errorCheck("invalid symbol", err)
pkeyStr := viper.GetString("token-regiser-cmd-registrar")
pkeyStr := viper.GetString("token-registry-register-cmd-registrar")
if pkeyStr == "" {
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")
}
fmt.Println(registrarPubKey.String())
tokenMetaAccount := solana.NewAccount()
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())
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)
_, err = trx.Sign(func(key solana.PublicKey) *solana.PrivateKey {
@ -94,6 +98,6 @@ var tokenRegisterCmd = &cobra.Command{
}
func init() {
tokenCmd.AddCommand(tokenRegisterCmd)
tokenRegisterCmd.PersistentFlags().String("registrar", "9hFtYBYmBJCVguRYs9pBTWKYAFoKfjYR7zBPpEkVsmD", "The public key that will register the token")
tokenRegistryCmd.AddCommand(tokenRegistryRegisterCmd)
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 {
payer *PublicKey
Payer PublicKey
}
func TransactionWithInstructions(instructions []TransactionInstruction, opt *Options) (*Transaction, error) {
@ -25,21 +25,21 @@ func TransactionWithInstructions(instructions []TransactionInstruction, opt *Opt
return nil, fmt.Errorf("requires at-least one instruction to create a transaction")
}
var feePayer PublicKey
if opt == nil {
opt = &Options{}
}
feePayer := opt.payer
if feePayer == nil {
found := false
for _, act := range instructions[0].Accounts() {
if act.IsSigner {
feePayer = &act.PublicKey
feePayer = act.PublicKey
found = true
break
}
}
}
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")
if !found {
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{}
@ -80,7 +80,7 @@ func TransactionWithInstructions(instructions []TransactionInstruction, opt *Opt
// Move fee payer to the front
feePayerIndex := -1
for idx, acc := range uniqAccounts {
if acc.PublicKey.Equals(*feePayer) {
if acc.PublicKey.Equals(feePayer) {
feePayerIndex = idx
}
}