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' ;
126import { OrdersMatched } from '../../generated/Core/IexecInterfaceToken' ;
137import { 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
1542describe ( '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+
118142export { OrdersMatched } ;
0 commit comments