Skip to content

Commit

Permalink
Merge pull request MoralisWeb3#13 from MoralisWeb3/unescape-queries
Browse files Browse the repository at this point in the history
unescape $
  • Loading branch information
jermay authored Apr 8, 2021
2 parents 7789eba + ccc3827 commit dd82d17
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 56 deletions.
10 changes: 5 additions & 5 deletions casino-dapp/cloud_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ Moralis.Cloud.define("biggestLosers", async function(request) {
const pipeline = [
{
group: {
objectId: "\$user",
totalLost: { \$sum: { \$toInt: "\$bet" }},
objectId: "$user",
totalLost: { $sum: { $toInt: "$bet" }},
},
},
{ sort: {totalLost: -1}},
Expand All @@ -24,8 +24,8 @@ Moralis.Cloud.define("biggestWinners", async function(request) {
const pipeline = [
{
group: {
objectId: "\$user",
totalWon: { \$sum: { \$toInt: "\$bet" }},
objectId: "$user",
totalWon: { $sum: { $toInt: "$bet" }},
},
},
{ sort: {totalWon: -1}},
Expand All @@ -46,7 +46,7 @@ Moralis.Cloud.define("biggestBets", async function(request) {
project: {
user: 1,
win: 1,
bet: { \$toInt: "\$bet" },
bet: { $toInt: "$bet" },
},
},
{ sort: {bet: -1}},
Expand Down
40 changes: 8 additions & 32 deletions moralis-gas-demo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,30 +63,9 @@ The current user will always be authenticated and will return `null` if there is

Once a user has been authenticated, the server can sync their transactions and that data can be queried. This requires some set up in the Moralis backend.

Log into the Moralis back end, find your server, click the "Cloud Functions" button, paste the following code, and click "Save File".

```javascript
// @@@ Paste this code into the Moralis Cloud Functions section on the server @@@

// List of user selected / default plugins
const plugins = [
"./eth/realtime/blocks",
"./eth/realtime/balances",
"./eth/realtime/token_transfers",
"./eth/historical/transactions",
function customUserPlugin() {
// PUT CUSTOM CLOUD FUNCTION CODE HERE
},
];

Moralis.Cloud.plugins.initialize(plugins);
```

This will run the sync job. If you log into the Moralis Dashboard you should see a `syncEthAddress` job in the `Jobs` section of the side panel. Once the transactions have been sync'd you will see rows in the `EthTokenBalance`, `EthTokenTransfers`, `EthTransfers` sections.

### Running Queries

Moralis extends the functionality of Parse and should be able to do anything Prase can. See the docs <a href="https://docs.parseplatform.org/js/guide/#queries" target="_blank">here</a> for more details.
Moralis extends the functionality of Parse and should be able to do anything Prase can. See the docs <a href="https://docs.moralis.io/queries" target="_blank">here</a> for more details.

Any of the collections listed under the "Browse" section in the Moralis Dashboard can be queried.

Expand All @@ -107,7 +86,7 @@ const results = await query.find();

## Cloud Functions

More advanced queries, like ones which use "group by" require a Could Function as they <a href="https://docs.parseplatform.org/js/guide/#aggregate" target="_blank">need special permissions</a>. This is a query which runs on the server and saves the client from doing more intense work that can be more easily processed server side.
More advanced queries, like ones which use "group by" require a Could Function as they <a href="https://docs.moralis.io/queries#aggregate" target="_blank">need special permissions</a>. This is a query which runs on the server and saves the client from doing more intense work that can be more easily processed server side.

Cloud Functions are defined as follows. Any params will be be in `request.params`.

Expand All @@ -123,19 +102,18 @@ The function definitions need to be added to the Moralis Server. Click the "Clou
```javascript
// @@@ Paste this code into the Moralis Cloud Functions section on the server @@@

// note: the $ signs currently need to be escaped to prevent parsing errors
Moralis.Cloud.define("topTenAvgGas", async function (request) {
const query = new Moralis.Query("EthTransactions");
const pipeline = [
{
// group by "from_address"
// add computed properties with the avg, min, max, count
group: {
objectId: "\$from_address",
avgGas: { \$avg: "\$gas_price" },
minGas: { \$min: "\$gas_price" },
maxGas: { \$max: "\$gas_price" },
count: { \$sum: 1 },
objectId: "$from_address",
avgGas: { $avg: "$gas_price" },
minGas: { $min: "$gas_price" },
maxGas: { $max: "$gas_price" },
count: { $sum: 1 },
},
},
{ sort: { avgGas: -1 } }, // descending
Expand All @@ -148,9 +126,7 @@ Moralis.Cloud.define("topTenAvgGas", async function (request) {
});
```

See the <a href="https://docs.parseplatform.org/js/guide/#aggregate" target="_blank">Parse</a> and <a href="https://docs.mongodb.com/v3.2/reference/operator/aggregation/" target="_blank">MongoDB</a> docs for more details on queries.

In the current beta version the special char `$` which denotes path variables needs to be escaped like `\$` to prevent a parsing error in the Moralis server back end code. This will be fixed in a future version.
See the <a href="https://docs.moralis.io/queries#aggregate" target="_blank">Query Aggregate</a> and <a href="https://docs.mongodb.com/v3.2/reference/operator/aggregation/" target="_blank">MongoDB</a> docs for more details on queries.

### Running Cloud Functions from the broswer

Expand Down
10 changes: 5 additions & 5 deletions moralis-gas-demo/cloud-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ Moralis.Cloud.define("topTenAvgGas", async function (request) {
// group by "from_address"
// add computed properties with the avg, min, max, count
group: {
objectId: "\$from_address",
avgGas: { \$avg: "\$gas_price" },
minGas: { \$min: "\$gas_price" },
maxGas: { \$max: "\$gas_price" },
count: { \$sum: 1 },
objectId: "$from_address",
avgGas: { $avg: "$gas_price" },
minGas: { $min: "$gas_price" },
maxGas: { $max: "$gas_price" },
count: { $sum: 1 },
},
},
{ sort: { avgGas: -1 } }, // descending
Expand Down
2 changes: 1 addition & 1 deletion swap-monitor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Table name: the name of the collection the data will be saved to
### 2. Create a query and subscribe to it

Now that Moralis is listening to the `Swap` event it will save all event logs to the `DaiWethSwaps` collection we named above. This data can now be queried. See the
<a href="https://docs.moralis.io/queries-2" target="_blank">Query</a> and <a href="https://docs.moralis.io/live-queries" target="_blank">Live Query</a> docs for more details on queries.
<a href="https://docs.moralis.io/queries" target="_blank">Query</a> and <a href="https://docs.moralis.io/live-queries" target="_blank">Live Query</a> docs for more details on queries.

```javascript
// select the 10 most recent swaps
Expand Down
26 changes: 13 additions & 13 deletions swap-monitor/cloud-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +57,29 @@ Moralis.Cloud.afterSave("DaiWethSwaps", async function (request) {
const end = new Date();
const start = new Date(end.valueOf() - 3600000); // 1 hour ago
const pipeline = [
{match: {block_timestamp: {\$gt: start}}},
{match: {block_timestamp: {$gt: start}}},
// convert text values into numbers so they can be summed
{addFields:{
nAmount0In: {\$toDouble: "\$amount0In"},
nAmount1In: {\$toDouble: "\$amount1In"},
nAmount0Out: {\$toDouble: "\$amount0Out"},
nAmount1Out: {\$toDouble: "\$amount1Out"},
nAmount0In: {$toDouble: "$amount0In"},
nAmount1In: {$toDouble: "$amount1In"},
nAmount0Out: {$toDouble: "$amount0Out"},
nAmount1Out: {$toDouble: "$amount1Out"},
}},
{
group: {
objectId: null,
totalAmount0In: {\$sum: "\$nAmount0In"},
totalAmount1In: {\$sum: "\$nAmount1In"},
totalAmount0Out: {\$sum: "\$nAmount0Out"},
totalAmount1Out: {\$sum: "\$nAmount1Out"},
totalAmount0In: {$sum: "$nAmount0In"},
totalAmount1In: {$sum: "$nAmount1In"},
totalAmount0Out: {$sum: "$nAmount0Out"},
totalAmount1Out: {$sum: "$nAmount1Out"},
}
},
// convert wei into ETH
{project: {
dTotalAmount0In: {\$divide: ["\$totalAmount0In", 1e18]},
dTotalAmount1In: {\$divide: ["\$totalAmount1In", 1e18]},
dTotalAmount0Out: {\$divide: ["\$totalAmount0Out", 1e18]},
dTotalAmount1Out: {\$divide: ["\$totalAmount1Out", 1e18]},
dTotalAmount0In: {$divide: ["$totalAmount0In", 1e18]},
dTotalAmount1In: {$divide: ["$totalAmount1In", 1e18]},
dTotalAmount0Out: {$divide: ["$totalAmount0Out", 1e18]},
dTotalAmount1Out: {$divide: ["$totalAmount1Out", 1e18]},
}},
];
const results = await query.aggregate(pipeline, {useMasterKey: true});
Expand Down

0 comments on commit dd82d17

Please sign in to comment.