Skip to content

Commit

Permalink
api: pass request context to ll-api
Browse files Browse the repository at this point in the history
  • Loading branch information
dcposch committed Sep 21, 2024
1 parent 70f0390 commit cf92c3f
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 49 deletions.
11 changes: 8 additions & 3 deletions packages/daimo-api/src/api/getAccountHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,19 @@ export async function getAccountHistory(

let landlineSessionKey: string | undefined;
if (getEnvApi().LANDLINE_API_URL) {
landlineSessionKey = (await getLandlineSession(address)).key;
const daimoAddress = address;
const llSession = await getLandlineSession({ daimoAddress }, ctx);
landlineSessionKey = llSession.key;
landlineSessionURL = getLandlineURL(address, landlineSessionKey);
landlineAccounts = await getLandlineAccounts(address);
landlineAccounts = await getLandlineAccounts({ daimoAddress }, ctx);
// Support for displaying landline transfers in the mobile app was added
// in version 1.9.7 and doesn't support backcompat.
// Only add landline transfers if the app version is > 1.9.6
if (appVersion && semver.gt(appVersion, "1.9.6")) {
const landlineTransfers = await getLandlineTransfers(address);
const landlineTransfers = await getLandlineTransfers(
{ daimoAddress },
ctx
);
transferClogs = addLandlineTransfers(
landlineTransfers,
transferClogs,
Expand Down
121 changes: 87 additions & 34 deletions packages/daimo-api/src/landline/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Address } from "viem";

import { landlineTrpc } from "./trpc";
import { getEnvApi } from "../env";
import { TrpcRequestContext } from "../server/trpc";

export interface LandlineSessionKey {
key: string;
Expand All @@ -21,15 +22,25 @@ export function getLandlineURL(daimoAddress: string, sessionKey: string) {
}

export async function getLandlineSession(
daimoAddress: Address
{
daimoAddress,
}: {
daimoAddress: Address;
},
context: TrpcRequestContext
): Promise<LandlineSessionKey> {
console.log(`[LANDLINE] getting session key for ${daimoAddress}`);

try {
// @ts-ignore
const sessionKey = await landlineTrpc.getOrCreateSessionKey.mutate({
daimoAddress,
});
const sessionKey = await landlineTrpc.getOrCreateSessionKey.mutate(
{
daimoAddress,
},
{
context,
}
);
console.log(`[LANDLINE] got session key for ${daimoAddress}`);
return sessionKey;
} catch (err: any) {
Expand All @@ -43,16 +54,26 @@ export async function getLandlineSession(
}

export async function getLandlineAccounts(
daimoAddress: Address
{
daimoAddress,
}: {
daimoAddress: Address;
},
context: TrpcRequestContext
): Promise<LandlineAccount[]> {
console.log(`[LANDLINE] getting external accounts for ${daimoAddress}`);

try {
const landlineAccounts =
// @ts-ignore
await landlineTrpc.getExternalAccountsTransferInfo.query({
daimoAddress,
});
await landlineTrpc.getExternalAccountsTransferInfo.query(
{
daimoAddress,
},
{
context,
}
);
console.log(`[LANDLINE] got external accounts for ${daimoAddress}`);
// TODO: change to number. Currently a string for backcompat
return landlineAccounts.map((account: any) => ({
Expand All @@ -70,28 +91,47 @@ export async function getLandlineAccounts(
}

export async function getLandlineTransfers(
daimoAddress: Address,
createdAfter?: number
{
daimoAddress,
createdAfterS,
}: {
daimoAddress: Address;
createdAfterS?: number;
},
context: TrpcRequestContext
): Promise<LandlineTransfer[]> {
// Convert createdAfter from Unix seconds to a Date object if it's provided
const createdAfterDate = createdAfter
? new Date(createdAfter * 1000)
const createdAfter = createdAfterS
? new Date(createdAfterS * 1000)
: undefined;

const transfers =
// @ts-ignore
await landlineTrpc.getAllLandlineTransfers.query({
daimoAddress,
createdAfter: createdAfterDate,
});
await landlineTrpc.getAllLandlineTransfers.query(
{
daimoAddress,
createdAfter,
},
{
context,
}
);
return transfers;
}

export async function landlineDeposit(
daimoAddress: Address,
landlineAccountUuid: string,
amount: string,
memo: string | undefined
{
daimoAddress,
landlineAccountUuid,
amount,
memo,
}: {
daimoAddress: Address;
landlineAccountUuid: string;
amount: string;
memo?: string;
},
context: TrpcRequestContext
): Promise<LandlineDepositResponse> {
console.log("[LANDLINE] making deposit", {
daimoAddress,
Expand All @@ -102,12 +142,17 @@ export async function landlineDeposit(

try {
// @ts-ignore
const depositResponse = await landlineTrpc.deposit.mutate({
daimoAddress,
landlineAccountUuid,
amount,
memo,
});
const depositResponse = await landlineTrpc.deposit.mutate(
{
daimoAddress,
landlineAccountUuid,
amount,
memo,
},
{
context,
}
);
console.log(
`[LANDLINE] created deposit for ${daimoAddress}, landlineAccountUuid: ${landlineAccountUuid}, amount: ${amount}, memo: ${memo}`,
depositResponse
Expand All @@ -133,18 +178,26 @@ export type ShouldFastFinishResponse = {
* @param amount amount in the units of the destination token (USDC)
* @returns
*/
export async function validateLandlineDeposit(args: {
daimoAddress: Address;
amount: string;
}) {
export async function validateLandlineDeposit(
args: {
daimoAddress: Address;
amount: string;
},
context: TrpcRequestContext
) {
console.log(`[LANDLINE] validating deposit ${debugJson(args)}`);
try {
const response: ShouldFastFinishResponse =
// @ts-ignore
await landlineTrpc.shouldFastDeposit.query({
daimoAddress: args.daimoAddress,
amount: args.amount,
});
await landlineTrpc.shouldFastDeposit.query(
{
daimoAddress: args.daimoAddress,
amount: args.amount,
},
{
context,
}
);
console.log(
`[LANDLINE] validateLandlineDeposit ${debugJson({ args, response })}`
);
Expand Down
17 changes: 10 additions & 7 deletions packages/daimo-api/src/landline/trpc.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { createTRPCClient, httpBatchLink } from "@trpc/client";

const createHeaders = () => {
const headers: Record<string, string> = {
"x-api-key": process.env.LANDLINE_API_KEY || "",
};
import { getEnvApi } from "../env";
import { TrpcRequestContext } from "../server/trpc";

return headers;
};
const env = getEnvApi();

// TODO(andrew): Add type to createTRPCClient
export const landlineTrpc = createTRPCClient({
links: [
httpBatchLink({
url: process.env.LANDLINE_API_URL || "",
headers: createHeaders(),
headers: (o) => {
const context = o.opList[0].context as TrpcRequestContext;
return {
"x-api-key": env.LANDLINE_API_KEY,
"x-forwarded-for": context.ipAddr,
};
},
}),
],
});
13 changes: 8 additions & 5 deletions packages/daimo-api/src/server/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -715,10 +715,13 @@ export function createRouter(
assert(action.type === "landlineDeposit", "Invalid action type");

const response = await landlineDeposit(
daimoAddress,
action.landlineAccountUuid,
action.amount,
action.memo
{
daimoAddress,
landlineAccountUuid: action.landlineAccountUuid,
amount: action.amount,
memo: action.memo,
},
opts.ctx
);

return response;
Expand All @@ -734,7 +737,7 @@ export function createRouter(
.query(async (opts) => {
const { daimoAddress, amount } = opts.input;
const response: ShouldFastFinishResponse =
await validateLandlineDeposit({ daimoAddress, amount });
await validateLandlineDeposit({ daimoAddress, amount }, opts.ctx);
return response;
}),

Expand Down

0 comments on commit cf92c3f

Please sign in to comment.