Skip to content

Commit

Permalink
Web3 Action to trigger Dune Queries (#139)
Browse files Browse the repository at this point in the history
* adds tenderly web3 action to trigger dune queries after Bid event in StakedLockedCrowdSale

* - moved constants to storage context
- merged dependencies into package.json of repo

* - commits hash of Bid event signature to source and adds comment

* - update README

* Dune Query Updates (#140)

* reorders tenderly files
versions dune sql queries

Signed-off-by: Stefan Adolf <[email protected]>

* drops ipnft prefix
updates cumulative bids query

Signed-off-by: Stefan Adolf <[email protected]>

* uses generic prices table to fetch decimals
reduces crowdsale bids query

Signed-off-by: Stefan Adolf <[email protected]>

* falls back to generic tokens.erc20 spell

Signed-off-by: stadolf <[email protected]>

---------

Signed-off-by: Stefan Adolf <[email protected]>
Signed-off-by: stadolf <[email protected]>

---------

Signed-off-by: Stefan Adolf <[email protected]>
Signed-off-by: stadolf <[email protected]>
Co-authored-by: Stefan Adolf <[email protected]>
  • Loading branch information
DorianWilhelm and elmariachi111 authored Jul 21, 2023
1 parent 40d8f9a commit c4edb42
Show file tree
Hide file tree
Showing 10 changed files with 283 additions and 3 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,16 @@ let account(1) fulfill the listing
grant read access to another party

`cast send --private-key $PRIVATE_KEY -i $IPNFT_ADDRESS "grantReadAccess(address,uint256,uint256)" 0x70997970C51812dc3A010C7d01b50e0d17dc79C8 1 1680265071`

## Actions

We are using Tenderly Web3Actions to trigger actions based on emitted Events from our deployed Contracts.

These are setup under the moleculeprotocol organization on Tenderly.
The QueryIds and API-KEY are stored in the Tenderly context and can be accessed via the Tenderly Frontend.
To update these actions you need the Tenderly login credentials.

- StakedLockingCrowdSale (Mainnet & Goerli): BidEvent => Triggers a POST request that executes Dune Queries to update the Dune Visualizations.

You can find out more about Web3Actions on Tenderly here: <https://docs.tenderly.co/web3-actions/intro-to-web3-actions>
How to init & deploy new Web3Actions: <https://docs.tenderly.co/web3-actions/tutorials-and-quickstarts/deploy-web3-action-via-cli>
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"license": "UNLICENSED",
"license": "MIT",
"scripts": {},
"devDependencies": {
"@nomicfoundation/hardhat-foundry": "^1.0.0",
Expand All @@ -10,6 +10,5 @@
"chai": "^4.3.7",
"ethers": "^5.7.2",
"hardhat": "^2.14.0"
},
"dependencies": {}
}
}
31 changes: 31 additions & 0 deletions periphery/dune/crowdsale_bids.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
with bids as (
SELECT
Bidder as bidder,
sum(amount) as bid_size
FROM
ipnft_{{chain}}.StakedLockingCrowdSale_evt_Bid
WHERE
saleId = cast('{{saleId}}' as uint256)
GROUP BY
Bidder,
amount
ORDER BY
bid_size DESC
),
decimals as (
SELECT erc20.decimals
FROM
(SELECT from_hex(json_extract_scalar(sale, '$.biddingToken')) as biddingTokenContract
FROM
ipnft_{{chain}}.StakedLockingCrowdSale_evt_Started
WHERE
saleId = cast('{{saleId}}' as uint256)
) as sale
LEFT JOIN tokens.erc20 as erc20
ON erc20.blockchain = '{{chain}}'
AND erc20.contract_address = sale.biddingTokenContract
)
select
Bidder,
bid_size / pow(10, COALESCE(decimals,18)) as bid_amount
FROM bids, decimals
15 changes: 15 additions & 0 deletions periphery/dune/cumulative_bids.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
SELECT
bids.evt_block_time,
SUM(previous_bids.amount) / 1e18 AS cumulative_growth
FROM
ipnft_{{chain}}.StakedLockingCrowdSale_evt_Bid as bids
LEFT JOIN ipnft_{{chain}}.StakedLockingCrowdSale_evt_Bid
AS previous_bids
ON previous_bids.evt_block_time <= bids.evt_block_time
WHERE
bids.saleId = cast('{{saleId}}' as uint256)
GROUP BY
bids.evt_block_time,
bids.amount
ORDER BY
bids.evt_block_time;
38 changes: 38 additions & 0 deletions periphery/tenderly.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
account_id: ''
project_slug: ''
actions:
moleculeprotocol/project:
runtime: v2
sources: tenderly
specs:
BidEventGoerli:
description: BidEvent on Goerli StakedLockingCrowdSale contract
function: dune:triggerDuneQuery
trigger:
type: transaction
transaction:
filters:
- eventEmitted:
contract:
address: 0x46c3369dEce07176Ad7164906D3593AA4C126d35
name: Bid
network: 5
status:
- mined
execution_type: ''
BidEventMainnet:
description: BidEvent on Mainnet StakedLockingCrowdSale contract
function: dune:triggerDuneQuery
trigger:
type: transaction
transaction:
filters:
- eventEmitted:
contract:
address: 0x35Bce29F52f51f547998717CD598068Afa2B29B7
name: Bid
network: 1
status:
- mined
execution_type: ''

5 changes: 5 additions & 0 deletions periphery/tenderly/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Dependency directories
node_modules/

# Ignore tsc output
out/**/*
43 changes: 43 additions & 0 deletions periphery/tenderly/dune.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import axios from 'axios'

const triggerDuneQuery = async (context, event) => {
// keccak256(Bid(uint256 saleId,address bidder,uint256 amount));
const BID_EVENT_SIG =
'0xdcd726e11f8b5e160f00290f0fe3a1abb547474e53a8e7a8f49a85e7b1ca3199'

const placeBidLog = event.logs.find((log) => log.topics[0] === BID_EVENT_SIG)
if (!placeBidLog) return

const saleId = BigInt(placeBidLog.topics[1]).toString()
const DUNE_API_KEY = await context.secrets.get('DUNE_API_KEY')

//[Cumulative Bids, CrowdSale Bids]
const duneQueryIds = [2709374, 2709364]

const query_parameters = {
saleId: saleId,
chain: event.network === '5' ? 'goerli' : 'ethereum'
}

for (const queryId of duneQueryIds) {
try {
const res = await axios.post(
`https://api.dune.com/api/v1/query/${queryId}/execute`,
{
query_parameters
},
{
headers: {
'Content-Type': 'application/json',
'X-Dune-API-Key': DUNE_API_KEY
}
}
)
console.log('response >> ', res.data)
} catch (e) {
console.log('error >> ', e)
}
}
}

module.exports = { triggerDuneQuery }
109 changes: 109 additions & 0 deletions periphery/tenderly/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions periphery/tenderly/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@moleculeprotocol/web3-actions",
"private": true,
"scripts": {},
"dependencies": {
"@tenderly/actions": "^0.2.0",
"axios": "^1.4.0"
}
}
18 changes: 18 additions & 0 deletions periphery/tenderly/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compileOnSave": true,
"compilerOptions": {
"module": "commonjs",
"noImplicitReturns": true,
"noUnusedLocals": true,
"outDir": "out",
"sourceMap": true,
"strict": true,
"target": "es2020"
},
"exclude": [
"**/*.spec.ts"
],
"include": [
"**/*"
]
}

0 comments on commit c4edb42

Please sign in to comment.