Skip to content

Commit

Permalink
subgraph: update ethStreamedPerTick on more events
Browse files Browse the repository at this point in the history
  • Loading branch information
eladmallel committed Nov 20, 2024
1 parent 8395db0 commit cf241d1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/nouns-subgraph/src/stream-escrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ export function handleStreamFastForwarded(event: StreamFastForwarded): void {
s.lastTick = event.params.newLastTick;
s.streamLengthInTicks = s.streamLengthInTicks - event.params.ticksToForward.toI32();
s.save();

const state = getStreamEscrowState();
state.ethStreamedPerTick = event.params.ethStreamedPerTick;
state.save();
}

export function handleStreamCanceled(event: StreamCanceled): void {
Expand All @@ -80,6 +84,10 @@ export function handleStreamCanceled(event: StreamCanceled): void {
s.canceled = true;
s.cancellationRefundAmount = event.params.amountToRefund;
s.save();

const state = getStreamEscrowState();
state.ethStreamedPerTick = event.params.ethStreamedPerTick;
state.save();
}

export function handleStreamsForwarded(event: StreamsForwarded): void {
Expand Down
34 changes: 34 additions & 0 deletions packages/nouns-subgraph/tests/stream-escrow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,22 @@ describe('stream-escrow', () => {
ed.newLastTick = BigInt.fromI32(6);
ed.ticksToForward = BigInt.fromI32(4);
ed.txHash = Bytes.fromI32(9876);
ed.ethStreamedPerTick = BigInt.fromI32(777);
let nounId = ed.nounId.toString();
let streamId = StreamsOfNoun.load(nounId)!.currentStream!;

// Stream state BEFORE
assert.fieldEquals('Stream', streamId, 'lastTick', BigInt.fromI32(12).toString());
assert.fieldEquals('Stream', streamId, 'streamLengthInTicks', BigInt.fromI32(10).toString());

// General state BEFORE
assert.fieldEquals(
'StreamEscrowState',
'STATE',
'ethStreamedPerTick',
BigInt.fromI32(0).toString(),
);

// handle the event
handleStreamFastForwarded(createStreamFastForwardedEvent(ed));

Expand Down Expand Up @@ -250,14 +259,31 @@ describe('stream-escrow', () => {
'streamLengthInTicks',
BigInt.fromI32(10).minus(ed.ticksToForward).toString(),
);

// General state AFTER
assert.fieldEquals(
'StreamEscrowState',
'STATE',
'ethStreamedPerTick',
ed.ethStreamedPerTick.toString(),
);
});
test('cancel a stream', () => {
const ed = new StreamCanceledData();
ed.nounId = BigInt.fromI32(2142);
ed.amountToRefund = BigInt.fromI32(142000);
ed.ethStreamedPerTick = BigInt.fromI32(765);
const nounId = ed.nounId.toString();
const streamId = StreamsOfNoun.load(nounId)!.currentStream!;

// General state BEFORE
assert.fieldEquals(
'StreamEscrowState',
'STATE',
'ethStreamedPerTick',
BigInt.fromI32(777).toString(),
);

handleStreamCanceled(createStreamCanceledEvent(ed));

assert.fieldEquals('Stream', streamId, 'canceled', true.toString());
Expand All @@ -267,6 +293,14 @@ describe('stream-escrow', () => {
'cancellationRefundAmount',
ed.amountToRefund.toString(),
);

// General state AFTER
assert.fieldEquals(
'StreamEscrowState',
'STATE',
'ethStreamedPerTick',
ed.ethStreamedPerTick.toString(),
);
});
test('forward streams', () => {
const stateBefore = getStreamEscrowState();
Expand Down
14 changes: 14 additions & 0 deletions packages/nouns-subgraph/tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,7 @@ export class StreamFastForwardedData {
nounId: BigInt = BIGINT_ZERO;
ticksToForward: BigInt = BIGINT_ZERO;
newLastTick: BigInt = BIGINT_ZERO;
ethStreamedPerTick: BigInt = BIGINT_ZERO;
eventBlockNumber: BigInt = BIGINT_ZERO;
eventBlockTimestamp: BigInt = BIGINT_ZERO;
txHash: Bytes = Bytes.fromI32(0);
Expand All @@ -927,6 +928,12 @@ export function createStreamFastForwardedEvent(
newEvent.parameters.push(
new ethereum.EventParam('newLastTick', ethereum.Value.fromUnsignedBigInt(input.newLastTick)),
);
newEvent.parameters.push(
new ethereum.EventParam(
'ethStreamedPerTick',
ethereum.Value.fromUnsignedBigInt(input.ethStreamedPerTick),
),
);

newEvent.block.number = input.eventBlockNumber;
newEvent.block.timestamp = input.eventBlockTimestamp;
Expand All @@ -939,6 +946,7 @@ export function createStreamFastForwardedEvent(
export class StreamCanceledData {
nounId: BigInt = BIGINT_ZERO;
amountToRefund: BigInt = BIGINT_ZERO;
ethStreamedPerTick: BigInt = BIGINT_ZERO;

eventBlockNumber: BigInt = BIGINT_ZERO;
eventBlockTimestamp: BigInt = BIGINT_ZERO;
Expand All @@ -959,6 +967,12 @@ export function createStreamCanceledEvent(input: StreamCanceledData): StreamCanc
ethereum.Value.fromUnsignedBigInt(input.amountToRefund),
),
);
newEvent.parameters.push(
new ethereum.EventParam(
'ethStreamedPerTick',
ethereum.Value.fromUnsignedBigInt(input.ethStreamedPerTick),
),
);

newEvent.block.number = input.eventBlockNumber;
newEvent.block.timestamp = input.eventBlockTimestamp;
Expand Down

0 comments on commit cf241d1

Please sign in to comment.