Skip to content
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

feat: implement ethereum event subscriber #75

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion server/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ import {DatabaseModule} from './port/module/database.module';
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
export class AppModule {
}
7 changes: 7 additions & 0 deletions server/src/config/auction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
abi: [{
name: 'auction',
type: 'function',
}],
address: 'ax1234',
};
7 changes: 7 additions & 0 deletions server/src/config/ghost.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
abi: [{
name: 'ghost',
type: 'function',
}],
address: 'ax1234',
};
5 changes: 5 additions & 0 deletions server/src/config/subscriber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
startBlock: 5853011,
endBlock: 'latest',
network: 'testNet',
};
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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {EventData} from 'web3-eth-contract';

export interface EventHandler {
callService(eventData: EventData[]): void;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {Inject, Injectable} from '@nestjs/common';
import {GhostService} from '../../../../../../../app/ghost/ghost.service';
import {EventData} from 'web3-eth-contract';
import {EventHandler} from './event.handler';
import {GhostDto} from '../../../../../../../app/ghost/dto/ghost.dto';

@Injectable()
export class GhostHandler implements EventHandler {
constructor(@Inject('GhostService') private ghostService: GhostService) {
}

public async callService(eventData: EventData[]): Promise<void> {
for (const data of eventData) {
if (data.event === 'Birth') {
await this.createEgg(data);
}
if (data.event === 'LevelUp') {
await this.levelUp(data);
}
if (data.event === 'Transfer') {
await this.transfer(data);
}
}
}

async createEgg(event: EventData): Promise<void> {
const {
owner,
gene,
} = event.returnValues;
await this.ghostService.createEgg(new GhostDto(owner, gene));
}

async levelUp(event: EventData): Promise<void> {
const {
gene,
level,
} = event.returnValues;
await this.ghostService.levelUp(gene, level);
}

async transfer(event: EventData): Promise<void> {
const {
from,
to,
gene,
} = event.returnValues;
await this.ghostService.transfer(from, to, gene);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
startBlock: 5853011,
endBlock: 'latest',
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
abi : [{
test: 'test',
}],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
abi: [{
test: 'test',
}],
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export default {
url: 'localhost:8545',
url: 'ws://localhost:8545',
type: 'socket',
abi : [{
test: 'test',
}],
};