Skip to content

Commit

Permalink
feat: implement event subscriber
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyudong3 committed Jul 15, 2019
1 parent 694c801 commit b81fde9
Show file tree
Hide file tree
Showing 15 changed files with 265 additions and 278 deletions.
2 changes: 0 additions & 2 deletions server/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {UserModule} from './port/module/user.module';
import {ItemModule} from './port/module/item.module';
import {TransactionModule} from './port/module/transaction.module';
import {DatabaseModule} from './port/module/database.module';
import {EventSubscriberModule} from './port/adapter/service/blockchain/ethereum/event/event.subscriber.module';

@Module({
imports: [
Expand All @@ -18,7 +17,6 @@ import {EventSubscriberModule} from './port/adapter/service/blockchain/ethereum/
TransactionModule,
UserModule,
DatabaseModule,
EventSubscriberModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down
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();
});
});
},
);
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();
}
}
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();
});
});
},
);
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();
}
}

This file was deleted.

This file was deleted.

This file was deleted.

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);
}
}
Loading

0 comments on commit b81fde9

Please sign in to comment.