Skip to content

Commit

Permalink
[SDK] Fix AddToCollection function (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreJeanjacquot authored May 7, 2024
2 parents 0abfd48 + 60136bb commit 478554d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
4 changes: 4 additions & 0 deletions packages/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ All notable changes to this project will be documented in this file.

- `consumeProtectedData()`: Add two new status to ConsumeProtectedDataStatuses: "FETCH_WORKERPOOL_ORDERBOOK" and "PUSH_ENCRYPTION_KEY"

### Changed

- fixed `addToCollection()` method issue when the DataProtectorSharing contract is previously approved for the protected data

## [2.0.0-beta.1] (2024-05-02)

### Added
Expand Down
19 changes: 12 additions & 7 deletions packages/sdk/src/lib/dataProtectorSharing/addToCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,18 @@ export const addToCollection = async ({
protectedData: vProtectedData,
sharingContractAddress,
});
vOnStatusUpdate({
title: 'APPROVE_COLLECTION_CONTRACT',
isDone: true,
payload: {
approveTxHash: approveTx.hash,
},
});

const hasContractAlreadyBeenApproved = !approveTx;
// If no transaction, it means the dataProtectorSharing Contract has already been approved
if (hasContractAlreadyBeenApproved) {
vOnStatusUpdate({
title: 'APPROVE_COLLECTION_CONTRACT',
isDone: true,
payload: {
approveTxHash: approveTx?.hash,
},
});
}

try {
vOnStatusUpdate({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { beforeAll, describe, expect, it, jest } from '@jest/globals';
import { Wallet, type HDNodeWallet } from 'ethers';
import { IExecDataProtector } from '../../../src/index.js';
import { getTestConfig, timeouts } from '../../test-utils.js';
import { IExec } from 'iexec';
import { DEFAULT_SHARING_CONTRACT_ADDRESS } from '../../../src/config/config.js';
import { approveCollectionContract } from '../../../src/lib/dataProtectorSharing/smartContract/approveCollectionContract.js';

describe('dataProtector.addToCollection()', () => {
let dataProtector: IExecDataProtector;
Expand Down Expand Up @@ -50,6 +53,50 @@ describe('dataProtector.addToCollection()', () => {
timeouts.createCollection +
timeouts.addToCollection
);

it(
'should work, if the protectedData has already been approved to the DataProtectorSharing Contract',
async () => {
// --- GIVEN
const { address: protectedData } = await dataProtector.core.protectData(
{
data: { doNotUse: 'test' },
name: 'test addToCollection',
}
);

const { collectionId } = await dataProtector.sharing.createCollection();

const onStatusUpdateMock = jest.fn();
const [ethProvider, options] = getTestConfig(wallet.privateKey);
const iexec = new IExec(
{ ethProvider },
{ ipfsGatewayURL: options.ipfsGateway, ...options?.iexecOptions }
);
await approveCollectionContract({
iexec,
protectedData,
sharingContractAddress: DEFAULT_SHARING_CONTRACT_ADDRESS,
});

// --- WHEN
await dataProtector.sharing.addToCollection({
collectionId,
addOnlyAppWhitelist,
protectedData,
onStatusUpdate: onStatusUpdateMock,
});

// --- THEN
expect(onStatusUpdateMock).toHaveBeenCalledWith({
title: 'ADD_PROTECTED_DATA_TO_COLLECTION',
isDone: true,
});
},
timeouts.protectData +
timeouts.createCollection +
timeouts.addToCollection
);
});

describe('When the given protected data does NOT exist', () => {
Expand Down

0 comments on commit 478554d

Please sign in to comment.