Skip to content

Commit

Permalink
Added listing holders for a given mint (token list holders)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandre Bourget committed Nov 30, 2021
1 parent d8a67f2 commit f05d17b
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
79 changes: 79 additions & 0 deletions cmd/slnc/cmd/token_list_holders.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2020 dfuse Platform Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"fmt"
"strings"

"github.com/spf13/viper"

"github.com/streamingfast/solana-go"

"github.com/ryanuber/columnize"
"github.com/spf13/cobra"
"github.com/streamingfast/solana-go/programs/token"
)

var tokenListHoldersCmd = &cobra.Command{
Use: "holders {mint}",
Short: "Lists token holders for a given mint",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
rpcCli := getClient()
toCSV := viper.GetBool("token-list-holders-cmd-to-csv")

ownerAddr, err := solana.PublicKeyFromBase58(args[0])
if err != nil {
return fmt.Errorf("decoding owner addr: %w", err)
}

accounts, err := token.FetchAccountHolders(cmd.Context(), rpcCli, ownerAddr)
if err != nil {
return fmt.Errorf("unable to retrieve mints: %w", err)
}

if toCSV {
fmt.Println("address,mint,owner,amount")
for _, a := range accounts {
line := []string{
fmt.Sprintf("%s", a.Key.String()),
fmt.Sprintf("%s", a.Mint.String()),
fmt.Sprintf("%s", a.Owner.String()),
fmt.Sprintf("%d", a.Amount),
}
fmt.Println(strings.Join(line, ","))
}
} else {
out := []string{"Address | Mint | Owner | Amount"}
for _, a := range accounts {
line := []string{
fmt.Sprintf("%s", a.Key.String()),
fmt.Sprintf("%s", a.Mint.String()),
fmt.Sprintf("%s", a.Owner.String()),
fmt.Sprintf("%d", a.Amount),
}
out = append(out, strings.Join(line, " | "))
}
fmt.Println(columnize.Format(out, nil))
}
return nil
},
}

func init() {
tokenListHoldersCmd.Flags().Bool("to-csv", false, "outputs the data in csv format")
tokenListCmd.AddCommand(tokenListHoldersCmd)
}
31 changes: 31 additions & 0 deletions programs/token/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,37 @@ func FetchAccountsForOwner(ctx context.Context, rpcCli *rpc.Client, owner solana
return
}

func FetchAccountHolders(ctx context.Context, rpcCli *rpc.Client, mint solana.PublicKey) (out []*Account, err error) {
resp, err := rpcCli.GetProgramAccounts(
ctx,
PROGRAM_ID,
&rpc.GetProgramAccountsOpts{
Filters: []rpc.RPCFilter{
{DataSize: ACCOUNT_SIZE},
{Memcmp: &rpc.RPCFilterMemcmp{Offset: 0, Bytes: mint[:]}},
},
},
)
if err != nil {
return nil, err
}
if resp == nil {
return nil, fmt.Errorf("resp empty... program account not found")
}

for _, keyedAcct := range resp {
acct := keyedAcct.Account

a := &Account{}
if err := a.Decode(keyedAcct.Pubkey, acct.Data); err != nil {
return nil, fmt.Errorf("unable to decode mint %q: %w", acct.Owner.String(), err)
}
out = append(out, a)

}
return
}

func TransferToken(ctx context.Context, rpcCli *rpc.Client, wsCli *ws.Client, amount uint64, senderSPLTokenAccount, mint, recipient solana.PublicKey, sender *solana.Account) (solana.PublicKey, string, error) {
blockHashResult, err := rpcCli.GetRecentBlockhash(ctx, rpc.CommitmentFinalized)
if err != nil {
Expand Down

0 comments on commit f05d17b

Please sign in to comment.