-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
265 additions
and
278 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
35 changes: 35 additions & 0 deletions
35
...er/src/port/adapter/service/blockchain/ethereum/contract/auction.contract.service.spec.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,35 @@ | ||
import {Test} from '@nestjs/testing'; | ||
import {ConfigModule} from 'nestjs-config'; | ||
import * as path from 'path'; | ||
import {Web3Module} from '../web3/web3.module'; | ||
import {instance, mock} from 'ts-mockito'; | ||
import {AuctionHandler} from '../event/handler/auction.event.handler'; | ||
import {AuctionContractService} from './auction.contract.service'; | ||
|
||
describe('AuctionContractService', () => { | ||
let auctionContractService: AuctionContractService; | ||
const mockAuctionEventHandler = mock(AuctionHandler); | ||
|
||
beforeAll( async () => { | ||
const module = await Test.createTestingModule({ | ||
imports: [ | ||
Web3Module, | ||
ConfigModule.load(path.resolve(__dirname, 'config', '**/!(*.d).{ts,js}')), | ||
], | ||
providers: [ | ||
AuctionContractService, | ||
{ | ||
provide: 'AuctionHandler', | ||
useValue: instance(mockAuctionEventHandler), | ||
}, | ||
], | ||
}).compile(); | ||
auctionContractService = module.get<AuctionContractService>(AuctionContractService); | ||
}); | ||
describe('#watchAuctionEvents()', () => { | ||
it('should check blockNum increase by one', async () => { | ||
await auctionContractService.watchAuctionEvents(); | ||
}); | ||
}); | ||
}, | ||
); |
42 changes: 42 additions & 0 deletions
42
server/src/port/adapter/service/blockchain/ethereum/contract/auction.contract.service.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,42 @@ | ||
import {Contract} from 'web3-eth-contract'; | ||
import Web3 from 'web3'; | ||
import {Inject, Injectable} from '@nestjs/common'; | ||
import {EventData} from 'web3-eth-contract'; | ||
import {InjectConfig} from 'nestjs-config'; | ||
import {AuctionHandler} from '../event/handler/auction.event.handler'; | ||
|
||
@Injectable() | ||
export class AuctionContractService { | ||
private auctionContract: Contract; | ||
private currentNum: number; | ||
|
||
constructor(@Inject('WEB3') private web3: Web3, @InjectConfig() private config, | ||
private auctionEventHandler: AuctionHandler) { | ||
this.createContract(); | ||
} | ||
|
||
private createContract(): void { | ||
this.auctionContract = new this.web3.eth.Contract(this.config.get('auction.abi'), this.config.get('auction.address')); | ||
} | ||
|
||
watchAuctionEvents() { | ||
setInterval(async () => { | ||
const blockNum = await this.web3.eth.getBlockNumber(); | ||
if (blockNum === this.currentNum) { | ||
// Nothing happen | ||
} else { | ||
const eventData: EventData[] | ||
= await this.auctionContract.getPastEvents('allEvents', { | ||
fromBlock: this.currentNum, | ||
toBlock: this.currentNum, | ||
}); | ||
await this.auctionEventHandler.callService(eventData); | ||
this.currentNum++; | ||
} | ||
}, 1000); | ||
} | ||
|
||
public watch() { | ||
this.watchAuctionEvents(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
server/src/port/adapter/service/blockchain/ethereum/contract/ghost.contract.service.spec.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,35 @@ | ||
import {Test} from '@nestjs/testing'; | ||
import {ConfigModule} from 'nestjs-config'; | ||
import * as path from 'path'; | ||
import {GhostContractService} from './ghost.contract.service'; | ||
import {Web3Module} from '../web3/web3.module'; | ||
import {GhostHandler} from '../event/handler/ghost.event.handler'; | ||
import {instance, mock} from 'ts-mockito'; | ||
|
||
describe('GhostContractService', () => { | ||
let ghostContractService: GhostContractService; | ||
const mockGhostEventHandler = mock(GhostHandler); | ||
|
||
beforeAll( async () => { | ||
const module = await Test.createTestingModule({ | ||
imports: [ | ||
Web3Module, | ||
ConfigModule.load(path.resolve(__dirname, 'config', '**/!(*.d).{ts,js}')), | ||
], | ||
providers: [ | ||
GhostContractService, | ||
{ | ||
provide: 'GhostHandler', | ||
useValue: instance(mockGhostEventHandler), | ||
}, | ||
], | ||
}).compile(); | ||
ghostContractService = module.get<GhostContractService>(GhostContractService); | ||
}); | ||
describe('#watchGhostEvents()', () => { | ||
it('should check blockNum increase by one', async () => { | ||
await ghostContractService.watchGhostEvents(); | ||
}); | ||
}); | ||
}, | ||
); |
42 changes: 42 additions & 0 deletions
42
server/src/port/adapter/service/blockchain/ethereum/contract/ghost.contract.service.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,42 @@ | ||
import {Contract} from 'web3-eth-contract'; | ||
import Web3 from 'web3'; | ||
import {Inject, Injectable} from '@nestjs/common'; | ||
import {EventData} from 'web3-eth-contract'; | ||
import {InjectConfig} from 'nestjs-config'; | ||
import {GhostHandler} from '../event/handler/ghost.event.handler'; | ||
|
||
@Injectable() | ||
export class GhostContractService { | ||
private ghostContract: Contract; | ||
private currentNum: number; | ||
|
||
constructor(@Inject('WEB3') private web3: Web3, @InjectConfig() private config, | ||
private ghostEventHandler: GhostHandler) { | ||
this.createContract(); | ||
} | ||
|
||
private createContract(): void { | ||
this.ghostContract = new this.web3.eth.Contract(this.config.get('ghost.abi'), this.config.get('ghost.address')); | ||
} | ||
|
||
watchGhostEvents() { | ||
setInterval(async () => { | ||
const blockNum = await this.web3.eth.getBlockNumber(); | ||
if (blockNum === this.currentNum) { | ||
console.log('nothing'); | ||
} else { | ||
const eventData: EventData[] | ||
= await this.ghostContract.getPastEvents('allEvents', { | ||
fromBlock: this.currentNum, | ||
toBlock: this.currentNum, | ||
}); | ||
await this.ghostEventHandler.callService(eventData); | ||
this.currentNum++; | ||
} | ||
}); | ||
} | ||
|
||
public watch() { | ||
this.watchGhostEvents(); | ||
} | ||
} |
17 changes: 0 additions & 17 deletions
17
server/src/port/adapter/service/blockchain/ethereum/event/event.subscriber.module.ts
This file was deleted.
Oops, something went wrong.
24 changes: 0 additions & 24 deletions
24
server/src/port/adapter/service/blockchain/ethereum/event/event.subscriber.spec.ts
This file was deleted.
Oops, something went wrong.
33 changes: 0 additions & 33 deletions
33
server/src/port/adapter/service/blockchain/ethereum/event/event.subscriber.ts
This file was deleted.
Oops, something went wrong.
59 changes: 59 additions & 0 deletions
59
server/src/port/adapter/service/blockchain/ethereum/event/handler/auction.event.handler.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,59 @@ | ||
import {Inject, Injectable} from '@nestjs/common'; | ||
import {EventHandler} from './event.handler'; | ||
import {AuctionService} from '../../../../../../../app/auction/auction.service'; | ||
import {EventData} from 'web3-eth-contract'; | ||
import {AuctionDto} from '../../../../../../../app/auction/dto/auction.dto'; | ||
|
||
@Injectable() | ||
export class AuctionHandler implements EventHandler { | ||
constructor(@Inject('AuctionService') private auctionService: AuctionService) { | ||
} | ||
|
||
public async callService(eventData: EventData[]): Promise<void> { | ||
for (const data of eventData) { | ||
if (data.event === 'AuctionCreated') { | ||
await this.createAuction(data); | ||
} | ||
if (data.event === 'AuctionSuccessful') { | ||
await this.updateWinner(data); | ||
} | ||
if (data.event === 'AuctionCancelled') { | ||
await this.cancelAuction(data); | ||
} | ||
// if (data.event === 'AuctionEnded') { | ||
// await this.endAuction(data); | ||
// } | ||
} | ||
} | ||
|
||
async createAuction(event: EventData): Promise<void> { | ||
const { | ||
gene, | ||
seller, | ||
duration, | ||
type, | ||
winner, | ||
bidAmount, | ||
} = event.returnValues; | ||
await this.auctionService.createAuction(new AuctionDto(gene, seller, duration, type, winner, bidAmount)); | ||
} | ||
|
||
async updateWinner(event: EventData): Promise<void> { | ||
const { | ||
gene, | ||
winner, | ||
bidAmount, | ||
} = event.returnValues; | ||
await this.auctionService.updateWinner(gene, winner, bidAmount); | ||
} | ||
|
||
async cancelAuction(event: EventData): Promise<void> { | ||
const { gene } = event.returnValues; | ||
await this.auctionService.cancelAuction(gene); | ||
} | ||
|
||
async endAuction(event: EventData): Promise<void> { | ||
const gene = event.returnValues.gene; | ||
await this.auctionService.endAuction(gene); | ||
} | ||
} |
Oops, something went wrong.