11import { getAuthToken } from "@/api/auth-token" ;
22import { NEXT_PUBLIC_THIRDWEB_API_HOST } from "@/constants/public-envs" ;
3- import type { ProductSKU } from "@/types/billing" ;
3+ import type { ChainInfraSKU , ProductSKU } from "@/types/billing" ;
44
55type InvoiceLine = {
66 // amount for this line item
@@ -22,7 +22,7 @@ type Invoice = {
2222
2323export type TeamSubscription = {
2424 id : string ;
25- type : "PLAN" | "USAGE" | "PLAN_ADD_ON" | "PRODUCT" ;
25+ type : "PLAN" | "USAGE" | "PLAN_ADD_ON" | "PRODUCT" | "CHAIN" ;
2626 status :
2727 | "incomplete"
2828 | "incomplete_expired"
@@ -37,6 +37,13 @@ export type TeamSubscription = {
3737 trialStart : string | null ;
3838 trialEnd : string | null ;
3939 upcomingInvoice : Invoice ;
40+ skus : ( ProductSKU | ChainInfraSKU ) [ ] ;
41+ } ;
42+
43+ type ChainTeamSubscription = Omit < TeamSubscription , "skus" > & {
44+ chainId : string ;
45+ skus : ChainInfraSKU [ ] ;
46+ isLegacy : boolean ;
4047} ;
4148
4249export async function getTeamSubscriptions ( slug : string ) {
@@ -60,3 +67,61 @@ export async function getTeamSubscriptions(slug: string) {
6067 }
6168 return null ;
6269}
70+
71+ const CHAIN_PLAN_TO_INFRA = {
72+ "chain:plan:gold" : [ "chain:infra:rpc" , "chain:infra:account_abstraction" ] ,
73+ "chain:plan:platinum" : [
74+ "chain:infra:rpc" ,
75+ "chain:infra:insight" ,
76+ "chain:infra:account_abstraction" ,
77+ ] ,
78+ "chain:plan:ultimate" : [
79+ "chain:infra:rpc" ,
80+ "chain:infra:insight" ,
81+ "chain:infra:account_abstraction" ,
82+ ] ,
83+ } ;
84+
85+ export async function getChainSubscriptions ( slug : string ) {
86+ const allSubscriptions = await getTeamSubscriptions ( slug ) ;
87+ if ( ! allSubscriptions ) {
88+ return null ;
89+ }
90+
91+ // first replace any sku that MIGHT match a chain plan
92+ const updatedSubscriptions = allSubscriptions
93+ . filter ( ( s ) => s . type === "CHAIN" )
94+ . map ( ( s ) => {
95+ const skus = s . skus ;
96+ const updatedSkus = skus . flatMap ( ( sku ) => {
97+ const plan =
98+ CHAIN_PLAN_TO_INFRA [ sku as keyof typeof CHAIN_PLAN_TO_INFRA ] ;
99+ return plan ? plan : sku ;
100+ } ) ;
101+ return {
102+ ...s ,
103+ isLegacy : updatedSkus . length !== skus . length ,
104+ skus : updatedSkus ,
105+ } ;
106+ } ) ;
107+
108+ return updatedSubscriptions . filter (
109+ ( s ) : s is ChainTeamSubscription =>
110+ "chainId" in s && typeof s . chainId === "string" ,
111+ ) ;
112+ }
113+
114+ export async function getChainSubscriptionForChain (
115+ slug : string ,
116+ chainId : number ,
117+ ) {
118+ const chainSubscriptions = await getChainSubscriptions ( slug ) ;
119+
120+ if ( ! chainSubscriptions ) {
121+ return null ;
122+ }
123+
124+ return (
125+ chainSubscriptions . find ( ( s ) => s . chainId === chainId . toString ( ) ) ?? null
126+ ) ;
127+ }
0 commit comments