Skip to content

Commit a89bc58

Browse files
feat: add name, accessPrice and remainingAccess properties to Contact (#66)
* feat: add name, accessPrice and remainingAccess properties to Contact fix: stop exporting internal types
1 parent 27fd440 commit a89bc58

File tree

8 files changed

+116
-58
lines changed

8 files changed

+116
-58
lines changed

src/utils/subgraphQuery.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { GraphQLClient, gql } from 'graphql-request';
2-
import { Contact, GraphQLResponse, ProtectedDataQuery } from '../index.js';
2+
import { Contact } from '../index.js';
33
import { WorkflowError } from './errors.js';
4+
import {
5+
GraphQLResponse,
6+
ProtectedDataQuery,
7+
} from '../web3telegram/internalTypes.js';
48

59
const checkProtectedDataQuery = gql`
610
query GetValidContacts(
@@ -21,13 +25,14 @@ const checkProtectedDataQuery = gql`
2125
orderDirection: desc
2226
) {
2327
id
28+
name
2429
}
2530
}
2631
`;
2732

2833
export const getValidContact = async (
2934
graphQLClient: GraphQLClient,
30-
contacts: Contact[]
35+
contacts: Omit<Contact, 'name'>[]
3136
): Promise<Contact[]> => {
3237
if (contacts.length === 0) {
3338
return [];
@@ -66,11 +71,14 @@ export const getValidContact = async (
6671
);
6772

6873
// Convert protectedData[] into Contact[] using the map for constant time lookups
69-
return protectedDataList.map(({ id }) => {
74+
return protectedDataList.map(({ id, name }) => {
7075
const contact = contactsMap.get(id);
7176
if (contact) {
7277
return {
7378
address: id,
79+
name: name,
80+
remainingAccess: contact.remainingAccess,
81+
accessPrice: contact.accessPrice,
7482
owner: contact.owner,
7583
accessGrantTimestamp: contact.accessGrantTimestamp,
7684
isUserStrict: contact.isUserStrict,

src/web3telegram/fetchMyContacts.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { booleanSchema, throwIfMissing } from '../utils/validators.js';
22
import { fetchUserContacts } from './fetchUserContacts.js';
33
import {
4-
Contact,
54
DappAddressConsumer,
65
DappWhitelistAddressConsumer,
7-
FetchMyContactsParams,
86
IExecConsumer,
97
SubgraphConsumer,
10-
} from './types.js';
8+
} from './internalTypes.js';
9+
import { Contact, FetchMyContactsParams } from './types.js';
1110

1211
export type FetchMyContacts = typeof fetchMyContacts;
1312

src/web3telegram/fetchUserContacts.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ import {
1010
isEnsTest,
1111
throwIfMissing,
1212
} from '../utils/validators.js';
13+
import { Contact, FetchUserContactsParams } from './types.js';
14+
import { IExec } from 'iexec';
15+
import { PublishedDatasetorder } from 'iexec/IExecOrderbookModule';
1316
import {
14-
Contact,
1517
DappAddressConsumer,
1618
DappWhitelistAddressConsumer,
17-
FetchUserContactsParams,
1819
IExecConsumer,
1920
SubgraphConsumer,
20-
} from './types.js';
21+
} from './internalTypes.js';
2122

2223
export const fetchUserContacts = async ({
2324
graphQLClient = throwIfMissing(),
@@ -63,7 +64,7 @@ export const fetchUserContacts = async ({
6364
}),
6465
]);
6566
const orders = dappOrders.concat(whitelistOrders);
66-
const myContacts: Contact[] = [];
67+
const myContacts: Omit<Contact, 'name'>[] = [];
6768
let web3DappResolvedAddress = vDappAddressOrENS;
6869
if (isEnsTest(vDappAddressOrENS)) {
6970
web3DappResolvedAddress = await iexec.ens.resolveName(vDappAddressOrENS);
@@ -78,6 +79,8 @@ export const fetchUserContacts = async ({
7879
const contact = {
7980
address: order.order.dataset.toLowerCase(),
8081
owner: order.signer.toLowerCase(),
82+
remainingAccess: order.remaining,
83+
accessPrice: order.order.datasetprice,
8184
accessGrantTimestamp: order.publicationTimestamp,
8285
isUserStrict: order.order.requesterrestrict !== ZeroAddress,
8386
};
@@ -102,7 +105,12 @@ async function fetchAllOrdersByApp({
102105
userAddress,
103106
appAddress,
104107
isUserStrict,
105-
}) {
108+
}: {
109+
iexec: IExec;
110+
userAddress: string;
111+
appAddress: string;
112+
isUserStrict: boolean;
113+
}): Promise<PublishedDatasetorder[]> {
106114
const ordersFirstPage = iexec.orderbook.fetchDatasetOrderbook(
107115
ANY_DATASET_ADDRESS,
108116
{

src/web3telegram/internalTypes.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { IExec } from 'iexec';
2+
import { AddressOrENS } from './types.js';
3+
import { GraphQLClient } from 'graphql-request';
4+
5+
export type ProtectedDataQuery = {
6+
id: string;
7+
name: string;
8+
};
9+
10+
export type GraphQLResponse = {
11+
protectedDatas: ProtectedDataQuery[];
12+
};
13+
14+
export type DappAddressConsumer = {
15+
dappAddressOrENS: AddressOrENS;
16+
};
17+
18+
export type IpfsNodeConfigConsumer = {
19+
ipfsNode: string;
20+
};
21+
22+
export type IpfsGatewayConfigConsumer = {
23+
ipfsGateway: string;
24+
};
25+
26+
export type DappWhitelistAddressConsumer = {
27+
dappWhitelistAddress: string;
28+
};
29+
30+
export type IExecConsumer = {
31+
iexec: IExec;
32+
};
33+
34+
export type SubgraphConsumer = {
35+
graphQLClient: GraphQLClient;
36+
};

src/web3telegram/sendTelegram.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,19 @@ import {
1818
senderNameSchema,
1919
booleanSchema,
2020
} from '../utils/validators.js';
21+
import { SendTelegramParams, SendTelegramResponse } from './types.js';
22+
import {
23+
checkUserVoucher,
24+
filterWorkerpoolOrders,
25+
} from '../utils/sendTelegram.models.js';
2126
import {
2227
DappAddressConsumer,
2328
DappWhitelistAddressConsumer,
2429
IExecConsumer,
2530
IpfsGatewayConfigConsumer,
2631
IpfsNodeConfigConsumer,
27-
SendTelegramParams,
28-
SendTelegramResponse,
2932
SubgraphConsumer,
30-
} from './types.js';
31-
import {
32-
checkUserVoucher,
33-
filterWorkerpoolOrders,
34-
} from '../utils/sendTelegram.models.js';
33+
} from './internalTypes.js';
3534

3635
export type SendTelegram = typeof sendTelegram;
3736

src/web3telegram/types.ts

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
import { GraphQLClient } from 'graphql-request';
2-
import { EnhancedWallet, IExec } from 'iexec';
1+
import { EnhancedWallet } from 'iexec';
32
import { IExecConfigOptions } from 'iexec/IExecConfig';
43

54
export type Web3SignerProvider = EnhancedWallet;
65

7-
export type IExecConsumer = {
8-
iexec: IExec;
9-
};
10-
116
export type ENS = string;
127

138
export type AddressOrENS = Address | ENS;
@@ -21,6 +16,9 @@ export type Contact = {
2116
owner: Address;
2217
accessGrantTimestamp: TimeStamp;
2318
isUserStrict: boolean;
19+
name: string;
20+
remainingAccess: number;
21+
accessPrice: number;
2422
};
2523

2624
export type SendTelegramParams = {
@@ -53,21 +51,6 @@ export type SendTelegramResponse = {
5351
taskId: string;
5452
};
5553

56-
/**
57-
* Internal props for querying the subgraph
58-
*/
59-
export type ProtectedDataQuery = {
60-
id: string;
61-
};
62-
63-
export type GraphQLResponse = {
64-
protectedDatas: ProtectedDataQuery[];
65-
};
66-
67-
export type SubgraphConsumer = {
68-
graphQLClient: GraphQLClient;
69-
};
70-
7154
/**
7255
* Configuration options for web3telegram.
7356
*/
@@ -115,19 +98,3 @@ export type Web3TelegramConfigOptions = {
11598
*/
11699
allowExperimentalNetworks?: boolean;
117100
};
118-
119-
export type DappAddressConsumer = {
120-
dappAddressOrENS: AddressOrENS;
121-
};
122-
123-
export type IpfsNodeConfigConsumer = {
124-
ipfsNode: string;
125-
};
126-
127-
export type IpfsGatewayConfigConsumer = {
128-
ipfsGateway: string;
129-
};
130-
131-
export type DappWhitelistAddressConsumer = {
132-
dappWhitelistAddress: string;
133-
};

tests/e2e/fetchUserContacts.test.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ describe('web3telegram.fetchMyContacts()', () => {
5252
userAddress: noAccessUser,
5353
isUserStrict: true,
5454
});
55-
console.log('🚀 ~ contacts:', contacts);
5655

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

6362
describe('when access is granted', () => {
63+
it('contacts should contain name, accessPrice, remainingAccess, owner, accessGrantTimestamp, and isUserStrict', async () => {
64+
const userWithAccess = Wallet.createRandom().address;
65+
66+
await web3telegram.init();
67+
// eslint-disable-next-line @typescript-eslint/dot-notation
68+
const authorizedApp = web3telegram['dappAddressOrENS'];
69+
70+
await dataProtector.grantAccess({
71+
authorizedApp: authorizedApp,
72+
protectedData: protectedData1.address,
73+
authorizedUser: userWithAccess,
74+
});
75+
76+
const contacts = await web3telegram.fetchUserContacts({
77+
userAddress: userWithAccess,
78+
isUserStrict: true,
79+
});
80+
expect(contacts.length).toBe(1);
81+
expect(contacts[0].address).toBe(protectedData1.address.toLowerCase());
82+
expect(contacts[0].owner).toBeDefined();
83+
expect(contacts[0].accessPrice).toBe(0);
84+
expect(contacts[0].remainingAccess).toBe(1);
85+
expect(contacts[0].accessGrantTimestamp).toBeDefined();
86+
expect(contacts[0].isUserStrict).toBe(true);
87+
expect(contacts[0].name).toBe('test do not use');
88+
});
89+
6490
it(
6591
'should return the user contacts for both app and whitelist',
6692
async () => {

tests/unit/utils/subgraphQuery.test.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { Contact } from '../../../src/web3telegram/types.js';
88

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

1414
beforeAll(() => {
@@ -19,18 +19,24 @@ describe('getValidContact', () => {
1919
owner: 'owner1',
2020
accessGrantTimestamp: '2023-06-08T09:32:29.761Z',
2121
isUserStrict: false,
22+
remainingAccess: 1,
23+
accessPrice: 0,
2224
},
2325
{
2426
address: 'address2',
2527
owner: 'owner2',
2628
accessGrantTimestamp: '2023-06-09T14:21:17.231Z',
2729
isUserStrict: false,
30+
remainingAccess: 1,
31+
accessPrice: 0,
2832
},
2933
{
3034
address: 'address3',
3135
owner: 'owner3',
3236
accessGrantTimestamp: '2023-06-10T14:21:17.231Z',
3337
isUserStrict: false,
38+
remainingAccess: 1,
39+
accessPrice: 0,
3440
},
3541
];
3642

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

4756
const validContacts = await getValidContact(graphQLClient, contacts);
@@ -52,12 +61,18 @@ describe('getValidContact', () => {
5261
owner: 'owner1',
5362
accessGrantTimestamp: '2023-06-08T09:32:29.761Z',
5463
isUserStrict: false,
64+
accessPrice: 0,
65+
remainingAccess: 1,
66+
name: 'Contact One',
5567
},
5668
{
5769
address: 'address3',
5870
owner: 'owner3',
5971
accessGrantTimestamp: '2023-06-10T14:21:17.231Z',
6072
isUserStrict: false,
73+
accessPrice: 0,
74+
remainingAccess: 1,
75+
name: 'Contact Three',
6176
},
6277
]);
6378

0 commit comments

Comments
 (0)