Skip to content

Commit be55145

Browse files
Migrate IexecOrderManagement unit tests (#101)
2 parents 05616c8 + cf94763 commit be55145

File tree

8 files changed

+430
-12
lines changed

8 files changed

+430
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## vNEXT
44
- Migrate unit test files to Typescript & Hardhat:
5+
- IexecOrderManagement (#101)
56
- IexecMaintenance (#100)
67
- IexecEscrowNative (#99)
78
- IexecERC20 (#98)

test/byContract/IexecOrderManagement/IexecOrderManagement.ts

Lines changed: 348 additions & 0 deletions
Large diffs are not rendered by default.

test/byContract/IexecOrderManagement/close.js renamed to test/byContract/IexecOrderManagement/close.js.skip

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// SPDX-FileCopyrightText: 2020-2024 IEXEC BLOCKCHAIN TECH <contact@iex.ec>
22
// SPDX-License-Identifier: Apache-2.0
33

4+
// TODO: Remove this file replaced by IexecOrderManagement.ts
5+
46
const loadTruffleFixtureDeployment = require('../../../scripts/truffle-fixture-deployer');
57
// Config
68
var DEPLOYMENT = require('../../../config/config.json').chains.default;

test/byContract/IexecOrderManagement/invalid.js renamed to test/byContract/IexecOrderManagement/invalid.js.skip

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// SPDX-FileCopyrightText: 2020-2024 IEXEC BLOCKCHAIN TECH <contact@iex.ec>
22
// SPDX-License-Identifier: Apache-2.0
33

4+
// TODO: Remove this file replaced by IexecOrderManagement.ts
5+
46
const loadTruffleFixtureDeployment = require('../../../scripts/truffle-fixture-deployer');
57
// Config
68
var DEPLOYMENT = require('../../../config/config.json').chains.default;

test/byContract/IexecOrderManagement/sign.js renamed to test/byContract/IexecOrderManagement/sign.js.skip

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// SPDX-FileCopyrightText: 2020-2024 IEXEC BLOCKCHAIN TECH <contact@iex.ec>
22
// SPDX-License-Identifier: Apache-2.0
33

4+
// TODO: Remove this file replaced by IexecOrderManagement.ts
5+
46
const loadTruffleFixtureDeployment = require('../../../scripts/truffle-fixture-deployer');
57
// Config
68
var DEPLOYMENT = require('../../../config/config.json').chains.default;

test/utils/IexecWrapper.ts

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-FileCopyrightText: 2024 IEXEC BLOCKCHAIN TECH <contact@iex.ec>
22
// SPDX-License-Identifier: Apache-2.0
33

4+
import { TypedDataDomain } from '@ethersproject/abstract-signer';
45
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers';
56
import { BigNumber, ContractReceipt } from 'ethers';
67
import hre, { ethers } from 'hardhat';
@@ -18,18 +19,32 @@ import {
1819
WorkerpoolRegistry__factory,
1920
} from '../../typechain';
2021
import { IexecPoco1__factory } from '../../typechain/factories/contracts/modules/interfaces/IexecPoco1.v8.sol';
21-
import { IexecOrders, Orders, hashOrder, signOrders } from '../../utils/createOrders';
22+
import {
23+
IexecOrders,
24+
OrderOperation,
25+
Orders,
26+
hashOrder,
27+
signOrderOperation,
28+
signOrders,
29+
} from '../../utils/createOrders';
2230
import { IexecAccounts, getDealId, getTaskId, setNextBlockTimestamp } from '../../utils/poco-tools';
2331
import { extractEventsFromReceipt } from '../../utils/tools';
2432
const DEPLOYMENT_CONFIG = config.chains.default;
2533

2634
export class IexecWrapper {
2735
proxyAddress: string;
2836
accounts: IexecAccounts;
37+
domain: TypedDataDomain;
2938

3039
constructor(proxyAddress: string, accounts: IexecAccounts) {
3140
this.proxyAddress = proxyAddress;
3241
this.accounts = accounts;
42+
this.domain = {
43+
name: 'iExecODB',
44+
version: '5.0.0',
45+
chainId: hre.network.config.chainId,
46+
verifyingContract: this.proxyAddress,
47+
};
3348
}
3449

3550
/**
@@ -89,6 +104,23 @@ export class IexecWrapper {
89104
.then((tx) => tx.wait());
90105
}
91106

107+
/**
108+
* Hash an order using current domain.
109+
*/
110+
hashOrder(order: Record<string, any>) {
111+
return hashOrder(this.domain, order);
112+
}
113+
114+
/**
115+
* Sign an order operation using current domain.
116+
*/
117+
async signOrderOperation(
118+
orderOperation: OrderOperation,
119+
signer: SignerWithAddress,
120+
): Promise<void> {
121+
return signOrderOperation(this.domain, orderOperation, signer);
122+
}
123+
92124
async signAndSponsorMatchOrders(orders: IexecOrders) {
93125
return this._signAndMatchOrders(orders, true);
94126
}
@@ -104,13 +136,7 @@ export class IexecWrapper {
104136
* Otherwise the requester will be in charge of paying for the deal.
105137
*/
106138
private async _signAndMatchOrders(orders: IexecOrders, withSponsor: boolean) {
107-
const domain = {
108-
name: 'iExecODB',
109-
version: '5.0.0',
110-
chainId: hre.network.config.chainId,
111-
verifyingContract: this.proxyAddress,
112-
};
113-
await signOrders(domain, orders, {
139+
await signOrders(this.domain, orders, {
114140
appOwner: this.accounts.appProvider,
115141
datasetOwner: this.accounts.datasetProvider,
116142
workerpoolOwner: this.accounts.scheduler,
@@ -124,9 +150,9 @@ export class IexecWrapper {
124150
await IexecAccessors__factory.connect(
125151
this.proxyAddress,
126152
this.accounts.anyone,
127-
).viewConsumed(hashOrder(domain, requestOrder))
153+
).viewConsumed(this.hashOrder(requestOrder))
128154
).toNumber();
129-
const dealId = getDealId(domain, requestOrder, taskIndex);
155+
const dealId = getDealId(this.domain, requestOrder, taskIndex);
130156
const taskId = getTaskId(dealId, taskIndex);
131157
const volume = Number(requestOrder.volume);
132158
const taskPrice =

utils/createOrders.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
// SPDX-FileCopyrightText: 2023 IEXEC BLOCKCHAIN TECH <contact@iex.ec>
1+
// SPDX-FileCopyrightText: 2023-2024 IEXEC BLOCKCHAIN TECH <contact@iex.ec>
22
// SPDX-License-Identifier: Apache-2.0
33

44
import { TypedDataDomain } from '@ethersproject/abstract-signer';
5+
import { BigNumber } from '@ethersproject/bignumber';
56
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers';
67
import { ethers } from 'hardhat';
78
import { IexecLibOrders_v5 } from '../typechain';
8-
import constants from './constants';
9+
import constants, { NULL } from './constants';
910
import { utils } from './odb-tools';
11+
import { OrderOperationEnum } from './poco-tools';
1012

1113
export type Orders = [
1214
IexecLibOrders_v5.AppOrderStruct,
@@ -52,6 +54,12 @@ export interface IexecOrders {
5254
requester: IexecLibOrders_v5.RequestOrderStruct;
5355
}
5456

57+
export interface OrderOperation {
58+
order: Record<string, any>;
59+
operation: BigNumber;
60+
sign: string;
61+
}
62+
5563
export function createEmptyAppOrder(): IexecLibOrders_v5.AppOrderStruct {
5664
return {
5765
app: constants.NULL.ADDRESS,
@@ -117,6 +125,13 @@ export function createEmptyDatasetOrder(): IexecLibOrders_v5.DatasetOrderStruct
117125
};
118126
}
119127

128+
/**
129+
* Create an order operation from an existing order.
130+
*/
131+
export function createOrderOperation<OrderType>(order: OrderType, operation: OrderOperationEnum) {
132+
return { order, operation: BigNumber.from(operation), sign: NULL.SIGNATURE };
133+
}
134+
120135
export function buildOrders(matchOrdersArgs: MatchOrdersArgs) {
121136
let requestOrder = createEmptyRequestOrder();
122137
let appOrder = createEmptyAppOrder();
@@ -240,6 +255,23 @@ export async function signOrder(
240255
return utils.signStruct(getTypeOf(order), order, domain, signer);
241256
}
242257

258+
/**
259+
* Sign an iExec EIP712 order operation for app, dataset, workerpool or request
260+
* order operations.
261+
*/
262+
export async function signOrderOperation(
263+
domain: TypedDataDomain,
264+
orderOperation: OrderOperation,
265+
signer: SignerWithAddress,
266+
): Promise<void> {
267+
return utils.signStruct(
268+
getTypeOf(orderOperation.order) + 'Operation',
269+
orderOperation,
270+
domain,
271+
signer,
272+
);
273+
}
274+
243275
/**
244276
* Get typed data hash of order: app, dataset, workerpool or request
245277
* @returns order hash

utils/poco-tools.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ export enum TaskStatusEnum {
2222
FAILED,
2323
}
2424

25+
export enum OrderOperationEnum {
26+
SIGN,
27+
CLOSE,
28+
}
29+
2530
export interface IexecAccounts {
2631
iexecAdmin: SignerWithAddress;
2732
requester: SignerWithAddress;

0 commit comments

Comments
 (0)