-
Notifications
You must be signed in to change notification settings - Fork 0
Database-user-repositories #1
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
Changes from 2 commits
bd17b27
42238f0
0c06e18
0089c5a
f44f131
6ac97e6
cac6216
4a7d522
968d8ee
f3bbb06
eb8dc00
dfef8c1
ef61f19
dc68588
98a99f1
92b9c0d
00fd414
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| @bonadocs:registry=https://npm.pkg.github.com | ||
| //npm.pkg.github.com/:_authToken=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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, jest } from '@jest/globals'; | ||
| import { QueryResult } from 'pg'; | ||
|
|
||
| import { BonadocsLogger, createLogger } from '@bonadocs/logger'; | ||
|
|
||
| import { getMockLogger } from '../../../test/util'; | ||
| import { DbContext } from '../../connection/dbcontext'; | ||
|
|
||
| import { EvmContractRepository } from './evm-contracts.repository'; | ||
| import { queries } from './queries'; | ||
|
|
||
| describe('EvmContractRepository', () => { | ||
| let evmContractsRepository: EvmContractRepository; | ||
| let context: DbContext; | ||
laken11 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let logger: BonadocsLogger; | ||
|
|
||
| beforeEach(() => { | ||
| logger = getMockLogger(); | ||
| evmContractsRepository = new EvmContractRepository(logger); | ||
| context = { | ||
| isDbContext: true as boolean, | ||
| entrypoint: 'test', | ||
| logger: createLogger({ context: 'test' }) as BonadocsLogger, | ||
|
|
||
| // Mock query method | ||
| query: jest.fn().mockImplementation(() => | ||
| Promise.resolve({ | ||
| rowCount: 0, | ||
| rows: [], | ||
| oid: 0, | ||
| fields: [], | ||
| command: '', | ||
| } as QueryResult), | ||
| ), | ||
|
|
||
| // Mock transaction methods | ||
| beginTransaction: jest.fn().mockImplementation(() => Promise.resolve()), | ||
| commitTransaction: jest.fn().mockImplementation(() => Promise.resolve()), | ||
| rollbackTransaction: jest.fn().mockImplementation(() => Promise.resolve()), | ||
| } as unknown as DbContext; | ||
|
||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.clearAllMocks(); | ||
laken11 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }); | ||
|
|
||
| describe('getContractAbiHash', () => { | ||
| it('should return null if no contract is found', async () => { | ||
| (context.query as jest.Mock).mockImplementation(() => | ||
|
||
| Promise.resolve({ | ||
|
||
| rowCount: 0, | ||
| rows: [], | ||
| oid: 0, | ||
| fields: [], | ||
| command: '', | ||
| } as QueryResult), | ||
| ); | ||
|
|
||
| const result = await evmContractsRepository.getContractAbiHash(1, '0x123', context); | ||
|
|
||
| expect(result).toBeNull(); | ||
| expect(context.query).toHaveBeenCalledWith({ | ||
| text: queries.getContractAbiHash, | ||
| values: [1, '0x123'], | ||
| }); | ||
| }); | ||
|
|
||
| it('should return abi_hash if contract is found', async () => { | ||
| const mockAbiHash = 'mockAbiHash'; | ||
| (context.query as jest.Mock).mockImplementation(() => | ||
| Promise.resolve({ | ||
| rowCount: 1, | ||
| rows: [{ abi_hash: mockAbiHash }], | ||
| oid: 0, | ||
| fields: [], | ||
| command: '', | ||
| } as QueryResult), | ||
| ); | ||
|
|
||
| const result = await evmContractsRepository.getContractAbiHash(1, '0x123', context); | ||
|
|
||
| expect(result).toBe(mockAbiHash); | ||
| expect(context.query).toHaveBeenCalledWith({ | ||
| text: queries.getContractAbiHash, | ||
| values: [1, '0x123'], | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('createContract', () => { | ||
| it('should return true if contract is created successfully', async () => { | ||
| (context.query as jest.Mock).mockImplementation(() => | ||
| Promise.resolve({ | ||
| rowCount: 1, | ||
| rows: [{ id: 1 }], | ||
| oid: 0, | ||
| fields: [], | ||
| command: '', | ||
| } as QueryResult), | ||
| ); | ||
|
|
||
| const result = await evmContractsRepository.createContract( | ||
| { address: '0x123', chainId: 1, abiHash: 'mockAbiHash' }, | ||
| context, | ||
| ); | ||
|
|
||
| expect(result).toBe(true); | ||
| expect(context.query).toHaveBeenCalledWith({ | ||
| text: queries.insertContract, | ||
| values: ['0x123', 1, 'mockAbiHash'], | ||
| }); | ||
| }); | ||
|
|
||
| it('should return false if contract creation fails', async () => { | ||
| (context.query as jest.Mock).mockImplementation(() => | ||
| Promise.resolve({ | ||
| rowCount: 0, | ||
| rows: [], | ||
| oid: 0, | ||
| fields: [], | ||
| command: '', | ||
| } as QueryResult), | ||
| ); | ||
|
|
||
| const result = await evmContractsRepository.createContract( | ||
| { address: '0x123', chainId: 1, abiHash: 'mockAbiHash' }, | ||
| context, | ||
| ); | ||
|
|
||
| expect(result).toBe(false); | ||
| expect(context.query).toHaveBeenCalledWith({ | ||
| text: queries.insertContract, | ||
| values: ['0x123', 1, 'mockAbiHash'], | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { Inject } from 'typedi'; | ||
|
|
||
| import { diConstants } from '@bonadocs/di'; | ||
| import { BonadocsLogger } from '@bonadocs/logger'; | ||
|
|
||
| import { DbContext, withDbContext } from '../../connection/dbcontext'; | ||
|
|
||
| import { queries } from './queries'; | ||
| import { CreateContractDto } from './types'; | ||
|
|
||
| export class EvmContractRepository { | ||
laken11 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| constructor(@Inject(diConstants.logger) private readonly logger: BonadocsLogger) {} | ||
|
|
||
| @withDbContext | ||
| async getContractAbiHash( | ||
| chainId: number, | ||
| contractAddress: string, | ||
| context: DbContext, | ||
laken11 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ): Promise<string | null> { | ||
| const result = await context.query({ | ||
| text: queries.getContractAbiHash, | ||
| values: [chainId, contractAddress], | ||
| }); | ||
|
|
||
| if (!result.rowCount) { | ||
| return null; | ||
| } | ||
| this.logger.info(`Getting contract api hash with chanId ${chainId}`); | ||
| return result.rows[0].abi_hash; | ||
| } | ||
|
|
||
| @withDbContext | ||
| async createContract(data: CreateContractDto, context: DbContext): Promise<boolean> { | ||
| const result = await context.query({ | ||
| text: queries.insertContract, | ||
| values: [data.address, data.chainId, data.abiHash], | ||
| }); | ||
| return !!result.rowCount; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export { EvmContractRepository } from './evm-contracts.repository'; | ||
| export * from './types'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| export const queries = { | ||
| getContractAbiHash: `SELECT contract.abi_hash | ||
| FROM "bonadocs"."evm_contracts" contract | ||
| WHERE contract.chain_id = $1 | ||
| AND contract.address = $2 | ||
| LIMIT 1`, | ||
|
|
||
| insertContract: | ||
| 'INSERT INTO "bonadocs"."evm_contracts" (address, chain_id, abi_hash) VALUES ($1, $2, $3)', | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| export interface CreateContractDto { | ||
| chainId: number; | ||
| address: string; | ||
| abiHash: string; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export { ProjectRepository } from './project.repository'; | ||
| export * from './types'; |
Uh oh!
There was an error while loading. Please reload this page.