|
| 1 | +import { Protocol } from "@src/solana/enums"; |
| 2 | +import { releasesQueries } from "@src/solana/releases/queries"; |
| 3 | +import type { Sablier } from "@src/types"; |
| 4 | +import _ from "lodash"; |
| 5 | +import { catalog } from "./catalog"; |
| 6 | + |
| 7 | +export const contractsQueries = { |
| 8 | + /** |
| 9 | + * Get a single contract using the following options: |
| 10 | + * |
| 11 | + * - { chainId, contractName, release } |
| 12 | + * - { chainId, contractAddress, protocol } |
| 13 | + * - { chainId, contractAddress, release } |
| 14 | + * - { chainId, contractAddress, protocol, release } |
| 15 | + * |
| 16 | + * Note: If a contract address exists in multiple releases for the same protocol, you must specify the release. |
| 17 | + */ |
| 18 | + get: (opts: { |
| 19 | + chainId: number; |
| 20 | + contractAddress?: string; |
| 21 | + contractName?: string; |
| 22 | + protocol?: Sablier.Solana.Protocol; |
| 23 | + release?: Sablier.Solana.Release; |
| 24 | + }): Sablier.Solana.Contract | undefined => { |
| 25 | + const { chainId, contractAddress, contractName, protocol, release } = opts; |
| 26 | + |
| 27 | + // Validation |
| 28 | + if (contractAddress && contractName) { |
| 29 | + throw new Error("Sablier SDK: Cannot specify both contractAddress and contractName"); |
| 30 | + } |
| 31 | + |
| 32 | + // Query by name requires release |
| 33 | + if (contractName) { |
| 34 | + if (!release) { |
| 35 | + throw new Error("Sablier SDK: contractName requires release to be specified"); |
| 36 | + } |
| 37 | + const dep = _.find(release.deployments, { chainId }); |
| 38 | + return dep ? _.find(dep.contracts, { name: contractName }) : undefined; |
| 39 | + } |
| 40 | + |
| 41 | + // Query by address |
| 42 | + if (contractAddress) { |
| 43 | + // Scoped to specific release |
| 44 | + if (release) { |
| 45 | + const dep = _.find(release.deployments, { chainId }); |
| 46 | + return dep ? _.find(dep.contracts, (c) => c.address === contractAddress) : undefined; |
| 47 | + } |
| 48 | + |
| 49 | + // Scoped to protocol - check for duplicates across releases |
| 50 | + if (protocol) { |
| 51 | + const releases = releasesQueries.getAll({ protocol }); |
| 52 | + const matches = releases.filter((rel) => { |
| 53 | + const dep = _.find(rel.deployments, { chainId }); |
| 54 | + return dep && _.some(dep.contracts, (c) => c.address === contractAddress); |
| 55 | + }); |
| 56 | + |
| 57 | + if (matches.length > 1) { |
| 58 | + const versions = matches.map((r) => r.version).join(", "); |
| 59 | + throw new Error( |
| 60 | + `Sablier SDK: Contract ${contractAddress} exists in multiple releases (${versions}) for "${protocol}". ` + |
| 61 | + `Specify release: { chainId, contractAddress, release }`, |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + return _.get(catalog, [protocol, chainId, contractAddress]); |
| 66 | + } |
| 67 | + |
| 68 | + // Fallback: search all protocols |
| 69 | + return ( |
| 70 | + _.get(catalog, [Protocol.Airdrops, chainId, contractAddress]) || |
| 71 | + _.get(catalog, [Protocol.Lockup, chainId, contractAddress]) |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + return undefined; |
| 76 | + }, |
| 77 | + /** |
| 78 | + * Get many contracts. |
| 79 | + * - no options ⇒ all |
| 80 | + * - { chainId } ⇒ all for that chain |
| 81 | + * - { protocol } ⇒ all for that protocol |
| 82 | + * - { protocol, chainId } ⇒ all for that protocol and chain |
| 83 | + * - { release } ⇒ all deployments of that release |
| 84 | + * - { release, chainId } ⇒ all for that release and chain |
| 85 | + */ |
| 86 | + getAll: (opts?: { |
| 87 | + chainId?: number; |
| 88 | + protocol?: Sablier.Solana.Protocol; |
| 89 | + release?: Sablier.Solana.Release; |
| 90 | + }): Sablier.Solana.Contract[] | undefined => { |
| 91 | + const { protocol, chainId, release } = opts || {}; |
| 92 | + |
| 93 | + if (protocol && release) { |
| 94 | + throw new Error("Sablier SDK: Cannot specify both protocol and release as query options"); |
| 95 | + } |
| 96 | + |
| 97 | + // by protocol |
| 98 | + if (protocol) { |
| 99 | + const releases = releasesQueries.getAll({ protocol }); |
| 100 | + let deps = _.flatMap(releases, (r) => r.deployments); |
| 101 | + if (chainId) { |
| 102 | + deps = _.filter(deps, (d) => d.chainId === chainId); |
| 103 | + if (deps.length === 0) return undefined; |
| 104 | + } |
| 105 | + return _.flatMap(deps, (d) => d.contracts); |
| 106 | + } |
| 107 | + |
| 108 | + // by explicit release |
| 109 | + if (release) { |
| 110 | + let deps = release.deployments; |
| 111 | + if (chainId) { |
| 112 | + deps = _.filter(deps, (d) => d.chainId === chainId); |
| 113 | + if (deps.length === 0) return undefined; |
| 114 | + } |
| 115 | + return _.flatMap(deps, (d) => d.contracts); |
| 116 | + } |
| 117 | + |
| 118 | + // by chain id |
| 119 | + if (chainId) { |
| 120 | + const deps = _.flatMap(releasesQueries.getAll(), (r) => r.deployments); |
| 121 | + const filtered = _.filter(deps, (d) => d.chainId === chainId); |
| 122 | + return _.flatMap(filtered, (d) => d.contracts); |
| 123 | + } |
| 124 | + |
| 125 | + // no filters → all |
| 126 | + return _.flatMap(releasesQueries.getAll(), (r) => r.deployments.flatMap((d) => d.contracts)); |
| 127 | + }, |
| 128 | +}; |
0 commit comments