mirror of
https://github.com/Permissionless-Software-Foundation/bch-dex.git
synced 2026-09-21 16:52:00 -07:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
611c2d1631 | ||
|
|
cb9f5af62e | ||
|
|
2fca908c97 | ||
|
|
3aba1e061b | ||
|
|
c398942c95 |
@@ -70,10 +70,10 @@ services:
|
||||
restart: always
|
||||
|
||||
bch-dex:
|
||||
#build:
|
||||
# context: ./bch-dex/
|
||||
# dockerfile: Dockerfile
|
||||
image: christroutner/bch-dex:v1.9.6
|
||||
build:
|
||||
context: ./bch-dex/
|
||||
dockerfile: Dockerfile
|
||||
#image: christroutner/bch-dex:v1.9.6
|
||||
container_name: bch-dex
|
||||
environment:
|
||||
CONSUMER_URL: 'https://free-bch.fullstack.cash'
|
||||
@@ -94,10 +94,10 @@ services:
|
||||
restart: always
|
||||
|
||||
dex-ui:
|
||||
#build:
|
||||
# context: ./bch-dex-ui/
|
||||
# dockerfile: Dockerfile
|
||||
image: christroutner/bch-dex-ui:v1.6.6
|
||||
build:
|
||||
context: ./bch-dex-ui/
|
||||
dockerfile: Dockerfile
|
||||
#image: christroutner/bch-dex-ui:v1.6.6
|
||||
container_name: dex-ui
|
||||
environment:
|
||||
SERVER: 'http://192.168.2.3'
|
||||
|
||||
@@ -28,6 +28,8 @@ const Offer = new mongoose.Schema({
|
||||
globalTimestamp: { type: String },
|
||||
localTimestamp: { type: String },
|
||||
displayCategory: { type: String },
|
||||
nsfw: { type: Boolean, default: false },
|
||||
flags: { type: Array },
|
||||
|
||||
// SWaP Protocol Properties
|
||||
lokadId: { type: String },
|
||||
|
||||
@@ -45,12 +45,23 @@ class P2WDBRESTControllerLib {
|
||||
const counterOffer = ctx.request.body
|
||||
|
||||
await _this.useCases.offer.acceptCounterOffer(counterOffer)
|
||||
|
||||
//
|
||||
} else if (dataType.includes('offer')) {
|
||||
// Detect and handle new Offers
|
||||
console.log('offer data detected')
|
||||
|
||||
const offerObj = ctx.request.body
|
||||
await _this.useCases.offer.createOffer(offerObj)
|
||||
|
||||
//
|
||||
} else if (dataType.includes('flag')) {
|
||||
// Detect and handle data generated by users flagging NSFW Offers.
|
||||
const flagData = ctx.request.body
|
||||
console.log('Flag type data received: ', flagData)
|
||||
|
||||
// const p2wdbHash = flagData.p2wdbHash
|
||||
await _this.useCases.offer.flagOffer(flagData)
|
||||
} else {
|
||||
console.log('Could not route P2WDB webhook data.')
|
||||
}
|
||||
|
||||
@@ -13,6 +13,9 @@
|
||||
a partially signed transaction.
|
||||
*/
|
||||
|
||||
// Global npm libraries
|
||||
const axios = require('axios')
|
||||
|
||||
// Local libraries
|
||||
const OfferEntity = require('../../entities/offer')
|
||||
const config = require('../../../config')
|
||||
@@ -39,6 +42,7 @@ class OfferUseCases {
|
||||
|
||||
// Encapsulate dependencies
|
||||
this.config = config
|
||||
this.axios = axios
|
||||
|
||||
this.offerEntity = new OfferEntity()
|
||||
this.OfferModel = this.adapters.localdb.Offer
|
||||
@@ -84,12 +88,22 @@ class OfferUseCases {
|
||||
const offerEntity = this.offerEntity.validate(offerObj)
|
||||
console.log('offerEntity: ', offerEntity)
|
||||
|
||||
// Get data about the token.
|
||||
const tokenId = offerEntity.tokenId
|
||||
const tokenData = await this.adapters.wallet.bchWallet.getTokenData(tokenId)
|
||||
console.log(`tokenData: ${JSON.stringify(tokenData, null, 2)}`)
|
||||
|
||||
// Generate a 'display category' for the token. This will allow the
|
||||
// front end UI to figure out how to display the token.
|
||||
const displayCategory = await this.categorizeToken(offerEntity)
|
||||
const displayCategory = await this.categorizeToken(offerEntity, tokenData)
|
||||
console.log('displayCategory: ', displayCategory)
|
||||
offerEntity.displayCategory = displayCategory
|
||||
|
||||
// Detect if user set the NSFW flag.
|
||||
let nsfw = false
|
||||
nsfw = await this.detectNsfw(tokenData)
|
||||
offerEntity.nsfw = nsfw
|
||||
|
||||
// Add offer to the local database.
|
||||
const offerModel = new this.OfferModel(offerEntity)
|
||||
await offerModel.save()
|
||||
@@ -101,6 +115,39 @@ class OfferUseCases {
|
||||
}
|
||||
}
|
||||
|
||||
// By default this function returns false, to indicate the NFT is safe for work.
|
||||
// If the user who created the token sets the nsfw property in the mutable data,
|
||||
// this function will return true.
|
||||
async detectNsfw (tokenData) {
|
||||
try {
|
||||
let nsfw = false
|
||||
|
||||
const cid = tokenData.mutableData.substring(7)
|
||||
|
||||
// Retrieve the mutable data from Filecoin/IPFS.
|
||||
const url = `https://${cid}.ipfs.w3s.link/data.json`
|
||||
const result = await axios.get(url)
|
||||
const mutableData = result.data
|
||||
console.log(`mutableData: ${JSON.stringify(mutableData, null, 2)}`)
|
||||
|
||||
// Logical tests
|
||||
const hasNsfw = !!mutableData.nsfw
|
||||
const nsfwSetTrue = mutableData.nsfw === true
|
||||
const nsfwStringTrue = mutableData.nsfw === 'true'
|
||||
const nsfwDetected = hasNsfw && (nsfwSetTrue || nsfwStringTrue)
|
||||
|
||||
if (nsfwDetected) {
|
||||
console.log('NSFW flag set as true')
|
||||
nsfw = true
|
||||
}
|
||||
|
||||
return nsfw
|
||||
} catch (err) {
|
||||
console.error('Error in detectNsfw(): ', err)
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
// Categorize the token for display purposes. This will categorize a token
|
||||
// into one of these categories:
|
||||
// - nft
|
||||
@@ -111,14 +158,14 @@ class OfferUseCases {
|
||||
// The first three are easy to categorize. The simple-nft is a fungible token
|
||||
// with a quantity of 1, decimals of 0, and no minting baton. Categorizing this
|
||||
// type of token is the main reason why this function exists.
|
||||
async categorizeToken (offerData) {
|
||||
async categorizeToken (offerData, tokenData) {
|
||||
try {
|
||||
console.log(`categorizeToken(): ${JSON.stringify(offerData, null, 2)}`)
|
||||
// console.log(`categorizeToken(): ${JSON.stringify(offerData, null, 2)}`)
|
||||
|
||||
const tokenId = offerData.tokenId
|
||||
|
||||
const tokenData = await this.adapters.wallet.bchWallet.getTokenData(tokenId)
|
||||
console.log(`tokenData: ${JSON.stringify(tokenData, null, 2)}`)
|
||||
// const tokenId = offerData.tokenId
|
||||
//
|
||||
// const tokenData = await this.adapters.wallet.bchWallet.getTokenData(tokenId)
|
||||
// console.log(`tokenData: ${JSON.stringify(tokenData, null, 2)}`)
|
||||
|
||||
if (tokenData.genesisData.type === 65) {
|
||||
return 'nft'
|
||||
@@ -160,9 +207,12 @@ class OfferUseCases {
|
||||
}
|
||||
}
|
||||
|
||||
async listNftOffers (page = 0) {
|
||||
async listNftOffers (page = 0, nsfw = false) {
|
||||
try {
|
||||
const data = await this.OfferModel.find({ displayCategory: { $ne: 'fungible' } })
|
||||
const data = await this.OfferModel.find({
|
||||
displayCategory: { $ne: 'fungible' },
|
||||
nsfw
|
||||
})
|
||||
// Sort entries so newest entries show first.
|
||||
.sort('-timestamp')
|
||||
// Skip to the start of the selected page.
|
||||
@@ -507,6 +557,34 @@ class OfferUseCases {
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
async flagOffer (flagData) {
|
||||
console.log(`flagData: ${JSON.stringify(flagData, null, 2)}`)
|
||||
|
||||
const p2wdbHash = flagData.data.p2wdbHash
|
||||
|
||||
// Get the offer from the database.
|
||||
const offer = await this.findOfferByHash(p2wdbHash)
|
||||
console.log(`Flagging this offer: ${JSON.stringify(offer, null, 2)}`)
|
||||
|
||||
if (!offer) {
|
||||
throw new Error(`Offer ${p2wdbHash} not found in the database.`)
|
||||
}
|
||||
|
||||
// Add the raw flag data to the database model.
|
||||
offer.flags.push(flagData)
|
||||
|
||||
// If flag count is 3 or more, mark the Offer as NSFW
|
||||
const flagCnt = offer.flags.length
|
||||
if (flagCnt >= 3) {
|
||||
offer.nsfw = true
|
||||
}
|
||||
|
||||
// Save the updated offer data to the database.
|
||||
await offer.save()
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = OfferUseCases
|
||||
|
||||
@@ -74,6 +74,9 @@ describe('#offer-use-case', () => {
|
||||
|
||||
// Mock dependencies
|
||||
sandbox.stub(uut.adapters.bchjs.Blockchain, 'getTxOut').resolves(null)
|
||||
sandbox.stub(uut.adapters.wallet.bchWallet, 'getTokenData').resolves({})
|
||||
sandbox.stub(uut, 'categorizeToken').resolves('nft')
|
||||
sandbox.stub(uut, 'detectNsfw').resolves(false)
|
||||
|
||||
const result = await uut.createOffer(offerObj)
|
||||
// console.log('result: ', result)
|
||||
@@ -120,6 +123,8 @@ describe('#offer-use-case', () => {
|
||||
coinbase: false
|
||||
})
|
||||
sandbox.stub(uut, 'categorizeToken').resolves('fungible')
|
||||
sandbox.stub(uut.adapters.wallet.bchWallet, 'getTokenData').resolves({})
|
||||
sandbox.stub(uut, 'detectNsfw').resolves(false)
|
||||
|
||||
const result = await uut.createOffer(offerObj)
|
||||
// console.log('result: ', result)
|
||||
@@ -131,33 +136,36 @@ describe('#offer-use-case', () => {
|
||||
describe('#categorizeToken', () => {
|
||||
it('should categorize an NFT', async () => {
|
||||
// Mock dependencies
|
||||
sandbox.stub(uut.adapters.wallet.bchWallet, 'getTokenData').resolves(mockData.nftTokenData01)
|
||||
// sandbox.stub(uut.adapters.wallet.bchWallet, 'getTokenData').resolves(mockData.nftTokenData01)
|
||||
|
||||
const offerData = mockData.nftOffer01
|
||||
const tokenData = mockData.nftTokenData01
|
||||
|
||||
const result = await uut.categorizeToken(offerData)
|
||||
const result = await uut.categorizeToken(offerData, tokenData)
|
||||
|
||||
assert.equal(result, 'nft')
|
||||
})
|
||||
|
||||
it('should categorize a simple NFT', async () => {
|
||||
// Mock dependencies
|
||||
sandbox.stub(uut.adapters.wallet.bchWallet, 'getTokenData').resolves(mockData.simpleNftTokenData01)
|
||||
// sandbox.stub(uut.adapters.wallet.bchWallet, 'getTokenData').resolves(mockData.simpleNftTokenData01)
|
||||
|
||||
const offerData = mockData.simpleNftOffer01
|
||||
const tokenData = mockData.simpleNftTokenData01
|
||||
|
||||
const result = await uut.categorizeToken(offerData)
|
||||
const result = await uut.categorizeToken(offerData, tokenData)
|
||||
|
||||
assert.equal(result, 'simple-nft')
|
||||
})
|
||||
|
||||
it('should categorize a fungible token', async () => {
|
||||
// Mock dependencies
|
||||
sandbox.stub(uut.adapters.wallet.bchWallet, 'getTokenData').resolves(mockData.fungibleTokenData01)
|
||||
// sandbox.stub(uut.adapters.wallet.bchWallet, 'getTokenData').resolves(mockData.fungibleTokenData01)
|
||||
|
||||
const offerData = mockData.fungibleOffer01
|
||||
const tokenData = mockData.fungibleTokenData01
|
||||
|
||||
const result = await uut.categorizeToken(offerData)
|
||||
const result = await uut.categorizeToken(offerData, tokenData)
|
||||
|
||||
assert.equal(result, 'fungible')
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user