Skip to content

Commit

Permalink
subgraph: add stream escrow tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eladmallel committed Nov 19, 2024
1 parent f4e472b commit 3634f28
Show file tree
Hide file tree
Showing 3 changed files with 330 additions and 8 deletions.
6 changes: 3 additions & 3 deletions packages/nouns-subgraph/src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,11 @@ export function getStreamEscrowState(): StreamEscrowState {
s.lastForwardTimestamp = BIGINT_ZERO;
s.ethStreamedPerTick = BIGINT_ZERO;
s.totalAmountStreamedToDAO = BIGINT_ZERO;
s.daoExecutor = '';
s.daoExecutor = ZERO_ADDRESS;
s.daoExecutorSetBlock = BIGINT_ZERO;
s.ethRecipient = '';
s.ethRecipient = ZERO_ADDRESS;
s.ethRecipientSetBlock = BIGINT_ZERO;
s.nounsRecipient = '';
s.nounsRecipient = ZERO_ADDRESS;
s.nounsRecipientSetBlock = BIGINT_ZERO;
}

Expand Down
161 changes: 156 additions & 5 deletions packages/nouns-subgraph/tests/stream-escrow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,39 @@ import {
createMockedFunction,
} from 'matchstick-as/assembly/index';
import {
handleAllowedToCreateStreamChanged,
handleDAOExecutorAddressSet,
handleETHRecipientSet,
handleETHStreamedToDAO,
handleNounsRecipientSet,
handleStreamCanceled,
handleStreamCreated,
handleStreamFastForwarded,
handleStreamsForwarded,
} from '../src/stream-escrow';
import {
AddressSetData,
AllowedToCreateStreamChangedData,
createAllowedToCreateStreamChangedEvent,
createDAOExecutorAddressSetEvent,
createETHRecipientSetEvent,
createETHStreamedToDAOEvent,
createNounsRecipientSetEvent,
createStreamCanceledEvent,
createStreamCreatedEvent,
createStreamFastForwardedEvent,
createStreamsForwardedEvent,
ETHStreamedToDAOData,
genericUniqueId,
StreamCanceledData,
StreamCreatedData,
StreamFastForwardedData,
StreamsForwardedData,
} from './utils';
import { BigInt, Bytes } from '@graphprotocol/graph-ts';
import { BIGINT_ONE, BIGINT_ZERO } from '../src/utils/constants';
import { StreamsOfNoun } from '../src/types/schema';
import { Address, BigInt, Bytes } from '@graphprotocol/graph-ts';
import { BIGINT_10K, BIGINT_ONE, BIGINT_ZERO, ZERO_ADDRESS } from '../src/utils/constants';
import { StreamCreationPermission, StreamsOfNoun } from '../src/types/schema';
import { getStreamEscrowState } from '../src/utils/helpers';

describe('stream-escrow', () => {
beforeEach(() => {
Expand Down Expand Up @@ -224,7 +241,141 @@ describe('stream-escrow', () => {
BigInt.fromI32(10).minus(ed.ticksToForward).toString(),
);
});
test('bla', () => {});
test('bla', () => {});
test('cancel a stream', () => {
const ed = new StreamCanceledData();
ed.nounId = BigInt.fromI32(2142);
ed.amountToRefund = BigInt.fromI32(142000);
const nounId = ed.nounId.toString();
const streamId = StreamsOfNoun.load(nounId)!.currentStream!;

handleStreamCanceled(createStreamCanceledEvent(ed));

assert.fieldEquals('Stream', streamId, 'canceled', true.toString());
assert.fieldEquals(
'Stream',
streamId,
'cancellationRefundAmount',
ed.amountToRefund.toString(),
);
});
test('forward streams', () => {
const stateBefore = getStreamEscrowState();
const ed = new StreamsForwardedData();
ed.currentTick = stateBefore.currentTick.plus(BIGINT_ONE);
ed.ethPerTickStreamEnded = BIGINT_ZERO;
ed.nextEthStreamedPerTick = stateBefore.ethStreamedPerTick.plus(BIGINT_ONE);
ed.lastForwardTimestamp = stateBefore.lastForwardTimestamp.plus(BIGINT_10K);

handleStreamsForwarded(createStreamsForwardedEvent(ed));

assert.fieldEquals('StreamEscrowState', 'STATE', 'currentTick', ed.currentTick.toString());
assert.fieldEquals(
'StreamEscrowState',
'STATE',
'ethStreamedPerTick',
ed.nextEthStreamedPerTick.toString(),
);
assert.fieldEquals(
'StreamEscrowState',
'STATE',
'lastForwardTimestamp',
ed.lastForwardTimestamp.toString(),
);
});
});
describe('admin events', () => {
test('handleAllowedToCreateStreamChanged', () => {
const ed = new AllowedToCreateStreamChangedData();
ed.address = Address.fromString('0x0000000000000000000000000000000000000001');
ed.allowed = true;
const entityId = ed.address.toHexString();

// Check BEFORE
assert.assertNull(StreamCreationPermission.load(entityId));

handleAllowedToCreateStreamChanged(createAllowedToCreateStreamChangedEvent(ed));

assert.fieldEquals('StreamCreationPermission', entityId, 'allowed', ed.allowed.toString());
});
test('handleDAOExecutorAddressSet', () => {
const ed = new AddressSetData();
ed.eventBlockNumber = BigInt.fromI32(1234);
ed.newAddress = Address.fromString('0x0000000000000000000000000000000000000001');

// Check BEFORE
assert.fieldEquals('StreamEscrowState', 'STATE', 'daoExecutor', ZERO_ADDRESS);
assert.fieldEquals(
'StreamEscrowState',
'STATE',
'daoExecutorSetBlock',
BIGINT_ZERO.toString(),
);

handleDAOExecutorAddressSet(createDAOExecutorAddressSetEvent(ed));

// Check AFTER
assert.fieldEquals('StreamEscrowState', 'STATE', 'daoExecutor', ed.newAddress.toHexString());
assert.fieldEquals(
'StreamEscrowState',
'STATE',
'daoExecutorSetBlock',
ed.eventBlockNumber.toString(),
);
});
test('handleETHRecipientSet', () => {
const ed = new AddressSetData();
ed.eventBlockNumber = BigInt.fromI32(1234);
ed.newAddress = Address.fromString('0x0000000000000000000000000000000000000001');

// Check BEFORE
assert.fieldEquals('StreamEscrowState', 'STATE', 'ethRecipient', ZERO_ADDRESS);
assert.fieldEquals(
'StreamEscrowState',
'STATE',
'ethRecipientSetBlock',
BIGINT_ZERO.toString(),
);

handleETHRecipientSet(createETHRecipientSetEvent(ed));

// Check AFTER
assert.fieldEquals('StreamEscrowState', 'STATE', 'ethRecipient', ed.newAddress.toHexString());
assert.fieldEquals(
'StreamEscrowState',
'STATE',
'ethRecipientSetBlock',
ed.eventBlockNumber.toString(),
);
});
test('handleNounsRecipientSet', () => {
const ed = new AddressSetData();
ed.eventBlockNumber = BigInt.fromI32(1234);
ed.newAddress = Address.fromString('0x0000000000000000000000000000000000000001');

// Check BEFORE
assert.fieldEquals('StreamEscrowState', 'STATE', 'nounsRecipient', ZERO_ADDRESS);
assert.fieldEquals(
'StreamEscrowState',
'STATE',
'nounsRecipientSetBlock',
BIGINT_ZERO.toString(),
);

handleNounsRecipientSet(createNounsRecipientSetEvent(ed));

// Check AFTER
assert.fieldEquals(
'StreamEscrowState',
'STATE',
'nounsRecipient',
ed.newAddress.toHexString(),
);
assert.fieldEquals(
'StreamEscrowState',
'STATE',
'nounsRecipientSetBlock',
ed.eventBlockNumber.toString(),
);
});
});
});
171 changes: 171 additions & 0 deletions packages/nouns-subgraph/tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,15 @@ import {
Transfer,
} from '../src/types/NounsToken/NounsToken';
import {
AllowedToCreateStreamChanged,
DAOExecutorAddressSet,
ETHRecipientSet,
ETHStreamedToDAO,
NounsRecipientSet,
StreamCanceled,
StreamCreated,
StreamFastForwarded,
StreamsForwarded,
} from '../src/types/StreamEscrow/StreamEscrow';

export function createProposalCreatedWithRequirementsEventV3(
Expand Down Expand Up @@ -930,6 +936,171 @@ export function createStreamFastForwardedEvent(
return newEvent;
}

export class StreamCanceledData {
nounId: BigInt = BIGINT_ZERO;
amountToRefund: BigInt = BIGINT_ZERO;

eventBlockNumber: BigInt = BIGINT_ZERO;
eventBlockTimestamp: BigInt = BIGINT_ZERO;
txHash: Bytes = Bytes.fromI32(0);
logIndex: BigInt = BIGINT_ZERO;
}

export function createStreamCanceledEvent(input: StreamCanceledData): StreamCanceled {
let newEvent = changetype<StreamCanceled>(newMockEvent());
newEvent.parameters = new Array();

newEvent.parameters.push(
new ethereum.EventParam('nounId', ethereum.Value.fromUnsignedBigInt(input.nounId)),
);
newEvent.parameters.push(
new ethereum.EventParam(
'amountToRefund',
ethereum.Value.fromUnsignedBigInt(input.amountToRefund),
),
);

newEvent.block.number = input.eventBlockNumber;
newEvent.block.timestamp = input.eventBlockTimestamp;
newEvent.transaction.hash = input.txHash;
newEvent.logIndex = input.logIndex;

return newEvent;
}

export class StreamsForwardedData {
currentTick: BigInt = BIGINT_ZERO;
ethPerTickStreamEnded: BigInt = BIGINT_ZERO;
nextEthStreamedPerTick: BigInt = BIGINT_ZERO;
lastForwardTimestamp: BigInt = BIGINT_ZERO;

eventBlockNumber: BigInt = BIGINT_ZERO;
eventBlockTimestamp: BigInt = BIGINT_ZERO;
txHash: Bytes = Bytes.fromI32(0);
logIndex: BigInt = BIGINT_ZERO;
}

export function createStreamsForwardedEvent(input: StreamsForwardedData): StreamsForwarded {
let newEvent = changetype<StreamsForwarded>(newMockEvent());
newEvent.parameters = new Array();

newEvent.parameters.push(
new ethereum.EventParam('currentTick', ethereum.Value.fromUnsignedBigInt(input.currentTick)),
);
newEvent.parameters.push(
new ethereum.EventParam(
'ethPerTickStreamEnded',
ethereum.Value.fromUnsignedBigInt(input.ethPerTickStreamEnded),
),
);
newEvent.parameters.push(
new ethereum.EventParam(
'nextEthStreamedPerTick',
ethereum.Value.fromUnsignedBigInt(input.nextEthStreamedPerTick),
),
);
newEvent.parameters.push(
new ethereum.EventParam(
'lastForwardTimestamp',
ethereum.Value.fromUnsignedBigInt(input.lastForwardTimestamp),
),
);

newEvent.block.number = input.eventBlockNumber;
newEvent.block.timestamp = input.eventBlockTimestamp;
newEvent.transaction.hash = input.txHash;
newEvent.logIndex = input.logIndex;

return newEvent;
}

export class AllowedToCreateStreamChangedData {
address: Address = Address.fromString('0x0000000000000000000000000000000000000000');
allowed: boolean = false;

eventBlockNumber: BigInt = BIGINT_ZERO;
eventBlockTimestamp: BigInt = BIGINT_ZERO;
txHash: Bytes = Bytes.fromI32(0);
logIndex: BigInt = BIGINT_ZERO;
}

export function createAllowedToCreateStreamChangedEvent(
input: AllowedToCreateStreamChangedData,
): AllowedToCreateStreamChanged {
let newEvent = changetype<AllowedToCreateStreamChanged>(newMockEvent());
newEvent.parameters = new Array();

newEvent.parameters.push(
new ethereum.EventParam('address_', ethereum.Value.fromAddress(input.address)),
);
newEvent.parameters.push(
new ethereum.EventParam('allowed', ethereum.Value.fromBoolean(input.allowed)),
);

newEvent.block.number = input.eventBlockNumber;
newEvent.block.timestamp = input.eventBlockTimestamp;
newEvent.transaction.hash = input.txHash;
newEvent.logIndex = input.logIndex;

return newEvent;
}

export class AddressSetData {
newAddress: Address = Address.fromString('0x0000000000000000000000000000000000000000');
eventBlockNumber: BigInt = BIGINT_ZERO;
eventBlockTimestamp: BigInt = BIGINT_ZERO;
txHash: Bytes = Bytes.fromI32(0);
logIndex: BigInt = BIGINT_ZERO;
}

export function createDAOExecutorAddressSetEvent(input: AddressSetData): DAOExecutorAddressSet {
let newEvent = changetype<DAOExecutorAddressSet>(newMockEvent());
newEvent.parameters = new Array();

newEvent.parameters.push(
new ethereum.EventParam('newAddress', ethereum.Value.fromAddress(input.newAddress)),
);

newEvent.block.number = input.eventBlockNumber;
newEvent.block.timestamp = input.eventBlockTimestamp;
newEvent.transaction.hash = input.txHash;
newEvent.logIndex = input.logIndex;

return newEvent;
}

export function createETHRecipientSetEvent(input: AddressSetData): ETHRecipientSet {
let newEvent = changetype<ETHRecipientSet>(newMockEvent());
newEvent.parameters = new Array();

newEvent.parameters.push(
new ethereum.EventParam('newAddress', ethereum.Value.fromAddress(input.newAddress)),
);

newEvent.block.number = input.eventBlockNumber;
newEvent.block.timestamp = input.eventBlockTimestamp;
newEvent.transaction.hash = input.txHash;
newEvent.logIndex = input.logIndex;

return newEvent;
}

export function createNounsRecipientSetEvent(input: AddressSetData): NounsRecipientSet {
let newEvent = changetype<NounsRecipientSet>(newMockEvent());
newEvent.parameters = new Array();

newEvent.parameters.push(
new ethereum.EventParam('newAddress', ethereum.Value.fromAddress(input.newAddress)),
);

newEvent.block.number = input.eventBlockNumber;
newEvent.block.timestamp = input.eventBlockTimestamp;
newEvent.transaction.hash = input.txHash;
newEvent.logIndex = input.logIndex;

return newEvent;
}

export function genericUniqueId(txHash: Bytes, logIndex: BigInt): string {
return txHash.toHexString().concat('-').concat(logIndex.toString());
}

0 comments on commit 3634f28

Please sign in to comment.