Compare commits

..
11 Commits
9 changed files with 237 additions and 34 deletions
+2 -2
View File
@@ -47,8 +47,8 @@ export default {
useFullStackCash: process.env.USE_FULLSTACKCASH ? true : false,
consumerUrl: process.env.CONSUMER_URL
? process.env.CONSUMER_URL
// : 'https://free-bch.fullstack.cash',
: 'https://dev-consumer.psfoundation.info',
: 'https://free-bch.fullstack.cash',
// : 'https://dev-consumer.psfoundation.info',
// : 'https://wa-usa-bch-consumer.fullstackcash.nl',
// P2WDB URL that will accept API calls from the p2wdb npm library.
+3 -1
View File
@@ -46,7 +46,9 @@ const Offer = new mongoose.Schema({
mutableDataCid: { type: String, default: null },
tokenIconUrl: { type: String, default: null },
tokenCategories: { type: Array, default: [] },
tokenTags: { type: Array, default: [] }
tokenTags: { type: Array, default: [] },
userDataStr: { type: String }, // Token user data
lastUpdatedTokenData: { type: String, default: null } // ISO timestamp of the last time token data was updated.
})
export default mongoose.model('offer', Offer)
+20
View File
@@ -56,6 +56,7 @@ class WalletAdapter {
this.completeTx = this.completeTx.bind(this)
this.reclaimTokens = this.reclaimTokens.bind(this)
this.moveTokensFromCustomWallet = this.moveTokensFromCustomWallet.bind(this)
this.cid2json = this.cid2json.bind(this)
}
// Open the wallet file, or create one if the file doesn't exist.
@@ -677,6 +678,25 @@ class WalletAdapter {
throw err
}
}
async cid2json (urlOrCid) {
try {
// Input validation
if (!urlOrCid || typeof urlOrCid !== 'string') {
throw new Error('urlOrCid must be a string!')
}
// Extract the cid from the url or cid.
const cid = urlOrCid.split('/').pop()
console.log('cid to json: ', cid)
const jsonRes = await this.bchWallet.cid2json({ cid })
const json = jsonRes.json
// console.log('json: ', json)
return json
} catch (err) {
console.error('Error in wallet.js/cid2json()', err.message)
throw err
}
}
}
export default WalletAdapter
+87 -14
View File
@@ -129,20 +129,57 @@ class OfferUseCases {
// Get data about the token.
const tokenId = offerEntity.tokenId
// const tokenData = await this.adapters.wallet.bchWallet.getTokenData(tokenId)
const tokenData = await this.retryQueue.addToQueue(this.adapters.wallet.bchWallet.getTokenData, tokenId)
// console.log(`tokenData: ${JSON.stringify(tokenData, null, 2)}`)
let tokenData = null
try {
tokenData = await this.retryQueue.addToQueue(this.adapters.wallet.bchWallet.getTokenData, tokenId)
} catch (err) {
console.error('Error in OfferUseCases/createOffer() getting token data: ', err.message)
}
// Generate a 'display category' for the token. This will allow the
// front end UI to figure out how to display the token.
const displayCategory = this.categorizeToken(offerEntity, tokenData)
console.log('displayCategory: ', displayCategory)
offerEntity.displayCategory = displayCategory
// Do additional data analysis if the token data was successfully retrieved.
if (tokenData) {
// Store the mutable and immutable data cids.
const mutableDataCid = tokenData.mutableData
const immutableDataCid = tokenData.immutableData
offerEntity.mutableDataCid = mutableDataCid
offerEntity.immutableDataCid = immutableDataCid
// Detect if user set the NSFW flag.
// const nsfw = false
// nsfw = await this.retryQueue.addToQueue(this.detectNsfw, tokenData)
// offerEntity.nsfw = nsfw
// Get the mutable data from the cid if it exists.
if (mutableDataCid && typeof mutableDataCid === 'string') {
let mutableData = null
try {
mutableData = await this.retryQueue.addToQueue(this.adapters.wallet.cid2json, mutableDataCid)
} catch (err) {
console.error('Error in OfferUseCases/createOffer() getting mutable data: ', err.message)
}
console.log('mutableData: ', mutableData)
if (mutableData) {
try {
offerEntity.tokenIconUrl = mutableData.tokenIcon
offerEntity.tokenCategories = mutableData.category
offerEntity.tokenTags = mutableData.tags
offerEntity.lastUpdatedTokenData = new Date().getTime()
offerEntity.userDataStr = JSON.stringify(mutableData.userData)
} catch (error) {
// skip error
}
}
}
console.log('offerEntity: ', offerEntity)
// Generate a 'display category' for the token. This will allow the
// front end UI to figure out how to display the token.
const displayCategory = this.categorizeToken(offerEntity, tokenData)
console.log('displayCategory: ', displayCategory)
offerEntity.displayCategory = displayCategory
// Detect if user set the NSFW flag.
// const nsfw = false
// nsfw = await this.retryQueue.addToQueue(this.detectNsfw, tokenData)
// offerEntity.nsfw = nsfw
}
// Add offer to the local database.
const offerModel = new this.OfferModel(offerEntity)
@@ -150,7 +187,7 @@ class OfferUseCases {
return true
} catch (err) {
console.error('Error in createOffer()', err.message)
console.error('\n\nError in createOffer()', err.message, '\n\n', err)
throw err
}
}
@@ -267,7 +304,7 @@ class OfferUseCases {
}
}
async listNftOffers (page = 0, nsfw = false) {
/* async listNftOffers (page = 0, nsfw = false) {
try {
const data = await this.OfferModel.find({
displayCategory: { $ne: 'fungible' },
@@ -287,6 +324,42 @@ class OfferUseCases {
console.error('Error in use-cases/offer/listNftOffers()')
throw error
}
} */
async listNftOffers (page = 0, nsfw = false) {
try {
const query = {
displayCategory: { $ne: 'fungible' },
nsfw
}
// Total query offers
const totalOffers = await this.OfferModel.countDocuments(query)
// Total pages
const totalPages = Math.ceil(totalOffers / NFT_ENTRIES_PER_PAGE)
const data = await this.OfferModel.find(query)
// Sort entries so newest entries show first.
.sort('-timestamp')
// Skip to the start of the selected page.
.skip(page * NFT_ENTRIES_PER_PAGE)
// Only return 20 results.
.limit(NFT_ENTRIES_PER_PAGE)
// console.log('listNftOffers() returning this data: ', data)
return {
data,
pagination: {
currentPage: page,
totalPages,
totalOffers,
pageSize: NFT_ENTRIES_PER_PAGE
}
}
} catch (error) {
console.error('Error in use-cases/offer/listNftOffers()')
throw error
}
}
async listFungibleOffers (page = 0) {
+31
View File
@@ -493,4 +493,35 @@ describe('#wallet', () => {
}
})
})
describe('#cid2json', () => {
it('should throw error if cid is not provided', async () => {
try {
await uut.cid2json()
assert.fail('Unexpected code path')
} catch (error) {
assert.equal(error.message, 'urlOrCid must be a string!')
}
})
it('should convert cid to json', async () => {
try {
sandbox.stub(uut.bchWallet, 'cid2json').resolves({ json: offerMockData.mutableDataMock })
const cid = 'bafkreidr6wfd6mcmwpea7abm5uk5rrprc2wfbcvo5wdcmtlolrpjab5oqm'
const result = await uut.cid2json(cid)
assert.isObject(result)
} catch (error) {
assert.fail('Unexpected code path')
}
})
it('should convert ipfs url to json', async () => {
try {
sandbox.stub(uut.bchWallet, 'cid2json').resolves({ json: offerMockData.mutableDataMock })
const url = 'https://ipfs.io/ipfs/bafkreidr6wfd6mcmwpea7abm5uk5rrprc2wfbcvo5wdcmtlolrpjab5oqm'
const result = await uut.cid2json(url)
assert.isObject(result)
} catch (error) {
assert.fail('Unexpected code path')
}
})
})
})
+1 -1
View File
@@ -149,7 +149,7 @@ const localdb = {
static findById () {}
static find () {}
static findOne () {}
static countDocuments(){}
async save () {
return {}
}
+1
View File
@@ -54,6 +54,7 @@ class MockBchWallet {
}
};
this.optimize = async () => { };
this.cid2json = async () => {};
this.ar = new AdapterRoute()
// Environment variable is used by wallet-balance.unit.js to force an error.
if (process.env.NO_UTXO) {
+16 -1
View File
@@ -126,6 +126,20 @@ const fungibleTokenData01 = {
"immutableData": "troutsblog.com",
"mutableData": ""
}
const mutableDataMock = {
schema: 'ps007-v1.0.3',
schemaDocs: 'https://github.com/Permissionless-Software-Foundation/specifications/blob/master/ps007-token-data-schema.md',
tokenIcon: 'https://files.tokentiger.com/ipfs/view/bafkreidr6wfd6mcmwpea7abm5uk5rrprc2wfbcvo5wdcmtlolrpjab5oqm',
fullSizedUrl: '',
nsfw: false,
userData: { currentUrl: 'https://tokentiger.com' },
jsonLd: {},
about: 'This is AI generated art.',
category: '',
tags: [],
mediaType: 'image',
currentOwner: {},
}
const offerMockData = {
data: {
@@ -214,5 +228,6 @@ export default {
fungibleTokenData01,
offerMockData,
deserealizeTxMockNoOperatorOut,
deserealizeTxMock
deserealizeTxMock,
mutableDataMock
};
+76 -15
View File
@@ -108,6 +108,73 @@ describe('#offer-use-case', () => {
assert.isFalse(result)
})
it('should create offer with mutable data', async () => {
const tokenDataMock = mockData.nftTokenData01
const offerObj = mockData.offerMockData
const mutableDataMock = mockData.mutableDataMock
// Mock dependencies
// sandbox.stub(uut.adapters.wallet.bchWallet, 'utxoIsValid').resolves(false)
sandbox.stub(uut, 'findOfferByTxid').throws(new Error('offer not found'))
sandbox.stub(uut.retryQueue, 'addToQueue')
.onCall(0).resolves({}) // Utxo Status call
.onCall(1).resolves(tokenDataMock) // Token Data call
.onCall(2).resolves(mutableDataMock)
const result = await uut.createOffer(offerObj)
assert.isTrue(result)
})
it('should skip userData stringify error', async () => {
const tokenDataMock = mockData.nftTokenData01
const offerObj = mockData.offerMockData
const mutableDataMock = mockData.mutableDataMock
mutableDataMock.userData = { n: 10n }
// Mock dependencies
// sandbox.stub(uut.adapters.wallet.bchWallet, 'utxoIsValid').resolves(false)
sandbox.stub(uut, 'findOfferByTxid').throws(new Error('offer not found'))
sandbox.stub(uut.retryQueue, 'addToQueue')
.onCall(0).resolves({}) // Utxo Status call
.onCall(1).resolves(tokenDataMock) // Token Data call
.onCall(2).resolves(mutableDataMock)
const result = await uut.createOffer(offerObj)
assert.isTrue(result)
})
it('should handle error getting token data', async () => {
// const tokenDataMock = mockData.nftTokenData01
const offerObj = mockData.offerMockData
const mutableDataMock = mockData.mutableDataMock
mutableDataMock.userData = { n: 10n }
// Mock dependencies
// sandbox.stub(uut.adapters.wallet.bchWallet, 'utxoIsValid').resolves(false)
sandbox.stub(uut, 'findOfferByTxid').throws(new Error('offer not found'))
sandbox.stub(uut.retryQueue, 'addToQueue')
.onCall(0).resolves({}) // Utxo Status call
.onCall(1).throws(new Error('test error'))
const result = await uut.createOffer(offerObj)
assert.isTrue(result)
})
it('should handle error getting mutable data', async () => {
const tokenDataMock = mockData.nftTokenData01
const offerObj = mockData.offerMockData
const mutableDataMock = mockData.mutableDataMock
mutableDataMock.userData = { n: 10n }
// Mock dependencies
// sandbox.stub(uut.adapters.wallet.bchWallet, 'utxoIsValid').resolves(false)
sandbox.stub(uut, 'findOfferByTxid').throws(new Error('offer not found'))
sandbox.stub(uut.retryQueue, 'addToQueue')
.onCall(0).resolves({}) // Utxo Status call
.onCall(1).resolves(tokenDataMock) // Token Data call
.onCall(2).throws(new Error('test error')) // cid2json call
const result = await uut.createOffer(offerObj)
assert.isTrue(result)
})
it('should create offer', async () => {
const tokenDataMock = mockData.simpleNftTokenData01
const offerObj = mockData.offerMockData
@@ -400,7 +467,7 @@ describe('#offer-use-case', () => {
}
})
it('should list orders', async () => {
it('should list offers', async () => {
const queryMock = {
sort () {
return this
@@ -414,7 +481,11 @@ describe('#offer-use-case', () => {
sandbox.stub(uut.OfferModel, 'find').returns(queryMock)
const result = await uut.listNftOffers(1, true)
assert.isArray(result)
assert.isObject(result)
assert.property(result, 'data')
assert.property(result, 'pagination')
assert.isArray(result.data)
assert.isObject(result.pagination)
})
})
@@ -781,27 +852,17 @@ describe('#offer-use-case', () => {
assert.equal(result, 'N/A')
})
it('should return if utxo cant be validated', async () => {
it('should return if utxo can not be validated', async () => {
// Mock dependencies
const mock = Object.assign({}, mockData.offerMockData.data)
sandbox.stub(uut.orderUseCase, 'findOrderByUtxo').resolves(mock)
sandbox.stub(uut.adapters.wallet.bchWallet, 'utxoIsValid').throws(new Error('test error'))
// sandbox.stub(uut.adapters.wallet.bchWallet, 'utxoIsValid').throws(new Error('test error'))
sandbox.stub(uut.retryQueue, 'addToQueue').throws(new Error('test error'))
const result = await uut.acceptCounterOffer({ data: { /** .... */ } })
assert.equal(result, 'N/A')
})
it('should handle axios error', async () => {
// Mock dependencies
const mock = Object.assign({}, mockData.offerMockData.data)
const stubErr = new Error()
stubErr.isAxiosError = true
sandbox.stub(uut.orderUseCase, 'findOrderByUtxo').resolves(mock)
sandbox.stub(uut.adapters.wallet.bchWallet, 'utxoIsValid').throws(stubErr)
const result = await uut.acceptCounterOffer({ data: { /** .... */ } })
assert.equal(result, 'N/A')
})
it('should return if utxo is invalid', async () => {
// Mock dependencies
const mock = Object.assign({}, mockData.offerMockData.data)