Skip to content

Commit

Permalink
login: skip session creation if project has no call targets
Browse files Browse the repository at this point in the history
  • Loading branch information
alecananian committed Jun 5, 2024
1 parent 0049a82 commit 64d48ee
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/soft-experts-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@treasure-dev/tdk-core": patch
---

Exported API response types
35 changes: 21 additions & 14 deletions apps/login/app/hooks/useLogin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { TDKAPI, getContractAddress } from "@treasure-dev/tdk-core";
import {
type Project,
TDKAPI,
getContractAddress,
} from "@treasure-dev/tdk-core";
import { useMemo, useReducer } from "react";
import type { Chain } from "thirdweb";
import { type ThirdwebClient, getContract, sendTransaction } from "thirdweb";
Expand Down Expand Up @@ -112,37 +116,35 @@ const reducer = (state: State, action: Action): State => {
};

type Props = {
project: string;
project: Project;
chain: Chain;
redirectUri: string;
backendWallet: string;
approvedCallTargets: string[];
};

const client: ThirdwebClient = {
clientId: env.VITE_THIRDWEB_CLIENT_ID,
secretKey: undefined,
};

export const useLogin = ({
project,
chain,
redirectUri,
backendWallet,
approvedCallTargets,
}: Props) => {
export const useLogin = ({ project, chain, redirectUri }: Props) => {
const [state, dispatch] = useReducer(reducer, DEFAULT_STATE);

const tdk = useMemo(
() =>
new TDKAPI({
baseUri: env.VITE_TDK_API_URL,
project,
project: project.slug,
chainId: chain.id,
}),
[project, chain.id],
);

const backendWallet = project.backendWallets[0];
const approvedCallTargets = project.callTargets;
const nativeTokenLimitPerTransaction = 0;
const requiresSession =
approvedCallTargets.length > 0 || nativeTokenLimitPerTransaction > 0;

const connectSmartWallet = async (inAppAccount: Account) => {
const wallet = smartWallet({
chain,
Expand All @@ -167,6 +169,7 @@ export const useLogin = ({
sessionKeyAddress: backendWallet,
permissions: {
approvedTargets: approvedCallTargets,
nativeTokenLimitPerTransaction,
permissionStartTimestamp: getDateHoursFromNow(-1),
permissionEndTimestamp: getDateDaysFromNow(1),
},
Expand All @@ -176,7 +179,7 @@ export const useLogin = ({
let didCreateSession = false;

// If smart wallet isn't deployed yet, create a new session to bundle the two txs
if (!isDeployed) {
if (!isDeployed && requiresSession) {
try {
console.debug("Deploying smart wallet and creating session key");
await sendTransaction({
Expand Down Expand Up @@ -228,7 +231,7 @@ export const useLogin = ({
}

// Check active signers to see if requested session is already available
if (!didCreateSession) {
if (!didCreateSession && requiresSession) {
console.debug("Checking for existing sessions");
const activeSigners = await getAllActiveSigners({
contract: smartAccountContract,
Expand Down Expand Up @@ -277,6 +280,10 @@ export const useLogin = ({
} else {
console.debug("Using existing session key");
}
} else if (!requiresSession) {
console.debug(
"Session not required by project, skipping session creation",
);
}

// Redirect back to project
Expand Down
12 changes: 7 additions & 5 deletions apps/login/app/routes/($slug)._index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import { useState } from "react";
import { useForm } from "react-hook-form";
import VerificationInput from "react-verification-input";
import { ClientOnly } from "remix-utils/client-only";
import { arbitrum, arbitrumSepolia } from "thirdweb/chains";
import emailImg from "~/assets/email.webp";
import logoImg from "~/assets/logo.svg";
import { SpinnerIcon } from "~/components/SpinnerIcon";
import { useLogin } from "~/hooks/useLogin";

import { CHAIN_ID_TO_CHAIN_MAPPING } from "~/utils/chain";
import { env } from "../utils/env";

type LoginForm = {
Expand Down Expand Up @@ -87,6 +87,10 @@ export const meta: MetaFunction<typeof loader> = ({ data }) => {

export default function LoginPage() {
const { project, chainId, redirectUri } = useLoaderData<typeof loader>();
const chain =
CHAIN_ID_TO_CHAIN_MAPPING[chainId] ??
CHAIN_ID_TO_CHAIN_MAPPING[DEFAULT_TDK_CHAIN_ID];

const [verificationInput, setVerificationInput] = useState<string>("");
const {
status,
Expand All @@ -98,11 +102,9 @@ export default function LoginPage() {
// logInWithCustomAuth,
reset,
} = useLogin({
project: project.slug,
chain: chainId === arbitrumSepolia.id ? arbitrumSepolia : arbitrum,
project,
chain,
redirectUri,
backendWallet: project.backendWallets[0],
approvedCallTargets: project.callTargets,
});

const { register, handleSubmit } = useForm<LoginForm>();
Expand Down
18 changes: 18 additions & 0 deletions apps/login/app/utils/chain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { TREASURE_RUBY_CHAIN_DEFINITION } from "@treasure-dev/tdk-react";
import {
arbitrum,
arbitrumSepolia,
defineChain,
mainnet,
sepolia,
} from "thirdweb/chains";

export const CHAIN_ID_TO_CHAIN_MAPPING = {
[arbitrum.id]: arbitrum,
[arbitrumSepolia.id]: arbitrumSepolia,
[mainnet.id]: mainnet,
[sepolia.id]: sepolia,
[TREASURE_RUBY_CHAIN_DEFINITION.id]: defineChain(
TREASURE_RUBY_CHAIN_DEFINITION,
),
};
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { TransactionReceipt } from "viem";

import type { TDKAPI } from "./api";
import type { SUPPORTED_CHAIN_IDS } from "./constants";

// General transactions
Expand Down Expand Up @@ -67,3 +68,14 @@ export type TokenStandard = "ERC20" | "ERC721" | "ERC1155";
// Payments
export type Token = "ARB" | "MAGIC" | "ETH" | AddressString;
export type Currency = Token | "USD";

// API
export type Project = Awaited<
ReturnType<(typeof TDKAPI)["prototype"]["project"]["findBySlug"]>
>;
export type Transaction = Awaited<
ReturnType<(typeof TDKAPI)["prototype"]["transaction"]["get"]>
>;
export type User = Awaited<
ReturnType<(typeof TDKAPI)["prototype"]["user"]["me"]>
>;

0 comments on commit 64d48ee

Please sign in to comment.