Skip to content

Commit

Permalink
Merge pull request #635 from nevermined-io/fix/app_adjustments
Browse files Browse the repository at this point in the history
NvmApp integration adjustments
  • Loading branch information
aaitor authored Mar 5, 2024
2 parents 0adda2a + 88ea6d0 commit 3771022
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 29 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ jobs:
steps:
- name: Reclaim some disk space
run: docker system prune --all --volumes -f
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18
- uses: nevermined-io/[email protected]
Expand Down Expand Up @@ -49,14 +49,14 @@ jobs:
steps:
- name: Reclaim some disk space
run: docker system prune --all --volumes -f
- uses: actions/checkout@v3
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: actions/checkout@v4
with:
repository: nevermined-io/contracts
ref: 'main'
path: contracts
token: ${{ secrets.API_TOKEN_GITHUB }}
- uses: actions/setup-node@v3
- uses: actions/setup-node@v4
with:
node-version: 18

Expand Down
12 changes: 6 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
- feat: add as an option in the config [`6f610b4`](https://github.com/nevermined-io/sdk-js/commit/6f610b4d6372c0d0843b70083b7018708f5a948d)
- chore: adding v2.0.0 Changelog updates [`4ddbf3d`](https://github.com/nevermined-io/sdk-js/commit/4ddbf3de548ed94c0a79341961693510151ce9bd)

### [v2.0.0](https://github.com/nevermined-io/sdk-js/compare/v2.0.0-rc9...v2.0.0)
#### [v2.0.0](https://github.com/nevermined-io/sdk-js/compare/v2.0.0-rc9...v2.0.0)

> 20 October 2023
Expand Down Expand Up @@ -792,7 +792,7 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
- ci: testing with https services [`bb96077`](https://github.com/nevermined-io/sdk-js/commit/bb96077c9403ea7ae26195a51d8a92e144cea384)
- linting [`d2547ed`](https://github.com/nevermined-io/sdk-js/commit/d2547edfbfebb49b488c97c4ce9e6d6f2ee849e4)

### [v1.0.0](https://github.com/nevermined-io/sdk-js/compare/v1.0.0-rc9...v1.0.0)
#### [v1.0.0](https://github.com/nevermined-io/sdk-js/compare/v1.0.0-rc9...v1.0.0)

> 14 February 2023
Expand Down Expand Up @@ -1531,7 +1531,7 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).

> 2 May 2022
- add aave service agreement to ddo when creating nft721 asset [`#248`](https://github.com/nevermined-io/sdk-js/pull/248)
- add aave service agreement to ddo when creating nft721 asset [`#248`](https://github.com/nevermined-io/sdk-js/pull/248)
- Adding v0.19.21 Changelog updates [`c93cdc5`](https://github.com/nevermined-io/sdk-js/commit/c93cdc55f139a43db4130ccb0f80924d2645a931)

#### [v0.19.21](https://github.com/nevermined-io/sdk-js/compare/v0.19.20...v0.19.21)
Expand Down Expand Up @@ -1794,7 +1794,7 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).

> 18 January 2022
- Removing not used parameter [`#186`](https://github.com/nevermined-io/sdk-js/pull/186)
- Removing not used parameter [`#186`](https://github.com/nevermined-io/sdk-js/pull/186)
- [wip] Create agreement+pay in one transaction [`#183`](https://github.com/nevermined-io/sdk-js/pull/183)
- Adapting to contracts `v1.3.3` [`#177`](https://github.com/nevermined-io/sdk-js/pull/177)
- Adding v0.17.2 Changelog updates [`5eddda4`](https://github.com/nevermined-io/sdk-js/commit/5eddda43954e013e6e6f7344c9a877d801aacb5c)
Expand Down Expand Up @@ -2300,7 +2300,7 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
- Feature/sync develop [`#268`](https://github.com/nevermined-io/sdk-js/pull/268)
- Quick fix for non-eip1559 networks [`#266`](https://github.com/nevermined-io/sdk-js/pull/266)
- integrate Permissions and refactor search query [`#264`](https://github.com/nevermined-io/sdk-js/pull/264)
- add aave service agreement to ddo when creating nft721 asset [`#248`](https://github.com/nevermined-io/sdk-js/pull/248)
- add aave service agreement to ddo when creating nft721 asset [`#248`](https://github.com/nevermined-io/sdk-js/pull/248)
- Get the keeper version from the artifacts instead of package version [`#244`](https://github.com/nevermined-io/sdk-js/pull/244)
- replace `metadata-api` url by `marketplace-api` and sort type [`#243`](https://github.com/nevermined-io/sdk-js/pull/243)
- fixing issues with BigNumbers [`#246`](https://github.com/nevermined-io/sdk-js/pull/246)
Expand Down Expand Up @@ -2339,7 +2339,7 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
- Feature/190 add did to sec markets [`#191`](https://github.com/nevermined-io/sdk-js/pull/191)
- Adding utility methods for getting nft token uri [`#189`](https://github.com/nevermined-io/sdk-js/pull/189)
- Lint ... [`#187`](https://github.com/nevermined-io/sdk-js/pull/187)
- Removing not used parameter [`#186`](https://github.com/nevermined-io/sdk-js/pull/186)
- Removing not used parameter [`#186`](https://github.com/nevermined-io/sdk-js/pull/186)
- [wip] Create agreement+pay in one transaction [`#183`](https://github.com/nevermined-io/sdk-js/pull/183)
- Adapting to contracts `v1.3.3` [`#177`](https://github.com/nevermined-io/sdk-js/pull/177)
- Correct typo in route [`#184`](https://github.com/nevermined-io/sdk-js/pull/184)
Expand Down
16 changes: 11 additions & 5 deletions integration/nevermined/NVMAppAPI.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,14 @@ describe('NVM App API', () => {
agreementId = orderResult.agreementId
})

it('I want to check my credits', async () => {
const planBalance = await nvmApp.getBalance(subscriptionDid)

assert.isDefined(planBalance)
assert.isTrue(planBalance.canAccess)
assert.isTrue(planBalance.balance > 0)
})

it('I want to get the token giving access to a remote agent', async () => {
const token = await nvmApp.getServiceAccessToken(agentDid)
assert.isDefined(token)
Expand All @@ -241,14 +249,12 @@ describe('NVM App API', () => {
})

it('I want to download a file asset', async () => {
const results = await nvmApp.downloadFiles(
datasetDid,
agreementId,
`/tmp/.nevermined/downloads/${datasetDid}/`,
)
const destination = `/tmp/.nevermined/downloads/${datasetDid}/`
const results = await nvmApp.downloadFiles(datasetDid, agreementId, destination)

assert.isDefined(results)
assert.isTrue(results.success)
console.log(`Files downloaded: ${destination}`)
})

it('I can disconnect and still search', async () => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nevermined-io/sdk",
"version": "2.2.7",
"version": "2.2.8",
"description": "Javascript SDK for connecting with Nevermined Data Platform ",
"main": "./dist/node/sdk.js",
"typings": "./dist/node/sdk.d.ts",
Expand Down
4 changes: 4 additions & 0 deletions src/models/NeverminedOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export interface NeverminedInitializationOptions {
}

export class NeverminedOptions {
/**
* The Nevermined App URL.
*/
public appUrl?: string
/**
* Ethereum Web3 Provider URL. This Url allows the SDK to connect to a blockchain.
* This provider depends on the network where you want to connect (Ethereum Mainnet, Rinkeby, Polygon Matic, etc).
Expand Down
75 changes: 63 additions & 12 deletions src/nevermined/NvmApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ export interface OperationResult {
success: boolean
}

export interface SubscriptionBalance {
subscriptionType: SubscriptionType
canAccess: boolean
isSubscriptionOwner: boolean
balance: bigint
}

export class NvmApp {
private configNVM: NeverminedAppOptions
private userAccount: Account | undefined
Expand All @@ -62,7 +69,7 @@ export class NvmApp {
private networkFeeReceiver: string | undefined
private networkFee: bigint | undefined

static readonly defaultAppInitializatioinOptions: NeverminedInitializationOptions = {
static readonly defaultAppInitializationOptions: NeverminedInitializationOptions = {
loadCore: true,
loadServiceAgreements: true,
loadNFTs1155: true,
Expand All @@ -76,10 +83,10 @@ export class NvmApp {
loadCompute: false,
}

static readonly publicationOptions = {
metadata: PublishMetadataOptions.OnlyMetadataAPI,
did: PublishOnChainOptions.DIDRegistry,
}
// static readonly publicationOptions = {
// metadata: PublishMetadataOptions.OnlyMetadataAPI,
// did: PublishOnChainOptions.DIDRegistry,
// }

public static async getInstance(
appEnv: NVMAppEnvironments,
Expand All @@ -106,8 +113,8 @@ export class NvmApp {
initOptions?: NeverminedInitializationOptions,
) {
const ops = initOptions
? { ...NvmApp.defaultAppInitializatioinOptions, ...initOptions }
: NvmApp.defaultAppInitializatioinOptions
? { ...NvmApp.defaultAppInitializationOptions, ...initOptions }
: NvmApp.defaultAppInitializationOptions
this.fullSDK = await Nevermined.getInstance(config ? config : this.configNVM, ops)

if (account instanceof ZeroDevAccountSigner) {
Expand Down Expand Up @@ -140,7 +147,7 @@ export class NvmApp {
if (!isAddress(this.subscriptionNFTContractAddress)) {
throw new Web3Error('Invalid Subscription NFT contract address')
}

this.sdk.contracts.loadNft1155(this.subscriptionNFTContractAddress)
this.networkFeeReceiver = await this.fullSDK.keeper.nvmConfig.getFeeReceiver()
this.networkFee = await this.fullSDK.keeper.nvmConfig.getNetworkFee()
}
Expand Down Expand Up @@ -220,7 +227,10 @@ export class NvmApp {
return await this.fullSDK.nfts1155.create(
nftAttributes,
this.userAccount,
NvmApp.publicationOptions,
{
metadata: PublishMetadataOptions.OnlyMetadataAPI,
did: PublishOnChainOptions.DIDRegistry,
},
{ ...(this.useZeroDevSigner && { zeroDevSigner: this.zeroDevSignerAccount }) },
)
}
Expand Down Expand Up @@ -262,7 +272,10 @@ export class NvmApp {
return await this.fullSDK.nfts1155.create(
nftAttributes,
this.userAccount,
NvmApp.publicationOptions,
{
metadata: PublishMetadataOptions.OnlyMetadataAPI,
did: PublishOnChainOptions.DIDRegistry,
},
{ ...(this.useZeroDevSigner && { zeroDevSigner: this.zeroDevSignerAccount }) },
)
}
Expand Down Expand Up @@ -307,6 +320,38 @@ export class NvmApp {
return { agreementId, success: transferResult }
}

public async getBalance(
subscriptionDid: string,
accountAddress?: string,
): Promise<SubscriptionBalance> {
if (!this.isWeb3Connected())
throw new Web3Error('Web3 not connected, try calling the connect method first')

const address = accountAddress ? accountAddress : this.userAccount.getId()

try {
const ddo = await this.fullSDK.assets.resolve(subscriptionDid)
const salesService = ddo.findServiceByReference('nft-sales')
const subscriptionType = salesService.attributes.main.nftAttributes.subscriptionType
const numberCredits = salesService.attributes.main.nftAttributes.amount

const subscriptionOwner = await this.fullSDK.assets.owner(subscriptionDid)
console.log(`Subscription Owner: ${subscriptionOwner}`)
console.log(`User Address: ${address}`)
const balance = await this.fullSDK.nfts1155.balance(subscriptionDid, address)
const isOwner = address.toLowerCase() === subscriptionOwner.toLowerCase()
const canAccess = isOwner || balance >= numberCredits
return {
subscriptionType,
canAccess,
isSubscriptionOwner: address.toLowerCase() === subscriptionOwner.toLowerCase(),
balance,
}
} catch (error) {
throw new Web3Error(`Error ordering subscription: ${error.message}`)
}
}

public async getServiceAccessToken(serviceDid: string): Promise<SubscriptionToken> {
if (!this.isWeb3Connected())
throw new Web3Error('Web3 not connected, try calling the connect method first')
Expand Down Expand Up @@ -370,7 +415,10 @@ export class NvmApp {
return await this.fullSDK.nfts1155.create(
nftAttributes,
this.userAccount,
NvmApp.publicationOptions,
{
metadata: PublishMetadataOptions.OnlyMetadataAPI,
did: PublishOnChainOptions.DIDRegistry,
},
{ ...(this.useZeroDevSigner && { zeroDevSigner: this.zeroDevSignerAccount }) },
)
}
Expand Down Expand Up @@ -408,7 +456,10 @@ export class NvmApp {
return await this.fullSDK.nfts1155.create(
nftAttributes,
this.userAccount,
NvmApp.publicationOptions,
{
metadata: PublishMetadataOptions.OnlyMetadataAPI,
did: PublishOnChainOptions.DIDRegistry,
},
{ ...(this.useZeroDevSigner && { zeroDevSigner: this.zeroDevSignerAccount }) },
)
}
Expand Down
1 change: 1 addition & 0 deletions src/nevermined/api/nfts/NFTsBaseApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ export abstract class NFTsBaseApi extends RegistryBaseApi {

// Download the files
this.logger.log('Downloading the files')

const result = await this.nevermined.services.node.downloadService(
files,
destination,
Expand Down
1 change: 1 addition & 0 deletions src/nevermined/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from './Account'
export * from './DID'
export * from './Files'
export * from './Nevermined'
export * from './NvmApp'
export * from './ProgressSteps'
export * from './Provider'
export * from './Token'
Expand Down
8 changes: 8 additions & 0 deletions src/nevermined/resources/AppNetworks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import { NeverminedOptions } from '../../models'

export class NeverminedAppOptions extends NeverminedOptions {
instanceName: string
appUrl?: string
nftContractAddress?: string
tokenAddress?: string // ERC-20 token address or zero address (0x000..) for native token
}

export class AppDeploymentLocal extends NeverminedAppOptions {
instanceName = 'localnet'
appUrl = 'http://localhost:3000'
web3ProviderUri = 'http://contracts.nevermined.localnet'
marketplaceUri = 'http://marketplace.nevermined.localnet'
graphHttpUri = undefined
Expand All @@ -21,6 +23,7 @@ export class AppDeploymentLocal extends NeverminedAppOptions {

export class AppDeploymentStaging extends NeverminedAppOptions {
instanceName = 'appStaging'
appUrl = 'https://staging.nevermined.app'
web3ProviderUri = 'https://sepolia-rollup.arbitrum.io/rpc'
marketplaceUri = 'https://marketplace-api.staging.nevermined.app'
graphHttpUri = 'https://api.thegraph.com/subgraphs/name/nevermined-io/public'
Expand All @@ -35,6 +38,7 @@ export class AppDeploymentStaging extends NeverminedAppOptions {

export class AppDeploymentTesting extends NeverminedAppOptions {
instanceName = 'appTesting'
appUrl = 'https://testing.nevermined.app'
web3ProviderUri = 'https://sepolia-rollup.arbitrum.io/rpc'
marketplaceUri = 'https://marketplace-api.testing.nevermined.app'
graphHttpUri = 'https://api.thegraph.com/subgraphs/name/nevermined-io/public'
Expand All @@ -49,6 +53,7 @@ export class AppDeploymentTesting extends NeverminedAppOptions {

export class AppDeploymentArbitrum extends NeverminedAppOptions {
instanceName = 'appArbitrum'
appUrl = 'https://nevermined.app'
web3ProviderUri = 'https://arb1.arbitrum.io/rpc'
marketplaceUri = 'https://marketplace-api.arbitrum.nevermined.app'
graphHttpUri = 'https://api.thegraph.com/subgraphs/name/nevermined-io/public'
Expand All @@ -63,6 +68,7 @@ export class AppDeploymentArbitrum extends NeverminedAppOptions {

export class AppDeploymentGnosis extends NeverminedAppOptions {
instanceName = 'appGnosis'
appUrl = 'https://gnosis.nevermined.app'
web3ProviderUri = 'https://rpc.gnosischain.com/'
marketplaceUri = 'https://marketplace-api.gnosis.nevermined.app'
graphHttpUri = 'https://api.thegraph.com/subgraphs/name/nevermined-io/public'
Expand All @@ -77,6 +83,7 @@ export class AppDeploymentGnosis extends NeverminedAppOptions {

export class AppDeploymentMumbai extends NeverminedAppOptions {
instanceName = 'appMumbai'
appUrl = 'https://mumbai.nevermined.app'
web3ProviderUri = 'https://matic-mumbai.chainstacklabs.com'
marketplaceUri = 'https://marketplace-api.mumbai.nevermined.app'
graphHttpUri = 'https://api.thegraph.com/subgraphs/name/nevermined-io/public'
Expand All @@ -91,6 +98,7 @@ export class AppDeploymentMumbai extends NeverminedAppOptions {

export class AppDeploymentMatic extends NeverminedAppOptions {
instanceName = 'appMatic'
appUrl = 'https://matic.nevermined.app'
web3ProviderUri = 'https://polygon-rpc.com'
marketplaceUri = 'https://marketplace-api.matic.nevermined.app'
graphHttpUri = 'https://api.thegraph.com/subgraphs/name/nevermined-io/public'
Expand Down

0 comments on commit 3771022

Please sign in to comment.