Skip to content

Commit

Permalink
added cache for oracle contract state
Browse files Browse the repository at this point in the history
  • Loading branch information
thepiwo authored and kenodressel committed Mar 25, 2020
1 parent 9956ad0 commit ca9bed4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
7 changes: 7 additions & 0 deletions logic/cacheLogic.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.exports = class CacheLogic {
const keepHotFunction = async () => {
await CacheLogic.getTipsCheckPreviews();
await CacheLogic.fetchChainNames();
await aeternity.getOracleState();
};

await cache.init(aeternity, keepHotFunction);
Expand Down Expand Up @@ -134,7 +135,13 @@ module.exports = class CacheLogic {
await cache.del(["getTips"]);
res.send({status: "OK"});
}

static async deliverAllItems(req, res) {
res.send(await CacheLogic.getAllTips());
}

static async deliverOracleState(req, res) {
res.send(await aeternity.getOracleState());
}

};
2 changes: 2 additions & 0 deletions routes/cacheRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const cache = new CacheLogic();
// Open api routes
router.get('/', CacheLogic.deliverAllItems);

router.get('/oracle', CacheLogic.deliverOracleState);

router.get('/invalidate/tips', CacheLogic.invalidateTips);

// View routes
Expand Down
14 changes: 13 additions & 1 deletion utils/aeternity.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,26 @@ class Aeternity {
return (await this.client.getNodeInfo()).nodeNetworkId
};

getOracleState = async () => {
if (!this.client) throw new Error('Init sdk first');

const fetchOracleState = () => this.oracleContract.methods.get_state().then(res => res.decodedResult);

return this.cache
? this.cache.getOrSet(["oracleState"], () => fetchOracleState(), this.cache.shortCacheTime)
: fetchOracleState();
};

getTips = async () => {
if (!this.client) throw new Error('Init sdk first');
const fetchTips = async () => {
const state = await this.contract.methods.get_state();
return this.getTipsRetips(state.decodedResult);
};

return this.cache ? this.cache.getOrSet(["getTips"], () => fetchTips(), this.cache.shortCacheTime) : fetchTips();
return this.cache
? this.cache.getOrSet(["getTips"], () => fetchTips(), this.cache.shortCacheTime)
: fetchTips();
};

getContractSource = () => {
Expand Down

0 comments on commit ca9bed4

Please sign in to comment.