Skip to content

Commit 2de04c2

Browse files
committed
Add program clients and ProgramProvider
1 parent d27d341 commit 2de04c2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+826
-729
lines changed

app/src/app/api/accounts/config/route.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
import { SPLURGE_CLIENT } from '@/lib/server/solana';
2+
import { parseConfig } from '@/types/accounts';
13
import { NextRequest, NextResponse } from 'next/server';
2-
import { fetchConfig } from '@/lib/accounts';
34

45
export async function GET(req: NextRequest) {
56
try {
67
return NextResponse.json(
78
{
8-
config: await fetchConfig(),
9+
config: await SPLURGE_CLIENT.fetchProgramAccount(
10+
SPLURGE_CLIENT.getConfigPda(),
11+
'config',
12+
parseConfig
13+
),
914
},
1015
{
1116
status: 200,

app/src/app/api/accounts/items/route.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { GetProgramAccountsFilter } from '@solana/web3.js';
22
import { NextRequest, NextResponse } from 'next/server';
3-
import { fetchAllItems, fetchItem, fetchMultipleItems } from '@/lib/accounts';
43
import { DISCRIMINATOR_SIZE } from '@/lib/constants';
4+
import { SPLURGE_CLIENT } from '@/lib/server/solana';
5+
import { parseItem } from '@/types/accounts';
56

67
export async function GET(req: NextRequest) {
78
const { searchParams } = new URL(req.url);
@@ -24,7 +25,11 @@ export async function GET(req: NextRequest) {
2425

2526
return NextResponse.json(
2627
{
27-
items: await fetchAllItems(filters),
28+
items: await SPLURGE_CLIENT.fetchAllProgramAccounts(
29+
'item',
30+
parseItem,
31+
filters
32+
),
2833
},
2934
{
3035
status: 200,
@@ -33,7 +38,11 @@ export async function GET(req: NextRequest) {
3338
} else if (pdas.length > 1) {
3439
return NextResponse.json(
3540
{
36-
items: await fetchMultipleItems(pdas),
41+
items: await SPLURGE_CLIENT.fetchMultipleProgramAccounts(
42+
pdas,
43+
'item',
44+
parseItem
45+
),
3746
},
3847
{
3948
status: 200,
@@ -42,7 +51,11 @@ export async function GET(req: NextRequest) {
4251
} else {
4352
return NextResponse.json(
4453
{
45-
item: await fetchItem(pdas[0]),
54+
item: await SPLURGE_CLIENT.fetchProgramAccount(
55+
pdas[0],
56+
'item',
57+
parseItem
58+
),
4659
},
4760
{
4861
status: 200,

app/src/app/api/accounts/orders/route.ts

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
import { GetProgramAccountsFilter } from '@solana/web3.js';
22
import { NextRequest, NextResponse } from 'next/server';
3-
import {
4-
fetchAllItems,
5-
fetchAllOrders,
6-
fetchMultipleOrders,
7-
fetchOrder,
8-
} from '@/lib/accounts';
93
import { DISCRIMINATOR_SIZE } from '@/lib/constants';
4+
import { SPLURGE_CLIENT } from '@/lib/server/solana';
5+
import { parseItem, parseOrder } from '@/types/accounts';
106

117
export async function GET(req: NextRequest) {
128
const { searchParams } = new URL(req.url);
@@ -28,18 +24,26 @@ export async function GET(req: NextRequest) {
2824
});
2925
}
3026

31-
let orders = await fetchAllOrders(filters);
27+
let orders = await SPLURGE_CLIENT.fetchAllProgramAccounts(
28+
'order',
29+
parseOrder,
30+
filters
31+
);
3232

3333
// filter for orders with a matching item PDA
3434
if (storePda) {
35-
const itemAccs = await fetchAllItems([
36-
{
37-
memcmp: {
38-
offset: DISCRIMINATOR_SIZE,
39-
bytes: storePda,
35+
const itemAccs = await SPLURGE_CLIENT.fetchAllProgramAccounts(
36+
'item',
37+
parseItem,
38+
[
39+
{
40+
memcmp: {
41+
offset: DISCRIMINATOR_SIZE,
42+
bytes: storePda,
43+
},
4044
},
41-
},
42-
]);
45+
]
46+
);
4347

4448
const itemPdas = itemAccs.map((item) => item.publicKey);
4549

@@ -57,7 +61,11 @@ export async function GET(req: NextRequest) {
5761
} else if (pdas.length > 1) {
5862
return NextResponse.json(
5963
{
60-
orders: await fetchMultipleOrders(pdas),
64+
orders: await SPLURGE_CLIENT.fetchMultipleProgramAccounts(
65+
pdas,
66+
'order',
67+
parseOrder
68+
),
6169
},
6270
{
6371
status: 200,
@@ -66,7 +74,11 @@ export async function GET(req: NextRequest) {
6674
} else {
6775
return NextResponse.json(
6876
{
69-
order: await fetchOrder(pdas[0]),
77+
order: await SPLURGE_CLIENT.fetchProgramAccount(
78+
pdas[0],
79+
'order',
80+
parseOrder
81+
),
7082
},
7183
{
7284
status: 200,

app/src/app/api/accounts/reviews/route.ts

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import { DISCRIMINATOR_SIZE } from '@/lib/constants';
2+
import { SPLURGE_CLIENT } from '@/lib/server/solana';
3+
import { parseOrder, parseReview } from '@/types/accounts';
24
import { NextRequest, NextResponse } from 'next/server';
3-
import {
4-
fetchAllOrders,
5-
fetchAllReviews,
6-
fetchMultipleReviews,
7-
fetchReview,
8-
} from '@/lib/accounts';
95

106
export async function GET(req: NextRequest) {
117
const { searchParams } = new URL(req.url);
@@ -15,18 +11,25 @@ export async function GET(req: NextRequest) {
1511

1612
try {
1713
if (!pdas.length) {
18-
let reviews = await fetchAllReviews();
14+
let reviews = await SPLURGE_CLIENT.fetchAllProgramAccounts(
15+
'review',
16+
parseReview
17+
);
1918

2019
// filter for reviews with a matching order PDA
2120
if (itemPda) {
22-
const orderAccs = await fetchAllOrders([
23-
{
24-
memcmp: {
25-
offset: DISCRIMINATOR_SIZE + 32,
26-
bytes: itemPda,
21+
const orderAccs = await SPLURGE_CLIENT.fetchAllProgramAccounts(
22+
'order',
23+
parseOrder,
24+
[
25+
{
26+
memcmp: {
27+
offset: DISCRIMINATOR_SIZE + 32,
28+
bytes: itemPda,
29+
},
2730
},
28-
},
29-
]);
31+
]
32+
);
3033

3134
const orderPdas = orderAccs.map(({ publicKey }) => publicKey);
3235

@@ -44,7 +47,11 @@ export async function GET(req: NextRequest) {
4447
} else if (pdas.length > 1) {
4548
return NextResponse.json(
4649
{
47-
reviews: await fetchMultipleReviews(pdas),
50+
reviews: await SPLURGE_CLIENT.fetchMultipleProgramAccounts(
51+
pdas,
52+
'review',
53+
parseReview
54+
),
4855
},
4956
{
5057
status: 200,
@@ -53,7 +60,11 @@ export async function GET(req: NextRequest) {
5360
} else {
5461
return NextResponse.json(
5562
{
56-
review: await fetchReview(pdas[0]),
63+
review: await SPLURGE_CLIENT.fetchProgramAccount(
64+
pdas[0],
65+
'review',
66+
parseReview
67+
),
5768
},
5869
{
5970
status: 200,

app/src/app/api/accounts/shoppers/route.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import {
2-
fetchAllShoppers,
3-
fetchMultipleShoppers,
4-
fetchShopper,
5-
} from '@/lib/accounts';
1+
import { SPLURGE_CLIENT } from '@/lib/server/solana';
2+
import { parseShopper } from '@/types/accounts';
63
import { NextRequest, NextResponse } from 'next/server';
74

85
export async function GET(req: NextRequest) {
@@ -14,7 +11,10 @@ export async function GET(req: NextRequest) {
1411
if (!pdas.length) {
1512
return NextResponse.json(
1613
{
17-
shoppers: await fetchAllShoppers(),
14+
shoppers: await SPLURGE_CLIENT.fetchAllProgramAccounts(
15+
'shopper',
16+
parseShopper
17+
),
1818
},
1919
{
2020
status: 200,
@@ -23,7 +23,11 @@ export async function GET(req: NextRequest) {
2323
} else if (pdas.length > 1) {
2424
return NextResponse.json(
2525
{
26-
shoppers: await fetchMultipleShoppers(pdas),
26+
shoppers: await SPLURGE_CLIENT.fetchMultipleProgramAccounts(
27+
pdas,
28+
'shopper',
29+
parseShopper
30+
),
2731
},
2832
{
2933
status: 200,
@@ -32,7 +36,11 @@ export async function GET(req: NextRequest) {
3236
} else {
3337
return NextResponse.json(
3438
{
35-
shopper: await fetchShopper(pdas[0]),
39+
shopper: await SPLURGE_CLIENT.fetchProgramAccount(
40+
pdas[0],
41+
'shopper',
42+
parseShopper
43+
),
3644
},
3745
{
3846
status: 200,

app/src/app/api/accounts/stores/route.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import {
2-
fetchAllStores,
3-
fetchMultipleStores,
4-
fetchStore,
5-
} from '@/lib/accounts';
1+
import { SPLURGE_CLIENT } from '@/lib/server/solana';
2+
import { parseStore } from '@/types/accounts';
63
import { NextRequest, NextResponse } from 'next/server';
74

85
export async function GET(req: NextRequest) {
@@ -14,7 +11,10 @@ export async function GET(req: NextRequest) {
1411
if (!pdas.length) {
1512
return NextResponse.json(
1613
{
17-
stores: await fetchAllStores(),
14+
stores: await SPLURGE_CLIENT.fetchAllProgramAccounts(
15+
'store',
16+
parseStore
17+
),
1818
},
1919
{
2020
status: 200,
@@ -23,7 +23,11 @@ export async function GET(req: NextRequest) {
2323
} else if (pdas.length > 1) {
2424
return NextResponse.json(
2525
{
26-
stores: await fetchMultipleStores(pdas),
26+
stores: await SPLURGE_CLIENT.fetchMultipleProgramAccounts(
27+
pdas,
28+
'store',
29+
parseStore
30+
),
2731
},
2832
{
2933
status: 200,
@@ -32,7 +36,11 @@ export async function GET(req: NextRequest) {
3236
} else {
3337
return NextResponse.json(
3438
{
35-
store: await fetchStore(pdas[0]),
39+
store: await SPLURGE_CLIENT.fetchProgramAccount(
40+
pdas[0],
41+
'store',
42+
parseStore
43+
),
3644
},
3745
{
3846
status: 200,

app/src/app/api/transaction/permissioned/route.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { VersionedTransaction } from '@solana/web3.js';
22
import { NextRequest, NextResponse } from 'next/server';
3-
import { ADMIN_KEYPAIR, CONNECTION, sendTx } from '@/lib/server/solana';
4-
import { v0TxToBase64, validateProgramIx } from '@/lib/utils';
3+
import {
4+
ADMIN_KEYPAIR,
5+
CONNECTION,
6+
sendTx,
7+
validateProgramIx,
8+
} from '@/lib/server/solana';
9+
import { v0TxToBase64 } from '@/lib/utils';
510

611
const allowedIxs = ['ship_order', 'cancel_order'];
712

app/src/app/orders/layout.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
'use client';
22

3-
import { getShopperPda } from '@/lib/pda';
43
import { ItemsProvider } from '@/providers/ItemsProvider';
54
import { OrdersProvider } from '@/providers/OrdersProvider';
5+
import { useProgram } from '@/providers/ProgramProvider';
66
import { useUnifiedWallet } from '@jup-ag/wallet-adapter';
77
import { ReactNode } from 'react';
88

99
export default function Layout({ children }: { children: ReactNode }) {
1010
const { publicKey } = useUnifiedWallet();
11+
const { splurgeClient } = useProgram();
1112

1213
if (!publicKey) {
1314
return <>{children}</>;
1415
}
1516

16-
const shopperPda = getShopperPda(publicKey).toBase58();
17+
const shopperPda = splurgeClient.getShopperPda(publicKey).toBase58();
1718

1819
return (
1920
<OrdersProvider shopperPda={shopperPda}>

app/src/app/page.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import { ItemCardSkeleton } from '@/components/ItemCardSkeleton';
66
import { MainSection } from '@/components/MainSection';
77
import { NoResultText } from '@/components/NoResultText';
88
import { SectionHeader } from '@/components/SectionHeader';
9-
import { getStorePda } from '@/lib/pda';
109
import { atomicToUsd, truncateAddress } from '@/lib/utils';
1110
import { ItemsProvider, useItems } from '@/providers/ItemsProvider';
11+
import { useProgram } from '@/providers/ProgramProvider';
1212
import { useShopper } from '@/providers/ShopperProvider';
1313
import { StoresProvider, useStores } from '@/providers/StoresProvider';
1414
import { useWallet } from '@jup-ag/wallet-adapter';
@@ -19,6 +19,7 @@ import { useMemo } from 'react';
1919

2020
function Section() {
2121
const { publicKey } = useWallet();
22+
const { splurgeClient } = useProgram();
2223
const { shopperData } = useShopper();
2324
const { storesData, storesLoading } = useStores();
2425
const { itemsData, itemsLoading } = useItems();
@@ -29,12 +30,12 @@ function Section() {
2930
let filtered = itemsData.filter(({ inventoryCount }) => inventoryCount > 0);
3031

3132
if (publicKey) {
32-
const storePda = getStorePda(publicKey);
33+
const storePda = splurgeClient.getStorePda(publicKey);
3334
filtered = filtered.filter(({ store }) => store !== storePda.toBase58());
3435
}
3536

3637
return filtered;
37-
}, [itemsData, publicKey]);
38+
}, [itemsData, publicKey, splurgeClient]);
3839

3940
return (
4041
<MainSection className="flex-1">

0 commit comments

Comments
 (0)