|
| 1 | +package dataset |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/duneanalytics/cli/cmdutil" |
| 8 | + "github.com/duneanalytics/cli/output" |
| 9 | + "github.com/duneanalytics/duneapi-client-go/models" |
| 10 | + "github.com/spf13/cobra" |
| 11 | +) |
| 12 | + |
| 13 | +func newSearchByContractCmd() *cobra.Command { |
| 14 | + cmd := &cobra.Command{ |
| 15 | + Use: "search-by-contract", |
| 16 | + Short: "Search for decoded tables associated with a specific contract address", |
| 17 | + Long: `Search for decoded tables (events and calls) associated with a specific contract address. |
| 18 | +
|
| 19 | +This command finds all available decoded tables for a given smart contract address. |
| 20 | +Decoded tables contain blockchain data that has been parsed according to a contract's ABI, |
| 21 | +providing a structured view of contract interactions. |
| 22 | +
|
| 23 | +Table types returned: |
| 24 | + - Event tables: contain parsed event logs emitted by smart contracts |
| 25 | + - Call tables: contain parsed function calls made to smart contracts |
| 26 | +
|
| 27 | +When to use this command: |
| 28 | + - You have a specific contract address (EVM or Tron) and want to find its decoded tables |
| 29 | + - You want to analyze on-chain activity for a particular smart contract |
| 30 | + - You need to find event or call tables for a known contract |
| 31 | +
|
| 32 | +For broader table discovery by keyword or project name, use 'dune dataset search' instead. |
| 33 | +
|
| 34 | +Examples: |
| 35 | + dune dataset search-by-contract --contract-address 0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984 |
| 36 | + dune dataset search-by-contract --contract-address 0x1f98... --blockchains ethereum --blockchains arbitrum |
| 37 | + dune dataset search-by-contract --contract-address 0x1f98... --include-schema |
| 38 | + dune dataset search-by-contract --contract-address 0x1f98... --limit 50 --offset 20`, |
| 39 | + RunE: runSearchByContract, |
| 40 | + } |
| 41 | + |
| 42 | + cmd.Flags().String("contract-address", "", |
| 43 | + "The contract address to search for. Accepts EVM addresses (starting with 0x) or Tron addresses. "+ |
| 44 | + "Example: '0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984'.") |
| 45 | + _ = cmd.MarkFlagRequired("contract-address") |
| 46 | + cmd.Flags().StringArray("blockchains", nil, |
| 47 | + "Filter results to specific blockchains (e.g. ethereum, arbitrum). "+ |
| 48 | + "Can be specified multiple times. If not provided, searches across all blockchains where the contract exists.") |
| 49 | + cmd.Flags().Bool("include-schema", false, |
| 50 | + "If set, include the column-level schema (name, type, nullable) for every result. "+ |
| 51 | + "Enable when preparing SQL generation.") |
| 52 | + cmd.Flags().Int32("limit", 20, |
| 53 | + "Maximum number of results to return. "+ |
| 54 | + "Use smaller values (5-10) for quick checks, larger values (50-100) for comprehensive discovery.") |
| 55 | + cmd.Flags().Int32("offset", 0, |
| 56 | + "Number of results to skip for pagination. Use for paginating through large result sets.") |
| 57 | + output.AddFormatFlag(cmd, "text") |
| 58 | + |
| 59 | + return cmd |
| 60 | +} |
| 61 | + |
| 62 | +func runSearchByContract(cmd *cobra.Command, _ []string) error { |
| 63 | + client := cmdutil.ClientFromCmd(cmd) |
| 64 | + |
| 65 | + contractAddress, _ := cmd.Flags().GetString("contract-address") |
| 66 | + |
| 67 | + req := models.SearchDatasetsByContractAddressRequest{ |
| 68 | + ContractAddress: contractAddress, |
| 69 | + } |
| 70 | + |
| 71 | + if cmd.Flags().Changed("blockchains") { |
| 72 | + v, _ := cmd.Flags().GetStringArray("blockchains") |
| 73 | + req.Blockchains = v |
| 74 | + } |
| 75 | + if cmd.Flags().Changed("include-schema") { |
| 76 | + v, _ := cmd.Flags().GetBool("include-schema") |
| 77 | + req.IncludeSchema = &v |
| 78 | + } |
| 79 | + if cmd.Flags().Changed("limit") { |
| 80 | + v, _ := cmd.Flags().GetInt32("limit") |
| 81 | + req.Limit = &v |
| 82 | + } |
| 83 | + if cmd.Flags().Changed("offset") { |
| 84 | + v, _ := cmd.Flags().GetInt32("offset") |
| 85 | + req.Offset = &v |
| 86 | + } |
| 87 | + |
| 88 | + resp, err := client.SearchDatasetsByContractAddress(req) |
| 89 | + if err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + |
| 93 | + w := cmd.OutOrStdout() |
| 94 | + switch output.FormatFromCmd(cmd) { |
| 95 | + case output.FormatJSON: |
| 96 | + return output.PrintJSON(w, resp) |
| 97 | + default: |
| 98 | + columns := []string{"FULL_NAME", "CATEGORY", "DATASET_TYPE", "BLOCKCHAINS"} |
| 99 | + rows := make([][]string, len(resp.Results)) |
| 100 | + for i, r := range resp.Results { |
| 101 | + dt := "" |
| 102 | + if r.DatasetType != nil { |
| 103 | + dt = *r.DatasetType |
| 104 | + } |
| 105 | + rows[i] = []string{ |
| 106 | + r.FullName, |
| 107 | + r.Category, |
| 108 | + dt, |
| 109 | + strings.Join(r.Blockchains, ", "), |
| 110 | + } |
| 111 | + } |
| 112 | + output.PrintTable(w, columns, rows) |
| 113 | + fmt.Fprintf(w, "\n%d of %d results\n", len(resp.Results), resp.Total) |
| 114 | + return nil |
| 115 | + } |
| 116 | +} |
0 commit comments