From 59a2e7a5019cdd9ad4441e5fab013235141ffab4 Mon Sep 17 00:00:00 2001 From: SIDANWhatever Date: Sun, 2 Mar 2025 18:07:42 +0800 Subject: [PATCH 01/18] feat: test ci --- .github/workflows/deploy.yml | 45 +++++++++++ apps/mesh-cloud/src/env.js | 7 +- .../src/hooks/useValidateStaking.ts | 11 ++- .../src/pages/api/blockfrost/[...slug].ts | 81 +++++++++++++++++++ backend/express/package.json | 2 +- package.json | 5 +- turbo.json | 9 ++- 7 files changed, 152 insertions(+), 8 deletions(-) create mode 100644 .github/workflows/deploy.yml create mode 100644 apps/mesh-cloud/src/pages/api/blockfrost/[...slug].ts diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..9b17c0a --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,45 @@ +# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created +# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages + +name: Lint, Test, and Build + +on: + release: + types: [created] + push: + branches: + - ci/github-action + pull_request: + branches: + - main + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + node-version: [20] + steps: + - uses: actions/checkout@v4 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v4 + - name: Install dependencies + run: npm install + - name: Build + run: npm run build + - name: Build + run: npm run build:mesh && npm run build:docs && npm run build:apps && npm run build:scripts + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + - name: Authenticate to Google Cloud + uses: google-github-actions/auth@v1 + with: + credentials_json: ${{ secrets.GCP_CREDENTIALS }} + - name: Set up Google Cloud SDK + uses: google-github-actions/setup-gcloud@v1 + with: + project_id: mesh-432507 + install_components: "gcloud" + - name: Deploy Backend + run: npm run deploy diff --git a/apps/mesh-cloud/src/env.js b/apps/mesh-cloud/src/env.js index 219f0b8..09c288d 100644 --- a/apps/mesh-cloud/src/env.js +++ b/apps/mesh-cloud/src/env.js @@ -34,7 +34,7 @@ export const env = createEnv({ */ client: { NEXT_PUBLIC_BACKEND_URL: z.string().url(), - NEXT_PUBLIC_BLOCKFROST_API_KEY_PREPROD: z.string(), + // NEXT_PUBLIC_BLOCKFROST_API_KEY_PREPROD: z.string(), }, /** @@ -49,7 +49,10 @@ export const env = createEnv({ NEXT_PUBLIC_BACKEND_URL: process.env.NEXT_PUBLIC_BACKEND_URL, // DISCORD_CLIENT_ID: process.env.DISCORD_CLIENT_ID, // DISCORD_CLIENT_SECRET: process.env.DISCORD_CLIENT_SECRET, - NEXT_PUBLIC_BLOCKFROST_API_KEY_PREPROD: process.env.NEXT_PUBLIC_BLOCKFROST_API_KEY_PREPROD + // BLOCKFROST_API_KEY_PREPROD: process.env.BLOCKFROST_API_KEY_PREPROD, + // BLOCKFROST_API_KEY_TESTNET: process.env.BLOCKFROST_API_KEY_TESTNET, + // BLOCKFROST_API_KEY_MAINNET: process.env.BLOCKFROST_API_KEY_MAINNET, + // BLOCKFROST_API_KEY_PREVIEW: process.env.BLOCKFROST_API_KEY_PREVIEW, }, /** * Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. This is especially diff --git a/apps/mesh-cloud/src/hooks/useValidateStaking.ts b/apps/mesh-cloud/src/hooks/useValidateStaking.ts index 2912500..3372556 100644 --- a/apps/mesh-cloud/src/hooks/useValidateStaking.ts +++ b/apps/mesh-cloud/src/hooks/useValidateStaking.ts @@ -10,12 +10,19 @@ import { import { useWallet } from "@meshsdk/react"; import { useCallback, useEffect, useState } from "react"; -const blockfrostApiKey = process.env.NEXT_PUBLIC_BLOCKFROST_API_KEY_PREPROD!; const meshPoolId = process.env.NEXT_PUBLIC_MESH_POOL_ID!; const meshDRepId = process.env.NEXT_PUBLIC_MESH_DREP_ID!; // (Leon, 10/08/2024) - Use CIP-105 DRep ID for backward compatibility, as current Mesh csl does not support CIP-129 yet +export function getProvider(network = "preprod") { + const blockchainProvider = new BlockfrostProvider( + `/api/blockfrost/${network}/`, + ); + blockchainProvider.setSubmitTxToBytes(false); + return blockchainProvider; +} + export const useValidateStaking = () => { - const blockchainProvider = new BlockfrostProvider(blockfrostApiKey); + const blockchainProvider = getProvider("mainnet"); const walletInfo = useWallet(); diff --git a/apps/mesh-cloud/src/pages/api/blockfrost/[...slug].ts b/apps/mesh-cloud/src/pages/api/blockfrost/[...slug].ts new file mode 100644 index 0000000..1fe32b2 --- /dev/null +++ b/apps/mesh-cloud/src/pages/api/blockfrost/[...slug].ts @@ -0,0 +1,81 @@ +import type { NextApiRequest, NextApiResponse } from "next"; +import axios from "axios"; + +import { toBytes } from "@meshsdk/common"; + +export default async function handler( + _req: NextApiRequest, + _res: NextApiResponse, +) { + try { + let { slug } = _req.query; + slug = slug as string[]; + + const network = slug[0]; + let key = process.env.BLOCKFROST_API_KEY_PREPROD; + switch (network) { + case "testnet": + key = process.env.BLOCKFROST_API_KEY_TESTNET; + break; + case "mainnet": + key = process.env.BLOCKFROST_API_KEY_MAINNET; + break; + case "preview": + key = process.env.BLOCKFROST_API_KEY_PREVIEW; + break; + } + + const axiosInstance = axios.create({ + baseURL: `https://cardano-${network}.blockfrost.io/api/v0`, + headers: { project_id: key }, + }); + + /** + * get url from slug + */ + let url = slug?.slice(1).join("/"); + + // get params if exists + const params = _req.query; + delete _req.query.slug; + if (Object.keys(params).length > 0) { + url += "?"; + + for (const key in params) { + url += `${key}=${params[key] as string}&`; + } + } + + // end get params if exists + if (url == "tx/submit") { + const body = _req.body; + + const headers = { "Content-Type": "application/cbor" }; + const { data, status } = await axiosInstance.post( + "tx/submit", + toBytes(body), + { + headers, + }, + ); + _res.status(status).json(data); + } else if (url == "utils/txs/evaluate") { + const body = _req.body; + + const headers = { "Content-Type": "application/cbor" }; + const { status, data } = await axiosInstance.post( + "utils/txs/evaluate", + body, + { + headers, + }, + ); + _res.status(status).json(data); + } else { + const { data, status } = await axiosInstance.get(`${url}`); + _res.status(status).json(data); + } + } catch (error) { + _res.status(500).json(error); + } +} diff --git a/backend/express/package.json b/backend/express/package.json index 7a1bc6c..df15b7a 100644 --- a/backend/express/package.json +++ b/backend/express/package.json @@ -55,4 +55,4 @@ "turbo": "^2.1.3", "typescript": "^5.6.3" } -} +} \ No newline at end of file diff --git a/package.json b/package.json index 682fede..da6f5be 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,8 @@ "dev": "turbo dev", "lint": "turbo lint", "format": "prettier --write \"**/*.{ts,tsx,md}\"", - "clean": "turbo run clean && rm -rf .turbo && rm -rf dist && rm -rf node_modules && rm package-lock.json" + "clean": "turbo run clean && rm -rf .turbo && rm -rf dist && rm -rf node_modules && rm package-lock.json", + "deploy": "turbo run sh:deploy" }, "devDependencies": { "prettier": "^3.2.5", @@ -23,4 +24,4 @@ "apps/*", "backend/*" ] -} +} \ No newline at end of file diff --git a/turbo.json b/turbo.json index 8930381..56c2989 100644 --- a/turbo.json +++ b/turbo.json @@ -2,6 +2,11 @@ "$schema": "https://turbo.build/schema.json", "ui": "tui", "tasks": { + "sh:deploy": { + "dependsOn": [ + "^build" + ] + }, "build": { "dependsOn": [ "^build:backend", @@ -40,7 +45,9 @@ ] }, "dev": { - "dependsOn": ["^build"], + "dependsOn": [ + "^build" + ], "cache": false, "persistent": true }, From 1321118239228916375a19c253a16288393acb88 Mon Sep 17 00:00:00 2001 From: SIDANWhatever Date: Sun, 2 Mar 2025 18:22:11 +0800 Subject: [PATCH 02/18] feat: test ci --- apps/mesh-cloud/package.json | 4 ++-- apps/mesh-cloud/src/apps/mesh-utils/serializeNativeScript.tsx | 2 ++ apps/mesh-cloud/src/env.js | 4 ++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/apps/mesh-cloud/package.json b/apps/mesh-cloud/package.json index 30ab682..7953012 100644 --- a/apps/mesh-cloud/package.json +++ b/apps/mesh-cloud/package.json @@ -42,7 +42,7 @@ "copy-to-clipboard": "^3.3.3", "geist": "^1.3.0", "lucide-react": "^0.436.0", - "next": "^14.2.4", + "next": "^15.2.0", "next-auth": "^4.24.7", "react": "^18.3.1", "react-dom": "^18.3.1", @@ -74,4 +74,4 @@ "initVersion": "7.37.0" }, "packageManager": "npm@10.7.0" -} +} \ No newline at end of file diff --git a/apps/mesh-cloud/src/apps/mesh-utils/serializeNativeScript.tsx b/apps/mesh-cloud/src/apps/mesh-utils/serializeNativeScript.tsx index 6a069e5..cc1b4d5 100644 --- a/apps/mesh-cloud/src/apps/mesh-utils/serializeNativeScript.tsx +++ b/apps/mesh-cloud/src/apps/mesh-utils/serializeNativeScript.tsx @@ -39,6 +39,8 @@ export default function SerializeNativeScript() { ], }; + console.log("EXPRESS_BACKEND_URL", EXPRESS_BACKEND_URL); + const res = await axios.post( `${EXPRESS_BACKEND_URL}users/meshUtilities/serializers/serializeNativeScript`, { diff --git a/apps/mesh-cloud/src/env.js b/apps/mesh-cloud/src/env.js index 09c288d..118944b 100644 --- a/apps/mesh-cloud/src/env.js +++ b/apps/mesh-cloud/src/env.js @@ -33,7 +33,7 @@ export const env = createEnv({ * `NEXT_PUBLIC_`. */ client: { - NEXT_PUBLIC_BACKEND_URL: z.string().url(), + NEXT_PUBLIC_EXPRESS_BACKEND_URL: z.string().url(), // NEXT_PUBLIC_BLOCKFROST_API_KEY_PREPROD: z.string(), }, @@ -46,7 +46,7 @@ export const env = createEnv({ NODE_ENV: process.env.NODE_ENV, // NEXTAUTH_SECRET: process.env.NEXTAUTH_SECRET, // NEXTAUTH_URL: process.env.NEXTAUTH_URL, - NEXT_PUBLIC_BACKEND_URL: process.env.NEXT_PUBLIC_BACKEND_URL, + NEXT_PUBLIC_EXPRESS_BACKEND_URL: process.env.NEXT_PUBLIC_EXPRESS_BACKEND_URL, // DISCORD_CLIENT_ID: process.env.DISCORD_CLIENT_ID, // DISCORD_CLIENT_SECRET: process.env.DISCORD_CLIENT_SECRET, // BLOCKFROST_API_KEY_PREPROD: process.env.BLOCKFROST_API_KEY_PREPROD, From f5fb3edc8c5765194cd26b72feb00f23b97952d2 Mon Sep 17 00:00:00 2001 From: SIDANWhatever Date: Sun, 2 Mar 2025 18:26:03 +0800 Subject: [PATCH 03/18] feat: test ci --- .github/workflows/deploy.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 9b17c0a..3d4c935 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -27,9 +27,6 @@ jobs: run: npm install - name: Build run: npm run build - - name: Build - run: npm run build:mesh && npm run build:docs && npm run build:apps && npm run build:scripts - - name: Set up Docker Buildx uses: docker/setup-buildx-action@v2 - name: Authenticate to Google Cloud From 9c228b10d078c9837169ed391c33df52a132efa6 Mon Sep 17 00:00:00 2001 From: SIDANWhatever Date: Sun, 2 Mar 2025 18:26:21 +0800 Subject: [PATCH 04/18] feat: test ci --- apps/mesh-cloud/src/env.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/mesh-cloud/src/env.js b/apps/mesh-cloud/src/env.js index 118944b..faffdb2 100644 --- a/apps/mesh-cloud/src/env.js +++ b/apps/mesh-cloud/src/env.js @@ -33,7 +33,7 @@ export const env = createEnv({ * `NEXT_PUBLIC_`. */ client: { - NEXT_PUBLIC_EXPRESS_BACKEND_URL: z.string().url(), + // NEXT_PUBLIC_EXPRESS_BACKEND_URL: z.string().url(), // NEXT_PUBLIC_BLOCKFROST_API_KEY_PREPROD: z.string(), }, @@ -46,7 +46,7 @@ export const env = createEnv({ NODE_ENV: process.env.NODE_ENV, // NEXTAUTH_SECRET: process.env.NEXTAUTH_SECRET, // NEXTAUTH_URL: process.env.NEXTAUTH_URL, - NEXT_PUBLIC_EXPRESS_BACKEND_URL: process.env.NEXT_PUBLIC_EXPRESS_BACKEND_URL, + // NEXT_PUBLIC_EXPRESS_BACKEND_URL: process.env.NEXT_PUBLIC_EXPRESS_BACKEND_URL, // DISCORD_CLIENT_ID: process.env.DISCORD_CLIENT_ID, // DISCORD_CLIENT_SECRET: process.env.DISCORD_CLIENT_SECRET, // BLOCKFROST_API_KEY_PREPROD: process.env.BLOCKFROST_API_KEY_PREPROD, From 7d77100093adc2241eb2114e7e5cbee123692393 Mon Sep 17 00:00:00 2001 From: SIDANWhatever Date: Sun, 2 Mar 2025 20:33:39 +0800 Subject: [PATCH 05/18] chore: update env --- apps/mesh-cloud/src/lib/axios.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/apps/mesh-cloud/src/lib/axios.ts b/apps/mesh-cloud/src/lib/axios.ts index 2b87035..7115128 100644 --- a/apps/mesh-cloud/src/lib/axios.ts +++ b/apps/mesh-cloud/src/lib/axios.ts @@ -1,7 +1,6 @@ import axios from "axios"; -import { env } from "@/env"; -const url = env.NEXT_PUBLIC_BACKEND_URL; +const url = process.env.NEXT_PUBLIC_YACI_BACKEND_URL; export const instance = axios.create({ baseURL: url, From e5512d44e117a3d91a1209ae8354e79ed61c0f14 Mon Sep 17 00:00:00 2001 From: SIDANWhatever Date: Sun, 2 Mar 2025 20:38:03 +0800 Subject: [PATCH 06/18] chore: fix linting --- apps/mesh-cloud/src/apps/dev/transaction.tsx | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/apps/mesh-cloud/src/apps/dev/transaction.tsx b/apps/mesh-cloud/src/apps/dev/transaction.tsx index 99837c2..96d40a5 100644 --- a/apps/mesh-cloud/src/apps/dev/transaction.tsx +++ b/apps/mesh-cloud/src/apps/dev/transaction.tsx @@ -32,9 +32,7 @@ export function SelectNetwork({ return (