Skip to content

Commit

Permalink
migrate to ecosystem wallets and pnpm (#127)
Browse files Browse the repository at this point in the history
* integrate ecosystem wallets

* streamline login functions; fix ecosystem env vars

* add changeset

* remove exports for in-app wallets

* upgrade thirdweb sdk

* upgrade thirdweb sdk

* add changeset

* remove duplicate changeset

* fix email login

* use ecosystem details on redirect auth

* fix electron app social auth

* fix comment

* use ecosystem wallets for email upsert check

* Migrate from npm to pnpm (#131)

* Switch npm to pnpm

* Update READMEs and dockerfile

* Install pnpm in workflows

* fix electron example

* fix for typescript and vite

* update root package.json to use workspace

* move package dependencies to workspace

* move more to catalog

* fix react example

* fix generate command

* fix dev commands

* add electron example deps to workspace

* move api deps to workspace

* fix

* move magicswap example to workspace

* fix local dependency linking

* Fix for examples

* re-add react optimize

---------

Co-authored-by: Alec Ananian <1013230+alecananian@users.noreply.github.com>

---------

Co-authored-by: Wyatt Mufson <wyatt@ryu.games>
  • Loading branch information
alecananian and WyattMufson authored Oct 8, 2024
1 parent 83ce381 commit d468a1b
Show file tree
Hide file tree
Showing 46 changed files with 14,840 additions and 22,106 deletions.
6 changes: 6 additions & 0 deletions .changeset/rotten-garlics-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@treasure-dev/tdk-react": major
"@treasure-dev/tdk-core": major
---

Migrated from in-app wallets to ecosystem wallets
12 changes: 8 additions & 4 deletions .github/workflows/deploy-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,22 @@ jobs:
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install pnpm
uses: pnpm/action-setup@v4
with:
version: 9
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
- name: Install dependencies
run: npm ci
run: pnpm install --frozen-lockfile
- name: Build packages
run: |
npm run build:auth
npm run build:core
pnpm build:auth
pnpm build:core
- name: Build code
run: npm run build:api
run: pnpm build:api
- name: Create Sentry release
uses: getsentry/action-release@v1
env:
Expand Down
16 changes: 10 additions & 6 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@ jobs:
steps:
- name: Check out repo
uses: actions/checkout@v4
- name: Install pnpm
uses: pnpm/action-setup@v4
with:
version: 9
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
- name: Install dependencies
run: npm ci
run: pnpm install --frozen-lockfile
- name: Generate code
run: npm run generate
run: pnpm generate
- name: Lint code
run: npm run lint
run: pnpm lint
- name: Analyze code
run: npm run knip
run: pnpm knip
- name: Test code
run: npm test
run: pnpm test
- name: Build code
run: npm run build
run: pnpm build
10 changes: 7 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@ jobs:
steps:
- name: Check out repo
uses: actions/checkout@v4
- name: Install pnpm
uses: pnpm/action-setup@v4
with:
version: 9
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
- name: Install dependencies
run: npm ci
run: pnpm install --frozen-lockfile
- name: Create release PR or publish to npm
uses: changesets/action@v1
with:
version: npm run package:version
publish: npm run package:publish
version: pnpm package:version
publish: pnpm package:publish
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
npx lint-staged && npm run knip
npx lint-staged && pnpm knip
2 changes: 1 addition & 1 deletion .lintstagedrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"*": "npm run fix"
"*": "pnpm fix"
}
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,28 @@ SDK for the Treasure ecosystem
Install dependencies:

```bash
npm install
pnpm install
```

Open the `README.md` files for the apps and examples you are contributing to and follow the instructions to finish configuring the packages.

Start watch mode for base workspaces:

```bash
npm run dev
pnpm dev
```

Start any example apps:

```bash
$ cd examples/connect-react
$ npm run dev
$ pnpm dev
```

### Contributing

Before creating a PR, add a [changeset](https://github.com/changesets/changesets/blob/main/docs/intro-to-using-changesets.md) to your commit:

```bash
npm run package:changeset
pnpm package:changeset
```
3 changes: 3 additions & 0 deletions apps/api/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ THIRDWEB_CLIENT_ID=
THIRDWEB_ENGINE_URL=
THIRDWEB_ENGINE_ACCESS_TOKEN=
THIRDWEB_SECRET_KEY=
THIRDWEB_ECOSYSTEM_ID=ecosystem.treasure
THIRDWEB_ECOSYSTEM_PARTNER_ID=
TREASURE_AUTH_KMS_KEY=
TROVE_API_URL=https://trove-api-dev.treasure.lol
TROVE_API_KEY=
ENGINE_MAINTENANCE_MODE_ENABLED=false
Expand Down
17 changes: 10 additions & 7 deletions apps/api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@ RUN apk update && \
# Copy application code
COPY . .

# Install dependencies
RUN npm install --include=dev
# Install pnpm
RUN npm install -g pnpm

# Install dependencies including dev dependencies
RUN pnpm install --prod=false

# Build packages
RUN npm run build:auth
RUN npm run build:core
RUN pnpm build:auth
RUN pnpm build:core

# Build application
RUN npm run build:api
RUN pnpm build:api

# Remove development dependencies
RUN npm prune --omit=dev
RUN pnpm prune --prod

# Start the server
EXPOSE 8080
CMD [ "npm", "run", "start:api" ]
CMD [ "pnpm", "start:api" ]
10 changes: 5 additions & 5 deletions apps/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Backend server powering the Treasure Development Kit
Install dependencies:

```bash
npm install
pnpm install
```

Create `.env` file based on example and fill in with relevant environment variables:
Expand All @@ -25,25 +25,25 @@ cp .env.example .env
Generate database client:

```bash
npm run generate
pnpm generate
```

Create initial tables in database:

```bash
npm run db:migrate
pnpm db:migrate
```

Seed database with development values:

```bash
npm run db:seed
pnpm db:seed
```

Start server:

```bash
npm run dev
pnpm dev
```

## Deployment
Expand Down
48 changes: 24 additions & 24 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,38 @@
"type": "module",
"scripts": {
"typecheck": "tsc --noEmit",
"build": "npm run generate && npm run typecheck && tsup",
"build": "pnpm generate && pnpm typecheck && tsup",
"generate": "prisma generate",
"dev": "npm run generate && tsup --watch --onSuccess 'npm run start'",
"dev": "pnpm generate && tsup --watch --onSuccess 'pnpm start'",
"start": "node ./dist/index.js",
"db:seed": "tsx ./prisma/seed.ts",
"db:seed:dev": "npm run db:seed dev",
"db:seed:prod": "npm run db:seed prod",
"db:seed:dev": "pnpm db:seed dev",
"db:seed:prod": "pnpm db:seed prod",
"db:migrate": "prisma migrate dev"
},
"dependencies": {
"@aws-sdk/client-secrets-manager": "^3.507.0",
"@fastify/cors": "^10.0.0",
"@fastify/swagger": "^9.0.0",
"@fastify/swagger-ui": "^5.0.1",
"@fastify/type-provider-typebox": "^5.0.0",
"@prisma/client": "^5.3.0",
"@sentry/node": "^8.7.0",
"@sinclair/typebox": "^0.33.7",
"@thirdweb-dev/engine": "^0.0.15",
"@treasure-dev/auth": "*",
"@treasure-dev/tdk-core": "*",
"@wagmi/core": "^2.9.1",
"abitype": "^1.0.5",
"dotenv": "^16.3.1",
"fastify": "^5.0.0",
"pino": "^9.4.0",
"pino-pretty": "^11.2.2",
"thirdweb": "^5.48.1",
"viem": "^2.5.0"
"@aws-sdk/client-secrets-manager": "catalog:",
"@fastify/cors": "catalog:",
"@fastify/swagger": "catalog:",
"@fastify/swagger-ui": "catalog:",
"@fastify/type-provider-typebox": "catalog:",
"@prisma/client": "catalog:",
"@sentry/node": "catalog:",
"@sinclair/typebox": "catalog:",
"@thirdweb-dev/engine": "catalog:",
"@treasure-dev/auth": "workspace:*",
"@treasure-dev/tdk-core": "workspace:*",
"@wagmi/core": "catalog:",
"abitype": "catalog:",
"dotenv": "catalog:",
"fastify": "catalog:",
"pino": "catalog:",
"pino-pretty": "catalog:",
"thirdweb": "catalog:",
"viem": "catalog:"
},
"devDependencies": {
"prisma": "^5.3.0"
"prisma": "catalog:"
},
"engines": {
"node": ">=20"
Expand Down
31 changes: 20 additions & 11 deletions apps/api/src/routes/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ import {
} from "../schema";
import type { TdkApiContext } from "../types";
import { USER_PROFILE_SELECT_FIELDS, USER_SELECT_FIELDS } from "../utils/db";
import { transformUserProfileResponseFields } from "../utils/user";
import {
parseThirdwebUserEmail,
transformUserProfileResponseFields,
} from "../utils/user";

export const authRoutes =
({
auth,
thirdwebAuth,
db,
env,
client,
engine,
}: TdkApiContext): FastifyPluginAsync =>
Expand Down Expand Up @@ -117,18 +121,23 @@ export const authRoutes =
// Look up any associated user details in the embedded wallet
const thirdwebUser = await getUser({
client,
ecosystem: {
id: env.THIRDWEB_ECOSYSTEM_ID,
partnerId: env.THIRDWEB_ECOSYSTEM_PARTNER_ID,
},
walletAddress: adminAddress,
});
if (thirdwebUser?.email) {
user = await db.user.update({
where: {
id: user.id,
},
data: {
email: thirdwebUser.email,
},
select: USER_SELECT_FIELDS,
});
if (thirdwebUser) {
const email = parseThirdwebUserEmail(thirdwebUser);
if (email) {
user = await db.user.update({
where: {
id: user.id,
},
data: { email },
select: USER_SELECT_FIELDS,
});
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions apps/api/src/utils/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { type Static, Type } from "@sinclair/typebox";
import { AssertError, Value } from "@sinclair/typebox/value";
import "dotenv/config";

import { DEFAULT_TDK_ECOSYSTEM_ID } from "@treasure-dev/tdk-core";
import { log } from "./log";

// Parse local environment variables
Expand All @@ -31,6 +32,11 @@ const envSchema = Type.Object({
THIRDWEB_ENGINE_URL: Type.String(),
THIRDWEB_ENGINE_ACCESS_TOKEN: Type.String(),
THIRDWEB_SECRET_KEY: Type.String(),
THIRDWEB_ECOSYSTEM_ID: Type.TemplateLiteral(
[Type.Literal("ecosystem."), Type.String()],
{ default: DEFAULT_TDK_ECOSYSTEM_ID },
),
THIRDWEB_ECOSYSTEM_PARTNER_ID: Type.String(),
TREASURE_AUTH_KMS_KEY: Type.String(),
TROVE_API_URL: Type.String({ default: "https://trove-api.treasure.lol" }),
TROVE_API_KEY: Type.String(),
Expand Down
21 changes: 21 additions & 0 deletions apps/api/src/utils/user.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { UserProfile } from "@prisma/client";
import type { GetUserResult } from "thirdweb";

export const transformUserProfileResponseFields = (
profile: Partial<UserProfile>,
Expand All @@ -10,3 +11,23 @@ export const transformUserProfileResponseFields = (
testnetFaucetLastUsedAt:
profile.testnetFaucetLastUsedAt?.toISOString() ?? null,
});

export const parseThirdwebUserEmail = (user: GetUserResult) => {
if (user.email) {
return user.email;
}

const profileEmail = user.profiles.find(({ type }) => type === "email")
?.details.email;
if (profileEmail) {
return profileEmail;
}

for (const profile of user.profiles) {
if (profile.details.email) {
return profile.details.email;
}
}

return undefined;
};
2 changes: 2 additions & 0 deletions examples/connect-core/.env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
VITE_TDK_API_URL=http://localhost:8080
VITE_TDK_CLIENT_ID=
VITE_TDK_ECOSYSTEM_ID=ecosystem.treasure
VITE_TDK_ECOSYSTEM_PARTNER_ID=
VITE_TDK_BACKEND_WALLET=
4 changes: 2 additions & 2 deletions examples/connect-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Example showing how vanilla JS/TS front-ends can interact with the Treasure Deve
Install dependencies:

```bash
npm install
pnpm install
```

Create `.env` file based on example and fill in with relevant environment variables:
Expand All @@ -24,5 +24,5 @@ cp .env.example .env
Start server:

```bash
npm run dev
pnpm dev
```
Loading

0 comments on commit d468a1b

Please sign in to comment.