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

Getting events from explorer datasource #5

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
22 changes: 18 additions & 4 deletions src/rskExplorerApi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ import {
TokensServerResponse,
RbtcBalancesServerResponse,
TransactionServerResponse,
InternalTransactionServerResponse
InternalTransactionServerResponse,
Events
} from './types'
import { fromApiToRtbcBalance, fromApiToTEvents, fromApiToTokens, fromApiToTokenWithBalance } from './utils'
import {
fromApiToRtbcBalance, fromApiToTEvents, fromApiToTokens,
fromApiToTokenWithBalance, fromExplorerApiToTEvents
} from './utils'
import { GetEventLogsByAddressAndTopic0 } from '../service/address/AddressService'

export class RSKExplorerAPI extends DataSource {
private chainId: number
Expand Down Expand Up @@ -140,7 +145,16 @@ export class RSKExplorerAPI extends DataSource {
throw new Error('Feature not supported')
}

getEventLogsByAddressAndTopic0 () {
throw new Error('Feature not supported')
getEventLogsByAddressAndTopic0 ({ address, topic0 } :
Omit<GetEventLogsByAddressAndTopic0, 'chainId'>) {
const params = {
module: 'events',
action: 'getEventsByAddress',
address: address.toLowerCase()
}
return this.axios!.get<Events>(this.url, { params })
.then(response => fromExplorerApiToTEvents(address, topic0,
response.data.data))
.catch(this.errorHandling)
}
}
48 changes: 48 additions & 0 deletions src/rskExplorerApi/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,51 @@ export interface IInternalTransaction {
export interface InternalTransactionServerResponse {
data: IInternalTransaction[]
}

export interface Pages {
sortDir: number;
limit: number;
next: null;
prev: null;
page: number;
total: null;
pages: any[];
}

export interface Datum {
eventId: string;
address: string;
blockHash: string;
blockNumber: number;
data: string;
event: null | string;
logIndex: number;
timestamp: number;
transactionHash: string;
transactionIndex: number;
txStatus: string;
topics: string[];
_addresses: string[];
abi: any;
_addressData: any;
args?: string[];
signature?: string;
}

export interface Events {
pages: Pages;
data: Datum[];
}

export interface ExplorerEvent {
address: string;
blockNumber: string;
data: string;
gasPrice: string;
gasUsed: string;
logIndex: string;
timeStamp: string;
topics: Array<null | string>;
transactionHash: string;
transactionIndex: string;
}
19 changes: 19 additions & 0 deletions src/rskExplorerApi/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {
Datum,
ExplorerEvent,
IApiEvents,
IApiTokens,
IEvent,
Expand Down Expand Up @@ -51,3 +53,20 @@ export const fromApiToTEvents = (apiEvent:IApiEvents): IEvent =>
transactionHash: apiEvent.transactionHash,
txStatus: apiEvent.txStatus
})

export const fromExplorerApiToTEvents = (address: string, topic0: string, events: Datum[]): ExplorerEvent[] => {
return events
.filter(event => event.topics.includes(topic0))
.map(event => ({
address,
blockNumber: event.blockNumber + '',
data: event.data,
gasPrice: event._addressData?.createdByTx?.gasPrice as string,
gasUsed: event._addressData?.createdByTx?.receipt?.gasUsed as string,
logIndex: event.logIndex + '',
timeStamp: event.timestamp + '',
topics: event.topics,
transactionHash: event.transactionHash,
transactionIndex: event.transactionIndex + ''
}))
}
Loading