Skip to content

Commit

Permalink
Merge branch 'fix/load_artifacts' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
aaitor committed Aug 31, 2023
2 parents 4541e6e + 53a2815 commit 29924a8
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 42 deletions.
1 change: 1 addition & 0 deletions aave_integration/nevermined/AaveCredit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ describe('AaveCredit', () => {

nevermined = await Nevermined.getInstance(config)
;({ agreementFee } = config.aaveConfig as AaveConfig)
await nevermined.keeper.loadAaveInstances()
;({ aaveCreditTemplate } = nevermined.keeper.templates)
;({ conditionStoreManager, didRegistry, agreementStoreManager } = nevermined.keeper)
;({ nft721LockCondition, aaveRepayCondition } = nevermined.keeper.conditions)
Expand Down
9 changes: 4 additions & 5 deletions integration/nevermined/Artifacts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ import { mkdtempSync, writeFileSync } from 'fs'
import { NeverminedOptions, Nevermined } from '../../src'
import fetch from 'node-fetch'
import { x } from 'tar'
import { infuraToken } from '../config'

describe('Artifacts', () => {
const artifactsRepo = 'https://artifacts.nevermined.network/'
const tests = [
{
web3ProviderUri: `https://polygon-mumbai.infura.io/v3/${infuraToken}`,
networkName: ['mumbai'],
networkId: [80001],
versions: ['v3.2.1'],
web3ProviderUri: 'https://goerli-rollup.arbitrum.io/rpc',
networkName: ['arbitrum-goerli'],
networkId: [421613],
versions: ['v3.2.2'],
tag: 'public',
},
]
Expand Down
51 changes: 51 additions & 0 deletions integration/nevermined/Assets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,57 @@ describe('Assets', () => {
assert.deepEqual(files, ['LICENSE'], 'Stored files are not correct.')
})

it('update the asset files', async () => {
const nonce = Math.random()
const name = `Updated Files Test ${nonce}`
const updatedMetadata = {
...createdMetadata,
name,
}
updatedMetadata.main.files = [
{
index: 0,
contentType: 'text/plain',
url: 'https://raw.githubusercontent.com/nevermined-io/sdk-js/main/LICENSE',
},
]

await nevermined.assets.update(ddo.shortId(), updatedMetadata, publisher)

const resolvedDDO = await nevermined.assets.resolve(ddo.id)
assert.isDefined(resolvedDDO)

const previousMetadata = ddo.findServiceByType('metadata')
const metadata = resolvedDDO.findServiceByType('metadata')

assert.equal(updatedMetadata.main.name, metadata.attributes.main.name)

assert.equal(metadata.attributes.main.files.length, 1)
assert.notEqual(
previousMetadata.attributes.main.files.length,
metadata.attributes.main.files.length,
)
assert.notEqual(
previousMetadata.attributes.encryptedFiles,
metadata.attributes.encryptedFiles,
)
})

it('should be able to download the updated files', async () => {
const folder = '/tmp/sdk-js/updated-files'
if (!fs.existsSync(folder)) fs.mkdirSync(folder, { recursive: true })

const path = (await nevermined.assets.download(ddo.id, publisher, folder, -1)) as string
assert.include(path, folder, 'The storage path is not correct.')
const files = await new Promise<string[]>((resolve) => {
fs.readdir(path, (e, fileList) => {
resolve(fileList)
})
})

assert.deepEqual(files, ['LICENSE'], 'Stored files are not correct.')
})

it('unlist and list an asset', async () => {
// Unlisting Asset
await nevermined.assets.list(ddo.shortId(), false, publisher)
Expand Down
70 changes: 48 additions & 22 deletions src/keeper/Keeper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,14 @@ export class Keeper extends Instantiable {
transferDidOwnershipCondition: TransferDIDOwnershipCondition.getInstance(
this.instanceConfig,
),
aaveBorrowCondition: AaveBorrowCondition.getInstance(this.instanceConfig),
aaveCollateralDepositCondition: AaveCollateralDepositCondition.getInstance(
this.instanceConfig,
),
aaveCollateralWithdrawCondition: AaveCollateralWithdrawCondition.getInstance(
this.instanceConfig,
),
aaveRepayCondition: AaveRepayCondition.getInstance(this.instanceConfig),

// Aave instances are optional
aaveBorrowCondition: undefined,
aaveCollateralDepositCondition: undefined,
aaveCollateralWithdrawCondition: undefined,
aaveRepayCondition: undefined,
aaveCreditTemplate: undefined,

nft721LockCondition: NFT721LockCondition.getInstance(this.instanceConfig),
distributeNftCollateralCondition: DistributeNFTCollateralCondition.getInstance(
this.instanceConfig,
Expand All @@ -115,15 +115,14 @@ export class Keeper extends Instantiable {
didSalesTemplate: DIDSalesTemplate.getInstance(this.instanceConfig),
nftSalesTemplate: NFTSalesTemplate.getInstance(this.instanceConfig),
nft721SalesTemplate: NFT721SalesTemplate.getInstance(this.instanceConfig),
aaveCreditTemplate: AaveCreditTemplate.getInstance(this.instanceConfig), // optional
standardRoyalties: StandardRoyalties.getInstance(this.instanceConfig), // optional
rewardsDistributor: RewardsDistributor.getInstance(this.instanceConfig),
nftUpgradeable: Nft1155Contract.getInstance(this.instanceConfig),
})

this.royalties = {
standard: this.instances.standardRoyalties,
curve: this.instances.curveRoyalties,
curve: undefined,
}

this.rewardsDistributor = this.instances.rewardsDistributor
Expand All @@ -136,7 +135,6 @@ export class Keeper extends Instantiable {
this.instances.didSalesTemplate,
this.instances.nftSalesTemplate,
this.instances.nft721SalesTemplate,
this.instances.aaveCreditTemplate,
]

const templateObj: any = {}
Expand Down Expand Up @@ -167,12 +165,6 @@ export class Keeper extends Instantiable {
this.logger.debug('Token not available on this network.')
}

try {
this.instances.curveRoyalties = await CurveRoyalties.getInstance(this.instanceConfig)
} catch {
this.logger.debug('CurveRoyalties not available on this network.')
}

// Main contracts
this.dispenser = this.instances.dispenser
this.token = this.instances.token
Expand All @@ -196,10 +188,10 @@ export class Keeper extends Instantiable {
transferNftCondition: this.instances.transferNftCondition,
transferNft721Condition: this.instances.transferNft721Condition,
transferDidOwnershipCondition: this.instances.transferDidOwnershipCondition,
aaveBorrowCondition: this.instances.aaveBorrowCondition,
aaveCollateralDepositCondition: this.instances.aaveCollateralDepositCondition,
aaveCollateralWithdrawCondition: this.instances.aaveCollateralWithdrawCondition,
aaveRepayCondition: this.instances.aaveRepayCondition,
aaveBorrowCondition: undefined,
aaveCollateralDepositCondition: undefined,
aaveCollateralWithdrawCondition: undefined,
aaveRepayCondition: undefined,
nft721LockCondition: this.instances.nft721LockCondition,
distributeNftCollateralCondition: this.instances.distributeNftCollateralCondition,
}
Expand All @@ -213,7 +205,7 @@ export class Keeper extends Instantiable {
nft721AccessTemplate: this.instances.nft721AccessTemplate,
nftSalesTemplate: this.instances.nftSalesTemplate,
nft721SalesTemplate: this.instances.nft721SalesTemplate,
aaveCreditTemplate: this.instances.aaveCreditTemplate,
aaveCreditTemplate: undefined,
}
this.templateList = [
this.instances.accessTemplate,
Expand Down Expand Up @@ -438,6 +430,40 @@ export class Keeper extends Instantiable {
public getAllInstances() {
return this.instances
}

public async loadAaveInstances() {
if (this.instances.aaveCreditTemplate) return this

this.logger.debug('Loading Aave contracts')
this.instances.aaveBorrowCondition = await AaveBorrowCondition.getInstance(this.instanceConfig)
this.instances.aaveCollateralDepositCondition =
await AaveCollateralDepositCondition.getInstance(this.instanceConfig)
this.instances.aaveCollateralWithdrawCondition =
await AaveCollateralWithdrawCondition.getInstance(this.instanceConfig)
this.instances.aaveRepayCondition = await AaveRepayCondition.getInstance(this.instanceConfig)

this.conditions.aaveBorrowCondition = this.instances.aaveBorrowCondition
this.conditions.aaveCollateralDepositCondition = this.instances.aaveCollateralDepositCondition
this.conditions.aaveCollateralWithdrawCondition = this.instances.aaveCollateralWithdrawCondition
this.conditions.aaveRepayCondition = this.instances.aaveRepayCondition
this.conditionsList = Object.values(this.conditions)

this.instances.aaveCreditTemplate = await AaveCreditTemplate.getInstance(this.instanceConfig)
this.templates.aaveCreditTemplate = this.instances.aaveCreditTemplate
this.agreementStoreManager.addTemplate('AaveCreditTemplate', this.instances.aaveCreditTemplate)
return this
}

public async loadCurveRoyaltiesInstance() {
if (this.royalties.curve) return this.royalties.curve

try {
this.royalties.curve = await CurveRoyalties.getInstance(this.instanceConfig)
} catch {
this.logger.debug('CurveRoyalties not available on this network.')
}
return this.royalties.curve
}
}

export default Keeper
7 changes: 6 additions & 1 deletion test/bookmarks/Bookmarks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Nevermined } from '../../src'
import { MarketplaceResults } from '../../src/common/interfaces'
import config from '../config'
import { Bookmark, Bookmarks, NewBookmark } from '../../src/services'
import TestContractHandler from '../keeper/TestContractHandler'

use(spies)

Expand All @@ -20,8 +21,12 @@ describe('Bookmarks', () => {
let bookmark: Bookmark
let bookmarksResults: MarketplaceResults<Bookmark>

beforeEach(async () => {
before(async () => {
await TestContractHandler.prepareContracts()
nevermined = await Nevermined.getInstance(config)
})

beforeEach(async () => {
bookmarks = nevermined.services.bookmarks // eslint-disable-line prefer-destructuring

newBookmark = {
Expand Down
8 changes: 8 additions & 0 deletions test/keeper/Keeper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ describe('Keeper', () => {
it('should have token', () => {
assert(keeper.token !== null)
})

it('should not have curve royalties', () => {
assert(keeper.royalties.curve === undefined, 'should not have curve royalties')
})

it('should not have Aave contracts', () => {
assert(keeper.templates!.aaveCreditTemplate! === undefined, 'should not have aave templates')
})
})

describe('#getNetworkName()', () => {
Expand Down
13 changes: 2 additions & 11 deletions test/keeper/conditions/AaveBorrowCondition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ describe('AaveBorrowCondition', () => {
nevermined = await Nevermined.getInstance(config)
;[user] = await nevermined.accounts.list()
;({ didRegistry } = nevermined.keeper)
condition = (await Nevermined.getInstance(config)).keeper.conditions.aaveBorrowCondition
condition = (await (await Nevermined.getInstance(config)).keeper.loadAaveInstances()).conditions
.aaveBorrowCondition
})

beforeEach(async () => {
Expand All @@ -48,16 +49,6 @@ describe('AaveBorrowCondition', () => {

const id = await condition.generateId(agreementId, hash)
assert.match(id, /^0x[a-f0-9]{64}$/i)

// const txReceipt: TransactionReceipt = await condition.fulfill(
// agreementId,
// did,
// vaultAddress,
// assetToBorrow,
// amount,
// interestRateMode,
// user
// )
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('AaveCollateralDepositCondition', () => {
nevermined = await Nevermined.getInstance(config)
;[user] = await nevermined.accounts.list()
;({ didRegistry } = nevermined.keeper)
condition = (await Nevermined.getInstance(config)).keeper.conditions
condition = (await (await Nevermined.getInstance(config)).keeper.loadAaveInstances()).conditions
.aaveCollateralDepositCondition
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('AaveCollateralWithdrawCondition', () => {
nevermined = await Nevermined.getInstance(config)
;[user] = await nevermined.accounts.list()
;({ didRegistry } = nevermined.keeper)
condition = (await Nevermined.getInstance(config)).keeper.conditions
condition = (await (await Nevermined.getInstance(config)).keeper.loadAaveInstances()).conditions
.aaveCollateralWithdrawCondition
})

Expand Down
3 changes: 2 additions & 1 deletion test/keeper/conditions/AaveRepayCondition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ describe('AaveRepayCondition', () => {
nevermined = await Nevermined.getInstance(config)
;[user] = await nevermined.accounts.list()
;({ didRegistry } = nevermined.keeper)
condition = (await Nevermined.getInstance(config)).keeper.conditions.aaveRepayCondition
condition = (await (await Nevermined.getInstance(config)).keeper.loadAaveInstances()).conditions
.aaveRepayCondition
})

beforeEach(async () => {
Expand Down

0 comments on commit 29924a8

Please sign in to comment.