Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## vNEXT
- Migrate unit test files to Typescript & Hardhat:
- IexecOrderManagement (#101)
- IexecMaintenance (#100)
- IexecEscrowNative (#99)
- IexecERC20 (#98)
Expand Down
348 changes: 348 additions & 0 deletions test/byContract/IexecOrderManagement/IexecOrderManagement.ts

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-FileCopyrightText: 2020-2024 IEXEC BLOCKCHAIN TECH <contact@iex.ec>
// SPDX-License-Identifier: Apache-2.0

// TODO: Remove this file replaced by IexecOrderManagement.ts

const loadTruffleFixtureDeployment = require('../../../scripts/truffle-fixture-deployer');
// Config
var DEPLOYMENT = require('../../../config/config.json').chains.default;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-FileCopyrightText: 2020-2024 IEXEC BLOCKCHAIN TECH <contact@iex.ec>
// SPDX-License-Identifier: Apache-2.0

// TODO: Remove this file replaced by IexecOrderManagement.ts

const loadTruffleFixtureDeployment = require('../../../scripts/truffle-fixture-deployer');
// Config
var DEPLOYMENT = require('../../../config/config.json').chains.default;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-FileCopyrightText: 2020-2024 IEXEC BLOCKCHAIN TECH <contact@iex.ec>
// SPDX-License-Identifier: Apache-2.0

// TODO: Remove this file replaced by IexecOrderManagement.ts

const loadTruffleFixtureDeployment = require('../../../scripts/truffle-fixture-deployer');
// Config
var DEPLOYMENT = require('../../../config/config.json').chains.default;
Expand Down
46 changes: 36 additions & 10 deletions test/utils/IexecWrapper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-FileCopyrightText: 2024 IEXEC BLOCKCHAIN TECH <contact@iex.ec>
// SPDX-License-Identifier: Apache-2.0

import { TypedDataDomain } from '@ethersproject/abstract-signer';
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers';
import { BigNumber, ContractReceipt } from 'ethers';
import hre, { ethers } from 'hardhat';
Expand All @@ -18,18 +19,32 @@ import {
WorkerpoolRegistry__factory,
} from '../../typechain';
import { IexecPoco1__factory } from '../../typechain/factories/contracts/modules/interfaces/IexecPoco1.v8.sol';
import { IexecOrders, Orders, hashOrder, signOrders } from '../../utils/createOrders';
import {
IexecOrders,
OrderOperation,
Orders,
hashOrder,
signOrderOperation,
signOrders,
} from '../../utils/createOrders';
import { IexecAccounts, getDealId, getTaskId, setNextBlockTimestamp } from '../../utils/poco-tools';
import { extractEventsFromReceipt } from '../../utils/tools';
const DEPLOYMENT_CONFIG = config.chains.default;

export class IexecWrapper {
proxyAddress: string;
accounts: IexecAccounts;
domain: TypedDataDomain;

constructor(proxyAddress: string, accounts: IexecAccounts) {
this.proxyAddress = proxyAddress;
this.accounts = accounts;
this.domain = {
name: 'iExecODB',
version: '5.0.0',
chainId: hre.network.config.chainId,
verifyingContract: this.proxyAddress,
};
}

/**
Expand Down Expand Up @@ -89,6 +104,23 @@ export class IexecWrapper {
.then((tx) => tx.wait());
}

/**
* Hash an order using current domain.
*/
hashOrder(order: Record<string, any>) {
return hashOrder(this.domain, order);
}

/**
* Sign an order operation using current domain.
*/
async signOrderOperation(
orderOperation: OrderOperation,
signer: SignerWithAddress,
): Promise<void> {
return signOrderOperation(this.domain, orderOperation, signer);
}

async signAndSponsorMatchOrders(orders: IexecOrders) {
return this._signAndMatchOrders(orders, true);
}
Expand All @@ -104,13 +136,7 @@ export class IexecWrapper {
* Otherwise the requester will be in charge of paying for the deal.
*/
private async _signAndMatchOrders(orders: IexecOrders, withSponsor: boolean) {
const domain = {
name: 'iExecODB',
version: '5.0.0',
chainId: hre.network.config.chainId,
verifyingContract: this.proxyAddress,
};
await signOrders(domain, orders, {
await signOrders(this.domain, orders, {
appOwner: this.accounts.appProvider,
datasetOwner: this.accounts.datasetProvider,
workerpoolOwner: this.accounts.scheduler,
Expand All @@ -124,9 +150,9 @@ export class IexecWrapper {
await IexecAccessors__factory.connect(
this.proxyAddress,
this.accounts.anyone,
).viewConsumed(hashOrder(domain, requestOrder))
).viewConsumed(this.hashOrder(requestOrder))
).toNumber();
const dealId = getDealId(domain, requestOrder, taskIndex);
const dealId = getDealId(this.domain, requestOrder, taskIndex);
const taskId = getTaskId(dealId, taskIndex);
const volume = Number(requestOrder.volume);
const taskPrice =
Expand Down
36 changes: 34 additions & 2 deletions utils/createOrders.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// SPDX-FileCopyrightText: 2023 IEXEC BLOCKCHAIN TECH <contact@iex.ec>
// SPDX-FileCopyrightText: 2023-2024 IEXEC BLOCKCHAIN TECH <contact@iex.ec>
// SPDX-License-Identifier: Apache-2.0

import { TypedDataDomain } from '@ethersproject/abstract-signer';
import { BigNumber } from '@ethersproject/bignumber';
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers';
import { ethers } from 'hardhat';
import { IexecLibOrders_v5 } from '../typechain';
import constants from './constants';
import constants, { NULL } from './constants';
import { utils } from './odb-tools';
import { OrderOperationEnum } from './poco-tools';

export type Orders = [
IexecLibOrders_v5.AppOrderStruct,
Expand Down Expand Up @@ -52,6 +54,12 @@ export interface IexecOrders {
requester: IexecLibOrders_v5.RequestOrderStruct;
}

export interface OrderOperation {
order: Record<string, any>;
operation: BigNumber;
sign: string;
}

export function createEmptyAppOrder(): IexecLibOrders_v5.AppOrderStruct {
return {
app: constants.NULL.ADDRESS,
Expand Down Expand Up @@ -117,6 +125,13 @@ export function createEmptyDatasetOrder(): IexecLibOrders_v5.DatasetOrderStruct
};
}

/**
* Create an order operation from an existing order.
*/
export function createOrderOperation<OrderType>(order: OrderType, operation: OrderOperationEnum) {
return { order, operation: BigNumber.from(operation), sign: NULL.SIGNATURE };
}

export function buildOrders(matchOrdersArgs: MatchOrdersArgs) {
let requestOrder = createEmptyRequestOrder();
let appOrder = createEmptyAppOrder();
Expand Down Expand Up @@ -240,6 +255,23 @@ export async function signOrder(
return utils.signStruct(getTypeOf(order), order, domain, signer);
}

/**
* Sign an iExec EIP712 order operation for app, dataset, workerpool or request
* order operations.
*/
export async function signOrderOperation(
domain: TypedDataDomain,
orderOperation: OrderOperation,
signer: SignerWithAddress,
): Promise<void> {
return utils.signStruct(
getTypeOf(orderOperation.order) + 'Operation',
orderOperation,
domain,
signer,
);
}

/**
* Get typed data hash of order: app, dataset, workerpool or request
* @returns order hash
Expand Down
5 changes: 5 additions & 0 deletions utils/poco-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ export enum TaskStatusEnum {
FAILED,
}

export enum OrderOperationEnum {
SIGN,
CLOSE,
}

export interface IexecAccounts {
iexecAdmin: SignerWithAddress;
requester: SignerWithAddress;
Expand Down