Skip to content

Commit

Permalink
feat: PRO-2395 SponsorshipPolicy validations, db model updates and do…
Browse files Browse the repository at this point in the history
…cker updates
  • Loading branch information
kanthgithub committed Jun 16, 2024
1 parent acb5e01 commit ecbbbf0
Show file tree
Hide file tree
Showing 16 changed files with 422 additions and 65 deletions.
1 change: 0 additions & 1 deletion backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ COPY --from=builder /app/build ./build
COPY ./migrations ./build/migrations
COPY package.json ./
COPY --from=builder /app/config.json.default /usr/app/config.json
RUN touch database.sqlite
RUN npm install
USER root
ENV NODE_ENV="production"
Expand Down
20 changes: 20 additions & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,23 @@ Parameters:
- `/deposit` - This url accepts one parameter and returns the submitted transaction hash if successful. This url is used to deposit some funds to the entryPointAddress from the sponsor wallet
1. amount - The amount to be deposited in ETH

## Local Docker Networks

1. Ensure the postgres docker instance is up and running

2. Here we need to create a network and tag backend & postgres on same network

```sh
docker network create arka-network
```

```sh
docker run --network arka-network --name local-setup-db-1 -d postgres
```

```sh
docker run --network arka-network --name arka-backend -d arka-backend
```



Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ async function up({ context: queryInterface }) {
startDate: {
type: Sequelize.DATE,
allowNull: true,
field: 'START_DATE'
field: 'START_TIME'
},
endDate: {
type: Sequelize.DATE,
allowNull: true,
field: 'END_DATE'
field: 'END_TIME'
},
globalMaxApplicable: {
type: Sequelize.BOOLEAN,
Expand Down
2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"check:types": "tsc --noEmit",
"build": "esbuild `find src \\( -name '*.ts' \\)` --platform=node --outdir=build --resolve-extensions=.js && cp -r ./src/migrations ./build/",
"build": "esbuild `find src \\( -name '*.ts' \\)` --platform=node --outdir=build --resolve-extensions=.js && cp -r ./migrations ./build/",
"build:docker:prod": "docker build . -t my-fastify-app --build-arg APP_ENV=production",
"start": "node build",
"dev": "tsx watch src | pino-pretty --colorize",
Expand Down
12 changes: 12 additions & 0 deletions backend/src/constants/ErrorMessage.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
export default {
INVALID_DATA: 'Invalid data provided',
INVALID_SPONSORSHIP_POLICY: 'Invalid sponsorship policy data',
INVALID_SPONSORSHIP_POLICY_ID: 'Invalid sponsorship policy id',
INVALID_API_KEY: 'Invalid Api Key',
UNSUPPORTED_NETWORK: 'Unsupported network',
UNSUPPORTED_NETWORK_TOKEN: 'Unsupported network/token',
EMPTY_BODY: 'Empty Body received',
API_KEY_DOES_NOT_EXIST_FOR_THE_WALLET_ADDRESS: 'Api Key does not exist for the wallet address',
FAILED_TO_CREATE_SPONSORSHIP_POLICY: 'Failed to create sponsorship policy',
FAILED_TO_UPDATE_SPONSORSHIP_POLICY: 'Failed to update sponsorship policy',
SPONSORSHIP_POLICY_NOT_FOUND: 'Sponsorship policy not found',
SPONSORSHIP_POLICY_ALREADY_EXISTS: 'Sponsorship policy already exists',
SPONSORSHIP_POLICY_IS_DISABLED: 'Sponsorship policy is disabled',
FAILED_TO_DELETE_SPONSORSHIP_POLICY: 'Failed to delete sponsorship policy',
FAILED_TO_ENABLE_SPONSORSHIP_POLICY: 'Failed to enable sponsorship policy',
FAILED_TO_DISABLE_SPONSORSHIP_POLICY: 'Failed to disable sponsorship policy',
FAILED_TO_QUERY_SPONSORSHIP_POLICY: 'Failed to query sponsorship policy',
FAILED_TO_PROCESS: 'Failed to process the request. Please try again or contact ARKA support team',
INVALID_MODE: 'Invalid mode selected',
DUPLICATE_RECORD: 'Duplicate record found',
Expand Down
2 changes: 2 additions & 0 deletions backend/src/constants/ReturnCode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default {
SUCCESS: 200,
FAILURE: 400,
BAD_REQUEST: 400,
NOT_FOUND: 404,
}
30 changes: 15 additions & 15 deletions backend/src/models/sponsorship-policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ export class SponsorshipPolicy extends Model {
public isEnabled: boolean = false;
public isApplicableToAllNetworks!: boolean;
public enabledChains?: number[];
public isPerpetual!: boolean;
public startTime!: Date | null;
public endTime!: Date | null;
public isPerpetual: boolean = false;
public startTime: Date | null = null;
public endTime: Date | null = null;
public globalMaximumApplicable: boolean = false;
public globalMaximumUsd!: number | null;
public globalMaximumNative!: number | null;
public globalMaximumOpCount!: number | null;
public globalMaximumUsd: number | null = null;
public globalMaximumNative: number | null = null;
public globalMaximumOpCount: number | null = null;
public perUserMaximumApplicable: boolean = false;
public perUserMaximumUsd!: number | null;
public perUserMaximumNative!: number | null;
public perUserMaximumOpCount!: number | null;
public perUserMaximumUsd: number | null = null;
public perUserMaximumNative: number | null = null;
public perUserMaximumOpCount: number | null = null;
public perOpMaximumApplicable: boolean = false;
public perOpMaximumUsd!: number | null;
public perOpMaximumNative!: number | null;
public perOpMaximumUsd: number | null = null;
public perOpMaximumNative: number | null = null;
public addressAllowList: string[] | null = null;
public addressBlockList: string[] | null = null;
public readonly createdAt!: Date;
Expand Down Expand Up @@ -103,15 +103,15 @@ export function initializeSponsorshipPolicyModel(sequelize: Sequelize, schema: s
defaultValue: false,
field: 'IS_PERPETUAL'
},
startDate: {
startTime: {
type: DataTypes.DATE,
allowNull: true,
field: 'START_DATE'
field: 'START_TIME'
},
endDate: {
endTime: {
type: DataTypes.DATE,
allowNull: true,
field: 'END_DATE'
field: 'END_TIME'
},
globalMaximumApplicable: {
type: DataTypes.BOOLEAN,
Expand Down
6 changes: 6 additions & 0 deletions backend/src/plugins/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export type ArkaConfig = Static<typeof ConfigSchema>;

const configPlugin: FastifyPluginAsync = async (server) => {
const validate = ajv.compile(ConfigSchema);
server.log.info("Validating .env file");
const valid = validate(process.env);
if (!valid) {
throw new Error(
Expand All @@ -46,6 +47,8 @@ const configPlugin: FastifyPluginAsync = async (server) => {
);
}

server.log.info("Configuring .env file");

const config = {
LOG_LEVEL: process.env.LOG_LEVEL ?? '',
API_PORT: process.env.API_PORT ?? '',
Expand All @@ -59,6 +62,9 @@ const configPlugin: FastifyPluginAsync = async (server) => {
DATABASE_SCHEMA_NAME: process.env.DATABASE_SCHEMA_NAME ?? 'arka',
}

server.log.info("Configured .env file");
server.log.info(`config: ${JSON.stringify(config, null, 2)}`);

server.decorate("config", config);
};

Expand Down
4 changes: 2 additions & 2 deletions backend/src/plugins/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { Umzug, SequelizeStorage } from 'umzug';

const databasePlugin: FastifyPluginAsync = async (server) => {

server.log.info(`Connecting to database... with URL: ${server.config.DATABASE_URL} and schemaName: ${server.config.DATABASE_SCHEMA_NAME}`);

const sequelize = new Sequelize(server.config.DATABASE_URL, {
schema: server.config.DATABASE_SCHEMA_NAME,
});
Expand All @@ -32,8 +34,6 @@ const databasePlugin: FastifyPluginAsync = async (server) => {
console.error('Migration failed:', err)
process.exitCode = 1
}

//server.decorate('sequelize', sequelize);
};

declare module "fastify" {
Expand Down
7 changes: 6 additions & 1 deletion backend/src/plugins/sequelizePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { initializeSponsorshipPolicyModel } from '../models/sponsorship-policy.j
import { initializeArkaConfigModel } from "../models/arka-config.js";
import { APIKeyRepository } from "../repository/api-key-repository.js";
import { ArkaConfigRepository } from "../repository/arka-config-repository.js";
import { SponsorshipPolicyRepository } from "../repository/sponsorship-policy-repository.js";
const pg = await import('pg');
const Client = pg.default.Client;

Expand Down Expand Up @@ -43,9 +44,10 @@ const sequelizePlugin: FastifyPluginAsync = async (server) => {
// Initialize models
initializeArkaConfigModel(sequelize, server.config.DATABASE_SCHEMA_NAME);
const initializedAPIKeyModel = initializeAPIKeyModel(sequelize, server.config.DATABASE_SCHEMA_NAME);
sequelize.models.APIKey = initializedAPIKeyModel;
//sequelize.models.APIKey = initializedAPIKeyModel;
server.log.info(`Initialized APIKey model... ${sequelize.models.APIKey}`);
initializeSponsorshipPolicyModel(sequelize, server.config.DATABASE_SCHEMA_NAME);
server.log.info('Initialized SponsorshipPolicy model...');

server.log.info('Initialized all models...');

Expand All @@ -55,6 +57,8 @@ const sequelizePlugin: FastifyPluginAsync = async (server) => {
server.decorate('apiKeyRepository', apiKeyRepository);
const arkaConfigRepository : ArkaConfigRepository = new ArkaConfigRepository(sequelize);
server.decorate('arkaConfigRepository', arkaConfigRepository);
const sponsorshipPolicyRepository = new SponsorshipPolicyRepository(sequelize);
server.decorate('sponsorshipPolicyRepository', sponsorshipPolicyRepository);

server.log.info('decorated fastify server with models...');

Expand All @@ -70,6 +74,7 @@ declare module "fastify" {
sequelize: Sequelize;
apiKeyRepository: APIKeyRepository;
arkaConfigRepository: ArkaConfigRepository;
sponsorshipPolicyRepository: SponsorshipPolicyRepository;
}
}

Expand Down
Loading

0 comments on commit ecbbbf0

Please sign in to comment.