Merge pull request #35 from Permissionless-Software-Foundation/ct-unstable

fix(webhook): Ensuring startup waits for webhook to be established
This commit is contained in:
Chris Troutner
2022-07-07 13:46:12 -07:00
committed by GitHub
6 changed files with 77 additions and 22 deletions
+20 -18
View File
@@ -24,8 +24,8 @@ const config = require('../config') // this first.
const AdminLib = require('../src/adapters/admin')
// const adminLib = new AdminLib()
const WebHookLib = require('../src/adapters/webhook')
const webhookLib = new WebHookLib()
// const WebHookLib = require('../src/adapters/webhook')
// const webhookLib = new WebHookLib()
// const JSONRPC = require('../src/rpc')
// const rpc = new JSONRPC()
@@ -103,24 +103,26 @@ class Server {
// MIDDLEWARE END
// 7/7/22 CT: This code paragraph can be deleted
// Delay startup to give the P2WDB time to start first, so that it accepts the webook call
await this.sleep(20000)
// if (this.config.env !== 'test') {
// await this.sleep(20000)
// }
// Create webhook
try {
try {
// Delete an old webhook if it exists.
await webhookLib.deleteWebhook(config.webhookTarget)
} catch (err) {
/* exit quietly */
// console.log('err deleting webhook: ', err)
}
await webhookLib.createWebhook(config.webhookTarget)
console.log('Webhook created')
} catch (error) {
console.log('Webhook cant be created')
}
// try {
// try {
// // Delete an old webhook if it exists.
// await webhookLib.deleteWebhook(config.webhookTarget)
// } catch (err) {
// /* exit quietly */
// // console.log('err deleting webhook: ', err)
// }
//
// await webhookLib.createWebhook(config.webhookTarget)
// console.log('Webhook created')
// } catch (error) {
// console.log('Webhook cant be created')
// }
// startServer()
this.server = await app.listen(config.port)
+1 -1
View File
@@ -73,7 +73,7 @@ services:
#build:
# context: ./bch-dex/
# dockerfile: Dockerfile
image: christroutner/bch-dex:v1.8.0
image: christroutner/bch-dex:v1.9.0
container_name: bch-dex
environment:
CONSUMER_URL: 'https://free-bch.fullstack.cash'
+5
View File
@@ -19,6 +19,7 @@ const FullStackJWT = require('./fullstack-jwt')
const BCHAdapter = require('./bch')
const WalletAdapter = require('./wallet')
const P2wdbAdapter = require('./p2wdb-adapter')
const Webhook = require('./webhook')
const config = require('../../config')
@@ -37,6 +38,7 @@ class Adapters {
this.config = config
this.wallet = new WalletAdapter()
this.p2wdb = new P2wdbAdapter(localConfig)
this.webhook = new Webhook()
// Get a valid JWT API key and instance bch-js.
this.fullStackJwt = new FullStackJWT(config)
@@ -65,6 +67,9 @@ class Adapters {
if (this.config.env !== 'test') {
// Instance the wallet.
await this.wallet.instanceWallet(walletData, this.bchjs)
// Wait until a webhook is established with the P2WDB
await this.webhook.waitUntilSuccess(this.config.webhookTarget)
}
console.log('Async Adapters have been started.')
+40
View File
@@ -59,6 +59,46 @@ class WebHook {
throw err
}
}
// Returns a promise that will not resolve until a webhook has been successfully
// created with the P2WDB.
// Dev Note: This was created because establishing a webhook between bch-dex
// and the P2WDB at startup is critical. If it's not established, then bch-dex
// can not 'see' new Offers and Counter Offers.
async waitUntilSuccess (url) {
try {
let success = false
do {
try {
// Delete an old webhook if it exists.
await this.deleteWebhook(this.config.webhookTarget)
} catch (err) {
/* exit quietly */
// console.log('err deleting webhook: ', err)
}
try {
await this.createWebhook(this.config.webhookTarget)
console.log('Webhook created')
success = true
} catch (err) {
const now = new Date()
console.log(`${now.toLocaleString()}: Error trying to create webhook with P2WDB. Trying again...`)
await sleep(2000)
}
} while (!success)
return true
} catch (err) {
console.error('Error in webhook.js/waitUntilSuccess()')
throw err
}
}
}
function sleep (ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
module.exports = WebHook
+3 -1
View File
@@ -28,7 +28,9 @@ class OrderLib {
async createOrder (entryObj) {
try {
console.log('createOrder(entryObj): ', entryObj)
// if (!entryObj) return
if (!entryObj) return false
if (!entryObj.tokenId) throw new Error('entry does not contain required properties')
// Specify the address to send payment.
entryObj.makerAddr = this.adapters.wallet.bchWallet.walletInfo.cashAddress
+8 -2
View File
@@ -125,12 +125,18 @@ describe('#order-use-case', () => {
it('should catch and throw an error', async () => {
try {
await uut.createOrder()
await uut.createOrder({ a: 'b' })
assert.fail('Unexpected code path')
} catch (err) {
assert.include(err.message, 'Cannot set')
assert.include(err.message, 'entry does not contain required properties')
}
})
it('should exit quietly if the entry is empty', async () => {
const result = await uut.createOrder()
assert.equal(result, false)
})
})
})