diff --git a/samples/minting-backend-with-postgres/.env b/samples/minting-backend-with-postgres/.env new file mode 100644 index 0000000000..0269ddb389 --- /dev/null +++ b/samples/minting-backend-with-postgres/.env @@ -0,0 +1,10 @@ +# ERC721 or ERC1155 contract address +COLLECTION_ADDRESS= +# immutable secret API from hub.immutable.com +IM_API_KEY= + +## DB +PG_USER=postgres +PG_PASSWORD=password +PG_HOST=postgres +DB_NAME=minting_backend \ No newline at end of file diff --git a/samples/minting-backend-with-postgres/.gitignore b/samples/minting-backend-with-postgres/.gitignore index f78b1dfaf9..d424775f46 100644 --- a/samples/minting-backend-with-postgres/.gitignore +++ b/samples/minting-backend-with-postgres/.gitignore @@ -66,4 +66,6 @@ package-lock.json # generated code examples/typescript-server.js -test/types/index.js \ No newline at end of file +test/types/index.js + +!.env \ No newline at end of file diff --git a/samples/minting-backend-with-postgres/README.md b/samples/minting-backend-with-postgres/README.md index 6fe0ae27ba..5138d4e540 100644 --- a/samples/minting-backend-with-postgres/README.md +++ b/samples/minting-backend-with-postgres/README.md @@ -4,10 +4,16 @@ This sample app uses Immutable minting backend SDK to manage minting. This repo ## Get Started +### pre-requisites +- NodeJS >= v20 +- Docker +- You have ERC721 or ERC1155 deployed +- You have enabled [minting api](https://docs.immutable.com/docs/zkEVM/products/minting/minting-api#minting-api-prerequisites) for your contract. + ### Install dependency ```bash -npm i # with nodejs version >= 20 +npm i ``` ### expose local port for webhook @@ -19,20 +25,22 @@ You can use services like below to expose ports locally. Please make sure the url with port 3000 exposed is set up in the webhook section in [Immutable Hub](hub.immmutable.com). -### run server +### Set environment variables +Set `COLLECTION_ADDRESS` and `IM_API_KEY` in the `./.env` file. + +### Run the app ```bash -# Replace the contract address and api key from hub in the command below -COLLECTION_ADDRESS={your_nft_contract_address} IM_API_KEY={your_api_key} npm run dev +docker compose up --build ``` -This will start a fastify server at port 3000. +This will start a fastify server at port 3000 and postgres container. Make sure to trigger the confirmation event again on webhook page via hub if you have missed it the first time. -### mint +### Mint -Replace YOUR_WALLET_ADDRESS with your NFT recipient's wallet address. You can get a random eth address from [here](https://vanity-eth.tk/). +Replace `YOUR_WALLET_ADDRESS` with your NFT recipient's wallet address in the below command. You can get a random eth address from [here](https://vanity-eth.tk/). ``` curl -X POST -H "Content-Type: application/json" -d '{"mintTo": "YOUR_WALLET_ADDRESS"}' http://localhost:3000/mint diff --git a/samples/minting-backend-with-postgres/docker-compose.yml b/samples/minting-backend-with-postgres/docker-compose.yml new file mode 100644 index 0000000000..77438b8d28 --- /dev/null +++ b/samples/minting-backend-with-postgres/docker-compose.yml @@ -0,0 +1,26 @@ +version: '3.9' +services: + postgres: + image: postgres:14 + restart: always + environment: + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=password + - POSTGRES_DB=minting_backend + expose: + - '5432' + ports: + - 5432:5432 + volumes: + - ../../packages/minting-backend/sdk/src/persistence/pg/seed.sql:/docker-entrypoint-initdb.d/seed.sql + backend: + image: node:20-alpine + restart: always + ports: + - 3000:3000 + depends_on: + - postgres + volumes: + - ./:/app + working_dir: /app + command: npm run dev diff --git a/samples/minting-backend-with-postgres/index.ts b/samples/minting-backend-with-postgres/index.ts index 242eeb46eb..0d6b73f565 100644 --- a/samples/minting-backend-with-postgres/index.ts +++ b/samples/minting-backend-with-postgres/index.ts @@ -2,6 +2,7 @@ import Fastify, { FastifyReply, FastifyRequest } from 'fastify' import { mintingBackend, config } from '@imtbl/sdk'; import { Pool } from 'pg'; import { v4 as uuidv4, parse } from 'uuid'; +import 'dotenv/config' const fastify = Fastify({ logger: true @@ -67,7 +68,7 @@ fastify.post(url, async (request: FastifyRequest, reply: any) => { */ const start = async () => { try { - await fastify.listen({ port: 3000 }) + await fastify.listen({ port: 3000, host: "0.0.0.0" }) // long running process to submit minting requests await minting.submitMintingRequests({}); diff --git a/samples/minting-backend-with-postgres/package.json b/samples/minting-backend-with-postgres/package.json index 8488b0ed02..6194047998 100644 --- a/samples/minting-backend-with-postgres/package.json +++ b/samples/minting-backend-with-postgres/package.json @@ -13,6 +13,7 @@ "@imtbl/sdk": "alpha", "@types/pg": "^8.11.6", "@types/uuid": "^9.0.8", + "dotenv": "^16.4.5", "fastify": "^4.27.0", "pg": "^8.11.5", "uuid": "^9.0.1" diff --git a/samples/minting-backend-with-postgres/seed.sql b/samples/minting-backend-with-postgres/seed.sql deleted file mode 100644 index 4311cb2107..0000000000 --- a/samples/minting-backend-with-postgres/seed.sql +++ /dev/null @@ -1,21 +0,0 @@ -CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; - -CREATE TABLE IF NOT EXISTS im_assets ( - id UUID PRIMARY KEY DEFAULT gen_random_uuid(), - asset_id UUID NOT NULL, - owner_address varchar NOT NULL, -- TODO: should be case insensitive - metadata jsonb, - token_id varchar NULL, - contract_address varchar NOT NULL, - error varchar NULL, - -- NOTE: minting_status is purposely not an enum for ease of future expansion without running a migration - minting_status varchar NULL, - metadata_id UUID NULL, - tried_count INT DEFAULT 0, - last_imtbl_zkevm_mint_request_updated_id UUID NULL, - amount INT NULL, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP -); - -CREATE UNIQUE INDEX IF NOT EXISTS im_assets_uindex ON im_assets (asset_id, contract_address); \ No newline at end of file