Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sponsorship Policy API functions and Integrate with paymaster data generation endpoint #111

Merged
merged 43 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
b4ec24b
feat: PRO-2395 Design doc for sponsorship policy management
kanthgithub Jun 5, 2024
df5476a
feat: PRO-2395 sponsorship policy db migration
kanthgithub Jun 6, 2024
ce1a9a2
feat: PRO-2395 sequelize integration to backend and property name upd…
kanthgithub Jun 7, 2024
508a357
feat: PRO-2395 update apiKey sequelize based response reference in ad…
kanthgithub Jun 7, 2024
b571783
feat: PRO-2395 sequelize models and centralised model association fil…
kanthgithub Jun 10, 2024
17d3603
feat: PRO-2395 refactor sequelize models and their initialisation in …
kanthgithub Jun 10, 2024
41757fe
fix: PRO-2395 admin-frontend to parse the error message from response…
kanthgithub Jun 10, 2024
2b14746
feat: PRO-2395 sequelize models updated with new association relation…
kanthgithub Jun 10, 2024
82b42b2
feat: PRO-2395 sequelize init and test
kanthgithub Jun 10, 2024
e2a2d45
feat: PRO-2395 migration scripts refactored and updated
kanthgithub Jun 12, 2024
72d9fc1
Merge branch 'master' into PRO-2395-Sponsorship-Policies
kanthgithub Jun 12, 2024
8ea0217
feat: PRO-2395 sequelize repository updates and conflict resolution
kanthgithub Jun 12, 2024
227452a
fix: PRO-2395 admin-frontend global-config components fix
kanthgithub Jun 12, 2024
4006f8b
chore: PRO-2395 logging updates
kanthgithub Jun 13, 2024
6ac302b
feat: PRO-2395 restricted sequelize references from repository, serve…
kanthgithub Jun 13, 2024
2709a72
fix: PRO-2395 Fix SponsorshipPolicy migrations and sequelize models n…
kanthgithub Jun 13, 2024
234d43c
chore: PRO-2395 delete obselete file at root directory
kanthgithub Jun 13, 2024
03d1f1d
feat: PRO-2395 denormalize SponsorshipPolicy table data
kanthgithub Jun 13, 2024
11fb2d7
feat: PRO-2395 refactor types and optimise imports
kanthgithub Jun 13, 2024
625afd0
fix: PRO-2395 adjust the property name to typescript model fieldnames
kanthgithub Jun 13, 2024
06e4617
feat: PRO-2395 SponsorshipPolicy repository functions and package ver…
kanthgithub Jun 14, 2024
acb5e01
fix: PRO-2395 fix imports in backend
kanthgithub Jun 14, 2024
ecbbbf0
feat: PRO-2395 SponsorshipPolicy validations, db model updates and do…
kanthgithub Jun 16, 2024
a4d8a9b
doc: PRO-2395 docker documentation for backend updated in readme file
kanthgithub Jun 16, 2024
6bc3ef6
feat: PRO-2455 validate userOp in sponsorMode with sponsorshipPolicy …
kanthgithub Jun 17, 2024
f1048ab
feat: PRO-2455 refactor sponsorship-policy to include EP version and …
kanthgithub Jun 18, 2024
1bed175
feat: PRO-2455 sponsorship-policy validation logic updated
kanthgithub Jun 18, 2024
c80ebc3
fix: PRO-2455 fix sequelize model update pattern
kanthgithub Jun 18, 2024
5ce1242
fix: PRO-2455 SponsorshipPolicy query functions from chainId, EntryPo…
kanthgithub Jun 19, 2024
2779225
feat: PRO-2455 api-endpoint refactoring for sponsorship policy extrac…
kanthgithub Jun 19, 2024
6c8ad04
feat: PRO-2455 HMAC env added and function imports fixed
kanthgithub Jun 20, 2024
4497b76
feat: PRO-2455 configure the SSL options for sequelize
kanthgithub Jun 20, 2024
177e4b0
feat: PRO-2455 sequelize migration standalone script and added script…
kanthgithub Jun 20, 2024
5072ea1
fix: PRO-2455 docker-compose fixes
kanthgithub Jun 20, 2024
de555a0
fix: PRO-2455 sponsorship-policy api endpoints fixed
kanthgithub Jun 20, 2024
2dd8641
feat: PRO-2455 externalise values from docker-compose to env variable…
kanthgithub Jun 22, 2024
ffcd5b0
doc: PRO-2455 backend and arka docker startup instructions
kanthgithub Jun 22, 2024
c0564e8
feat: rearrange docker-compose files
kanthgithub Jun 24, 2024
1f302e7
Merge branch 'master' into PRO-2455-Sponsorship-Policy-Integration
kanthgithub Jun 24, 2024
254778c
feat: PRO-2455 add new api route functions for sponsorship-policy
kanthgithub Jun 24, 2024
b7ce71d
feat: PRO-2455 split functions in routes
kanthgithub Jun 24, 2024
1e47614
feat: PRO-2455 split functions in routes
kanthgithub Jun 24, 2024
024696e
feat: PRO-2455 refactor local docker setup instructions and local pos…
kanthgithub Jun 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: PRO-2395 sequelize models updated with new association relation…
…ships
  • Loading branch information
kanthgithub committed Jun 10, 2024
commit 2b14746fd1047b4e75426702e7c2f290bd58ee0b
22 changes: 11 additions & 11 deletions backend/src/migrations/001.default.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
--------------------------------------------------------------------------------

CREATE TABLE IF NOT EXISTS config (
id INTEGER PRIMARY KEY,
ID SERIAL PRIMARY KEY,
DEPLOYED_ERC20_PAYMASTERS TEXT NOT NULL,
PYTH_MAINNET_URL TEXT NOT NULL,
PYTH_TESTNET_URL TEXT NOT NULL,
Expand All @@ -25,18 +25,18 @@ INSERT INTO config (
CUSTOM_CHAINLINK_DEPLOYED,
COINGECKO_IDS,
COINGECKO_API_URL) VALUES (
"ewogICAgIjQyMCI6IFsiMHg1M0Y0ODU3OTMwOWY4ZEJmRkU0ZWRFOTIxQzUwMjAwODYxQzI0ODJhIl0sCiAgICAiNDIxNjEzIjogWyIweDBhNkFhMUJkMzBENjk1NGNBNTI1MzE1Mjg3QWRlZUVjYmI2ZUZCNTkiXSwKICAgICI1MDAxIjogWyIweDZFYTI1Y2JiNjAzNjAyNDNFODcxZEQ5MzUyMjVBMjkzYTc4NzA0YTgiXSwKICAgICI4MDAwMSI6IFsiMHhjMzNjMzhBN0JGRUJiQjk5N2RENDAxMUNEZEFmNGViRDFlODgwM0MwIl0KfQ==",
"https://hermes.pyth.network/api/latest_vaas?ids%5B%5D=",
"https://hermes-beta.pyth.network/api/latest_vaas?ids%5B%5D=",
"5001",
"5000",
"0 0 * * *",
"ewogICAgIjgwMDAxIjogWyIweGMzM2MzOEE3QkZFQmJCOTk3ZEQ0MDExQ0RkQWY0ZWJEMWU4ODAzQzAiXQp9",
"eyI4MDAwMSI6WyJwYW50aGVyIl19",
"https://api.coingecko.com/api/v3/simple/price?vs_currencies=usd&precision=8&ids=");
'ewogICAgIjQyMCI6IFsiMHg1M0Y0ODU3OTMwOWY4ZEJmRkU0ZWRFOTIxQzUwMjAwODYxQzI0ODJhIl0sCiAgICAiNDIxNjEzIjogWyIweDBhNkFhMUJkMzBENjk1NGNBNTI1MzE1Mjg3QWRlZUVjYmI2ZUZCNTkiXSwKICAgICI1MDAxIjogWyIweDZFYTI1Y2JiNjAzNjAyNDNFODcxZEQ5MzUyMjVBMjkzYTc4NzA0YTgiXSwKICAgICI4MDAwMSI6IFsiMHhjMzNjMzhBN0JGRUJiQjk5N2RENDAxMUNEZEFmNGViRDFlODgwM0MwIl0KfQ==',
'https://hermes.pyth.network/api/latest_vaas?ids%5B%5D=',
'https://hermes-beta.pyth.network/api/latest_vaas?ids%5B%5D=',
'5001',
'5000',
'0 0 * * *',
'ewogICAgIjgwMDAxIjogWyIweGMzM2MzOEE3QkZFQmJCOTk3ZEQ0MDExQ0RkQWY0ZWJEMWU4ODAzQzAiXQp9',
'eyI4MDAwMSI6WyJwYW50aGVyIl19',
'https://api.coingecko.com/api/v3/simple/price?vs_currencies=usd&precision=8&ids=');

--------------------------------------------------------------------------------
-- Down
--------------------------------------------------------------------------------

DROP TABLE IF EXISTS config
DROP TABLE IF EXISTS config;
20 changes: 10 additions & 10 deletions backend/src/migrations/002.apiKeys.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
CREATE TABLE IF NOT EXISTS api_keys (
API_KEY TEXT NOT NULL PRIMARY KEY,
WALLET_ADDRESS TEXT NOT NULL UNIQUE,
PRIVATE_KEY varchar NOT NULL,
SUPPORTED_NETWORKS varchar DEFAULT NULL,
ERC20_PAYMASTERS varchar DEFAULT NULL,
MULTI_TOKEN_PAYMASTERS varchar DEFAULT NULL,
MULTI_TOKEN_ORACLES varchar DEFAULT NULL,
SPONSOR_NAME varchar DEFAULT NULL,
LOGO_URL varchar DEFAULT NULL,
PRIVATE_KEY TEXT NOT NULL,
SUPPORTED_NETWORKS TEXT DEFAULT NULL,
ERC20_PAYMASTERS TEXT DEFAULT NULL,
MULTI_TOKEN_PAYMASTERS TEXT DEFAULT NULL,
MULTI_TOKEN_ORACLES TEXT DEFAULT NULL,
SPONSOR_NAME TEXT DEFAULT NULL,
LOGO_URL TEXT DEFAULT NULL,
TRANSACTION_LIMIT INT NOT NULL,
NO_OF_TRANSACTIONS_IN_A_MONTH int,
INDEXER_ENDPOINT varchar
NO_OF_TRANSACTIONS_IN_A_MONTH INT,
INDEXER_ENDPOINT TEXT
);

--------------------------------------------------------------------------------
-- Down
--------------------------------------------------------------------------------

DROP TABLE IF EXISTS api_keys
DROP TABLE IF EXISTS api_keys;
33 changes: 16 additions & 17 deletions backend/src/migrations/003.sponsorship-policy.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,39 @@
-- Up
--------------------------------------------------------------------------------

-- Create POLICIES Table
-- Create sponsorship_policies Table
CREATE TABLE IF NOT EXISTS sponsorship_policies (
POLICY_ID INTEGER PRIMARY KEY,
WALLET_ADDRESS TEXT NOT NULL,
ID SERIAL PRIMARY KEY,
WALLET_ADDRESS TEXT NOT NULL UNIQUE,
NAME TEXT NOT NULL,
DESCRIPTION TEXT,
START_DATE DATE,
END_DATE DATE,
IS_PERPETUAL BOOLEAN DEFAULT FALSE,
IS_UNIVERSAL BOOLEAN DEFAULT FALSE,
CONTRACT_RESTRICTIONS TEXT, -- Stores JSON string because SQLite doesn't support JSON natively
CONTRACT_RESTRICTIONS TEXT, -- Stores JSON string because PostgreSQL supports JSON natively
FOREIGN KEY (WALLET_ADDRESS) REFERENCES api_keys(WALLET_ADDRESS) ON DELETE CASCADE
);

-- Create POLICY_LIMITS Table
-- Create sponsorship_policy_limits Table
CREATE TABLE IF NOT EXISTS sponsorship_policy_limits (
LIMIT_ID INTEGER PRIMARY KEY,
POLICY_ID INTEGER NOT NULL,
POLICY_ID INT NOT NULL,
LIMIT_TYPE TEXT NOT NULL,
MAX_USD REAL, -- REAL used in SQLite for floating-point numbers
MAX_ETH REAL,
MAX_OPERATIONS INTEGER,
FOREIGN KEY (POLICY_ID) REFERENCES sponsorship_policies(POLICY_ID) ON DELETE CASCADE
MAX_USD FLOAT, -- FLOAT used in PostgreSQL for floating-point numbers
MAX_ETH FLOAT,
MAX_OPERATIONS INT,
FOREIGN KEY (POLICY_ID) REFERENCES sponsorship_policies(ID) ON DELETE CASCADE,
PRIMARY KEY (POLICY_ID, LIMIT_TYPE) -- Composite primary key
);

-- Create POLICY_CHAINS Table
-- Create sponsorship_policy_chains Table
CREATE TABLE IF NOT EXISTS sponsorship_policy_chains (
POLICY_CHAIN_ID INTEGER PRIMARY KEY,
POLICY_ID INTEGER NOT NULL,
CHAIN_NAME TEXT NOT NULL,
FOREIGN KEY (POLICY_ID) REFERENCES sponsorship_policies(POLICY_ID) ON DELETE CASCADE
POLICY_ID INT NOT NULL,
CHAIN_ID TEXT NOT NULL,
FOREIGN KEY (POLICY_ID) REFERENCES sponsorship_policies(ID) ON DELETE CASCADE,
PRIMARY KEY (POLICY_ID, CHAIN_ID) -- Composite primary key
);


--------------------------------------------------------------------------------
-- Down
--------------------------------------------------------------------------------
Expand Down
4 changes: 3 additions & 1 deletion backend/src/models/APIKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export class APIKey extends Model {
declare transactionLimit: number;
declare noOfTransactionsInAMonth: number | null;
declare indexerEndpoint: string | null;
declare createdAt: Date; // Added this line
declare updatedAt: Date; // Added this line
}

export function initializeAPIKeyModel(sequelize: Sequelize) {
Expand All @@ -26,6 +28,7 @@ export function initializeAPIKeyModel(sequelize: Sequelize) {
walletAddress: {
type: DataTypes.TEXT,
allowNull: false,
unique: true,
field: 'WALLET_ADDRESS'
},
privateKey: {
Expand Down Expand Up @@ -81,6 +84,5 @@ export function initializeAPIKeyModel(sequelize: Sequelize) {
}, {
tableName: 'api_keys',
sequelize, // passing the `sequelize` instance is required
timestamps: false, // this will deactivate the `createdAt` and `updatedAt` columns
});
}
6 changes: 3 additions & 3 deletions backend/src/models/SponsorshipPolicy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Sequelize, DataTypes, Model } from 'sequelize';

export class SponsorshipPolicy extends Model {
declare policyId: number;
declare id: number;
declare walletAddress: string;
declare name: string;
declare description: string | null;
Expand All @@ -14,10 +14,10 @@ export class SponsorshipPolicy extends Model {

export function initializeSponsorshipPolicyModel(sequelize: Sequelize) {
SponsorshipPolicy.init({
policyId: {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
field: 'POLICY_ID'
field: 'ID'
},
walletAddress: {
type: DataTypes.STRING,
Expand Down
16 changes: 6 additions & 10 deletions backend/src/models/SponsorshipPolicyChain.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
import { Sequelize, DataTypes, Model } from 'sequelize';

export class SponsorshipPolicyChain extends Model {
declare policyChainId: number;
declare policyId: number;
declare chainName: string;
declare chainId: number;
}

export function initializeSponsorshipPolicyChainModel(sequelize: Sequelize) {
SponsorshipPolicyChain.init({
policyChainId: {
type: DataTypes.INTEGER,
primaryKey: true,
field: 'POLICY_CHAIN_ID'
},
policyId: {
type: DataTypes.INTEGER,
primaryKey: true,
allowNull: false,
field: 'POLICY_ID'
},
chainName: {
type: DataTypes.STRING,
chainId: {
type: DataTypes.INTEGER,
primaryKey: true,
allowNull: false,
field: 'CHAIN_NAME'
field: 'CHAIN_ID'
},
}, {
tableName: 'sponsorship_policy_chains',
Expand Down
31 changes: 9 additions & 22 deletions backend/src/models/SponsorshipPolicyLimit.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Sequelize, DataTypes, Model } from 'sequelize';

export class SponsorshipPolicyLimit extends Model {
declare limitId: number;
declare policyId: number;
declare limitType: string;
declare maxUsd: number | null;
Expand All @@ -11,36 +10,24 @@ export class SponsorshipPolicyLimit extends Model {

export function initializeSponsorshipPolicyLimitModel(sequelize: Sequelize) {
SponsorshipPolicyLimit.init({
limitId: {
type: DataTypes.INTEGER,
primaryKey: true,
field: 'LIMIT_ID'
},
policyId: {
type: DataTypes.INTEGER,
primaryKey: true,
allowNull: false,
references: {
model: 'SponsorshipPolicy', // name of your model for sponsorship policies
key: 'id', // key in SponsorshipPolicy that policyId references
},
onDelete: 'CASCADE', // Add this line
field: 'POLICY_ID'
},
limitType: {
type: DataTypes.STRING,
type: DataTypes.STRING, // Adjust this if limitType is not a string
primaryKey: true,
allowNull: false,
field: 'LIMIT_TYPE'
},
maxUsd: {
type: DataTypes.REAL,
allowNull: true,
field: 'MAX_USD'
},
maxEth: {
type: DataTypes.REAL,
allowNull: true,
field: 'MAX_ETH'
},
maxOperations: {
type: DataTypes.INTEGER,
allowNull: true,
field: 'MAX_OPERATIONS'
},
// ... other fields ...
}, {
tableName: 'sponsorship_policy_limits',
sequelize,
Expand Down
63 changes: 42 additions & 21 deletions backend/src/models/associations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,71 @@ import { SponsorshipPolicyChain } from './SponsorshipPolicyChain';
import { SponsorshipPolicyLimit } from './SponsorshipPolicyLimit';

export function setupAssociations() {
// APIKey to Policy

/**
* APIKey to SponsorshipPolicy
* A single APIKey (the parent) can have many SponsorshipPolicies (the children).
* The link between them is made using the 'walletAddress' field of the APIKey and the 'walletAddress' field of the SponsorshipPolicy.
*/
APIKey.hasMany(SponsorshipPolicy, {
foreignKey: 'walletAddress',
sourceKey: 'walletAddress', // This is the new line
as: 'sponsorshipPolicies',
onDelete: 'CASCADE',
onUpdate: 'CASCADE'
sourceKey: 'walletAddress',
as: 'sponsorshipPolicies'
});

/**
* SponsorshipPolicy to APIKey
* A single SponsorshipPolicy (the child) belongs to one APIKey (the parent).
* The link between them is made using the 'walletAddress' field of the SponsorshipPolicy and the 'walletAddress' field of the APIKey.
*/
SponsorshipPolicy.belongsTo(APIKey, {
foreignKey: 'walletAddress',
targetKey: 'walletAddress',
as: 'apiKey', // Optional alias
onDelete: 'CASCADE',
onUpdate: 'CASCADE'
});

// Policy to PolicyChain
/**
* SponsorshipPolicy to SponsorshipPolicyChain
* A single SponsorshipPolicy (the parent) can have many SponsorshipPolicyChains (the children).
* The link between them is made using the 'id' field of the SponsorshipPolicy and the 'policyId' field of the SponsorshipPolicyChain
*/
SponsorshipPolicy.hasMany(SponsorshipPolicyChain, {
foreignKey: 'policyId',
as: 'policyChains', // Optional alias for easier access in code
onDelete: 'CASCADE', // Ensures related policy chains are deleted when a Policy is deleted
onUpdate: 'CASCADE'
sourceKey: 'id',
as: 'policyChains'
});


/**
* SponsorshipPolicyChain to SponsorshipPolicy
* A single SponsorshipPolicyChain (the child) belongs to one SponsorshipPolicy (the parent).
* The link between them is made using the 'policyId' field of the SponsorshipPolicyChain and the 'id' field of the SponsorshipPolicy.
*/
SponsorshipPolicyChain.belongsTo(SponsorshipPolicy, {
foreignKey: 'policyId',
as: 'policy', // Optional alias
onDelete: 'CASCADE',
onUpdate: 'CASCADE'
targetKey: 'id',
as: 'policy'
});

// Policy to PolicyLimit
/**
* SponsorshipPolicy to SponsorshipPolicyLimit
* A single SponsorshipPolicy (the parent) can have many SponsorshipPolicyLimits (the children).
* The link between them is made using the 'id' field of the SponsorshipPolicy and the 'policyId' field of the SponsorshipPolicyLimit.
*/
SponsorshipPolicy.hasMany(SponsorshipPolicyLimit, {
foreignKey: 'policyId',
as: 'policyLimits', // Optional alias for easier access in code
onDelete: 'CASCADE', // Ensures related policy limits are deleted when a Policy is deleted
onUpdate: 'CASCADE'
sourceKey: 'id',
as: 'policyLimits'
});

/**
* SponsorshipPolicyLimit to SponsorshipPolicy
* A single SponsorshipPolicyLimit (the child) belongs to one SponsorshipPolicy (the parent).
* The link between them is made using the 'policyId' field of the SponsorshipPolicyLimit and the 'id' field of the SponsorshipPolicy.
*/
SponsorshipPolicyLimit.belongsTo(SponsorshipPolicy, {
foreignKey: 'policyId',
as: 'policy', // Optional alias
onDelete: 'CASCADE',
onUpdate: 'CASCADE'
targetKey: 'id',
as: 'policy'
});
}