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
14 changes: 11 additions & 3 deletions src/utils/subgraphQuery.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { GraphQLClient, gql } from 'graphql-request';
import { Contact, GraphQLResponse, ProtectedDataQuery } from '../index.js';
import { Contact } from '../index.js';
import { WorkflowError } from './errors.js';
import {
GraphQLResponse,
ProtectedDataQuery,
} from '../web3telegram/internalTypes.js';

const checkProtectedDataQuery = gql`
query GetValidContacts(
Expand All @@ -21,13 +25,14 @@ const checkProtectedDataQuery = gql`
orderDirection: desc
) {
id
name
}
}
`;

export const getValidContact = async (
graphQLClient: GraphQLClient,
contacts: Contact[]
contacts: Omit<Contact, 'name'>[]
): Promise<Contact[]> => {
if (contacts.length === 0) {
return [];
Expand Down Expand Up @@ -66,11 +71,14 @@ export const getValidContact = async (
);

// Convert protectedData[] into Contact[] using the map for constant time lookups
return protectedDataList.map(({ id }) => {
return protectedDataList.map(({ id, name }) => {
const contact = contactsMap.get(id);
if (contact) {
return {
address: id,
name: name,
remainingAccess: contact.remainingAccess,
accessPrice: contact.accessPrice,
owner: contact.owner,
accessGrantTimestamp: contact.accessGrantTimestamp,
isUserStrict: contact.isUserStrict,
Expand Down
5 changes: 2 additions & 3 deletions src/web3telegram/fetchMyContacts.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { booleanSchema, throwIfMissing } from '../utils/validators.js';
import { fetchUserContacts } from './fetchUserContacts.js';
import {
Contact,
DappAddressConsumer,
DappWhitelistAddressConsumer,
FetchMyContactsParams,
IExecConsumer,
SubgraphConsumer,
} from './types.js';
} from './internalTypes.js';
import { Contact, FetchMyContactsParams } from './types.js';

export type FetchMyContacts = typeof fetchMyContacts;

Expand Down
18 changes: 13 additions & 5 deletions src/web3telegram/fetchUserContacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import {
isEnsTest,
throwIfMissing,
} from '../utils/validators.js';
import { Contact, FetchUserContactsParams } from './types.js';
import { IExec } from 'iexec';
import { PublishedDatasetorder } from 'iexec/IExecOrderbookModule';
import {
Contact,
DappAddressConsumer,
DappWhitelistAddressConsumer,
FetchUserContactsParams,
IExecConsumer,
SubgraphConsumer,
} from './types.js';
} from './internalTypes.js';

export const fetchUserContacts = async ({
graphQLClient = throwIfMissing(),
Expand Down Expand Up @@ -63,7 +64,7 @@ export const fetchUserContacts = async ({
}),
]);
const orders = dappOrders.concat(whitelistOrders);
const myContacts: Contact[] = [];
const myContacts: Omit<Contact, 'name'>[] = [];
let web3DappResolvedAddress = vDappAddressOrENS;
if (isEnsTest(vDappAddressOrENS)) {
web3DappResolvedAddress = await iexec.ens.resolveName(vDappAddressOrENS);
Expand All @@ -78,6 +79,8 @@ export const fetchUserContacts = async ({
const contact = {
address: order.order.dataset.toLowerCase(),
owner: order.signer.toLowerCase(),
remainingAccess: order.remaining,
accessPrice: order.order.datasetprice,
accessGrantTimestamp: order.publicationTimestamp,
isUserStrict: order.order.requesterrestrict !== ZeroAddress,
};
Expand All @@ -102,7 +105,12 @@ async function fetchAllOrdersByApp({
userAddress,
appAddress,
isUserStrict,
}) {
}: {
iexec: IExec;
userAddress: string;
appAddress: string;
isUserStrict: boolean;
}): Promise<PublishedDatasetorder[]> {
const ordersFirstPage = iexec.orderbook.fetchDatasetOrderbook(
ANY_DATASET_ADDRESS,
{
Expand Down
36 changes: 36 additions & 0 deletions src/web3telegram/internalTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { IExec } from 'iexec';
import { AddressOrENS } from './types.js';
import { GraphQLClient } from 'graphql-request';

export type ProtectedDataQuery = {
id: string;
name: string;
};

export type GraphQLResponse = {
protectedDatas: ProtectedDataQuery[];
};

export type DappAddressConsumer = {
dappAddressOrENS: AddressOrENS;
};

export type IpfsNodeConfigConsumer = {
ipfsNode: string;
};

export type IpfsGatewayConfigConsumer = {
ipfsGateway: string;
};

export type DappWhitelistAddressConsumer = {
dappWhitelistAddress: string;
};

export type IExecConsumer = {
iexec: IExec;
};

export type SubgraphConsumer = {
graphQLClient: GraphQLClient;
};
13 changes: 6 additions & 7 deletions src/web3telegram/sendTelegram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,19 @@ import {
senderNameSchema,
booleanSchema,
} from '../utils/validators.js';
import { SendTelegramParams, SendTelegramResponse } from './types.js';
import {
checkUserVoucher,
filterWorkerpoolOrders,
} from '../utils/sendTelegram.models.js';
import {
DappAddressConsumer,
DappWhitelistAddressConsumer,
IExecConsumer,
IpfsGatewayConfigConsumer,
IpfsNodeConfigConsumer,
SendTelegramParams,
SendTelegramResponse,
SubgraphConsumer,
} from './types.js';
import {
checkUserVoucher,
filterWorkerpoolOrders,
} from '../utils/sendTelegram.models.js';
} from './internalTypes.js';

export type SendTelegram = typeof sendTelegram;

Expand Down
41 changes: 4 additions & 37 deletions src/web3telegram/types.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import { GraphQLClient } from 'graphql-request';
import { EnhancedWallet, IExec } from 'iexec';
import { EnhancedWallet } from 'iexec';
import { IExecConfigOptions } from 'iexec/IExecConfig';

export type Web3SignerProvider = EnhancedWallet;

export type IExecConsumer = {
iexec: IExec;
};

export type ENS = string;

export type AddressOrENS = Address | ENS;
Expand All @@ -21,6 +16,9 @@ export type Contact = {
owner: Address;
accessGrantTimestamp: TimeStamp;
isUserStrict: boolean;
name: string;
remainingAccess: number;
accessPrice: number;
};

export type SendTelegramParams = {
Expand Down Expand Up @@ -53,21 +51,6 @@ export type SendTelegramResponse = {
taskId: string;
};

/**
* Internal props for querying the subgraph
*/
export type ProtectedDataQuery = {
id: string;
};

export type GraphQLResponse = {
protectedDatas: ProtectedDataQuery[];
};

export type SubgraphConsumer = {
graphQLClient: GraphQLClient;
};

/**
* Configuration options for web3telegram.
*/
Expand Down Expand Up @@ -115,19 +98,3 @@ export type Web3TelegramConfigOptions = {
*/
allowExperimentalNetworks?: boolean;
};

export type DappAddressConsumer = {
dappAddressOrENS: AddressOrENS;
};

export type IpfsNodeConfigConsumer = {
ipfsNode: string;
};

export type IpfsGatewayConfigConsumer = {
ipfsGateway: string;
};

export type DappWhitelistAddressConsumer = {
dappWhitelistAddress: string;
};
28 changes: 27 additions & 1 deletion tests/e2e/fetchUserContacts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ describe('web3telegram.fetchMyContacts()', () => {
userAddress: noAccessUser,
isUserStrict: true,
});
console.log('🚀 ~ contacts:', contacts);

expect(contacts.length).toBe(0);
},
Expand All @@ -61,6 +60,33 @@ describe('web3telegram.fetchMyContacts()', () => {
});

describe('when access is granted', () => {
it('contacts should contain name, accessPrice, remainingAccess, owner, accessGrantTimestamp, and isUserStrict', async () => {
const userWithAccess = Wallet.createRandom().address;

await web3telegram.init();
// eslint-disable-next-line @typescript-eslint/dot-notation
const authorizedApp = web3telegram['dappAddressOrENS'];

await dataProtector.grantAccess({
authorizedApp: authorizedApp,
protectedData: protectedData1.address,
authorizedUser: userWithAccess,
});

const contacts = await web3telegram.fetchUserContacts({
userAddress: userWithAccess,
isUserStrict: true,
});
expect(contacts.length).toBe(1);
expect(contacts[0].address).toBe(protectedData1.address.toLowerCase());
expect(contacts[0].owner).toBeDefined();
expect(contacts[0].accessPrice).toBe(0);
expect(contacts[0].remainingAccess).toBe(1);
expect(contacts[0].accessGrantTimestamp).toBeDefined();
expect(contacts[0].isUserStrict).toBe(true);
expect(contacts[0].name).toBe('test do not use');
});

it(
'should return the user contacts for both app and whitelist',
async () => {
Expand Down
19 changes: 17 additions & 2 deletions tests/unit/utils/subgraphQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { Contact } from '../../../src/web3telegram/types.js';

describe('getValidContact', () => {
// Define the variables in the outermost scope
let contacts: Contact[];
let contacts: Omit<Contact, 'name'>[];
let graphQLClient: GraphQLClient;

beforeAll(() => {
Expand All @@ -19,18 +19,24 @@ describe('getValidContact', () => {
owner: 'owner1',
accessGrantTimestamp: '2023-06-08T09:32:29.761Z',
isUserStrict: false,
remainingAccess: 1,
accessPrice: 0,
},
{
address: 'address2',
owner: 'owner2',
accessGrantTimestamp: '2023-06-09T14:21:17.231Z',
isUserStrict: false,
remainingAccess: 1,
accessPrice: 0,
},
{
address: 'address3',
owner: 'owner3',
accessGrantTimestamp: '2023-06-10T14:21:17.231Z',
isUserStrict: false,
remainingAccess: 1,
accessPrice: 0,
},
];

Expand All @@ -41,7 +47,10 @@ describe('getValidContact', () => {
it('should fetch valid contacts', async () => {
// Mock the request method of the GraphQLClient instance
jest.spyOn(graphQLClient, 'request').mockResolvedValue({
protectedDatas: [{ id: 'address1' }, { id: 'address3' }],
protectedDatas: [
{ id: 'address1', name: 'Contact One' },
{ id: 'address3', name: 'Contact Three' },
],
});

const validContacts = await getValidContact(graphQLClient, contacts);
Expand All @@ -52,12 +61,18 @@ describe('getValidContact', () => {
owner: 'owner1',
accessGrantTimestamp: '2023-06-08T09:32:29.761Z',
isUserStrict: false,
accessPrice: 0,
remainingAccess: 1,
name: 'Contact One',
},
{
address: 'address3',
owner: 'owner3',
accessGrantTimestamp: '2023-06-10T14:21:17.231Z',
isUserStrict: false,
accessPrice: 0,
remainingAccess: 1,
name: 'Contact Three',
},
]);

Expand Down
Loading