-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from eq-lab/feature/use-typeorm
use typeorm
- Loading branch information
Showing
15 changed files
with
1,715 additions
and
60 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,4 +1,5 @@ | ||
.vscode | ||
dist | ||
node_modules | ||
*.sqlite | ||
*.sqlite | ||
.env |
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
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,23 @@ | ||
import "reflect-metadata" | ||
import { DataSource } from "typeorm" | ||
import { SlenderKeeperState } from "./entity/SlenderKeeperState" | ||
import { SlenderBorrower } from "./entity/SlenderBorrowers" | ||
import { DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME } from '../../configuration'; | ||
|
||
export const AppDataSource = new DataSource({ | ||
type: "mssql", | ||
host: DB_HOST, | ||
username: DB_USERNAME, | ||
password: DB_PASSWORD, | ||
database: DB_NAME, | ||
synchronize: false, | ||
logging: false, | ||
entities: [SlenderKeeperState, SlenderBorrower], | ||
migrations: [], | ||
subscribers: [], | ||
options: { | ||
encrypt: false | ||
} | ||
}); | ||
|
||
process.on('exit', () => AppDataSource.destroy()); |
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,54 @@ | ||
import { CHAIN, POOL_ID } from "../../configuration" | ||
import { AppDataSource } from "./data-source" | ||
import { SlenderBorrower } from "./entity/SlenderBorrowers" | ||
import { SlenderKeeperState } from "./entity/SlenderKeeperState" | ||
|
||
export const readLastSyncedLedger = async () => { | ||
const slenderKeeperRepository = AppDataSource.getRepository(SlenderKeeperState); | ||
let currentState = await slenderKeeperRepository.findOneBy({ chain: CHAIN, contractAddress: POOL_ID }); | ||
if (currentState === null || currentState === undefined) { | ||
currentState = new SlenderKeeperState(); | ||
currentState.lastSynced = 0; | ||
currentState.chain = CHAIN; | ||
currentState.contractAddress = POOL_ID; | ||
await slenderKeeperRepository.save(currentState); | ||
} | ||
return currentState.lastSynced; | ||
} | ||
|
||
export const updateLastSyncedLedger = async (lastSyncedLedger: number) => { | ||
const slenderKeeperRepository = AppDataSource.getRepository(SlenderKeeperState); | ||
const currentState = await slenderKeeperRepository.findOneBy({ chain: CHAIN, contractAddress: POOL_ID }); | ||
currentState.lastSynced = lastSyncedLedger; | ||
await slenderKeeperRepository.save(currentState); | ||
} | ||
|
||
export const readBorrowers = async () => { | ||
const slenderKeeperRepository = AppDataSource.getRepository(SlenderKeeperState); | ||
const currentState = await slenderKeeperRepository.findOneBy({ chain: CHAIN, contractAddress: POOL_ID }); | ||
const slenderBorrowerRepository = AppDataSource.getRepository(SlenderBorrower); | ||
const borrowers = await slenderBorrowerRepository.findBy({ keeperStateId: currentState.id }); | ||
return borrowers; | ||
} | ||
|
||
export const insertBorrowers = async (borrowers: string[]) => { | ||
const slenderKeeperRepository = AppDataSource.getRepository(SlenderKeeperState); | ||
const currentState = await slenderKeeperRepository.findOneBy({ chain: CHAIN, contractAddress: POOL_ID }); | ||
const slenderBorrowerRepository = AppDataSource.getRepository(SlenderBorrower); | ||
for (const borrower of borrowers) { | ||
const b = new SlenderBorrower(); | ||
b.borrower = borrower; | ||
b.keeperStateId = currentState.id; | ||
await slenderBorrowerRepository.save(b); | ||
} | ||
} | ||
|
||
export const deleteBorrowers = async (borrowers: string[]) => { | ||
const slenderKeeperRepository = AppDataSource.getRepository(SlenderKeeperState); | ||
const currentState = await slenderKeeperRepository.findOneBy({ chain: CHAIN, contractAddress: POOL_ID }); | ||
const slenderBorrowerRepository = AppDataSource.getRepository(SlenderBorrower); | ||
for (const borrower of borrowers) { | ||
const b = await slenderBorrowerRepository.findOneBy({ keeperStateId: currentState.id, borrower }); | ||
slenderBorrowerRepository.remove(b); | ||
} | ||
} |
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,9 @@ | ||
import { Entity, PrimaryColumn } from "typeorm" | ||
|
||
@Entity("SlenderBorrowers") | ||
export class SlenderBorrower { | ||
@PrimaryColumn() | ||
keeperStateId: number | ||
@PrimaryColumn() | ||
borrower: string | ||
} |
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,16 @@ | ||
import { Column, Entity, PrimaryColumn, PrimaryGeneratedColumn } from "typeorm" | ||
|
||
@Entity("SlenderKeeperState") | ||
export class SlenderKeeperState { | ||
@PrimaryGeneratedColumn() | ||
id: number | ||
|
||
@Column() | ||
lastSynced: number | ||
|
||
@PrimaryColumn() | ||
contractAddress: string | ||
|
||
@PrimaryColumn() | ||
chain: string | ||
} |
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
File renamed without changes.
File renamed without changes.
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
Oops, something went wrong.