Skip to content

Commit

Permalink
fix: google cloud storage (#706)
Browse files Browse the repository at this point in the history
Revert "fix: google cloud storage (#705)"

This reverts commit 8afc4cd.
  • Loading branch information
RiXelanya authored Mar 25, 2024
1 parent 8afc4cd commit e0e74e8
Show file tree
Hide file tree
Showing 49 changed files with 3,641 additions and 1,155 deletions.
3,534 changes: 2,722 additions & 812 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
"test:e2e:watch": "jest --config ./test/e2e/jest-e2e-endpoint.json --watch"
},
"dependencies": {
"@debionetwork/nestjs-gcloud-secret-manager": "0.0.3",
"@debionetwork/nestjs-gcloud-storage": "0.0.2",
"@debionetwork/polkadot-provider": "0.0.18",
"@elastic/elasticsearch": "^7.14.0",
"@nestjs-modules/mailer": "^1.8.1",
Expand Down Expand Up @@ -60,9 +62,7 @@
"handlebars": "^4.7.7",
"helmet": "^4.6.0",
"jest-when": "^3.4.2",
"minio": "^7.1.3",
"nestjs-ethers": "^0.3.1",
"nestjs-minio": "^2.1.17",
"node-mocks-http": "^1.11.0",
"nodemailer": "^6.6.3",
"pg": "^8.6.0",
Expand Down
39 changes: 30 additions & 9 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,22 @@ import { TransactionModule } from './endpoints/transaction/transaction.module';
import { SpecializationModule } from './endpoints/category/specialization/specialization.module';
import { NotificationEndpointModule } from './endpoints/notification-endpoint/notification-endpoint.module';
import { DnaCollectionModule } from './endpoints/category/dna-collection/dna-collection.module';
import {
GCloudSecretManagerModule,
GCloudSecretManagerService,
} from '@debionetwork/nestjs-gcloud-secret-manager';
import { keyList, SecretKeyList } from './common/secrets';
import { ConversionModule } from './endpoints/conversion/conversion.module';
import { PinataModule } from './endpoints/pinata/pinata.module';
import { MyriadModule } from './endpoints/myriad/myriad.module';
import { SecondOpinionModule } from './endpoints/second-opinion/second-opinion.module';
import { config } from './config';

require('dotenv').config(); // eslint-disable-line

@Module({
imports: [
ScheduleModule.forRoot(),
GCloudSecretManagerModule.withConfig(process.env.PARENT, SecretKeyList),
TypeOrmModule.forRootAsync({
imports: [
ProcessEnvModule.setDefault({
Expand All @@ -47,15 +52,23 @@ require('dotenv').config(); // eslint-disable-line
DB_POSTGRES: 'DB_POSTGRES',
DB_LOCATIONS: 'DB_LOCATIONS',
}),
GCloudSecretManagerModule.withConfig(process.env.PARENT, SecretKeyList),
],
inject: [ProcessEnvProxy],
useFactory: async (processEnvProxy: ProcessEnvProxy) => {
inject: [ProcessEnvProxy, GCloudSecretManagerService],
useFactory: async (
processEnvProxy: ProcessEnvProxy,
gCloudSecretManagerService: GCloudSecretManagerService<keyList>,
) => {
return {
type: 'postgres',
host: processEnvProxy.env.HOST_POSTGRES,
port: 5432,
username: config.POSTGRES_USERNAME.toString(),
password: config.POSTGRES_PASSWORD.toString(),
username: gCloudSecretManagerService
.getSecret('POSTGRES_USERNAME')
.toString(),
password: gCloudSecretManagerService
.getSecret('POSTGRES_PASSWORD')
.toString(),
database: processEnvProxy.env.DB_POSTGRES,
entities: [LabRating, TransactionRequest],
autoLoadEntities: true,
Expand All @@ -71,15 +84,23 @@ require('dotenv').config(); // eslint-disable-line
DB_POSTGRES: 'DB_POSTGRES',
DB_LOCATIONS: 'DB_LOCATIONS',
}),
GCloudSecretManagerModule.withConfig(process.env.PARENT, SecretKeyList),
],
inject: [ProcessEnvProxy],
useFactory: async (processEnvProxy: ProcessEnvProxy) => {
inject: [ProcessEnvProxy, GCloudSecretManagerService],
useFactory: async (
processEnvProxy: ProcessEnvProxy,
gCloudSecretManagerService: GCloudSecretManagerService<keyList>,
) => {
return {
type: 'postgres',
host: processEnvProxy.env.HOST_POSTGRES,
port: 5432,
username: config.POSTGRES_USERNAME.toString(),
password: config.POSTGRES_PASSWORD.toString(),
username: gCloudSecretManagerService
.getSecret('POSTGRES_USERNAME')
.toString(),
password: gCloudSecretManagerService
.getSecret('POSTGRES_PASSWORD')
.toString(),
database: processEnvProxy.env.DB_LOCATIONS,
entities: [...LocationEntities],
autoLoadEntities: true,
Expand Down
17 changes: 11 additions & 6 deletions src/common/modules/caches/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import { CacheModule, Module } from '@nestjs/common';
import { CachesService } from './caches.service';
import * as redisStore from 'cache-manager-redis-store';
import { config } from '../../../config';
import { GCloudSecretManagerService } from '@debionetwork/nestjs-gcloud-secret-manager';
import { keyList } from '../../secrets';

@Module({
imports: [
CacheModule.registerAsync({
inject: [],
useFactory: async () => {
inject: [GCloudSecretManagerService],
useFactory: async (
gCloudSecretManagerService: GCloudSecretManagerService<keyList>,
) => {
return {
store: redisStore,
host: config.REDIS_HOST.toString(),
port: config.REDIS_PORT.toString(),
auth_pass: config.REDIS_PASSWORD.toString(),
host: gCloudSecretManagerService.getSecret('REDIS_HOST').toString(),
port: gCloudSecretManagerService.getSecret('REDIS_PORT').toString(),
auth_pass: gCloudSecretManagerService
.getSecret('REDIS_PASSWORD')
.toString(),
};
},
}),
Expand Down
17 changes: 11 additions & 6 deletions src/common/modules/debio-conversion/debio-conversion.module.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import { DebioConversionService } from './debio-conversion.service';
import { GCloudSecretManagerService } from '@debionetwork/nestjs-gcloud-secret-manager';
import { CacheModule, Module } from '@nestjs/common';
import * as redisStore from 'cache-manager-redis-store';
import { config } from '../../../config';
import { keyList } from '../../../common/secrets';

@Module({
imports: [
CacheModule.registerAsync({
inject: [],
useFactory: async () => {
inject: [GCloudSecretManagerService],
useFactory: async (
gCloudSecretManagerService: GCloudSecretManagerService<keyList>,
) => {
return {
store: redisStore,
host: config.REDIS_HOST.toString(),
port: config.REDIS_PORT.toString(),
auth_pass: config.REDIS_PASSWORD.toString(),
host: gCloudSecretManagerService.getSecret('REDIS_HOST').toString(),
port: gCloudSecretManagerService.getSecret('REDIS_PORT').toString(),
auth_pass: gCloudSecretManagerService
.getSecret('REDIS_PASSWORD')
.toString(),
ttl: 2 * 60 * 60,
};
},
Expand Down
28 changes: 20 additions & 8 deletions src/common/modules/debio-conversion/debio-conversion.service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { GCloudSecretManagerService } from '@debionetwork/nestjs-gcloud-secret-manager';
import { keyList } from '../../secrets';
import axios from 'axios';
import { CACHE_MANAGER, Inject, Injectable } from '@nestjs/common';
import { Cache } from 'cache-manager';
Expand All @@ -8,11 +10,13 @@ import {
estimateSwap,
getExpectedOutputFromSwapTodos,
} from '@ref-finance/ref-sdk';
import { config } from '../../../config';

@Injectable()
export class DebioConversionService {
constructor(@Inject(CACHE_MANAGER) private readonly cacheManager: Cache) {}
constructor(
private readonly gCloudSecretManagerService: GCloudSecretManagerService<keyList>,
@Inject(CACHE_MANAGER) private readonly cacheManager: Cache,
) {}

async getCacheExchange() {
return this.cacheManager.get<Exchange>('exchange');
Expand All @@ -23,8 +27,10 @@ export class DebioConversionService {
}

async setCacheExchangeFromTo(from: string, to: string) {
const listApiKey: string[] =
config.COINMARKETCAP_API_KEY.toString().split(',');
const listApiKey: string[] = this.gCloudSecretManagerService
.getSecret('COINMARKETCAP_API_KEY')
.toString()
.split(',');
const indexCurrentApiKey: number = await this.cacheManager.get<number>(
'index_api_key',
);
Expand Down Expand Up @@ -62,8 +68,10 @@ export class DebioConversionService {
async setCacheExchange() {
const sodaki = await this.getSodakiExchange();

const listApiKey: string[] =
config.COINMARKETCAP_API_KEY.toString().split(',');
const listApiKey: string[] = this.gCloudSecretManagerService
.getSecret('COINMARKETCAP_API_KEY')
.toString()
.split(',');
const indexCurrentApiKey: number = await this.cacheManager.get<number>(
'index_api_key',
);
Expand Down Expand Up @@ -141,7 +149,9 @@ export class DebioConversionService {

async convertDaiToUsd(apiKey: string, daiAmount: number): Promise<number> {
const response = await axios.get(
`${config.COINMARKETCAP_HOST.toString()}/tools/price-conversion`,
`${this.gCloudSecretManagerService
.getSecret('COINMARKETCAP_HOST')
.toString()}/tools/price-conversion`,
{
headers: {
'X-CMC_PRO_API_KEY': apiKey,
Expand All @@ -164,7 +174,9 @@ export class DebioConversionService {
to: string,
): Promise<number> {
const response = await axios.get(
`${config.COINMARKETCAP_HOST.toString()}/tools/price-conversion`,
`${this.gCloudSecretManagerService
.getSecret('COINMARKETCAP_HOST')
.toString()}/tools/price-conversion`,
{
headers: {
'X-CMC_PRO_API_KEY': apiKey,
Expand Down
12 changes: 9 additions & 3 deletions src/common/modules/escrow/escrow.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { EthereumService, SubstrateService } from '../..';
import { setOrderPaid } from '@debionetwork/polkadot-provider';
import { ethers } from 'ethers';
import AsyncLock from 'async-lock';
import { config } from '../../../config';
import { GCloudSecretManagerService } from '@debionetwork/nestjs-gcloud-secret-manager';
import { keyList } from '../../secrets';

const lock = new AsyncLock();
const ESCROW_WALLET_LOCK_KEY = 'escrow-wallet-lock';
Expand All @@ -13,6 +14,7 @@ let nonce = 0;
@Injectable()
export class EscrowService {
constructor(
private readonly gCloudSecretManagerService: GCloudSecretManagerService<keyList>,
private readonly substrateService: SubstrateService,
private readonly ethereumService: EthereumService,
) {}
Expand All @@ -22,7 +24,9 @@ export class EscrowService {
async onModuleInit(): Promise<void> {
this.provider = await this.ethereumService.getEthersProvider();
this.escrowWallet = await new ethers.Wallet(
config.DEBIO_ESCROW_PRIVATE_KEY.toString(),
this.gCloudSecretManagerService
.getSecret('DEBIO_ESCROW_PRIVATE_KEY')
.toString(),
this.provider,
);
}
Expand Down Expand Up @@ -134,7 +138,9 @@ export class EscrowService {
const tokenAmount = ethers.utils.parseUnits(String(amount), 18);
const tokenContract = this.ethereumService.getContract();
const wallet: WalletSigner = await this.ethereumService.createWallet(
config.DEBIO_ESCROW_PRIVATE_KEY.toString(),
this.gCloudSecretManagerService
.getSecret('DEBIO_ESCROW_PRIVATE_KEY')
.toString(),
);
const tokenContractWithSigner = tokenContract.connect(wallet);
const options = {
Expand Down
14 changes: 10 additions & 4 deletions src/common/modules/ethereum/ethereum.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import ABI from './utils/ABI.json';
import escrowContract from './utils/Escrow.json';
import { ethers } from 'ethers';
import { CachesService } from '../caches';
import { config } from '../../../config';
import { GCloudSecretManagerService } from '@debionetwork/nestjs-gcloud-secret-manager';
import { keyList } from '../../secrets';

@Injectable()
export class EthereumService {
constructor(
private readonly gCloudSecretManagerService: GCloudSecretManagerService<keyList>,
private readonly ethersContract: EthersContract,
private readonly ethersSigner: EthersSigner,
private readonly cachesService: CachesService,
Expand All @@ -36,15 +38,17 @@ export class EthereumService {

getEthersProvider(): ethers.providers.JsonRpcProvider {
const provider = new ethers.providers.JsonRpcProvider(
config.WEB3_RPC_HTTPS.toString(),
this.gCloudSecretManagerService.getSecret('WEB3_RPC_HTTPS').toString(),
);
return provider;
}

getContract(): SmartContract {
try {
const contract: SmartContract = this.ethersContract.create(
config.ESCROW_CONTRACT_ADDRESS.toString(),
this.gCloudSecretManagerService
.getSecret('ESCROW_CONTRACT_ADDRESS')
.toString(),
ABI,
);

Expand All @@ -58,7 +62,9 @@ export class EthereumService {
try {
const provider = this.getEthersProvider();
const contract = new ethers.Contract(
config.ESCROW_CONTRACT_ADDRESS.toString(),
this.gCloudSecretManagerService
.getSecret('ESCROW_CONTRACT_ADDRESS')
.toString(),
escrowContract.abi,
provider,
);
Expand Down
21 changes: 15 additions & 6 deletions src/common/modules/health-indicators/elasticsearch.health/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
import { GCloudSecretManagerService } from '@debionetwork/nestjs-gcloud-secret-manager';
import { Module } from '@nestjs/common';
import { ElasticsearchModule } from '@nestjs/elasticsearch';
import { TerminusModule } from '@nestjs/terminus';
import { ElasticsearchHealthIndicator } from './elasticsearch.health.indicator';
import { config } from '../../../../config';
import { keyList } from '../../../secrets';

@Module({
imports: [
TerminusModule,
ElasticsearchModule.registerAsync({
inject: [],
useFactory: async () => {
inject: [GCloudSecretManagerService],
useFactory: async (
gCloudSecretManagerService: GCloudSecretManagerService<keyList>,
) => {
return {
node: config.ELASTICSEARCH_NODE.toString(),
node: gCloudSecretManagerService
.getSecret('ELASTICSEARCH_NODE')
.toString(),
auth: {
username: config.ELASTICSEARCH_USERNAME.toString(),
password: config.ELASTICSEARCH_PASSWORD.toString(),
username: gCloudSecretManagerService
.getSecret('ELASTICSEARCH_USERNAME')
.toString(),
password: gCloudSecretManagerService
.getSecret('ELASTICSEARCH_PASSWORD')
.toString(),
},
};
},
Expand Down
Loading

0 comments on commit e0e74e8

Please sign in to comment.