Skip to content

Commit

Permalink
fix: dependency on google secret (#396)
Browse files Browse the repository at this point in the history
* fix

* fix

* fix

* Update package-lock.json

* fix

* fix

* fix
  • Loading branch information
RiXelanya authored Mar 27, 2024
1 parent 6de8886 commit 14d8d2b
Show file tree
Hide file tree
Showing 65 changed files with 222 additions and 2,638 deletions.
1,292 changes: 11 additions & 1,281 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
},
"dependencies": {
"@debionetwork/polkadot-provider": "0.0.17",
"@debionetwork/nestjs-gcloud-secret-manager": "^0.0.3",
"@elastic/elasticsearch": "^7.13.0",
"@google-cloud/storage": "^6.2.2",
"@nestjs-modules/mailer": "^1.8.1",
Expand Down
39 changes: 9 additions & 30 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import {
GCloudSecretManagerModule,
GCloudSecretManagerService,
} from '@debionetwork/nestjs-gcloud-secret-manager';
import { MailerModule } from '@nestjs-modules/mailer';
import { Module } from '@nestjs/common';
import { ScheduleModule } from '@nestjs/schedule';
Expand All @@ -15,39 +11,30 @@ import { EscrowAccounts } from '@common/escrow/models/deposit.entity';
import { IndexerModule } from '@indexer/indexer.module';
import { SubstrateListenerModule } from '@listeners/substrate-listener/substrate-listener.module';
import { SchedulersModule } from '@schedulers/schedulers.module';
import { keyList, SecretKeyList } from '@common/secrets';
import { config } from './config';

// eslint-disable-next-line @typescript-eslint/no-var-requires
require('dotenv').config();

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

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

@Module({
imports: [
CacheModule.registerAsync({
imports: [
GCloudSecretManagerModule.withConfig(process.env.PARENT, SecretKeyList),
],
inject: [GCloudSecretManagerService],
useFactory: async (
gCloudSecretManagerService: GCloudSecretManagerService<keyList>,
) => {
imports: [],
inject: [],
useFactory: async () => {
return {
store: redisStore,
host: gCloudSecretManagerService.getSecret('REDIS_HOST').toString(),
port: gCloudSecretManagerService.getSecret('REDIS_PORT'),
auth_pass: gCloudSecretManagerService
.getSecret('REDIS_PASSWORD')
.toString(),
host: config.REDIS_HOST.toString(),
port: config.REDIS_PORT,
auth_pass: config.REDIS_PASSWORD.toString(),
};
},
}),
Expand Down
28 changes: 7 additions & 21 deletions src/common/common.module.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,21 @@
import {
GCloudSecretManagerModule,
GCloudSecretManagerService,
} from '@debionetwork/nestjs-gcloud-secret-manager';
import { Module } from '@nestjs/common';
import { CqrsModule } from '@nestjs/cqrs';
import { ElasticsearchModule } from '@nestjs/elasticsearch';
import { keyList, SecretKeyList } from '@common/secrets';
import { config } from '../config';

// eslint-disable-next-line @typescript-eslint/no-var-requires
require('dotenv').config();

@Module({
imports: [
ElasticsearchModule.registerAsync({
imports: [
GCloudSecretManagerModule.withConfig(process.env.PARENT, SecretKeyList),
],
inject: [GCloudSecretManagerService],
useFactory: async (
gCloudSecretManagerService: GCloudSecretManagerService<keyList>,
) => ({
node: gCloudSecretManagerService
.getSecret('ELASTICSEARCH_NODE')
.toString(),
imports: [],
inject: [],
useFactory: async () => ({
node: config.ELASTICSEARCH_NODE.toString(),
auth: {
username: gCloudSecretManagerService
.getSecret('ELASTICSEARCH_USERNAME')
.toString(),
password: gCloudSecretManagerService
.getSecret('ELASTICSEARCH_PASSWORD')
.toString(),
username: config.ELASTICSEARCH_USERNAME.toString(),
password: config.ELASTICSEARCH_PASSWORD.toString(),
},
}),
}),
Expand Down
6 changes: 1 addition & 5 deletions src/common/debio-conversion/debio-conversion.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { GCloudSecretManagerModule } from '@debionetwork/nestjs-gcloud-secret-manager';
import { Module } from '@nestjs/common';
import { DebioConversionService } from './debio-conversion.service';
import { SecretKeyList } from '@common/secrets';

@Module({
imports: [
GCloudSecretManagerModule.withConfig(process.env.PARENT, SecretKeyList),
],
imports: [],
providers: [DebioConversionService],
exports: [DebioConversionService],
})
Expand Down
30 changes: 7 additions & 23 deletions src/common/debio-conversion/debio-conversion.service.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
import { GCloudSecretManagerService } from '@debionetwork/nestjs-gcloud-secret-manager';
import { Injectable, Logger } from '@nestjs/common';
import axios from 'axios';
import { keyList } from '@common/secrets';
import { config } from '../../config';

@Injectable()
export class DebioConversionService {
private readonly logger: Logger = new Logger(DebioConversionService.name);
constructor(
private readonly gCloudSecretManagerService: GCloudSecretManagerService<keyList>,
) {}

async getExchange() {
try {
const res = await axios.get(
`${this.gCloudSecretManagerService
.getSecret('REDIS_STORE_URL')
.toString()}/cache`,
`${config.REDIS_STORE_URL.toString()}/cache`,
{
auth: {
username: this.gCloudSecretManagerService
.getSecret('REDIS_STORE_USERNAME')
.toString(),
password: this.gCloudSecretManagerService
.getSecret('REDIS_STORE_PASSWORD')
.toString(),
username: config.REDIS_STORE_USERNAME.toString(),
password: config.REDIS_PASSWORD.toString(),
},
},
);
Expand All @@ -37,21 +27,15 @@ export class DebioConversionService {
async getExchangeFromTo(from: string, to: string) {
try {
const res = await axios.get(
`${this.gCloudSecretManagerService
.getSecret('REDIS_STORE_URL')
.toString()}/cache`,
`${config.REDIS_STORE_URL.toString()}/cache`,
{
params: {
from,
to,
},
auth: {
username: this.gCloudSecretManagerService
.getSecret('REDIS_STORE_USERNAME')
.toString(),
password: this.gCloudSecretManagerService
.getSecret('REDIS_STORE_PASSWORD')
.toString(),
username: config.REDIS_STORE_USERNAME.toString(),
password: config.REDIS_PASSWORD.toString(),
},
},
);
Expand Down
3 changes: 0 additions & 3 deletions src/common/escrow/escrow.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ import { EscrowAccounts } from './models/deposit.entity';
import { EscrowService } from './escrow.service';
import { EthereumModule, SubstrateModule } from '../index';
import { ErrorLoggingModule } from '../error-logging';
import { GCloudSecretManagerModule } from '@debionetwork/nestjs-gcloud-secret-manager';
import { SecretKeyList } from '@common/secrets';

require('dotenv').config(); // eslint-disable-line
@Module({
imports: [
TypeOrmModule.forFeature([EscrowAccounts]),
GCloudSecretManagerModule.withConfig(process.env.PARENT, SecretKeyList),
SubstrateModule,
ErrorLoggingModule,
forwardRef(() => EthereumModule),
Expand Down
12 changes: 3 additions & 9 deletions src/common/escrow/escrow.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import { EthereumService, SubstrateService } from '../index';
import { setOrderPaid } from '@debionetwork/polkadot-provider';
import { ethers } from 'ethers';
import AsyncLock from 'async-lock';
import { GCloudSecretManagerService } from '@debionetwork/nestjs-gcloud-secret-manager';
import { ErrorLoggingService } from '../error-logging';
import { ErrorLoggingDto } from '../error-logging/dto/error-logging.dto';
import { keyList } from '@common/secrets';
import { config } from '../../config';

const lock = new AsyncLock();
const ESCROW_WALLET_LOCK_KEY = 'escrow-wallet-lock';
Expand All @@ -16,7 +15,6 @@ let nonce = 0;
@Injectable()
export class EscrowService {
constructor(
private readonly gCloudSecretManagerService: GCloudSecretManagerService<keyList>,
private readonly substrateService: SubstrateService,
private readonly ethereumService: EthereumService,
private readonly errorLoggingRepository: ErrorLoggingService,
Expand All @@ -27,9 +25,7 @@ export class EscrowService {
async onModuleInit(): Promise<void> {
this.provider = await this.ethereumService.getEthersProvider();
this.escrowWallet = await new ethers.Wallet(
this.gCloudSecretManagerService
.getSecret('DEBIO_ESCROW_PRIVATE_KEY')
.toString(),
config.DEBIO_ESCROW_PRIVATE_KEY.toString(),
this.provider,
);
}
Expand Down Expand Up @@ -167,9 +163,7 @@ export class EscrowService {
const tokenAmount = ethers.utils.parseUnits(String(amount), 18);
const tokenContract = this.ethereumService.getContract();
const wallet: WalletSigner = await this.ethereumService.createWallet(
this.gCloudSecretManagerService
.getSecret('DEBIO_ESCROW_PRIVATE_KEY')
.toString(),
config.DEBIO_ESCROW_PRIVATE_KEY.toString(),
);
const tokenContractWithSigner = tokenContract.connect(wallet);
const options = {
Expand Down
16 changes: 4 additions & 12 deletions src/common/ethereum/ethereum.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ import { EthersModule } from 'nestjs-ethers';
import { EthereumService } from './ethereum.service';
import { CachesModule } from '../caches';
import { ProcessEnvModule } from '../proxies';
import {
GCloudSecretManagerModule,
GCloudSecretManagerService,
} from '@debionetwork/nestjs-gcloud-secret-manager';
import { SecretKeyList, keyList } from '@common/secrets';
import { config } from '../../config';

@Module({
imports: [
Expand All @@ -18,20 +14,16 @@ import { SecretKeyList, keyList } from '@common/secrets';
HOST_POSTGRES: 'HOST_POSTGRES',
DB_POSTGRES: 'debio_escrow_dev',
}),
GCloudSecretManagerModule.withConfig(process.env.PARENT, SecretKeyList),
],
inject: [GCloudSecretManagerService],
useFactory: async (
gCloudSecretManagerService: GCloudSecretManagerService<keyList>,
) => {
inject: [],
useFactory: async () => {
return {
network: gCloudSecretManagerService.getSecret('WEB3_RPC').toString(),
network: config.WEB3_RPC_HTTPS.toString(),
useDefaultProvider: true,
};
},
}),
CachesModule,
GCloudSecretManagerModule.withConfig(process.env.PARENT, SecretKeyList),
],
providers: [EthereumService],
exports: [CachesModule, EthersModule, EthereumService],
Expand Down
14 changes: 4 additions & 10 deletions src/common/ethereum/ethereum.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ import ABI from './utils/ABI.json';
import escrowContract from './utils/Escrow.json';
import { ethers } from 'ethers';
import { CachesService } from '../caches';
import { GCloudSecretManagerService } from '@debionetwork/nestjs-gcloud-secret-manager';
import { keyList } from '@common/secrets';
import { config } from '../../config';

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

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

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

Expand All @@ -62,9 +58,7 @@ export class EthereumService {
try {
const provider = this.getEthersProvider();
const contract = new ethers.Contract(
this.gCloudSecretManagerService
.getSecret('ESCROW_CONTRACT_ADDRESS')
.toString(),
config.ESCROW_CONTRACT_ADDRESS.toString(),
escrowContract.abi,
provider,
);
Expand Down
Loading

0 comments on commit 14d8d2b

Please sign in to comment.