-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP Adding recurring donations and anchorContractAddress entities
related to #1142
- Loading branch information
1 parent
4ef7d20
commit f7c452a
Showing
10 changed files
with
308 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
import {MigrationInterface, QueryRunner} from "typeorm"; | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
||
export class addnullableTransactionId1701979390554 implements MigrationInterface { | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE public.donation ALTER COLUMN "transactionId" DROP NOT NULL`); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE public.donation ALTER COLUMN "transactionId" SET NOT NULL`); | ||
} | ||
export class addnullableTransactionId1701979390554 | ||
implements MigrationInterface | ||
{ | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`ALTER TABLE public.donation ALTER COLUMN "transactionId" DROP NOT NULL`, | ||
); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`ALTER TABLE public.donation ALTER COLUMN "transactionId" SET NOT NULL`, | ||
); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
migration/1702364570535-create_anchor_contract_address_table.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
||
export class createAnchorContractAddressTable1702364570535 | ||
implements MigrationInterface | ||
{ | ||
async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(` | ||
CREATE TABLE "anchor_contract_address" ( | ||
"id" SERIAL PRIMARY KEY, | ||
"networkId" INTEGER NOT NULL, | ||
"isActive" BOOLEAN DEFAULT false, | ||
"address" TEXT NOT NULL, | ||
"projectId" INTEGER NULL, | ||
"creatorId" INTEGER NULL, | ||
"ownerId" INTEGER NULL, | ||
"updatedAt" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"createdAt" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
CONSTRAINT "UQ_address_networkId_project" UNIQUE ("address", "networkId", "projectId") | ||
); | ||
CREATE INDEX "IDX_address" ON "anchor_contract_address" ("address"); | ||
CREATE INDEX "IDX_networkId" ON "anchor_contract_address" ("networkId"); | ||
CREATE INDEX "IDX_projectId" ON "anchor_contract_address" ("projectId"); | ||
CREATE INDEX "IDX_creatorId" ON "anchor_contract_address" ("creatorId"); | ||
CREATE INDEX "IDX_ownerId" ON "anchor_contract_address" ("ownerId"); | ||
`); | ||
|
||
await queryRunner.query(` | ||
ALTER TABLE "anchor_contract_address" | ||
ADD CONSTRAINT "FK_anchor_contract_address_project" | ||
FOREIGN KEY ("projectId") REFERENCES "project"("id") | ||
ON DELETE SET NULL; | ||
`); | ||
} | ||
|
||
async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(` | ||
ALTER TABLE "anchor_contract_address" | ||
DROP CONSTRAINT "FK_anchor_contract_address_project"; | ||
`); | ||
|
||
await queryRunner.query(` | ||
DROP TABLE "anchor_contract_address"; | ||
`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { | ||
BaseEntity, | ||
Column, | ||
CreateDateColumn, | ||
Entity, | ||
Index, | ||
ManyToOne, | ||
PrimaryGeneratedColumn, | ||
RelationId, | ||
Unique, | ||
UpdateDateColumn, | ||
} from 'typeorm'; | ||
import { Field, ID, ObjectType } from 'type-graphql'; | ||
import { Project } from './project'; | ||
import { User } from './user'; | ||
|
||
@Entity() | ||
@ObjectType() | ||
@Unique(['address', 'networkId', 'project']) | ||
export class AnchorContractAddress extends BaseEntity { | ||
@Field(type => ID) | ||
@PrimaryGeneratedColumn() | ||
readonly id: number; | ||
|
||
@Field() | ||
@Column('boolean', { default: false }) | ||
isActive: boolean; | ||
|
||
@Field() | ||
@Column() | ||
networkId: number; | ||
|
||
@Index() | ||
@Field() | ||
@Column() | ||
address: string; | ||
|
||
@Index() | ||
@Field(type => Project) | ||
@ManyToOne(type => Project) | ||
project: Project; | ||
|
||
@RelationId((relatedAddress: AnchorContractAddress) => relatedAddress.project) | ||
@Column({ nullable: true }) | ||
projectId: number; | ||
|
||
@Index() | ||
@Field(type => User, { nullable: true }) | ||
@ManyToOne(type => User, { eager: true, nullable: true }) | ||
creator: User; | ||
|
||
@RelationId( | ||
(anchorContractAddress: AnchorContractAddress) => | ||
anchorContractAddress.creator, | ||
) | ||
@Column({ nullable: true }) | ||
creatorId: number; | ||
|
||
@Index() | ||
@Field(type => User, { nullable: true }) | ||
@ManyToOne(type => User, { eager: true, nullable: true }) | ||
owner: User; | ||
|
||
@RelationId( | ||
(anchorContractAddress: AnchorContractAddress) => | ||
anchorContractAddress.owner, | ||
) | ||
@Column({ nullable: true }) | ||
ownerId: number; | ||
|
||
@UpdateDateColumn() | ||
updatedAt: Date; | ||
|
||
@CreateDateColumn() | ||
createdAt: Date; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { | ||
BaseEntity, | ||
Column, | ||
CreateDateColumn, | ||
Entity, | ||
Index, | ||
ManyToOne, | ||
PrimaryGeneratedColumn, | ||
RelationId, | ||
Unique, | ||
UpdateDateColumn, | ||
} from 'typeorm'; | ||
import { Field, ID, ObjectType } from 'type-graphql'; | ||
import { Project } from './project'; | ||
import { User } from './user'; | ||
|
||
@Entity() | ||
@ObjectType() | ||
@Unique(['txHash', 'networkId', 'project']) | ||
// TODO entity is not completed | ||
export class RecurringDonation extends BaseEntity { | ||
@Field(type => ID) | ||
@PrimaryGeneratedColumn() | ||
readonly id: number; | ||
|
||
@Field() | ||
@Column() | ||
networkId: number; | ||
|
||
@Index() | ||
@Field() | ||
@Column() | ||
txHash: string; | ||
|
||
@Index() | ||
@Field(type => Project) | ||
@ManyToOne(type => Project) | ||
project: Project; | ||
|
||
@RelationId( | ||
(recurringDonation: RecurringDonation) => recurringDonation.project, | ||
) | ||
@Column({ nullable: true }) | ||
projectId: number; | ||
|
||
@Index() | ||
@Field(type => User, { nullable: true }) | ||
@ManyToOne(type => User, { eager: true, nullable: true }) | ||
donor: User; | ||
|
||
@RelationId((recurringDonation: RecurringDonation) => recurringDonation.donor) | ||
@Column({ nullable: true }) | ||
donorId: number; | ||
|
||
@UpdateDateColumn() | ||
updatedAt: Date; | ||
|
||
@CreateDateColumn() | ||
createdAt: Date; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { AnchorContractAddress } from '../entities/anchorContractAddress'; | ||
import { Project } from '../entities/project'; | ||
import { User } from '../entities/user'; | ||
|
||
export const addNewAnchorAddress = async (params: { | ||
project: Project; | ||
owner: User; | ||
creator: User; | ||
address: string; | ||
networkId: number; | ||
}): Promise<AnchorContractAddress> => { | ||
const anchorContractAddress = await AnchorContractAddress.create({ | ||
...params, | ||
isActive: true, | ||
}); | ||
return anchorContractAddress.save(); | ||
}; | ||
|
||
export const findActiveAnchorAddress = async (params: { | ||
projectId: number; | ||
networkId: number; | ||
}) => { | ||
const { projectId, networkId } = params; | ||
return AnchorContractAddress.findOne({ | ||
where: { | ||
isActive: true, | ||
networkId, | ||
projectId, | ||
}, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Arg, Ctx, Int, Query, Resolver } from 'type-graphql'; | ||
|
||
import { QfRoundHistory } from '../entities/qfRoundHistory'; | ||
import { getQfRoundHistory } from '../repositories/qfRoundHistoryRepository'; | ||
import { AnchorContractAddress } from '../entities/anchorContractAddress'; | ||
import { findProjectById } from '../repositories/projectRepository'; | ||
import { | ||
errorMessages, | ||
i18n, | ||
translationErrorMessagesKeys, | ||
} from '../utils/errorMessages'; | ||
import { | ||
addNewAnchorAddress, | ||
findActiveAnchorAddress, | ||
} from '../repositories/anchorContractAddressRepository'; | ||
import { ApolloContext } from '../types/ApolloContext'; | ||
import { findUserById } from '../repositories/userRepository'; | ||
|
||
@Resolver(of => AnchorContractAddress) | ||
export class AnchorContractAddressResolver { | ||
@Query(() => AnchorContractAddress, { nullable: true }) | ||
async addAnchorContractAddress( | ||
@Ctx() ctx: ApolloContext, | ||
@Arg('projectId', () => Int) projectId: number, | ||
@Arg('networkId', () => Int) networkId: number, | ||
@Arg('address', () => String) address: string, | ||
): Promise<AnchorContractAddress> { | ||
const userId = ctx?.req?.user?.userId; | ||
const creatorUser = await findUserById(userId); | ||
if (!creatorUser) { | ||
throw new Error(i18n.__(translationErrorMessagesKeys.UN_AUTHORIZED)); | ||
} | ||
const project = await findProjectById(projectId); | ||
if (!project) { | ||
throw new Error(i18n.__(translationErrorMessagesKeys.PROJECT_NOT_FOUND)); | ||
} | ||
const currentAnchorProjectAddress = await findActiveAnchorAddress({ | ||
projectId, | ||
networkId, | ||
}); | ||
|
||
if ( | ||
currentAnchorProjectAddress && | ||
project.adminUser.id !== creatorUser.id | ||
) { | ||
throw new Error( | ||
i18n.__( | ||
translationErrorMessagesKeys.THERE_IS_AN_ACTIVE_ANCHOR_ADDRESS_FOR_THIS_PROJECT_ONLY_ADMIN_CAN_CHANGE_IT, | ||
), | ||
); | ||
} | ||
|
||
// Validate anchor address, the owner of contract must be the project owner | ||
|
||
return addNewAnchorAddress({ | ||
project, | ||
owner: project.adminUser, | ||
creator: creatorUser, | ||
address, | ||
networkId, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters