|
| 1 | +import type { Chain } from "../chains/types.js"; |
| 2 | +import { getCachedChain } from "../chains/utils.js"; |
| 3 | +import type { ThirdwebClient } from "../client/client.js"; |
| 4 | +import { |
| 5 | + type PreparedTransaction, |
| 6 | + prepareTransaction, |
| 7 | +} from "../transaction/prepare-transaction.js"; |
| 8 | +import type { Address } from "../utils/address.js"; |
| 9 | +import { toBigInt } from "../utils/bigint.js"; |
| 10 | +import type { Hex } from "../utils/encoding/hex.js"; |
| 11 | +import { getClientFetch } from "../utils/fetch.js"; |
| 12 | +import type { Account } from "../wallets/interfaces/wallet.js"; |
| 13 | + |
| 14 | +const NEBULA_API_URL = "https://nebula-api.thirdweb.com"; |
| 15 | + |
| 16 | +export type Input = { |
| 17 | + client: ThirdwebClient; |
| 18 | + prompt: string | string[]; |
| 19 | + account?: Account; |
| 20 | + context?: { |
| 21 | + chains?: Chain[]; |
| 22 | + walletAddresses?: string[]; |
| 23 | + contractAddresses?: string[]; |
| 24 | + }; |
| 25 | + sessionId?: string; |
| 26 | +}; |
| 27 | + |
| 28 | +export type Output = { |
| 29 | + message: string; |
| 30 | + sessionId: string; |
| 31 | + transactions: PreparedTransaction[]; |
| 32 | +}; |
| 33 | + |
| 34 | +type ApiResponse = { |
| 35 | + message: string; |
| 36 | + session_id: string; |
| 37 | + actions?: { |
| 38 | + type: "init" | "presence" | "sign_transaction"; |
| 39 | + source: string; |
| 40 | + data: string; |
| 41 | + }[]; |
| 42 | +}; |
| 43 | + |
| 44 | +export async function nebulaFetch( |
| 45 | + mode: "execute" | "chat", |
| 46 | + input: Input, |
| 47 | +): Promise<Output> { |
| 48 | + const fetch = getClientFetch(input.client); |
| 49 | + const response = await fetch(`${NEBULA_API_URL}/${mode}`, { |
| 50 | + method: "POST", |
| 51 | + headers: { |
| 52 | + "Content-Type": "application/json", |
| 53 | + }, |
| 54 | + body: JSON.stringify({ |
| 55 | + message: input.prompt, // TODO: support array of messages |
| 56 | + session_id: input.sessionId, |
| 57 | + ...(input.account |
| 58 | + ? { |
| 59 | + execute_config: { |
| 60 | + mode: "client", |
| 61 | + signer_wallet_address: input.account.address, |
| 62 | + }, |
| 63 | + } |
| 64 | + : {}), |
| 65 | + ...(input.context |
| 66 | + ? { |
| 67 | + context_filter: { |
| 68 | + chain_ids: |
| 69 | + input.context.chains?.map((c) => c.id.toString()) || [], |
| 70 | + signer_wallet_address: input.context.walletAddresses || [], |
| 71 | + contract_addresses: input.context.contractAddresses || [], |
| 72 | + }, |
| 73 | + } |
| 74 | + : {}), |
| 75 | + }), |
| 76 | + }); |
| 77 | + if (!response.ok) { |
| 78 | + const error = await response.text(); |
| 79 | + throw new Error(`Nebula API error: ${error}`); |
| 80 | + } |
| 81 | + const data = (await response.json()) as ApiResponse; |
| 82 | + |
| 83 | + // parse transactions if present |
| 84 | + let transactions: PreparedTransaction[] = []; |
| 85 | + if (data.actions) { |
| 86 | + transactions = data.actions.map((action) => { |
| 87 | + const tx = JSON.parse(action.data) as { |
| 88 | + chainId: number; |
| 89 | + to: Address | undefined; |
| 90 | + value: Hex; |
| 91 | + data: Hex; |
| 92 | + }; |
| 93 | + return prepareTransaction({ |
| 94 | + chain: getCachedChain(tx.chainId), |
| 95 | + client: input.client, |
| 96 | + to: tx.to, |
| 97 | + value: tx.value ? toBigInt(tx.value) : undefined, |
| 98 | + data: tx.data, |
| 99 | + }); |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + return { |
| 104 | + message: data.message, |
| 105 | + sessionId: data.session_id, |
| 106 | + transactions, |
| 107 | + }; |
| 108 | +} |
0 commit comments