forked from UMAprotocol/protocol
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cloud-functions): initial simple cloud functions for calculating…
… UMA TVL and fetching KPI options recipients (UMAprotocol#2697) * Added cloud functions package Signed-off-by: Christopher Maree <[email protected]> * Added serverless functions Signed-off-by: Christopher Maree <[email protected]> * Added yarn lock Signed-off-by: Christopher Maree <[email protected]> * removed lock files Signed-off-by: Christopher Maree <[email protected]> * Added readme file Signed-off-by: Christopher Maree <[email protected]> * Added cashing solution to tvl Signed-off-by: Christopher Maree <[email protected]> * Updated README.md Signed-off-by: Christopher Maree <[email protected]> * Updated Signed-off-by: Christopher Maree <[email protected]> * Updated for cors Signed-off-by: Christopher Maree <[email protected]> * Updated cors logig Signed-off-by: Christopher Maree <[email protected]> * Added private Signed-off-by: Christopher Maree <[email protected]>
- Loading branch information
1 parent
52fba46
commit fa39f30
Showing
9 changed files
with
112 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,6 +67,6 @@ | |
"*.{js,ts,tsx}": "eslint --cache --fix" | ||
}, | ||
"workspaces": [ | ||
"packages/*" | ||
"packages/**" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# @uma/merkle-distributor | ||
|
||
This package contains a number of serverless functions used to wrap core UMA logic in a serverless consumable format. | ||
|
||
## Key packages included: | ||
|
||
1. `uma-tvl-calculator` wraps the `merkle-distributor-helper` package's `calculateCurrentTvl` method to create a simple interface for computing the current UMA TVL. Once the TVL is calculated, this method will store it within GCP data store with the Key being the computation timestamp. This should be run by a cron job to ensure the data store always has the most up to data information. | ||
2. `uma-tvl-fetcher` pulls data from GCP data store to quickly return the most recent TVL. | ||
3. `Merkle-distributor-helper` wraps the the `merkle-distributor-helper` package's `getClaimsForAddress` method to create a simple interface to fetch claims for a particular address. | ||
|
||
## Using these functions | ||
|
||
These serverless functions are designed to work in any serverless framework. They are used by UMA within GCP cloud functions but they should work equally well within Vercel's serverless, AWS Lambda or any other serverless framework of your choosing. |
25 changes: 25 additions & 0 deletions
25
packages/cloud-functions/merkle-distributor-helper/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const { getClaimsForAddress } = require("@uma/merkle-distributor"); | ||
|
||
exports.getClaimsForAddress = async (req, res) => { | ||
try { | ||
res.set("Access-Control-Allow-Origin", "*"); | ||
if (req.method === "OPTIONS") { | ||
res.set("Access-Control-Allow-Methods", "GET, PUT, POST, OPTIONS"); | ||
res.set("Access-Control-Allow-Headers", "Content-Type"); | ||
res.status(204).send(""); | ||
} else { | ||
const body = req.body; | ||
["merkleDistributorAddress", "claimerAddress", "chainId"].forEach(requiredKey => { | ||
if (!Object.keys(body).includes(requiredKey)) | ||
throw "Missing key in req body! required: merkleDistributorAddress, claimerAddress, chainId"; | ||
}); | ||
|
||
const claims = await getClaimsForAddress(body.merkleDistributorAddress, body.claimerAddress, body.chainId); | ||
console.log(`Fetched claims for ${JSON.stringify(body)}. Claims: ${JSON.stringify(claims)}`); | ||
res.status(200).send(claims); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
res.status(500).send({ message: "Error in fetching claims", error: e instanceof Error ? e.message : e }); | ||
} | ||
}; |
8 changes: 8 additions & 0 deletions
8
packages/cloud-functions/merkle-distributor-helper/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "merkle-distributor-helper", | ||
"version": "0.0.1", | ||
"dependencies": { | ||
"@uma/merkle-distributor": "npm:@uma/[email protected]" | ||
}, | ||
"private": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const { Datastore } = require("@google-cloud/datastore"); | ||
const datastore = new Datastore(); | ||
|
||
const { calculateCurrentTvl } = require("@uma/merkle-distributor"); | ||
|
||
exports.calculateCurrentTvl = async (req, res) => { | ||
try { | ||
const tvl = await calculateCurrentTvl(); | ||
|
||
const currentTime = Math.round(new Date().getTime() / 1000); | ||
const key = datastore.key(["UmaTvl", currentTime]); | ||
const dataBlob = { | ||
key: key, | ||
data: { | ||
tvl: tvl.currentTvl, | ||
created: currentTime | ||
} | ||
}; | ||
await datastore.save(dataBlob); | ||
|
||
console.log(`Fetched and saved current TVL: ${JSON.stringify(tvl)}`); | ||
res.status(200).send(tvl); | ||
} catch (e) { | ||
console.error(e); | ||
res.status(500).send({ message: "Error in fetching and saving TVL", error: e instanceof Error ? e.message : e }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"name": "uma-tvl-calculator", | ||
"version": "0.0.1", | ||
"dependencies": { | ||
"@uma/merkle-distributor": "npm:@uma/[email protected]", | ||
"@google-cloud/datastore": "^6.3.1" | ||
}, | ||
"private": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const { Datastore } = require("@google-cloud/datastore"); | ||
const datastore = new Datastore(); | ||
|
||
exports.fetchLatestTvl = async (req, res) => { | ||
try { | ||
res.set("Access-Control-Allow-Origin", "*"); | ||
const [result] = await datastore.runQuery( | ||
datastore | ||
.createQuery("UmaTvl") | ||
.order("created", { descending: true }) | ||
.limit(1) | ||
); | ||
const responseObject = { currentTvl: result[0].tvl, currentTime: result[0].created }; | ||
console.log(`Fetched TVL from data store: ${JSON.stringify(responseObject)}`); | ||
res.status(200).send(responseObject); | ||
} catch (e) { | ||
console.error(e); | ||
res.status(500).send({ message: "Error in fetching TVL", error: e instanceof Error ? e.message : e }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "uma-tvl-fetcher", | ||
"version": "0.0.1", | ||
"dependencies": { | ||
"@google-cloud/datastore": "^6.3.1" | ||
}, | ||
"private": true | ||
} |