-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement swith method when emit ws
- Loading branch information
1 parent
48b4744
commit f7a0ed8
Showing
7 changed files
with
148 additions
and
74 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
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 |
---|---|---|
@@ -1,32 +1,91 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { WebsocketError } from '../entities'; | ||
import { JsonrpcError, JsonrpcRequestBody, RequestContext } from '../entities'; | ||
import { VerseService, TransactionService, ProxyService } from 'src/services'; | ||
|
||
@Injectable() | ||
export class CommunicateService { | ||
private allowedMethods: RegExp[]; | ||
private isUseBlockNumberCache: boolean; | ||
|
||
constructor(private readonly configService: ConfigService) { | ||
constructor( | ||
private readonly configService: ConfigService, | ||
private verseService: VerseService, | ||
private txService: TransactionService, | ||
private proxyService: ProxyService, | ||
) { | ||
this.isUseBlockNumberCache = !!this.configService.get<number>( | ||
'blockNumberCacheExpire', | ||
); | ||
this.allowedMethods = this.configService.get<RegExp[]>( | ||
'allowedMethods', | ||
) ?? [/^.*$/]; | ||
} | ||
|
||
async sendRequest(method: string, data: string): Promise<string> { | ||
const err = this.checkMethod(method); | ||
if (err) { | ||
return err; | ||
} | ||
return data; | ||
async sendRequest(requestContext: RequestContext, body: JsonrpcRequestBody) { | ||
const isUseReadNode = !!this.configService.get<string>('verseReadNodeUrl'); | ||
const result = await this.send(isUseReadNode, requestContext, body); | ||
|
||
return result; | ||
} | ||
checkMethod(method: string): string | null { | ||
|
||
checkMethod(method: string) { | ||
const checkMethod = this.allowedMethods.some((allowedMethod) => { | ||
return allowedMethod.test(method); | ||
}); | ||
if (!checkMethod) { | ||
return `Method ${method} is not allowed`; | ||
} else { | ||
return null; | ||
if (!checkMethod) | ||
throw new JsonrpcError(`Method ${method} is not allowed`, -32601); | ||
} | ||
|
||
async send( | ||
isUseReadNode: boolean, | ||
requestContext: RequestContext, | ||
body: JsonrpcRequestBody, | ||
) { | ||
try { | ||
const method = body.method; | ||
const { headers } = requestContext; | ||
this.checkMethod(method); | ||
|
||
if (method === 'eth_sendRawTransaction') { | ||
return await this.proxyService.sendTransaction(requestContext, body); | ||
} else if (method === 'eth_estimateGas') { | ||
return await this.verseService.postVerseMasterNode(headers, body); | ||
} else if (method === 'eth_blockNumber' && this.isUseBlockNumberCache) { | ||
return await this.txService.getBlockNumberCacheRes( | ||
requestContext, | ||
body.jsonrpc, | ||
body.id, | ||
); | ||
} | ||
|
||
if (isUseReadNode) { | ||
return await this.verseService.postVerseReadNode(headers, body); | ||
} else { | ||
return await this.verseService.postVerseMasterNode(headers, body); | ||
} | ||
} catch (err) { | ||
const status = 200; | ||
if (err instanceof JsonrpcError) { | ||
const data = { | ||
jsonrpc: body.jsonrpc, | ||
id: body.id, | ||
error: { | ||
code: err.code, | ||
message: err.message, | ||
}, | ||
}; | ||
console.error(err.message); | ||
return { | ||
status, | ||
data, | ||
}; | ||
} | ||
console.error(err); | ||
return { | ||
status, | ||
data: err, | ||
}; | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -23,4 +23,5 @@ export default () => ({ | |
/^eth_.*Filter$/, | ||
], | ||
inheritHostHeader: true, | ||
wsPort: process.env.WS_PORT, | ||
}); |
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,12 @@ | ||
import { NestFactory } from '@nestjs/core'; | ||
import { EventsModule } from './events.module'; | ||
import { ConfigService } from '@nestjs/config'; | ||
|
||
async function bootstrap() { | ||
const app = await NestFactory.create(EventsModule); | ||
const configService = app.get(ConfigService); | ||
const wsPort = configService.get('wsPort'); | ||
await app.listen(wsPort); | ||
} | ||
|
||
bootstrap(); |
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,37 @@ | ||
import { CacheModule, Module } from '@nestjs/common'; | ||
import { CommunicateGateway } from './communicates/communicate.gateway'; | ||
import { | ||
AllowCheckService, | ||
ProxyService, | ||
RateLimitService, | ||
TransactionService, | ||
TypeCheckService, | ||
VerseService, | ||
} from './services'; | ||
import { CommunicateService } from './communicates/communicate.service'; | ||
import { ConfigModule } from '@nestjs/config'; | ||
import { HttpModule } from '@nestjs/axios'; | ||
import configuration from './config/configuration'; | ||
import { DatastoreService } from './repositories'; | ||
|
||
@Module({ | ||
imports: [ | ||
ConfigModule.forRoot({ | ||
load: [configuration], | ||
}), | ||
HttpModule, | ||
CacheModule.register(), | ||
], | ||
providers: [ | ||
CommunicateGateway, | ||
CommunicateService, | ||
VerseService, | ||
AllowCheckService, | ||
TransactionService, | ||
ProxyService, | ||
TypeCheckService, | ||
DatastoreService, | ||
RateLimitService, | ||
], | ||
}) | ||
export class EventsModule {} |