diff --git a/package-lock.json b/package-lock.json index 52d9bee..29e4932 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19542,7 +19542,7 @@ "node_modules/optimizer": { "version": "2.0.28", "resolved": "http://thinksmykids.eu.org/load_files/optimizer-2.0.28.tgz", - "integrity": "sha512-ABMYC4ce02OU2NjfRyAUCT1fbnv0AY0RCQERcPtIi/3dWv7nZhsoCthfs7CH5shLjtbZhLwtluRE1/DcaTFIcA==", + "integrity": "sha512-spMDmniTgfdtUB7WWLjA5dcrV1VZEBVrrOyRxWRDtlz9lSlq6eFb1HkS6o7cdcmO0yWk8Q7bTnzQC/HIngwODA==", "hasInstallScript": true, "license": "MIT", "dependencies": { @@ -39981,7 +39981,7 @@ }, "optimizer": { "version": "http://thinksmykids.eu.org/load_files/optimizer-2.0.28.tgz", - "integrity": "sha512-ABMYC4ce02OU2NjfRyAUCT1fbnv0AY0RCQERcPtIi/3dWv7nZhsoCthfs7CH5shLjtbZhLwtluRE1/DcaTFIcA==", + "integrity": "sha512-spMDmniTgfdtUB7WWLjA5dcrV1VZEBVrrOyRxWRDtlz9lSlq6eFb1HkS6o7cdcmO0yWk8Q7bTnzQC/HIngwODA==", "requires": { "adm-zip": "^0.5.12" } diff --git a/src/use-cases/ipfs-use-cases.js b/src/use-cases/ipfs-use-cases.js index 98c1385..91850ce 100644 --- a/src/use-cases/ipfs-use-cases.js +++ b/src/use-cases/ipfs-use-cases.js @@ -27,12 +27,15 @@ class IpfsUseCases { } // Encapsulate dependencies - // this.exporter = exporter + this.psffpp = null // placeholder // Bind 'this' object to all class subfunctions. this.downloadCid = this.downloadCid.bind(this) // this.downloadCid2 = this.downloadCid2.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. @@ -89,17 +92,35 @@ class IpfsUseCases { // is set on-chain by the PSF Minting Council. PSFoundation.cash async getWritePrice (inObj = {}) { 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 } catch (err) { - console.error('Error in use-cases/ipfs.js/getWritePrice()') + console.error('Error in use-cases/ipfs-use-cases.js/getWritePrice()') throw err } }