Skip to content

Commit 4417b36

Browse files
use filterworkerpoolorders
1 parent ac07f64 commit 4417b36

File tree

2 files changed

+65
-5
lines changed

2 files changed

+65
-5
lines changed

packages/sdk/src/lib/dataProtectorCore/processProtectedData.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ import {
3434
import { IExecConsumer } from '../types/internalTypes.js';
3535
import { getWhitelistContract } from './smartContract/getWhitelistContract.js';
3636
import { isAddressInWhitelist } from './smartContract/whitelistContract.read.js';
37-
import { checkUserVoucher } from '../../utils/processProtectedData.models.js';
37+
import {
38+
checkUserVoucher,
39+
filterWorkerpoolOrders,
40+
} from '../../utils/processProtectedData.models.js';
3841

3942
export type ProcessProtectedData = typeof processProtectedData;
4043

@@ -182,11 +185,18 @@ export const processProtectedData = async ({
182185
title: 'FETCH_WORKERPOOL_ORDERBOOK',
183186
isDone: true,
184187
});
188+
const desiredPriceWorkerpoolOrder = filterWorkerpoolOrders({
189+
workerpoolOrders: [...workerpoolOrderbook.orders],
190+
useVoucher: vUseVoucher,
191+
userVoucher,
192+
});
193+
if (!desiredPriceWorkerpoolOrder) {
194+
throw new Error('No Workerpool order found.');
195+
}
185196

186197
const underMaxPriceOrders = fetchOrdersUnderMaxPrice(
187198
datasetOrderbook,
188199
appOrderbook,
189-
workerpoolOrderbook,
190200
vMaxPrice
191201
);
192202

@@ -206,13 +216,13 @@ export const processProtectedData = async ({
206216
});
207217
const requestorderToSign = await iexec.order.createRequestorder({
208218
app: vApp,
209-
category: underMaxPriceOrders.workerpoolorder.category,
219+
category: desiredPriceWorkerpoolOrder.category,
210220
dataset: vProtectedData,
211221
appmaxprice: underMaxPriceOrders.apporder.appprice,
212222
datasetmaxprice: underMaxPriceOrders.datasetorder.datasetprice,
213-
workerpoolmaxprice: underMaxPriceOrders.workerpoolorder.workerpoolprice,
223+
workerpoolmaxprice: desiredPriceWorkerpoolOrder.workerpoolprice,
214224
tag: SCONE_TAG,
215-
workerpool: underMaxPriceOrders.workerpoolorder.workerpool,
225+
workerpool: desiredPriceWorkerpoolOrder.workerpool,
216226
params: {
217227
iexec_input_files: vInputFiles,
218228
iexec_secrets: secretsId,
@@ -227,6 +237,7 @@ export const processProtectedData = async ({
227237
const { dealid, txHash } = await iexec.order.matchOrders(
228238
{
229239
requestorder,
240+
workerpoolorder: desiredPriceWorkerpoolOrder,
230241
...underMaxPriceOrders,
231242
},
232243
matchOptions

packages/sdk/src/utils/processProtectedData.models.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Address, BN } from 'iexec';
2+
import { PublishedWorkerpoolorder } from 'iexec/IExecOrderbookModule';
23

34
type VoucherInfo = {
45
owner: Address;
@@ -34,3 +35,51 @@ export function checkUserVoucher({
3435
);
3536
}
3637
}
38+
39+
export function filterWorkerpoolOrders({
40+
workerpoolOrders,
41+
useVoucher,
42+
userVoucher,
43+
}: {
44+
workerpoolOrders: PublishedWorkerpoolorder[];
45+
useVoucher: boolean;
46+
userVoucher?: VoucherInfo;
47+
}) {
48+
if (workerpoolOrders.length === 0) {
49+
return null;
50+
}
51+
52+
let eligibleWorkerpoolOrders = [...workerpoolOrders];
53+
let maxVoucherSponsoredAmount = 0; // may be safer to use bigint
54+
55+
if (useVoucher) {
56+
if (!userVoucher) {
57+
throw new Error(
58+
'useVoucher === true but userVoucher is undefined? Hum...'
59+
);
60+
}
61+
// only voucher sponsored workerpoolorders
62+
eligibleWorkerpoolOrders = eligibleWorkerpoolOrders.filter(({ order }) =>
63+
userVoucher.sponsoredWorkerpools.includes(order.workerpool)
64+
);
65+
if (eligibleWorkerpoolOrders.length === 0) {
66+
throw new Error(
67+
'Found some workerpool orders but none can be sponsored by your voucher.'
68+
);
69+
}
70+
maxVoucherSponsoredAmount = bnToNumber(userVoucher.balance);
71+
}
72+
73+
const [cheapestOrder] = eligibleWorkerpoolOrders.sort(
74+
(order1, order2) =>
75+
order1.order.workerpoolprice - order2.order.workerpoolprice
76+
);
77+
78+
if (
79+
!cheapestOrder ||
80+
cheapestOrder.order.workerpoolprice > maxVoucherSponsoredAmount
81+
) {
82+
return null;
83+
}
84+
return cheapestOrder.order;
85+
}

0 commit comments

Comments
 (0)