-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcontext.ts
133 lines (123 loc) · 3.7 KB
/
context.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import {
createAxelarConfigClient,
createAxelarQueryClient,
} from "@axelarjs/api";
import {
IERC20BurnableMintableClient,
InterchainTokenClient,
InterchainTokenFactoryClient,
InterchainTokenServiceClient,
TokenManagerClient,
} from "@axelarjs/evm";
import type { NextApiRequest, NextApiResponse } from "next";
import { getServerSession, type AuthOptions } from "next-auth";
import { kv } from "@vercel/kv";
import OpenAI from "openai";
import type { Chain } from "viem";
import {
NEXT_PUBLIC_INTERCHAIN_TOKEN_FACTORY_ADDRESS,
NEXT_PUBLIC_INTERCHAIN_TOKEN_SERVICE_ADDRESS,
NEXT_PUBLIC_NETWORK_ENV,
} from "~/config/env";
import { NEXT_AUTH_OPTIONS, type Web3Session } from "~/config/next-auth";
import { WAGMI_CHAIN_CONFIGS } from "~/config/wagmi";
import db from "~/lib/drizzle/client";
import axelarjsSDKClient from "~/services/axelarjsSDK";
import axelarscanClient from "~/services/axelarscan";
import MaestroKVClient from "~/services/db/kv";
import MaestroPostgresClient from "~/services/db/postgres";
import gmpClient from "~/services/gmp";
import { axelarConfigs, evmChains } from "./utils";
export interface ContextConfig {
req: NextApiRequest;
res: NextApiResponse;
}
const createContextInner = async ({ req, res }: ContextConfig) => {
const session = await getServerSession<AuthOptions, Web3Session>(
req,
res,
NEXT_AUTH_OPTIONS
);
const maestroKVClient = new MaestroKVClient(kv);
const maestroPostgresClient = new MaestroPostgresClient(db);
const openaiClient = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
const axelarQueryClient = createAxelarQueryClient(NEXT_PUBLIC_NETWORK_ENV);
const axelarConfigClient = createAxelarConfigClient(NEXT_PUBLIC_NETWORK_ENV);
return {
req,
res,
session,
services: {
gmp: gmpClient,
axelarscan: axelarscanClient,
axelarjsSDK: axelarjsSDKClient,
axelarQuery: axelarQueryClient,
openai: openaiClient,
},
configs: {
/**
* Cached accessor for EVM chain configs
*/
evmChains: evmChains.bind(
null,
maestroKVClient,
axelarscanClient,
"evmChains" as const
),
axelarConfigs: axelarConfigs.bind(
null,
maestroKVClient,
axelarConfigClient,
"axelarConfigs" as const
),
wagmiChainConfigs: WAGMI_CHAIN_CONFIGS,
},
persistence: {
/**
* key-value store adapter
*/
kv: maestroKVClient,
/**
* postgres adapter
*/
postgres: maestroPostgresClient,
},
contracts: {
createERC20Client(chain: Chain, address: `0x${string}`) {
return new IERC20BurnableMintableClient({ chain, address });
},
createInterchainTokenClient(chain: Chain, address: `0x${string}`) {
return new InterchainTokenClient({ chain, address });
},
createTokenManagerClient(chain: Chain, address: `0x${string}`) {
return new TokenManagerClient({ chain, address });
},
createInterchainTokenServiceClient(
chain: Chain,
address?: `0x${string}`
) {
return new InterchainTokenServiceClient({
chain,
address: address ?? NEXT_PUBLIC_INTERCHAIN_TOKEN_SERVICE_ADDRESS,
});
},
createInterchainTokenFactoryClient(
chain: Chain,
address?: `0x${string}`
) {
return new InterchainTokenFactoryClient({
chain,
address: address ?? NEXT_PUBLIC_INTERCHAIN_TOKEN_FACTORY_ADDRESS,
});
},
},
};
};
/**
* Creates context for an incoming request
* @link https://trpc.io/docs/context
*/
export const createContext = createContextInner;
export type Context = Awaited<ReturnType<typeof createContextInner>>;