Skip to content

Commit

Permalink
Merge branch 'master' into feat/batch-conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
olivier7delf committed Sep 2, 2022
2 parents 9439c7d + 58bc597 commit fc1c6d2
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
40 changes: 32 additions & 8 deletions packages/payment-detection/src/erc777/superfluid-retriever.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,36 +44,54 @@ export class SuperFluidInfoRetriever {
};
}

/**
* Chronological sorting of events having payment reference and closing events without payment reference
* @returns List of streaming events
*/
protected async getStreamingEvents(): Promise<Partial<FlowUpdatedEvent>[]> {
const variables = this.getGraphVariables();
const { flow, untagged } = await this.client.GetSuperFluidEvents(variables);
return flow.concat(untagged).sort((a, b) => a.timestamp - b.timestamp);
}

/**
* First MVP version which convert :
* stream events queried from SuperFluid subgraph
* into payment events with the parameters expected by extractEvents function
* to compute balance from amounts in ERC20 style transactions
*/
public async getTransferEvents(): Promise<PaymentTypes.ERC777PaymentNetworkEvent[]> {
const variables = this.getGraphVariables();
const { flow, untagged } = await this.client.GetSuperFluidEvents(variables);
// Chronological sorting of events having payment reference and closing events without payment reference
const streamEvents = flow.concat(untagged).sort((a, b) => a.timestamp - b.timestamp);
const streamEvents = await this.getStreamingEvents();
const paymentEvents: PaymentTypes.ERC777PaymentNetworkEvent[] = [];
if (streamEvents.length < 1) {
return paymentEvents;
}

// if last event is ongoing stream then create end of stream to help compute balance
if (streamEvents[streamEvents.length - 1].flowRate > 0) {
const lastEventOngoing = streamEvents[streamEvents.length - 1].flowRate > 0;
if (lastEventOngoing) {
streamEvents.push({
oldFlowRate: streamEvents[streamEvents.length - 1].flowRate,
flowRate: 0,
timestamp: Utils.getCurrentTimestampInSecond(),
blockNumber: parseInt(streamEvents[streamEvents.length - 1].blockNumber),
blockNumber: streamEvents[streamEvents.length - 1].blockNumber,
transactionHash: streamEvents[streamEvents.length - 1].transactionHash,
} as FlowUpdatedEvent);
}

const TYPE_BEGIN = 0;
// const TYPE_UPDATE = 1;
const TYPE_END = 2;
const StreamEventMap: Record<number, PaymentTypes.STREAM_EVENT_NAMES> = {
0: PaymentTypes.STREAM_EVENT_NAMES.START_STREAM,
1: PaymentTypes.STREAM_EVENT_NAMES.UPDATE_STREAM,
2: PaymentTypes.STREAM_EVENT_NAMES.END_STREAM,
};
const getEventName = (flowEvent: Partial<FlowUpdatedEvent>) => {
if (flowEvent.type) {
return StreamEventMap[flowEvent.type];
}
};

for (let index = 1; index < streamEvents.length; index++) {
// we have to manage update of flowrate to pay different payment references with the same token
// but we do not manage in the MVP updating flowrate of ongoing payment
Expand All @@ -95,12 +113,18 @@ export class SuperFluidInfoRetriever {
name: this.eventName,
parameters: {
to: this.toAddress,
block: parseInt(streamEvents[index].blockNumber),
block: streamEvents[index].blockNumber,
txHash: streamEvents[index].transactionHash,
streamEventName: getEventName(streamEvents[index]),
},
timestamp: streamEvents[index].timestamp,
});
}
const newLastParameters = paymentEvents[paymentEvents.length - 1].parameters;
if (lastEventOngoing && newLastParameters) {
newLastParameters.streamEventName = PaymentTypes.STREAM_EVENT_NAMES.START_STREAM;
paymentEvents[paymentEvents.length - 1].parameters = newLastParameters;
}
return paymentEvents;
}
}
2 changes: 2 additions & 0 deletions packages/payment-detection/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { NearNativeTokenPaymentDetector } from './near-detector';
import { FeeReferenceBasedDetector } from './fee-reference-based-detector';
import { SuperFluidPaymentDetector } from './erc777/superfluid-detector';
import { EscrowERC20InfoRetriever } from './erc20/escrow-info-retriever';
import { SuperFluidInfoRetriever } from './erc777/superfluid-retriever';

export type { TheGraphClient } from './thegraph';

Expand All @@ -37,6 +38,7 @@ export {
SuperFluidPaymentDetector,
NearNativeTokenPaymentDetector,
EscrowERC20InfoRetriever,
SuperFluidInfoRetriever,
setProviderFactory,
initPaymentDetectionApiKeys,
getDefaultProvider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ const testSuiteWithDaix = (network: string, fDAIxToken: string) => {
expect(transferEvents[2].amount).toEqual('40509259259259000');
expect(transferEvents[0].parameters?.txHash).toEqual(paymentData.txHash);
expect(transferEvents[0].parameters?.block).toEqual(paymentData.block);
expect(transferEvents[0].parameters?.streamEventName).toEqual(
PaymentTypes.STREAM_EVENT_NAMES.END_STREAM,
);
});
});

Expand Down Expand Up @@ -91,6 +94,9 @@ const testSuiteWithDaix = (network: string, fDAIxToken: string) => {
expect(transferEvents[0].amount).toEqual(paymentData.amount);
expect(transferEvents[0].name).toEqual('payment');
expect(transferEvents[0].parameters?.to).toEqual(paymentData.to);
expect(transferEvents[0].parameters?.streamEventName).toEqual(
PaymentTypes.STREAM_EVENT_NAMES.END_STREAM,
);
});
});
});
Expand Down
6 changes: 6 additions & 0 deletions packages/types/src/payment-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,18 @@ export interface IPaymentNetworkBaseInfoRetriever<
* ERC777 networks and events
*/

export enum STREAM_EVENT_NAMES {
START_STREAM = 'start_stream',
END_STREAM = 'end_stream',
UPDATE_STREAM = 'update_stream',
}
/** Parameters for events of ERC777 payments */
export interface IERC777PaymentEventParameters {
from?: string;
to: string;
block?: number;
txHash?: string;
streamEventName?: STREAM_EVENT_NAMES;
}

/** ERC777 Payment Network Event */
Expand Down

0 comments on commit fc1c6d2

Please sign in to comment.