diff --git a/src/utils/subgraphQuery.ts b/src/utils/subgraphQuery.ts index f3d02b9..933e74d 100644 --- a/src/utils/subgraphQuery.ts +++ b/src/utils/subgraphQuery.ts @@ -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( @@ -21,13 +25,14 @@ const checkProtectedDataQuery = gql` orderDirection: desc ) { id + name } } `; export const getValidContact = async ( graphQLClient: GraphQLClient, - contacts: Contact[] + contacts: Omit[] ): Promise => { if (contacts.length === 0) { return []; @@ -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, diff --git a/src/web3telegram/fetchMyContacts.ts b/src/web3telegram/fetchMyContacts.ts index 6978ece..f7c6e7a 100644 --- a/src/web3telegram/fetchMyContacts.ts +++ b/src/web3telegram/fetchMyContacts.ts @@ -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; diff --git a/src/web3telegram/fetchUserContacts.ts b/src/web3telegram/fetchUserContacts.ts index 558b631..ed5f8b5 100644 --- a/src/web3telegram/fetchUserContacts.ts +++ b/src/web3telegram/fetchUserContacts.ts @@ -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(), @@ -63,7 +64,7 @@ export const fetchUserContacts = async ({ }), ]); const orders = dappOrders.concat(whitelistOrders); - const myContacts: Contact[] = []; + const myContacts: Omit[] = []; let web3DappResolvedAddress = vDappAddressOrENS; if (isEnsTest(vDappAddressOrENS)) { web3DappResolvedAddress = await iexec.ens.resolveName(vDappAddressOrENS); @@ -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, }; @@ -102,7 +105,12 @@ async function fetchAllOrdersByApp({ userAddress, appAddress, isUserStrict, -}) { +}: { + iexec: IExec; + userAddress: string; + appAddress: string; + isUserStrict: boolean; +}): Promise { const ordersFirstPage = iexec.orderbook.fetchDatasetOrderbook( ANY_DATASET_ADDRESS, { diff --git a/src/web3telegram/internalTypes.ts b/src/web3telegram/internalTypes.ts new file mode 100644 index 0000000..d6ea054 --- /dev/null +++ b/src/web3telegram/internalTypes.ts @@ -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; +}; diff --git a/src/web3telegram/sendTelegram.ts b/src/web3telegram/sendTelegram.ts index aca0b45..0d7a3fb 100644 --- a/src/web3telegram/sendTelegram.ts +++ b/src/web3telegram/sendTelegram.ts @@ -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; diff --git a/src/web3telegram/types.ts b/src/web3telegram/types.ts index ee81fe4..0c6bfff 100644 --- a/src/web3telegram/types.ts +++ b/src/web3telegram/types.ts @@ -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; @@ -21,6 +16,9 @@ export type Contact = { owner: Address; accessGrantTimestamp: TimeStamp; isUserStrict: boolean; + name: string; + remainingAccess: number; + accessPrice: number; }; export type SendTelegramParams = { @@ -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. */ @@ -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; -}; diff --git a/tests/e2e/fetchUserContacts.test.ts b/tests/e2e/fetchUserContacts.test.ts index 2ef8b51..1e8760d 100644 --- a/tests/e2e/fetchUserContacts.test.ts +++ b/tests/e2e/fetchUserContacts.test.ts @@ -52,7 +52,6 @@ describe('web3telegram.fetchMyContacts()', () => { userAddress: noAccessUser, isUserStrict: true, }); - console.log('🚀 ~ contacts:', contacts); expect(contacts.length).toBe(0); }, @@ -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 () => { diff --git a/tests/unit/utils/subgraphQuery.test.ts b/tests/unit/utils/subgraphQuery.test.ts index aa6c8ef..de0d559 100644 --- a/tests/unit/utils/subgraphQuery.test.ts +++ b/tests/unit/utils/subgraphQuery.test.ts @@ -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[]; let graphQLClient: GraphQLClient; beforeAll(() => { @@ -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, }, ]; @@ -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); @@ -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', }, ]);