CAFECA-IO / TideBit-DeFi

TideBit Decentralize Finance Version
https://tidebit-defi.vercel.app
GNU General Public License v3.0
1 stars 0 forks source link

不能在 docker 上的 TideBit DeFi 入金 #1511

Closed arealclimber closed 6 months ago

arealclimber commented 7 months ago

在 docker 上運行 mongo db, nest.js, next.js ,登入後不能入金,Network request 回傳201,backend console 顯示以下錯誤訊息,餘額沒有增加,因為我沒有設定 replica set

Screenshot 2024-01-29 at 18 42 09

image

[DepositService] balanceEntity: {
  _id: '65b7797f05fd6172cb99bf38',
  user_address: '0xb54898DB1250A6a629E5B566367E9C60a7Dd6C30',
  currency: 'USDT',
  available: '100',
  locked: '0',
  block_number: 0,
  created_at: 1706523007,
  updated_at: 1706525220,
  __v: 0
}
[DepositService] addDepositToDB error MongoServerError: Transaction numbers are only allowed on a replica set member or mongos
    at Connection.onMessage (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:202:26)
    at MessageStream.<anonymous> (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:61:60)
    at MessageStream.emit (node:events:517:28)
    at processIncomingData (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/cmap/message_stream.js:124:16)
    at MessageStream._write (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/cmap/message_stream.js:33:9)
    at writeOrBuffer (node:internal/streams/writable:392:12)
    at _write (node:internal/streams/writable:333:10)
    at Writable.write (node:internal/streams/writable:337:10)
    at Socket.ondata (node:internal/streams/readable:809:22)
    at Socket.emit (node:events:517:28) {
  ok: 0,
  code: 20,
  codeName: 'IllegalOperation',
  [Symbol(errorLabels)]: Set(0) {}
}
[DepositService] addDepositToDB abortTransaction
[DepositService] addDepositToDB endSession
[DepositServices] error MongoServerError: Transaction numbers are only allowed on a replica set member or mongos
    at Connection.onMessage (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:202:26)
    at MessageStream.<anonymous> (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:61:60)
    at MessageStream.emit (node:events:517:28)
    at processIncomingData (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/cmap/message_stream.js:124:16)
    at MessageStream._write (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/cmap/message_stream.js:33:9)
    at writeOrBuffer (node:internal/streams/writable:392:12)
    at _write (node:internal/streams/writable:333:10)
    at Writable.write (node:internal/streams/writable:337:10)
    at Socket.ondata (node:internal/streams/readable:809:22)
    at Socket.emit (node:events:517:28) {
  ok: 0,
  code: 20,
  codeName: 'IllegalOperation',
  [Symbol(errorLabels)]: Set(0) {}
} depositId undefined

MongoDB 設定

arealclimber commented 7 months ago

docker-compose.yml

version: "3.1"
services:
  mongo:
    image: mongo
    restart: always
    command: mongod --replSet rs0
    networks:
      - mongodb-net
    ports:
      - 27017:27017
    volumes:
      - mongo_data:/data/db

networks:
  mongodb-net:
    driver: bridge

volumes:
  mongo_data:

mongod.conf

replication:
  replSetName: rs0

5ee439fc01412e2b956160b15e6aad6c3724cbaca6383ea4888b18b55f71af51 docker: Error response from daemon: Mounts denied: The path /mongod.conf is not shared from the host and is not known to Docker. You can configure shared paths from Docker -> Preferences... -> Resources -> File Sharing. See https://docs.docker.com/desktop/mac for more info.


- 
arealclimber commented 7 months ago

https://github.com/CAFECA-IO/tbd-backend/pull/325 的方法重新試一遍之後,拿到 replica set state log,但 tableplus 還連不上,但是輸入 http://127.0.0.1:27017/ 可以拿到 It looks like you are trying to access MongoDB over HTTP on the native driver port.

從log 跟連線看起來沒問題,但是要找 tableplus 跟後端連線入口

rs0 [direct: primary] testdb> rs.status()
{
  set: 'rs0',
  date: ISODate('2024-02-01T10:06:00.196Z'),
  myState: 1,
  term: Long('1'),
  syncSourceHost: '',
  syncSourceId: -1,
  heartbeatIntervalMillis: Long('2000'),
  majorityVoteCount: 1,
  writeMajorityCount: 1,
  votingMembersCount: 1,
  writableVotingMembersCount: 1,
  optimes: {
    lastCommittedOpTime: { ts: Timestamp({ t: 1706781953, i: 1 }), t: Long('1') },
    lastCommittedWallTime: ISODate('2024-02-01T10:05:53.033Z'),
    readConcernMajorityOpTime: { ts: Timestamp({ t: 1706781953, i: 1 }), t: Long('1') },
    appliedOpTime: { ts: Timestamp({ t: 1706781953, i: 1 }), t: Long('1') },
    durableOpTime: { ts: Timestamp({ t: 1706781953, i: 1 }), t: Long('1') },
    lastAppliedWallTime: ISODate('2024-02-01T10:05:53.033Z'),
    lastDurableWallTime: ISODate('2024-02-01T10:05:53.033Z')
  },
  lastStableRecoveryTimestamp: Timestamp({ t: 1706781893, i: 1 }),
  electionCandidateMetrics: {
    lastElectionReason: 'electionTimeout',
    lastElectionDate: ISODate('2024-02-01T09:59:02.746Z'),
    electionTerm: Long('1'),
    lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 1706781542, i: 1 }), t: Long('-1') },
    lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1706781542, i: 1 }), t: Long('-1') },
    numVotesNeeded: 1,
    priorityAtElection: 1,
    electionTimeoutMillis: Long('10000'),
    newTermStartDate: ISODate('2024-02-01T09:59:02.816Z'),
    wMajorityWriteAvailabilityDate: ISODate('2024-02-01T09:59:02.849Z')
  },
  members: [
    {
      _id: 0,
      name: 'f7efa7552798:27017',
      health: 1,
      state: 1,
      stateStr: 'PRIMARY',
      uptime: 564,
      optime: { ts: Timestamp({ t: 1706781953, i: 1 }), t: Long('1') },
      optimeDate: ISODate('2024-02-01T10:05:53.000Z'),
      lastAppliedWallTime: ISODate('2024-02-01T10:05:53.033Z'),
      lastDurableWallTime: ISODate('2024-02-01T10:05:53.033Z'),
      syncSourceHost: '',
      syncSourceId: -1,
      infoMessage: '',
      electionTime: Timestamp({ t: 1706781542, i: 2 }),
      electionDate: ISODate('2024-02-01T09:59:02.000Z'),
      configVersion: 46994,
      configTerm: -1,
      self: true,
      lastHeartbeatMessage: ''
    }
  ],
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1706781953, i: 1 }),
    signature: {
      hash: Binary.createFromBase64('AAAAAAAAAAAAAAAAAAAAAAAAAAA=', 0),
      keyId: Long('0')
    }
  },
  operationTime: Timestamp({ t: 1706781953, i: 1 })
}

image

arealclimber commented 7 months ago

taking 0.5 hrs remaining 2.5 hrs

arealclimber commented 6 months ago

後端跟資料庫設置

Dockerfile

# Base image
FROM node:18

# Create app directory
WORKDIR /usr/src/app

# A wildcard is used to ensure both package.json AND package-lock.json are copied
COPY package*.json ./

# Install app dependencies
RUN npm install

# Bundle app source
COPY . .

# Copy the .env and .env.development files
COPY development.env ./

# Creates a "dist" folder with the production build
RUN npm run build

# Expose the port on which the app will run
EXPOSE 3001

# Start the server using the production build
CMD ["npm", "run", "start:prod"]

docker-compose.yml

version: '3.8'
services:
  mongodb:
    image: mongo:latest
    command: mongod --replSet rs0 --bind_ip_all
    ports:
      - '27017:27017'
    volumes:
      - mongodb-data:/data/db
    networks:
      - mongodb-net

  nestjs-app:
    build: .
    ports:
      - '3001:80'
    environment:
      - MONGO_URI=mongodb://mongodb:27017/nestjs?replicaSet=rs0
    depends_on:
      - mongodb
    networks:
      - mongodb-net

networks:
  mongodb-net:
    name: mongodb-net
    driver: bridge

volumes:
  mongodb-data:

development.env

MONGO_PROTOCOL=mongodb
MONGO_USERNAME=
MONGO_PASSWORD=
MONGO_RESOURCE=mongodb
MONGO_DATABASE=nestjs
MONGO_PORT=27017
MONGO_URI=mongodb://mongodb:27017/nestjs?replicaSet=rs0

mongo.config.ts

import { registerAs } from '@nestjs/config';

export default registerAs('mongo', () => {
  const protocol = process.env.MONGO_PROTOCOL;
  const username = process.env.MONGO_USERNAME;
  const password = encodeURIComponent(process.env.MONGO_PASSWORD);
  const resource = process.env.MONGO_RESOURCE;
  const database = process.env.MONGO_DATABASE;
  const port = process.env.MONGO_PORT;
  const URL = process.env.MONGO_URL;
  const uri = `${protocol}://${resource}:${port}/${database}?retryWrites=true&w=majority`;
  console.log('uri in registerAs', uri);
  console.log('MONGO_URI in registerAs', URL);
  return {
    username,
    password,
    resource,
    uri,
    useNewUrlParser: true,
    useUnifiedTopology: true,
  };
});

common.module.ts

 MongooseModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: async (config: ConfigService) => ({
        // uri: config.get<string>('MONGO_URI'),
        uri: config.get<string>('mongo.uri'),
      }),
    }),

table plus 連線

URL: mongodb://localhost:27017

arealclimber commented 6 months ago

前端設置

networks: mongodb-net: name: mongodb-net driver: bridge

volumes: mongodb-data:



- 接著在 tbd-backend 底下執行指令,就能在 `http:localhost:3000` 執行登入、入金、開倉、更新倉位、關倉
    - `docker-compose build`
    - `docker-compose up -d`
arealclimber commented 6 months ago

Open position

https://github.com/CAFECA-IO/TideBit-DeFi/assets/20677913/f4dbf454-7d10-4f3a-aaf6-8d2cb3beda93

Update position

https://github.com/CAFECA-IO/TideBit-DeFi/assets/20677913/e5cf436d-6179-4270-98bc-73f314926dee

Close position

https://github.com/CAFECA-IO/TideBit-DeFi/assets/20677913/a088d464-ea4b-4765-931b-52d23aff62c7

Leaderboard & My assets

https://github.com/CAFECA-IO/TideBit-DeFi/assets/20677913/f4f0065d-ff3f-4d49-a5d3-a621ec03a4b9

交易之後的首頁

Screenshot 2024-02-05 at 12 40 06

arealclimber commented 6 months ago

taking 6 hrs