Skip to content

Commit 335c9e3

Browse files
Refactor should handle OrdersMatched test
1 parent 5a6252e commit 335c9e3

File tree

5 files changed

+245
-87
lines changed

5 files changed

+245
-87
lines changed

src/Modules/IexecPoco.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ export function handleOrdersMatched(event: OrdersMatchedEvent): void {
131131
fetchAccount(viewedDeal.requester.toHex()).save();
132132
fetchAccount(viewedDeal.beneficiary.toHex()).save();
133133
fetchAccount(viewedDeal.callback.toHex()).save();
134+
134135
let deal = fetchDeal(event.params.dealid.toHex());
135136
deal.app = viewedDeal.app.pointer.toHex();
136137
deal.appOwner = viewedDeal.app.owner.toHex();

test/Modules/IexecPoco.test.ts

Lines changed: 111 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,59 @@
11
// SPDX-FileCopyrightText: 2024-2025 IEXEC BLOCKCHAIN TECH <[email protected]>
22
// SPDX-License-Identifier: Apache-2.0
33

4-
import { Address, BigInt, Bytes, ethereum } from '@graphprotocol/graph-ts';
5-
import {
6-
assert,
7-
createMockedFunction,
8-
describe,
9-
newTypedMockEventWithParams,
10-
test,
11-
} from 'matchstick-as/assembly/index';
4+
import { BigInt, ethereum } from '@graphprotocol/graph-ts';
5+
import { assert, describe, newTypedMockEventWithParams, test } from 'matchstick-as/assembly/index';
126
import { OrdersMatched } from '../../generated/Core/IexecInterfaceToken';
137
import { handleOrdersMatched } from '../../src/Modules';
8+
import { EventParamBuilder } from '../utils/EventParamBuilder';
9+
import { buildDeal, mockAddress, mockBytes32, mockViewDeal } from '../utils/mock';
10+
import { nRLCToRLC } from '../utils/utils';
11+
12+
const pocoAddress = mockAddress('pocoAddress');
13+
const dealId = mockBytes32('dealId');
14+
const appHash = mockBytes32('appHash');
15+
const datasetHash = mockBytes32('datasetHash');
16+
const workerpoolHash = mockBytes32('workerpoolHash');
17+
const requestHash = mockBytes32('requestHash');
18+
const timestamp = BigInt.fromI32(123456789);
19+
const appAddress = mockAddress('appAddress');
20+
const appOwner = mockAddress('appOwner');
21+
const appPrice = BigInt.fromI32(1);
22+
const datasetAddress = mockAddress('datasetAddress');
23+
const datasetOwner = mockAddress('datasetOwner');
24+
const datasetPrice = BigInt.fromI32(2);
25+
const workerpoolAddress = mockAddress('workerpoolAddress');
26+
const workerpoolOwner = mockAddress('workerpoolOwner');
27+
const workerpoolPrice = BigInt.fromI32(3);
28+
const trust = BigInt.fromI32(4);
29+
const category = BigInt.fromI32(5);
30+
const tag = mockBytes32('tag');
31+
const requester = mockAddress('requester');
32+
const beneficiary = mockAddress('beneficiary');
33+
const callback = mockAddress('callback');
34+
const params = 'params';
35+
const startTime = BigInt.fromI32(6);
36+
const botFirst = BigInt.fromI32(7);
37+
const botSize = BigInt.fromI32(8);
38+
const workerStake = BigInt.fromI32(9);
39+
const schedulerRewardRatio = BigInt.fromI32(10);
40+
const sponsor = mockAddress('sponsor');
1441

1542
describe('IexecPoco', () => {
1643
test('Should handle OrdersMatched', () => {
17-
let pocoProxyAddress = Address.fromString('0x89205A3A3b2A69De6Dbf7f01ED13B2108B2c43e7');
18-
const dealId = Bytes.fromHexString(
19-
'0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef',
20-
);
21-
const appHash = dealId;
22-
const datasetHash = dealId;
23-
const workerpoolHash = dealId;
24-
const requestHash = dealId;
25-
const sponsor = Address.fromString('0xabcdef1234567890abcdef1234567890abcdef12');
26-
const timestamp = BigInt.fromI32(123456789);
27-
const assetAddress = Address.fromString('0x90cBa2Bbb19ecc291A12066Fd8329D65FA1f1947');
28-
const assetOwner = Address.fromString('0x90cBa2Bbb19ecc291A12066Fd8329D65FA1f1947');
29-
const assetPrice = 100;
30-
const uint256 = 200;
31-
const bytes32 = dealId; // change it
32-
const address = assetAddress; // change it
33-
const string = 'a string';
34-
createMockedFunction(
35-
pocoProxyAddress,
36-
'viewDeal',
37-
'viewDeal(bytes32):(((address,address,uint256),(address,address,uint256),(address,address,uint256),uint256,uint256,bytes32,address,address,address,string,uint256,uint256,uint256,uint256,uint256,address))',
38-
)
39-
.withArgs([ethereum.Value.fromFixedBytes(dealId)])
40-
.returns([
41-
ethereum.Value.fromTuple(
42-
changetype<ethereum.Tuple>([
43-
ethereum.Value.fromTuple(
44-
// app
45-
changetype<ethereum.Tuple>([
46-
ethereum.Value.fromAddress(assetAddress),
47-
ethereum.Value.fromAddress(assetOwner),
48-
ethereum.Value.fromI32(assetPrice),
49-
]),
50-
),
51-
ethereum.Value.fromTuple(
52-
// dataset
53-
changetype<ethereum.Tuple>([
54-
ethereum.Value.fromAddress(assetAddress),
55-
ethereum.Value.fromAddress(assetOwner),
56-
ethereum.Value.fromI32(assetPrice),
57-
]),
58-
),
59-
ethereum.Value.fromTuple(
60-
// workerpool
61-
changetype<ethereum.Tuple>([
62-
ethereum.Value.fromAddress(assetAddress),
63-
ethereum.Value.fromAddress(assetOwner),
64-
ethereum.Value.fromI32(assetPrice),
65-
]),
66-
),
67-
ethereum.Value.fromI32(uint256), // trust
68-
ethereum.Value.fromI32(uint256), // category
69-
ethereum.Value.fromFixedBytes(bytes32), // tag
70-
ethereum.Value.fromAddress(address), // requester
71-
ethereum.Value.fromAddress(address), // beneficiary
72-
ethereum.Value.fromAddress(address), // callback
73-
ethereum.Value.fromString(string), // params
74-
ethereum.Value.fromI32(uint256), // startTime
75-
ethereum.Value.fromI32(uint256), // botFirst
76-
ethereum.Value.fromI32(uint256), // botSize
77-
ethereum.Value.fromI32(uint256), // workerStake
78-
ethereum.Value.fromI32(uint256), // schedulerRewardRatio
79-
ethereum.Value.fromAddress(sponsor), // sponsor
80-
]),
81-
),
82-
]);
83-
44+
mockViewDeal(pocoAddress, dealId).returns([buildDefaultDeal()]);
8445
// Create the mock event
85-
let mockEvent = newTypedMockEventWithParams<OrdersMatched>([
86-
new ethereum.EventParam('dealid', ethereum.Value.fromFixedBytes(dealId)),
87-
new ethereum.EventParam('appHash', ethereum.Value.fromFixedBytes(appHash)),
88-
new ethereum.EventParam('datasetHash', ethereum.Value.fromFixedBytes(datasetHash)),
89-
new ethereum.EventParam(
90-
'workerpoolHash',
91-
ethereum.Value.fromFixedBytes(workerpoolHash),
92-
),
93-
new ethereum.EventParam('requestHash', ethereum.Value.fromFixedBytes(requestHash)),
94-
]);
46+
let mockEvent = newTypedMockEventWithParams<OrdersMatched>(
47+
EventParamBuilder.init()
48+
.bytes('dealid', dealId)
49+
.bytes('appHash', appHash)
50+
.bytes('datasetHash', datasetHash)
51+
.bytes('workerpoolHash', workerpoolHash)
52+
.bytes('requestHash', requestHash)
53+
.build(),
54+
);
9555
mockEvent.block.timestamp = timestamp;
96-
mockEvent.address = pocoProxyAddress;
56+
mockEvent.address = pocoAddress;
9757

9858
// Call the handler
9959
handleOrdersMatched(mockEvent);
@@ -106,13 +66,77 @@ describe('IexecPoco', () => {
10666

10767
assert.fieldEquals('OrdersMatched', entityId, 'deal', dealId.toHex());
10868
assert.fieldEquals('OrdersMatched', entityId, 'timestamp', timestamp.toString());
69+
// Check deal
70+
assert.fieldEquals('Deal', dealId.toHex(), 'app', appAddress.toHex());
71+
assert.fieldEquals('Deal', dealId.toHex(), 'appOwner', appOwner.toHex());
72+
assert.fieldEquals('Deal', dealId.toHex(), 'appPrice', nRLCToRLC(appPrice).toString());
73+
assert.fieldEquals('Deal', dealId.toHex(), 'dataset', datasetAddress.toHex());
74+
assert.fieldEquals('Deal', dealId.toHex(), 'datasetOwner', datasetOwner.toHex());
75+
assert.fieldEquals(
76+
'Deal',
77+
dealId.toHex(),
78+
'datasetPrice',
79+
nRLCToRLC(datasetPrice).toString(),
80+
);
81+
assert.fieldEquals('Deal', dealId.toHex(), 'workerpool', workerpoolAddress.toHex());
82+
assert.fieldEquals('Deal', dealId.toHex(), 'workerpoolOwner', workerpoolOwner.toHex());
83+
assert.fieldEquals(
84+
'Deal',
85+
dealId.toHex(),
86+
'workerpoolPrice',
87+
nRLCToRLC(workerpoolPrice).toString(),
88+
);
89+
assert.fieldEquals('Deal', dealId.toHex(), 'trust', trust.toString());
90+
assert.fieldEquals('Deal', dealId.toHex(), 'category', category.toString());
91+
assert.fieldEquals('Deal', dealId.toHex(), 'tag', tag.toHex());
92+
assert.fieldEquals('Deal', dealId.toHex(), 'requester', requester.toHex());
93+
assert.fieldEquals('Deal', dealId.toHex(), 'beneficiary', beneficiary.toHex());
94+
assert.fieldEquals('Deal', dealId.toHex(), 'callback', callback.toHex());
95+
assert.fieldEquals('Deal', dealId.toHex(), 'params', params);
96+
assert.fieldEquals('Deal', dealId.toHex(), 'startTime', startTime.toString());
97+
assert.fieldEquals('Deal', dealId.toHex(), 'botFirst', botFirst.toString());
98+
assert.fieldEquals('Deal', dealId.toHex(), 'botSize', botSize.toString());
99+
assert.fieldEquals('Deal', dealId.toHex(), 'workerStake', workerStake.toString());
100+
assert.fieldEquals(
101+
'Deal',
102+
dealId.toHex(),
103+
'schedulerRewardRatio',
104+
schedulerRewardRatio.toString(),
105+
);
109106
assert.fieldEquals('Deal', dealId.toHex(), 'sponsor', sponsor.toHex());
110-
// TODO: Verify others fields
107+
// TODO: Check other saved entities
111108

112109
// Assert that a transaction was logged (if applicable)
113110
const transactionId = mockEvent.transaction.hash.toHex();
114111
assert.fieldEquals('Transaction', transactionId, 'id', transactionId);
115112
});
116113
});
117114

115+
function buildDefaultDeal(): ethereum.Value {
116+
return buildDeal(
117+
appAddress,
118+
appOwner,
119+
appPrice,
120+
datasetAddress,
121+
datasetOwner,
122+
datasetPrice,
123+
workerpoolAddress,
124+
workerpoolOwner,
125+
workerpoolPrice,
126+
trust,
127+
category,
128+
tag,
129+
requester,
130+
beneficiary,
131+
callback,
132+
params,
133+
startTime,
134+
botFirst,
135+
botSize,
136+
workerStake,
137+
schedulerRewardRatio,
138+
sponsor,
139+
);
140+
}
141+
118142
export { OrdersMatched };

test/utils/EventParamBuilder.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// SPDX-FileCopyrightText: 2025 IEXEC BLOCKCHAIN TECH <[email protected]>
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import { Address, BigInt, Bytes, ethereum } from '@graphprotocol/graph-ts';
5+
6+
export class EventParamBuilder {
7+
private params: ethereum.EventParam[] = new Array<ethereum.EventParam>();
8+
9+
static init(): EventParamBuilder {
10+
return new EventParamBuilder();
11+
}
12+
13+
address(key: string, value: Address): EventParamBuilder {
14+
this.params.push(new ethereum.EventParam(key, ethereum.Value.fromAddress(value)));
15+
return this;
16+
}
17+
18+
bytes(key: string, value: Bytes): EventParamBuilder {
19+
this.params.push(new ethereum.EventParam(key, ethereum.Value.fromBytes(value)));
20+
return this;
21+
}
22+
23+
bigInt(key: string, value: BigInt): EventParamBuilder {
24+
this.params.push(new ethereum.EventParam(key, ethereum.Value.fromUnsignedBigInt(value)));
25+
return this;
26+
}
27+
28+
string(key: string, value: string): EventParamBuilder {
29+
this.params.push(new ethereum.EventParam(key, ethereum.Value.fromString(value)));
30+
return this;
31+
}
32+
33+
build(): ethereum.EventParam[] {
34+
return this.params;
35+
}
36+
}

test/utils/mock.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// SPDX-FileCopyrightText: 2025 IEXEC BLOCKCHAIN TECH <[email protected]>
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import { Address, BigInt, ByteArray, Bytes, crypto, ethereum } from '@graphprotocol/graph-ts';
5+
import { createMockedFunction, MockedFunction } from 'matchstick-as';
6+
7+
export function mockBytes32(input: string): Bytes {
8+
return Bytes.fromHexString(crypto.keccak256(ByteArray.fromUTF8(input)).toHex());
9+
}
10+
11+
export function mockAddress(input: string): Address {
12+
return Address.fromString(mockBytes32(input).toHex().slice(0, 42));
13+
}
14+
15+
export function mockViewDeal(pocoProxyAddress: Address, dealId: Bytes): MockedFunction {
16+
return createMockedFunction(
17+
pocoProxyAddress,
18+
'viewDeal',
19+
'viewDeal(bytes32):(((address,address,uint256),(address,address,uint256),(address,address,uint256),uint256,uint256,bytes32,address,address,address,string,uint256,uint256,uint256,uint256,uint256,address))',
20+
).withArgs([ethereum.Value.fromFixedBytes(dealId)]);
21+
}
22+
23+
export function buildDeal(
24+
appAddress: Address,
25+
appOwner: Address,
26+
appPrice: BigInt,
27+
datasetAddress: Address,
28+
datasetOwner: Address,
29+
datasetPrice: BigInt,
30+
workerpoolAddress: Address,
31+
workerpoolOwner: Address,
32+
workerpoolPrice: BigInt,
33+
trust: BigInt,
34+
category: BigInt,
35+
tag: Bytes,
36+
requester: Address,
37+
beneficiary: Address,
38+
callback: Address,
39+
params: string,
40+
startTime: BigInt,
41+
botFirst: BigInt,
42+
botSize: BigInt,
43+
workerStake: BigInt,
44+
schedulerRewardRatio: BigInt,
45+
sponsor: Address,
46+
): ethereum.Value {
47+
return ethereum.Value.fromTuple(
48+
changetype<ethereum.Tuple>([
49+
ethereum.Value.fromTuple(
50+
// app
51+
changetype<ethereum.Tuple>([
52+
ethereum.Value.fromAddress(appAddress),
53+
ethereum.Value.fromAddress(appOwner),
54+
ethereum.Value.fromI32(appPrice.toI32()),
55+
]),
56+
),
57+
ethereum.Value.fromTuple(
58+
// dataset
59+
changetype<ethereum.Tuple>([
60+
ethereum.Value.fromAddress(datasetAddress),
61+
ethereum.Value.fromAddress(datasetOwner),
62+
ethereum.Value.fromI32(datasetPrice.toI32()),
63+
]),
64+
),
65+
ethereum.Value.fromTuple(
66+
// workerpool
67+
changetype<ethereum.Tuple>([
68+
ethereum.Value.fromAddress(workerpoolAddress),
69+
ethereum.Value.fromAddress(workerpoolOwner),
70+
ethereum.Value.fromI32(workerpoolPrice.toI32()),
71+
]),
72+
),
73+
ethereum.Value.fromI32(trust.toI32()),
74+
ethereum.Value.fromI32(category.toI32()),
75+
ethereum.Value.fromFixedBytes(tag),
76+
ethereum.Value.fromAddress(requester),
77+
ethereum.Value.fromAddress(beneficiary),
78+
ethereum.Value.fromAddress(callback),
79+
ethereum.Value.fromString(params),
80+
ethereum.Value.fromI32(startTime.toI32()),
81+
ethereum.Value.fromI32(botFirst.toI32()),
82+
ethereum.Value.fromI32(botSize.toI32()),
83+
ethereum.Value.fromI32(workerStake.toI32()),
84+
ethereum.Value.fromI32(schedulerRewardRatio.toI32()),
85+
ethereum.Value.fromAddress(sponsor),
86+
]),
87+
);
88+
}

test/utils/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// SPDX-FileCopyrightText: 2025 IEXEC BLOCKCHAIN TECH <[email protected]>
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import { BigDecimal, BigInt } from '@graphprotocol/graph-ts';
5+
6+
export function nRLCToRLC(value: BigInt): BigDecimal {
7+
let divisor = BigDecimal.fromString('1000000000');
8+
return value.divDecimal(divisor);
9+
}

0 commit comments

Comments
 (0)