Skip to content

Commit

Permalink
Merge pull request #135 from P4-Games/feat/20241027
Browse files Browse the repository at this point in the history
merge feat/20241027 into develop
  • Loading branch information
dappsar authored Oct 27, 2024
2 parents e92d6c6 + dcf22bc commit 7551d26
Show file tree
Hide file tree
Showing 17 changed files with 475 additions and 95 deletions.
12 changes: 11 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@


FROM node:18-alpine AS deps
WORKDIR /app
COPY package.json package-lock.json ./
Expand All @@ -15,6 +17,8 @@ ARG BOT_API_TOKEN
ARG BOT_API_URL
ARG BOT_API_WAPP_ENABLED
ARG BACKEND_API_URL
ARG BACKEND_API_TOKEN
ARG BACKEND_API_TOKEN_SECRET
ARG NODE_PROVIDER_SEPOLIA_URL
ARG NODE_PROVIDER_MUMBAI_URL
ARG NODE_PROVIDER_SCROLL_URL
Expand All @@ -29,7 +33,6 @@ ARG RECAPTCHA_API_KEY
ARG NEXT_PUBLIC_RECAPTCHA_SITE_KEY
ARG NEXT_PUBLIC_NFT_IMAGE_REPOSITORY
ARG NEXT_PUBLIC_NFT_MARKETPLACE_URL

#
# env
ENV NODE_ENV production
Expand All @@ -41,6 +44,8 @@ ENV BOT_API_TOKEN $BOT_API_TOKEN
ENV BOT_API_URL $BOT_API_URL
ENV BOT_API_WAPP_ENABLED $BOT_API_WAPP_ENABLED
ENV BACKEND_API_URL $BACKEND_API_URL
ENV BACKEND_API_TOKEN $BACKEND_API_TOKEN
ENV BACKEND_API_TOKEN_SECRET $BACKEND_API_TOKEN_SECRET
ENV NODE_PROVIDER_SEPOLIA_URL $NODE_PROVIDER_SEPOLIA_URL
ENV NODE_PROVIDER_MUMBAI_URL $NODE_PROVIDER_MUMBAI_URL
ENV NODE_PROVIDER_SCROLL_URL $NODE_PROVIDER_SCROLL_URL
Expand All @@ -58,14 +63,19 @@ ENV NEXT_PUBLIC_NFT_MARKETPLACE_URL $NEXT_PUBLIC_NFT_MARKETPLACE_URL
#
COPY --from=deps /app/node_modules ./node_modules
COPY . .

RUN echo "***********************"
RUN env
RUN echo $BACKEND_API_TOKEN
RUN echo $BACKEND_API_TOKEN_SECRET
RUN echo "***********************"

RUN npm run build
#
FROM node:18-alpine AS runner
WORKDIR /app
ENV NODE_ENV production

RUN addgroup --system --gid 1001 nodejs && adduser --system --uid 1001 nextjs
COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/public ./public
Expand Down
137 changes: 84 additions & 53 deletions cloudbuild.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
steps:
# Para para generar un .env para que sea tomado por el step de build.
# Las variables serán tomadas de la definición del "trigger",
# "variables de sustitución", en Google Cloud Build.
# en gcp, se tiene que poner como nombre de cada variable, el unserscore
# como prefijo. Ejemplo: "_APP_ENV"
#
# This step is kept as a workaround, generating a .env file that is required by the
# library used with MongoDB. If it is removed, attempting to connect to the database
# throws an error indicating that the URI does not exist in the environment.
#
- name: 'gcr.io/cloud-builders/npm'
id: CreateEnv
Expand All @@ -17,6 +16,8 @@ steps:
- BOT_API_URL=${_BOT_API_URL}
- BOT_API_WAPP_ENABLED=${_BOT_API_WAPP_ENABLED}
- BACKEND_API_URL=${_BACKEND_API_URL}
- BACKEND_API_TOKEN=${_BACKEND_API_TOKEN}
#- BACKEND_API_TOKEN_SECRET=$$BACKEND_API_TOKEN_SECRET
- NODE_PROVIDER_SEPOLIA_URL=${_NODE_PROVIDER_SEPOLIA_URL}
- NODE_PROVIDER_MUMBAI_URL=${_NODE_PROVIDER_MUMBAI_URL}
- NODE_PROVIDER_SCROLL_URL=${_NODE_PROVIDER_SCROLL_URL}
Expand All @@ -31,67 +32,97 @@ steps:
- NEXT_PUBLIC_RECAPTCHA_SITE_KEY=${_NEXT_PUBLIC_RECAPTCHA_SITE_KEY}
- NEXT_PUBLIC_NFT_IMAGE_REPOSITORY=${_NEXT_PUBLIC_NFT_IMAGE_REPOSITORY}
- NEXT_PUBLIC_NFT_MARKETPLACE_URL=${_NEXT_PUBLIC_NFT_MARKETPLACE_URL}

# build the container images
- name: "gcr.io/cloud-builders/docker"
secretEnv: [
'BACKEND_API_TOKEN_SECRET'
]
#
# Build docker imagen.
# Variables with a prefix '_' are from Cloud Build 'substitution' (declared in the job).
# Variables with a prefix '$$' are from Secret Manager.
#
- name: 'gcr.io/cloud-builders/docker'
id: Build
args: [
"build",
"--no-cache",
"-t",
"$_AR_HOSTNAME/$PROJECT_ID/cloud-run-source-deploy/$REPO_NAME/$_SERVICE_NAME:$COMMIT_SHA",
"--build-arg", "APP_ENV=${_APP_ENV}",
"--build-arg", "MONGODB=${_MONGODB}",
"--build-arg", "MONGODB_BOT=${_MONGODB_BOT}",
"--build-arg", "DB_CHATTERPAY_NAME=${_DB_CHATTERPAY_NAME}",
"--build-arg", "RECAPTCHA_API_KEY=${_RECAPTCHA_API_KEY}",
"--build-arg", "NEXT_PUBLIC_RECAPTCHA_SITE_KEY=${_NEXT_PUBLIC_RECAPTCHA_SITE_KEY}",
"--build-arg", "NEXT_PUBLIC_NFT_IMAGE_REPOSITORY=${_NEXT_PUBLIC_NFT_IMAGE_REPOSITORY}",
"--build-arg", "NEXT_PUBLIC_NFT_MARKETPLACE_URL=${_NEXT_PUBLIC_NFT_MARKETPLACE_URL}",
"--build-arg", "BOT_API_TOKEN=${_BOT_API_TOKEN}",
"--build-arg", "BOT_API_URL=${_BOT_API_URL}",
"--build-arg", "BOT_API_WAPP_ENABLED=${_BOT_API_WAPP_ENABLED}",
"--build-arg", "BACKEND_API_URL=${_BACKEND_API_URL}",
"--build-arg", "NODE_PROVIDER_SEPOLIA_URL=${_NODE_PROVIDER_SEPOLIA_URL}",
"--build-arg", "NODE_PROVIDER_MUMBAI_URL=${_NODE_PROVIDER_MUMBAI_URL}",
"--build-arg", "NODE_PROVIDER_SCROLL_URL=${_NODE_PROVIDER_SCROLL_URL}",
"--build-arg", "HANDLE_VERCEL_FREE_PLAN_TIMEOUT=${_HANDLE_VERCEL_FREE_PLAN_TIMEOUT}",
"--build-arg", "NEXT_PUBLIC_USE_MOCK=${_NEXT_PUBLIC_USE_MOCK}",
"--build-arg", "API3_ENABLED=${_API3_ENABLED}",
"--build-arg", "JWT_SECRET=${_JWT_SECRET}",
"--build-arg", "NEXT_PUBLIC_FROM_ICP=${_NEXT_PUBLIC_FROM_ICP}",
"--build-arg", "NEXT_PUBLIC_UI_URL=${_NEXT_PUBLIC_UI_URL}",
"--build-arg", "NEXT_PUBLIC_ALLOWED_ORIGINS=${_NEXT_PUBLIC_ALLOWED_ORIGINS}",
"."
'build',
'--no-cache',
'-t',
'$_AR_HOSTNAME/$PROJECT_ID/cloud-run-source-deploy/$REPO_NAME/$_SERVICE_NAME:$COMMIT_SHA',
'--build-arg', 'APP_ENV=${_APP_ENV}',
'--build-arg', 'MONGODB=${_MONGODB}',
'--build-arg', 'MONGODB_BOT=${_MONGODB_BOT}',
'--build-arg', 'DB_CHATTERPAY_NAME=${_DB_CHATTERPAY_NAME}',
'--build-arg', 'RECAPTCHA_API_KEY=${_RECAPTCHA_API_KEY}',
'--build-arg', 'NEXT_PUBLIC_RECAPTCHA_SITE_KEY=${_NEXT_PUBLIC_RECAPTCHA_SITE_KEY}',
'--build-arg', 'NEXT_PUBLIC_NFT_IMAGE_REPOSITORY=${_NEXT_PUBLIC_NFT_IMAGE_REPOSITORY}',
'--build-arg', 'NEXT_PUBLIC_NFT_MARKETPLACE_URL=${_NEXT_PUBLIC_NFT_MARKETPLACE_URL}',
'--build-arg', 'BOT_API_TOKEN=${_BOT_API_TOKEN}',
'--build-arg', 'BOT_API_URL=${_BOT_API_URL}',
'--build-arg', 'BOT_API_WAPP_ENABLED=${_BOT_API_WAPP_ENABLED}',
'--build-arg', 'BACKEND_API_URL=${_BACKEND_API_URL}',
'--build-arg', 'BACKEND_API_TOKEN=${_BACKEND_API_TOKEN}',
'--build-arg', 'BACKEND_API_TOKEN_SECRET=$$BACKEND_API_TOKEN_SECRET',
'--build-arg', 'NODE_PROVIDER_SEPOLIA_URL=${_NODE_PROVIDER_SEPOLIA_URL}',
'--build-arg', 'NODE_PROVIDER_MUMBAI_URL=${_NODE_PROVIDER_MUMBAI_URL}',
'--build-arg', 'NODE_PROVIDER_SCROLL_URL=${_NODE_PROVIDER_SCROLL_URL}',
'--build-arg', 'HANDLE_VERCEL_FREE_PLAN_TIMEOUT=${_HANDLE_VERCEL_FREE_PLAN_TIMEOUT}',
'--build-arg', 'NEXT_PUBLIC_USE_MOCK=${_NEXT_PUBLIC_USE_MOCK}',
'--build-arg', 'API3_ENABLED=${_API3_ENABLED}',
'--build-arg', 'JWT_SECRET=${_JWT_SECRET}',
'--build-arg', 'NEXT_PUBLIC_FROM_ICP=${_NEXT_PUBLIC_FROM_ICP}',
'--build-arg', 'NEXT_PUBLIC_UI_URL=${_NEXT_PUBLIC_UI_URL}',
'--build-arg', 'NEXT_PUBLIC_ALLOWED_ORIGINS=${_NEXT_PUBLIC_ALLOWED_ORIGINS}',
'.'
]
secretEnv: [
'BACKEND_API_TOKEN_SECRET'
]

#
# push the container images to Container Registry
- name: "gcr.io/cloud-builders/docker"
#
- name: 'gcr.io/cloud-builders/docker'
id: Push
args: [
"push",
"$_AR_HOSTNAME/$PROJECT_ID/cloud-run-source-deploy/$REPO_NAME/$_SERVICE_NAME:$COMMIT_SHA"
'push',
'$_AR_HOSTNAME/$PROJECT_ID/cloud-run-source-deploy/$REPO_NAME/$_SERVICE_NAME:$COMMIT_SHA'
]

#
# Deploy container images to Cloud Run
- name: "gcr.io/cloud-builders/gcloud"
#
- name: 'gcr.io/cloud-builders/gcloud'
id: Deploy
entrypoint: gcloud
args: [
"run",
"deploy",
"$_SERVICE_NAME",
"--platform",
"managed",
"--region",
"$_DEPLOY_REGION",
"--image",
"$_AR_HOSTNAME/$PROJECT_ID/cloud-run-source-deploy/$REPO_NAME/$_SERVICE_NAME:$COMMIT_SHA",
"--labels",
"managed-by=gcp-cloud-build-deploy-cloud-run,commit-sha=$COMMIT_SHA,gcb-build-id=$BUILD_ID,gcb-trigger-id=$_TRIGGER_ID",
"--quiet",
"--allow-unauthenticated",
'run',
'deploy',
'$_SERVICE_NAME',
'--platform',
'managed',
'--region',
'$_DEPLOY_REGION',
'--image',
'$_AR_HOSTNAME/$PROJECT_ID/cloud-run-source-deploy/$REPO_NAME/$_SERVICE_NAME:$COMMIT_SHA',
'--labels',
'managed-by=gcp-cloud-build-deploy-cloud-run,commit-sha=$COMMIT_SHA,gcb-build-id=$BUILD_ID,gcb-trigger-id=$_TRIGGER_ID',
'--quiet',
'--allow-unauthenticated',
]
#
# print secret variables
#
- name: 'bash'
script: |
#!/usr/bin/env bash
echo 'BACKEND_API_TOKEN en cloudbuild.yaml 1: $BACKEND_API_TOKEN_SECRET'
secretEnv: [
'BACKEND_API_TOKEN_SECRET'
]

availableSecrets:
secretManager:
- versionName: 'projects/$PROJECT_ID/secrets/${_BACKEND_API_TOKEN_SECRET_NAME}/versions/latest'
env: 'BACKEND_API_TOKEN_SECRET'

images:
- $_AR_HOSTNAME/$PROJECT_ID/cloud-run-source-deploy/$REPO_NAME/$_SERVICE_NAME:$COMMIT_SHA

Expand Down
1 change: 1 addition & 0 deletions example_env
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ APP_ENV='development'
BOT_API_TOKEN={api token}
BOT_API_URL=http://localhost:3000
BACKEND_API_URL=https://dev.back.chatterpay.net
BACKEND_API_TOKEN='backend token'
BOT_API_WAPP_ENABLED=true
MONGODB='mongodb://localhost:27017'
MONGODB_BOT='BOT mongo db url'
Expand Down
14 changes: 9 additions & 5 deletions scripts/docker-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@ docker build \
--build-arg MONGODB="$MONGODB" \
--build-arg MONGODB_BOT="$MONGODB_BOT" \
--build-arg DB_CHATTERPAY_NAME="$DB_CHATTERPAY_NAME" \
--build-arg RECAPTCHA_API_KEY="$RECAPTCHA_API_KEY" \
--build-arg NEXT_PUBLIC_RECAPTCHA_SITE_KEY="$NEXT_PUBLIC_RECAPTCHA_SITE_KEY" \
--build-arg NEXT_PUBLIC_NFT_IMAGE_REPOSITORY="$NEXT_PUBLIC_NFT_IMAGE_REPOSITORY" \
--build-arg NEXT_PUBLIC_NFT_MARKETPLACE_URL="$NEXT_PUBLIC_NFT_MARKETPLACE_URL" \
--build-arg BOT_API_TOKEN="$BOT_API_TOKEN" \
--build-arg BOT_API_URL="$BOT_API_URL" \
--build-arg BOT_API_WAPP_ENABLED="$BOT_API_WAPP_ENABLED" \
--build-arg BACKEND_API_URL="$BACKEND_API_URL" \
--build-arg BACKEND_API_TOKEN="$BACKEND_API_TOKEN" \
--build-arg NODE_PROVIDER_SEPOLIA_URL="$NODE_PROVIDER_SEPOLIA_URL" \
--build-arg NODE_PROVIDER_MUMBAI_URL="$NODE_PROVIDER_MUMBAI_URL" \
--build-arg NODE_PROVIDER_SCROLL_URL="$NODE_PROVIDER_SCROLL_URL" \
--build-arg HANDLE_VERCEL_FREE_PLAN_TIMEOUT="$HANDLE_VERCEL_FREE_PLAN_TIMEOUT" \
--build-arg NEXT_PUBLIC_USE_MOCK="$NEXT_PUBLIC_USE_MOCK" \
--build-arg API3_ENABLED="$API3_ENABLED" \
--build-arg JWT_SECRET="$JWT_SECRET" \
--build-arg NEXT_PUBLIC_FROM_ICP="$NEXT_PUBLIC_FROM_ICP" \
--build-arg NEXT_PUBLIC_USE_MOCK="$NEXT_PUBLIC_USE_MOCK" \
--build-arg NEXT_PUBLIC_UI_URL="$NEXT_PUBLIC_UI_URL" \
--build-arg NEXT_PUBLIC_ALLOWED_ORIGINS="$NEXT_PUBLIC_ALLOWED_ORIGINS" \
--build-arg RECAPTCHA_API_KEY="$RECAPTCHA_API_KEY" \
--build-arg NEXT_PUBLIC_RECAPTCHA_SITE_KEY="$NEXT_PUBLIC_RECAPTCHA_SITE_KEY" \
--build-arg NEXT_PUBLIC_NFT_IMAGE_REPOSITORY="$NEXT_PUBLIC_NFT_IMAGE_REPOSITORY" \
--build-arg NEXT_PUBLIC_NFT_MARKETPLACE_URL="$NEXT_PUBLIC_NFT_MARKETPLACE_URL" \
-t my-nextjs-app ..
41 changes: 17 additions & 24 deletions src/app/api/_data/blk-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
defaultBalance,
BACKEND_API_URL,
tokensByNetwork,
BACKEND_API_TOKEN,
nodeProviderUrlSepolia
} from 'src/config-global'

Expand All @@ -16,6 +17,17 @@ import TokenPriceFeedsAbi from './_abis/TokenPriceFeedsAbi.json'

// ---------------------------------------------------------------------------------------------

export async function transferAll(walletTo: string): Promise<boolean> {
try {
// TODO:
// const data = { wallet: walletTo }
// const response = await axios.post(`${BACKEND_API_URL}/transferall`, data)
return true
} catch (error) {
console.error('Error transfering all funds:', error)
throw error
}
}
export async function getBalancesWithTotalsFromBackend(walletAddress: string): Promise<IBalances> {
let balances: IBalances
const cacheKey = `getBalancesWithTotalsFromBackend.${walletAddress}`
Expand All @@ -27,7 +39,11 @@ export async function getBalancesWithTotalsFromBackend(walletAddress: string): P
}

try {
const response = await axios.get(`${BACKEND_API_URL}/balance/${walletAddress}`)
const response = await axios.get(`${BACKEND_API_URL}/balance/${walletAddress}`, {
headers: {
Authorization: `Bearer ${BACKEND_API_TOKEN}`
}
})
const data = response.data as IBalances

// Convert keys to lowercase
Expand Down Expand Up @@ -211,29 +227,6 @@ const fethCustomTokens = async (address: string) => {
}
}

/*
const fetchNftById = async (id: string) => {
try {
const response = await axios.get(`${BACKEND_API_URL}/nft/${id}`)
const { image, image_url, ...rest } = response.data
const default_image_url = {
gcp: '/assets/images/nfts/default_nft.png',
ipfs: '/assets/images/nfts/default_nft.png',
icp: '/assets/images/nfts/default_nft.png'
}
const result = {
...rest,
image_url: image_url || image || default_image_url
}
return result
} catch (error) {
console.error('Error fetching nft id:', id, error)
throw error
}
}
*/
async function getTokenBalance(
tokenContract: ethers.Contract,
walletAddress: string
Expand Down
3 changes: 2 additions & 1 deletion src/app/api/_hooks/api-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,15 @@ export const endpoints = {
wallet: {
balance: (id: string) => getFullUIEndpoint(`wallet/${id}/balance`),
transactions: (id: string) => getFullUIEndpoint(`wallet/${id}/transactions`),
transferAll: (id: string) => getFullUIEndpoint(`wallet/${id}/transferAll`),
nfts: {
root: (id: string) => getFullUIEndpoint(`wallet/${id}/nfts`),
id: (walletId: string, nftId: string) =>
getFullUIEndpoint(`wallet/${walletId}/nfts/${nftId}`)
}
}
},
backend: {
backend_bot: {
sendMessage: () => getFullBotEndpoint('chatbot/conversations/send-message'),
control: () => getFullBotEndpoint('chatbot/conversations/control')
}
Expand Down
9 changes: 8 additions & 1 deletion src/app/api/_hooks/use-wallet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { endpoints } from 'src/app/api/_hooks/api-resolver'
import { post, endpoints } from 'src/app/api/_hooks/api-resolver'

import { useGetCommon } from './common'

Expand All @@ -19,3 +19,10 @@ export function useGetWalletNfts(walletId: string) {
export function useGetWalletNft(walletId: string, nftId: string) {
return useGetCommon(endpoints.dashboard.wallet.nfts.id(walletId, nftId))
}

// ----------------------------------------------------------------------

export async function transferAll(walletId: string, data: { walletTo: string }) {
const res = await post(endpoints.dashboard.wallet.transferAll(walletId), data, {})
return res
}
2 changes: 1 addition & 1 deletion src/app/api/v1/_common/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { post, endpoints } from '../../_hooks/api-resolver'
// ----------------------------------------------------------------------

export async function send2FACode(phone: string, code: number, codeMsg: string) {
const botSendMsgEndpoint = endpoints.backend.sendMessage()
const botSendMsgEndpoint = endpoints.backend_bot.sendMessage()
const botSendMsgData = {
data_token: BOT_API_TOKEN,
channel_user_id: phone,
Expand Down
6 changes: 3 additions & 3 deletions src/app/api/v1/user/[id]/email/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ export async function POST(req: NextRequest, { params }: { params: IParams }) {
}

user.email = email
const reult: boolean = await updateUserEmail(user)
const result: boolean = await updateUserEmail(user)

if (!reult) {
if (!result) {
return new NextResponse(
JSON.stringify({
code: 'USER_UPDATE_ERROR',
Expand All @@ -83,7 +83,7 @@ export async function POST(req: NextRequest, { params }: { params: IParams }) {

updateUserCode(user.id, undefined)

return NextResponse.json({ reult })
return NextResponse.json({ result })
} catch (ex) {
console.error(ex)
return new NextResponse(JSON.stringify({ error: 'Error in update user' }), {
Expand Down
Loading

0 comments on commit 7551d26

Please sign in to comment.