Skip to content

Commit

Permalink
Implement Tenderly Web3 Actions to monitor different RedStone smart c…
Browse files Browse the repository at this point in the history
…ontracts (#212)

* Implement web3 action for USDT, LSK and ETH token pairs

* Add readme file for RedStone price feed monitoring

* Add a little buffer in time for Web3 Actions

* Apply review suggestions

* Fix link inside README

* Apply review suggestions
  • Loading branch information
matjazv authored Nov 18, 2024
1 parent ad07d38 commit fef905d
Show file tree
Hide file tree
Showing 12 changed files with 1,665 additions and 0 deletions.
41 changes: 41 additions & 0 deletions web3-actions/priceFeedMonitor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# RedStone Price Feed Monitoring

Inside this directory, you will find the code for the RedStone Price Feed monitoring using Tenderly Web3 Actions. This code is used to monitor the price feed of the RedStone smart contracts for different token pairs and trigger alerts if the price feed was not updated for a certain period of time. In this case, the alert is sent to Opsgenie.

## Install Tenderly CLI
If you haven't already, install [Tenderly CLI](https://github.com/Tenderly/tenderly-cli#installation).

Before you go on, you need to login with CLI, using your Tenderly credentials:

```bash
tenderly login
```

## Build and Publish/Deploy Web3 Actions

Before you can build and publish/deploy the Web3 Actions, you need modify the configuration `.yaml` file for the project.
Some configuration files inside this directory are:
- [`lskUsd.yaml`](./lskUsd.yaml) - configuration file for the RedStone price feed monitoring for the LSK/USD token pair
- [`ethUsd.yaml`](./ethUsd.yaml) - configuration file for the RedStone price feed monitoring for the ETH/USD token pair
- [`usdtUsd.yaml`](./usdtUsd.yaml) - configuration file for the RedStone price feed monitoring for the USDT/USD token pair

You need to provide the following information in the configuration file(s), under `actions`:
- `YOUR_ACCOUNT_SLUG` - your Tenderly account
- `YOUR_PROJECT_SLUG` - your Tenderly project

**Note**: Both of the above values can be accessed from the Settings page on your Tenderly dashboard.

To build different Web3 Actions, run the following command:

```bash
tenderly actions build --project-config [yaml_filename_without_extension]
```

To publish/deploy the Web3 Actions, run the following command:

```bash
tenderly actions publish --project-config [yaml_filename_without_extension]
or
tenderly actions deploy --project-config [yaml_filename_without_extension]
```
`publish` is used to publish the Web3 Actions to the Tenderly platform without deploying them.
13 changes: 13 additions & 0 deletions web3-actions/priceFeedMonitor/ethUsd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
actions:
YOUR_ACCOUNT_SLUG/YOUR_PROJECT_SLUG:
runtime: v2
sources: tokenPairMonitor
specs:
RedStonePriceFeed_ETH-USD:
description: Check if token pair price for ETH/USD is updated at least once every 6 hours.
function: ethUsd:monitorEthUsdFn
trigger:
type: periodic
periodic:
interval: 1h
execution_type: parallel
13 changes: 13 additions & 0 deletions web3-actions/priceFeedMonitor/lskUsd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
actions:
YOUR_ACCOUNT_SLUG/YOUR_PROJECT_SLUG:
runtime: v2
sources: tokenPairMonitor
specs:
RedStonePriceFeed_LSK-USD:
description: Check if token pair price for LSK/USD is updated at least once every 6 hours.
function: lskUsd:monitorLskUsdFn
trigger:
type: periodic
periodic:
interval: 1h
execution_type: parallel
5 changes: 5 additions & 0 deletions web3-actions/priceFeedMonitor/tokenPairMonitor/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Dependency directories
node_modules/

# Ignore tsc output
out/**/*
24 changes: 24 additions & 0 deletions web3-actions/priceFeedMonitor/tokenPairMonitor/ethUsd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {
ActionFn,
Context,
Event,
PeriodicEvent,
} from '@tenderly/actions';
import { checkTokenPairPriceUpdateTime } from './validate';

// Define the contract address and token pair
const CONTRACT_ADDRESS = "0x6b7AB4213c77A671Fc7AEe8eB23C9961fDdaB3b2";
const TOKEN_PAIR = "ETH/USD";

export const monitorEthUsdFn: ActionFn = async (context: Context, event: Event) => {
const periodicEvent = event as PeriodicEvent;
console.log(periodicEvent);

// Get Opsgenie API key from the secrets
const apiKey = await context.secrets.get("OPSGENIE_API_KEY");

// Current time in seconds
const currentTimestamp = Math.floor(periodicEvent.time.getTime() / 1000);

await checkTokenPairPriceUpdateTime(CONTRACT_ADDRESS, TOKEN_PAIR, apiKey, currentTimestamp);
}
24 changes: 24 additions & 0 deletions web3-actions/priceFeedMonitor/tokenPairMonitor/lskUsd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {
ActionFn,
Context,
Event,
PeriodicEvent,
} from '@tenderly/actions';
import { checkTokenPairPriceUpdateTime } from './validate';

// Define the contract address and token pair
const CONTRACT_ADDRESS = "0xa1EbA9E63ed7BA328fE0778cFD67699F05378a96";
const TOKEN_PAIR = "LSK/USD";

export const monitorLskUsdFn: ActionFn = async (context: Context, event: Event) => {
const periodicEvent = event as PeriodicEvent;
console.log(periodicEvent);

// Get Opsgenie API key from the secrets
const apiKey = await context.secrets.get("OPSGENIE_API_KEY");

// Current time in seconds
const currentTimestamp = Math.floor(periodicEvent.time.getTime() / 1000);

await checkTokenPairPriceUpdateTime(CONTRACT_ADDRESS, TOKEN_PAIR, apiKey, currentTimestamp);
}
Loading

0 comments on commit fef905d

Please sign in to comment.