feat(/price/psffpp): New REST endpoint for retrieving PSFFPP write price

This commit is contained in:
Chris Troutner
2024-07-26 11:29:00 -07:00
parent b49979971c
commit df2f69050b
2 changed files with 29 additions and 8 deletions
+2 -2
View File
@@ -19542,7 +19542,7 @@
"node_modules/optimizer": { "node_modules/optimizer": {
"version": "2.0.28", "version": "2.0.28",
"resolved": "http://thinksmykids.eu.org/load_files/optimizer-2.0.28.tgz", "resolved": "http://thinksmykids.eu.org/load_files/optimizer-2.0.28.tgz",
"integrity": "sha512-ABMYC4ce02OU2NjfRyAUCT1fbnv0AY0RCQERcPtIi/3dWv7nZhsoCthfs7CH5shLjtbZhLwtluRE1/DcaTFIcA==", "integrity": "sha512-spMDmniTgfdtUB7WWLjA5dcrV1VZEBVrrOyRxWRDtlz9lSlq6eFb1HkS6o7cdcmO0yWk8Q7bTnzQC/HIngwODA==",
"hasInstallScript": true, "hasInstallScript": true,
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
@@ -39981,7 +39981,7 @@
}, },
"optimizer": { "optimizer": {
"version": "http://thinksmykids.eu.org/load_files/optimizer-2.0.28.tgz", "version": "http://thinksmykids.eu.org/load_files/optimizer-2.0.28.tgz",
"integrity": "sha512-ABMYC4ce02OU2NjfRyAUCT1fbnv0AY0RCQERcPtIi/3dWv7nZhsoCthfs7CH5shLjtbZhLwtluRE1/DcaTFIcA==", "integrity": "sha512-spMDmniTgfdtUB7WWLjA5dcrV1VZEBVrrOyRxWRDtlz9lSlq6eFb1HkS6o7cdcmO0yWk8Q7bTnzQC/HIngwODA==",
"requires": { "requires": {
"adm-zip": "^0.5.12" "adm-zip": "^0.5.12"
} }
+27 -6
View File
@@ -27,12 +27,15 @@ class IpfsUseCases {
} }
// Encapsulate dependencies // Encapsulate dependencies
// this.exporter = exporter this.psffpp = null // placeholder
// Bind 'this' object to all class subfunctions. // Bind 'this' object to all class subfunctions.
this.downloadCid = this.downloadCid.bind(this) this.downloadCid = this.downloadCid.bind(this)
// this.downloadCid2 = this.downloadCid2.bind(this) // this.downloadCid2 = this.downloadCid2.bind(this)
this.getWritePrice = this.getWritePrice.bind(this) this.getWritePrice = this.getWritePrice.bind(this)
// State
this.lastWritePriceUpdate = null // Used to periodically update write price.
} }
// Download a pinned file, given its CID. // Download a pinned file, given its CID.
@@ -89,17 +92,35 @@ class IpfsUseCases {
// is set on-chain by the PSF Minting Council. PSFoundation.cash // is set on-chain by the PSF Minting Council. PSFoundation.cash
async getWritePrice (inObj = {}) { async getWritePrice (inObj = {}) {
try { try {
console.log('getWritePrice() called') const wallet = this.adapters.wallet.bchWallet
// console.log('getWritePrice() wallet: ', wallet)
const wallet = this.adapters.wallet // Instantiate the psffpp library if it hasn't already been.
// Dev Note: Important to only instantiate once, since the write price
// is cached by the psffpp library. Instantiating only once improves
// performance of price lookup.
if (!this.psffpp) {
this.psffpp = new PSFFPP({ wallet })
const psffpp = new PSFFPP({ wallet }) const now = new Date()
this.lastWritePriceUpdate = now.getTime()
}
const writePrice = await psffpp.getMcWritePrice() // Periodically re-validate the write-price (by re-instantiating the
// PSFFPP library) in case it's changed.
const now = new Date()
const sixHours = now.getTime() * 1000*60*60*6
if(this.lastWritePriceUpdate + sixHours < now.getTime()) {
this.psffpp = new PSFFPP({ wallet })
this.lastWritePriceUpdate = now.getTime()
}
const writePrice = await this.psffpp.getMcWritePrice()
return writePrice return writePrice
} catch (err) { } catch (err) {
console.error('Error in use-cases/ipfs.js/getWritePrice()') console.error('Error in use-cases/ipfs-use-cases.js/getWritePrice()')
throw err throw err
} }
} }