forked from mito-systems/sol-mem-gen
Use typeORM for interacting with DB
This commit is contained in:
parent
6e39f156d5
commit
77a59ac520
2082
package-lock.json
generated
2082
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -17,7 +17,9 @@
|
||||
"openai": "^4.77.0",
|
||||
"react": "^18",
|
||||
"react-dom": "^18",
|
||||
"level": "^7.0.0"
|
||||
"sqlite": "^4.0.25",
|
||||
"sqlite3": "^5.1.7",
|
||||
"typeorm": "^0.3.12"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^20",
|
||||
|
20
src/data-source.ts
Normal file
20
src/data-source.ts
Normal file
@ -0,0 +1,20 @@
|
||||
import { DataSource } from 'typeorm';
|
||||
import { Payment } from './entity/Payment';
|
||||
|
||||
export const AppDataSource = new DataSource({
|
||||
type: 'sqlite',
|
||||
database: './database.sqlite',
|
||||
synchronize: true,
|
||||
logging: false,
|
||||
entities: [Payment],
|
||||
migrations: [],
|
||||
subscribers: [],
|
||||
});
|
||||
|
||||
AppDataSource.initialize()
|
||||
.then(() => {
|
||||
console.log('Data Source has been initialized!');
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error('Error during Data Source initialization:', err);
|
||||
});
|
10
src/entity/Payment.ts
Normal file
10
src/entity/Payment.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
|
||||
|
||||
@Entity()
|
||||
export class Payment {
|
||||
@PrimaryGeneratedColumn()
|
||||
id!: number;
|
||||
|
||||
@Column({ unique: true })
|
||||
transactionSignature!: string;
|
||||
}
|
@ -1,13 +1,12 @@
|
||||
import level from 'level';
|
||||
|
||||
import { Connection } from '@solana/web3.js';
|
||||
import { TOKEN_PROGRAM_ID } from '@solana/spl-token';
|
||||
|
||||
import { AppDataSource } from '../data-source';
|
||||
import { Payment } from '../entity/Payment';
|
||||
|
||||
const SOLANA_RPC_URL = process.env.NEXT_PUBLIC_SOLANA_RPC_URL;
|
||||
const SOLANA_WEBSOCKET_URL = process.env.NEXT_PUBLIC_SOLANA_WEBSOCKET_URL;
|
||||
|
||||
const db = level('./usedSignaturesDB');
|
||||
|
||||
const connection = new Connection(
|
||||
SOLANA_RPC_URL,
|
||||
{
|
||||
@ -18,19 +17,16 @@ const connection = new Connection(
|
||||
);
|
||||
|
||||
export async function isSignatureUsed(transactionSignature: string): Promise<boolean> {
|
||||
try {
|
||||
await db.get(transactionSignature);
|
||||
return true;
|
||||
} catch (error) {
|
||||
if (error.notFound) {
|
||||
return false;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
const paymentRepository = AppDataSource.getRepository(Payment);
|
||||
const payment = await paymentRepository.findOneBy({ transactionSignature });
|
||||
return !!payment;
|
||||
}
|
||||
|
||||
export async function markSignatureAsUsed(transactionSignature: string): Promise<void> {
|
||||
await db.put(transactionSignature, 'used');
|
||||
const paymentRepository = AppDataSource.getRepository(Payment);
|
||||
const payment = new Payment();
|
||||
payment.transactionSignature = transactionSignature;
|
||||
await paymentRepository.save(payment);
|
||||
}
|
||||
|
||||
export async function verifyPayment(
|
||||
|
@ -16,6 +16,8 @@
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": true,
|
||||
"jsx": "preserve",
|
||||
"experimentalDecorators": true,
|
||||
"emitDecoratorMetadata": true,
|
||||
"plugins": [
|
||||
{
|
||||
"name": "next"
|
||||
|
Loading…
Reference in New Issue
Block a user