Skip to content

Commit

Permalink
feat: additional datahub functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tomicvladan committed Jan 18, 2024
1 parent 615aeff commit c85255c
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,24 @@ const fdpCache = new FdpStorage('https://localhost:1633', batchId, {
fdpCache.cache.object = JSON.parse(cache)
```

There are available function for interacting with DataHub contract. For example to list all available subscriptions:

```js
const subs = await fdp.personalStorage.getAllSubscriptions()
```

To get user's subscriptions:

```js
const subItems = await fdp.personalStorage.getAllSubItems()
```

And to get pod information of a subItem:

```js
const podShareInfo = await fdp.personalStorage.openSubscribedPod(subItems[0].subHash, subItems[0].unlockKeyLocation)
```

## Documentation

You can generate API docs locally with:
Expand Down
59 changes: 58 additions & 1 deletion src/pod/personal-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,18 @@ import { prepareEthAddress, preparePrivateKey } from '../utils/wallet'
import { getCacheKey, setEpochCache } from '../cache/utils'
import { getPodsList } from './cache/api'
import { getNextEpoch } from '../feed/lookup/utils'
import { DataHub, ENS, ENS_DOMAIN, SubItem, Subscription } from '@fairdatasociety/fdp-contracts-js'
import {
ActiveBid,
DataHub,
ENS,
ENS_DOMAIN,
SubItem,
Subscription,
SubscriptionRequest,
} from '@fairdatasociety/fdp-contracts-js'
import { decryptWithBytes, deriveSecretFromKeys } from '../utils/encryption'
import { namehash } from 'ethers/lib/utils'
import { BigNumber } from 'ethers'

export const POD_TOPIC = 'Pods'

Expand Down Expand Up @@ -234,4 +243,52 @@ export class PersonalStorage {

return data
}

async getActiveBids(): Promise<ActiveBid[]> {
return this.dataHub.getActiveBids(this.accountData.wallet!.address)
}

async getListedSubs(address: string): Promise<string[]> {
return this.dataHub.getListedSubs(address)
}

async getSubRequests(address: string): Promise<SubscriptionRequest[]> {
return this.dataHub.getSubRequests(address)
}

async bidSub(subHash: string, buyerUsername: string, value: BigNumber): Promise<void> {
return this.dataHub.requestSubscription(subHash, buyerUsername, value)
}

async createSubscription(
sellerUsername: string,
swarmLocation: string,
price: BigNumber,
categoryHash: string,
podAddress: string,
daysValid: number,
value?: BigNumber,
): Promise<void> {
return this.dataHub.createSubscription(
sellerUsername,
swarmLocation,
price,
categoryHash,
podAddress,
daysValid,
value,
)
}

async getAllSubscriptions(): Promise<Subscription[]> {
return this.dataHub.getSubs()
}

async getSubscriptionsByCategory(categoryHash: string) {
const subs = await this.getAllSubscriptions()

// TODO temporary until the category gets added to fdp-contracts
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return subs.filter(sub => (sub as any).category === categoryHash)
}
}

0 comments on commit c85255c

Please sign in to comment.