Skip to content

Commit

Permalink
Merge pull request #939 from rainlanguage/2024-10-12-debug-subgraph
Browse files Browse the repository at this point in the history
update subgraph tests
  • Loading branch information
thedavidmeister authored Oct 14, 2024
2 parents 01aaba5 + e4cba0c commit 114e4e2
Show file tree
Hide file tree
Showing 4 changed files with 498 additions and 249 deletions.
22 changes: 15 additions & 7 deletions subgraph/src/clear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,21 @@ import {
store,
} from "@graphprotocol/graph-ts";

export function makeClearV2Id(event: ethereum.Event): Bytes {
export function makeClearEventId(event: ethereum.Event): Bytes {
return Bytes.fromByteArray(
crypto.keccak256(event.address.concat(event.transaction.hash))
);
}

export function getOrderHash(value: ethereum.Value): Bytes {
return Bytes.fromByteArray(crypto.keccak256(ethereum.encode(value)!));
export function getOrdersHash(event: ClearV2): Bytes[] {
return [
Bytes.fromByteArray(
crypto.keccak256(ethereum.encode(event.parameters[1].value)!)
),
Bytes.fromByteArray(
crypto.keccak256(ethereum.encode(event.parameters[2].value)!)
),
];
}

export function makeClearBountyId(
Expand Down Expand Up @@ -195,10 +202,11 @@ export function handleClearBounty(
}

export function handleClear(event: ClearV2): void {
let clearTemporaryData = new ClearTemporaryData(makeClearV2Id(event));
let clearTemporaryData = new ClearTemporaryData(makeClearEventId(event));

let aliceOrderHash = getOrderHash(event.parameters[1].value);
let bobOrderHash = getOrderHash(event.parameters[2].value);
let hashes = getOrdersHash(event);
let aliceOrderHash = hashes[0];
let bobOrderHash = hashes[1];

let aliceInput =
event.params.alice.validInputs[
Expand Down Expand Up @@ -240,7 +248,7 @@ export function handleClear(event: ClearV2): void {
}

export function handleAfterClear(event: AfterClear): void {
let clearTemporaryData = ClearTemporaryData.load(makeClearV2Id(event));
let clearTemporaryData = ClearTemporaryData.load(makeClearEventId(event));
if (clearTemporaryData) {
// alice
createTrade(
Expand Down
146 changes: 27 additions & 119 deletions subgraph/tests/event-mocks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export class Evaluable {
}
}

function createOrder(
export function createOrder(
owner: Address,
evaluable: Evaluable,
validInputs: Array<IO>,
Expand Down Expand Up @@ -328,58 +328,17 @@ export function createMetaEvent(
return metaEvent;
}

export class ClearV2Struct {
owner: Address;
evaluable: Evaluable;
validInputs: Array<IO>;
validOutputs: Array<IO>;
nonce: Bytes;

constructor(
owner: Address,
evaluable: Evaluable,
validInputs: Array<IO>,
validOutputs: Array<IO>,
nonce: Bytes
) {
this.owner = owner;
this.evaluable = evaluable;
this.validInputs = validInputs;
this.validOutputs = validOutputs;
this.nonce = nonce;
}
}

export class ClearV2ClearConfigStruct {
aliceInputIOIndex: BigInt;
aliceOutputIOIndex: BigInt;
bobInputIOIndex: BigInt;
bobOutputIOIndex: BigInt;
aliceBountyVaultId: BigInt;
bobBountyVaultId: BigInt;

constructor(
aliceInputIOIndex: BigInt,
aliceOutputIOIndex: BigInt,
bobInputIOIndex: BigInt,
bobOutputIOIndex: BigInt,
aliceBountyVaultId: BigInt,
bobBountyVaultId: BigInt
) {
this.aliceInputIOIndex = aliceInputIOIndex;
this.aliceOutputIOIndex = aliceOutputIOIndex;
this.bobInputIOIndex = bobInputIOIndex;
this.bobOutputIOIndex = bobOutputIOIndex;
this.aliceBountyVaultId = aliceBountyVaultId;
this.bobBountyVaultId = bobBountyVaultId;
}
}

// event ClearV2(address,(address,(address,address,bytes),(address,uint8,uint256)[],(address,uint8,uint256)[],bytes32),(address,(address,address,bytes),(address,uint8,uint256)[],(address,uint8,uint256)[],bytes32),(uint256,uint256,uint256,uint256,uint256,uint256))
export function createClearEvent(
sender: Address,
alice: ClearV2Struct,
bob: ClearV2Struct,
clearConfig: ClearV2ClearConfigStruct
aliceOrder: ethereum.Tuple,
bobOrder: ethereum.Tuple,
aliceInputIOIndex: BigInt,
aliceOutputIOIndex: BigInt,
bobInputIOIndex: BigInt,
bobOutputIOIndex: BigInt,
aliceBountyVaultId: BigInt,
bobBountyVaultId: BigInt
): ClearV2 {
let mockEvent = newMockEvent();
let clearEvent = new ClearV2(
Expand All @@ -397,48 +356,20 @@ export function createClearEvent(
clearEvent.parameters.push(
new ethereum.EventParam("sender", ethereum.Value.fromAddress(sender))
);

let _alice = createOrder(
alice.owner,
alice.evaluable,
alice.validInputs,
alice.validOutputs,
alice.nonce
);
clearEvent.parameters.push(
new ethereum.EventParam("alice", ethereum.Value.fromTuple(_alice))
);

let _bob = createOrder(
bob.owner,
bob.evaluable,
bob.validInputs,
bob.validOutputs,
bob.nonce
new ethereum.EventParam("alice", ethereum.Value.fromTuple(aliceOrder))
);
clearEvent.parameters.push(
new ethereum.EventParam("bob", ethereum.Value.fromTuple(_bob))
new ethereum.EventParam("bob", ethereum.Value.fromTuple(bobOrder))
);

let _clearConfig = new ethereum.Tuple();
_clearConfig.push(
ethereum.Value.fromUnsignedBigInt(clearConfig.aliceInputIOIndex)
);
_clearConfig.push(
ethereum.Value.fromUnsignedBigInt(clearConfig.aliceOutputIOIndex)
);
_clearConfig.push(
ethereum.Value.fromUnsignedBigInt(clearConfig.bobInputIOIndex)
);
_clearConfig.push(
ethereum.Value.fromI32(clearConfig.bobOutputIOIndex.toI32())
);
_clearConfig.push(
ethereum.Value.fromUnsignedBigInt(clearConfig.aliceBountyVaultId)
);
_clearConfig.push(
ethereum.Value.fromUnsignedBigInt(clearConfig.bobBountyVaultId)
);
_clearConfig.push(ethereum.Value.fromUnsignedBigInt(aliceInputIOIndex));
_clearConfig.push(ethereum.Value.fromUnsignedBigInt(aliceOutputIOIndex));
_clearConfig.push(ethereum.Value.fromUnsignedBigInt(bobInputIOIndex));
_clearConfig.push(ethereum.Value.fromUnsignedBigInt(bobOutputIOIndex));
_clearConfig.push(ethereum.Value.fromUnsignedBigInt(aliceBountyVaultId));
_clearConfig.push(ethereum.Value.fromUnsignedBigInt(bobBountyVaultId));
clearEvent.parameters.push(
new ethereum.EventParam(
"clearConfig",
Expand All @@ -449,28 +380,13 @@ export function createClearEvent(
return clearEvent;
}

export class AfterClearClearStateChangeStruct {
aliceOutput: BigInt;
bobOutput: BigInt;
aliceInput: BigInt;
bobInput: BigInt;

constructor(
aliceOutput: BigInt,
bobOutput: BigInt,
aliceInput: BigInt,
bobInput: BigInt
) {
this.aliceOutput = aliceOutput;
this.bobOutput = bobOutput;
this.aliceInput = aliceInput;
this.bobInput = bobInput;
}
}

// event AfterClear(address,(uint256,uint256,uint256,uint256))
export function createAfterClearEvent(
sender: Address,
clearStateChange: AfterClearClearStateChangeStruct
aliceOutput: BigInt,
bobOutput: BigInt,
aliceInput: BigInt,
bobInput: BigInt
): AfterClear {
let mockEvent = newMockEvent();
let afterClearEvent = new AfterClear(
Expand All @@ -488,18 +404,10 @@ export function createAfterClearEvent(
new ethereum.EventParam("sender", ethereum.Value.fromAddress(sender))
);
let _clearStateChange = new ethereum.Tuple();
_clearStateChange.push(
ethereum.Value.fromUnsignedBigInt(clearStateChange.aliceOutput)
);
_clearStateChange.push(
ethereum.Value.fromUnsignedBigInt(clearStateChange.bobOutput)
);
_clearStateChange.push(
ethereum.Value.fromUnsignedBigInt(clearStateChange.aliceInput)
);
_clearStateChange.push(
ethereum.Value.fromUnsignedBigInt(clearStateChange.bobInput)
);
_clearStateChange.push(ethereum.Value.fromUnsignedBigInt(aliceOutput));
_clearStateChange.push(ethereum.Value.fromUnsignedBigInt(bobOutput));
_clearStateChange.push(ethereum.Value.fromUnsignedBigInt(aliceInput));
_clearStateChange.push(ethereum.Value.fromUnsignedBigInt(bobInput));
afterClearEvent.parameters.push(
new ethereum.EventParam(
"clearStateChange",
Expand Down
Loading

0 comments on commit 114e4e2

Please sign in to comment.