Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

# CODEOWNERS: https://help.github.com/articles/about-codeowners/

# NOTE: Order is important; the last matching pattern takes the
# most precedence.

# Primary repo maintainers
* @ojo-network/core-devs
71 changes: 71 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
name: Release contract monitor

on:
push:
branches:
- main

# Sequence of patterns matched against refs/tags
tags:
- "v[0-9]+\\.[0-9]+\\.[0-9]+" # Official release version tags e.g. v2.0.5
- "v[0-9]+\\.[0-9]+\\.[0-9]+-rc[0-9]+" # Release candidate tags e.g. v1.0.3-rc4
- "v[0-9]+\\.[0-9]+\\.[0-9]+-alpha[0-9]+" # Alpha release testing tags e.g. v0.0.3-alpha1

permissions:
contents: write
id-token: write

jobs:
goreleaser:
strategy:
matrix:
os: [ubuntu-latest, ubuntu-20.04]
runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-go@v4
with:
go-version: 1.19
cache: true
cache-dependency-path: go.sum

- name: Set env
run: echo "RELEASE_VERSION=${GITHUB_REF#refs/tags/*}" >> $GITHUB_ENV

- name: Check GLIBC version
if: startsWith(matrix.os, 'ubuntu-')
run: |
echo "GLIBC_VERSION=$(ldd --version | grep ldd | awk '{print $NF}')" >> $GITHUB_ENV

# Ref: https://goreleaser.com/limitations/semver
- name: Tag without prefix locally to avoid error in goreleaser
run: |-
git tag -d ${{ env.RELEASE_VERSION }} || echo "No such a tag exists before"
git tag ${{ env.RELEASE_VERSION }} HEAD

- name: Build
uses: goreleaser/goreleaser-action@v4
if: github.event_name == 'pull_request' && contains(github.event.pull_request.labels.*.name, 'Enable:ReleaseBuild')
with:
version: latest
args: build --rm-dist --skip-validate # skip validate skips initial sanity checks in order to be able to fully run
env:
GORELEASER_CURRENT_TAG: ${{ env.RELEASE_VERSION }}
GLIBC_VERSION: ${{ env.GLIBC_VERSION }}

- name: release
if: startsWith(github.ref, 'refs/tags/')
uses: goreleaser/goreleaser-action@v4
with:
# Note, we have to pin to v0.179.0 due to newer releases enforcing
# correct semantic versioning even when '--skip-validate' is provided.
#
# Ref: https://github.com/goreleaser/goreleaser/pull/2503
version: v0.179.0
args: release --rm-dist --skip-validate
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GORELEASER_CURRENT_TAG: ${{ env.RELEASE_VERSION }}
GLIBC_VERSION: ${{ env.GLIBC_VERSION }}
48 changes: 48 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
project_name: contractMonitor

env:
- CGO_ENABLED=1

before:
hooks:
- go mod download

builds:
- main: ./
id: "contractMonitor"
binary: contractMonitor
mod_timestamp: "{{ .CommitTimestamp }}"
flags:
- -trimpath
ldflags:
- -s -w -X main.commit={{.Commit}} -X main.date={{ .CommitDate }}
goos:
- linux
goarch:
- amd64

archives:
- id: bin
format: binary
name_template: "{{ .Binary }}-v{{ .Version }}-{{ .Os }}-{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}-glibc-{{.Env.GLIBC_VERSION}}"

- id: tarball
format: tar.gz
wrap_in_directory: true
format_overrides:
- goos: windows
format: zip
name_template: '{{ .Binary }}-v{{ .Version }}-{{ .Os }}-{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}-glibc-{{.Env.GLIBC_VERSION}}'

release:
disable: false

snapshot:
name_template: SNAPSHOT-{{ .Commit }}

checksum:
name_template: 'SHA256SUMS-{{ replace .Version "version/" "cw-relayer-version-" }}-glibc-{{.Env.GLIBC_VERSION}}.txt'
algorithm: sha256

changelog:
skip: false
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
BUILD_DIR ?= $(CURDIR)/build
build:
go build -o ./build/ ./...

build: go.sum
CGO_ENABLED=0 go build -mod=readonly -o $(BUILD_DIR)/monitor ./...
start:
${MAKE} build
./contractMonitor ./config.toml

.PHONY: build start
10 changes: 6 additions & 4 deletions config.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
cron_interval="30s"
cron_interval="50s"

[address_map.juno-testnet]
[address_map.juno]
contract_address = "juno1yqm8q56hjv8sd4r37wdhkkdt3wu45gc5ptrjmd9k0nhvavl0354qwcf249"
relayer_address = "juno1rkhrfuq7k2k68k0hctrmv8efyxul6tgn8hny6y"
denom="ujuno"
warning_threshold=5000000
warning_threshold=500000000000000000
threshold=1000000
report_median = true
report_deviation = true

[network_rpc]
juno-testnet = "https://juno-api.polkachu.com"
juno = "https://juno-api.polkachu.com"
15 changes: 10 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ type (
}

Relayer struct {
ContractAddress string `mapstructure:"contract_address"`
RelayerAddress string `mapstructure:"relayer_address"`
Denom string `mapstructure:"denom"`
WarningThreshold int64 `mapstructure:"warning_threshold"`
Threshold int64 `mapstructure:"threshold"`
ContractAddress string `mapstructure:"contract_address" validate:"required"`
RelayerAddress string `mapstructure:"relayer_address" validate:"required"`
Denom string `mapstructure:"denom" validate:"required"`

// threshold is less than warning threshold
WarningThreshold int64 `mapstructure:"warning_threshold" validate:"required"`
Threshold int64 `mapstructure:"threshold" validate:"required"`

ReportMedian bool `mapstructure:"report_median" validate:"required"`
ReportDeviation bool `mapstructure:"report_deviation" validate:"required"`
}

AccessToken struct {
Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ require (
github.com/slack-go/slack v0.12.2
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.16.0
golang.org/x/sync v0.1.0
gopkg.in/yaml.v3 v3.0.1
)

require (
Expand All @@ -16,8 +18,8 @@ require (
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/spf13/afero v1.9.5 // indirect
Expand All @@ -28,5 +30,4 @@ require (
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
9 changes: 7 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,12 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ=
Expand Down Expand Up @@ -292,6 +294,8 @@ golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down Expand Up @@ -329,6 +333,7 @@ golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
99 changes: 52 additions & 47 deletions monitor/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,36 @@ import (
"github.com/ojo-network/contractMonitor/config"
)

// base64 encoding for queries
const (
// {"get_deviation_ref": {"symbol": "ATOM"}}
deviation = "eyJnZXRfZGV2aWF0aW9uX3JlZiI6IHsic3ltYm9sIjogIkFUT00ifX0="

// {"get_median_ref": {"symbol": "ATOM"}}
median = "eyJnZXRfbWVkaWFuX3JlZiI6IHsic3ltYm9sIjogIkFUT00ifX0="

// {"get_ref": {"symbol": "ATOM"}}
rate = "eyJnZXRfcmVmIjogeyJzeW1ib2wiOiAiQVRPTSJ9fQ=="
)

var (
slackchan chan slack.Attachment
errchan chan error
check sync.Mutex
globallist map[string]int64
type IDS struct {
requestID int64
medianID int64
deviationID int64
}

LowBalance = "Low Balance"
StaleRequestID = "No New Request id"
var (
slackChan chan slack.Attachment
wg sync.WaitGroup
)

const RELAYER = "cw-relayer"
const (
StaleRateRequestID = "No New Request id"
StaleMedianRequestID = "No New Median id"
StaleDeviationRequestID = "No New Deviation id"
LowBalance = "Low Balance"
Relayer = "Relayer"
)

var rootCmd = &cobra.Command{
Use: "monitor [config-file]",
Expand All @@ -44,6 +59,8 @@ func Execute() {
fmt.Println(err)
os.Exit(1)
}

rootCmd.AddCommand(getVersionCmd())
}

func cwRelayerCmdHandler(cmd *cobra.Command, args []string) error {
Expand All @@ -53,57 +70,46 @@ func cwRelayerCmdHandler(cmd *cobra.Command, args []string) error {
}

client := slack.New(accessToken.SlackToken, slack.OptionDebug(false))
slackchan = make(chan slack.Attachment, len(config.AddressMap))

errchan = make(chan error, len(config.AddressMap))
globallist = make(map[string]int64)
slackChan = make(chan slack.Attachment, len(config.AddressMap))

logger := zerolog.New(zerolog.ConsoleWriter{Out: os.Stderr}).Level(zerolog.InfoLevel).With().Timestamp().Logger()

cronDuration, err := time.ParseDuration(config.CronInterval)
if err != nil {
return err
}

logger.Info().Msg("Contract monitor starting...")

ctx, cancel := context.WithCancel(cmd.Context())
var wg sync.WaitGroup
for network, asset := range config.AddressMap {
globallist[asset.ContractAddress] = 0
rpc := config.NetworkRpc[network]
wg.Add(1)
go func(ctx context.Context, threshold, warning int64, network, denom, rpc, relayer, contractAddress string) {
defer wg.Done()
for {
select {
case <-ctx.Done():
return
default:
if err := checkBalance(threshold, warning, network, denom, rpc, relayer); err != nil {
errchan <- err
}

err := checkQuery(network, rpc, contractAddress)
if err != nil {
errchan <- err
}

time.Sleep(cronDuration)
}
}
}(ctx, asset.Threshold, asset.WarningThreshold, network, asset.Denom, rpc, asset.RelayerAddress, asset.ContractAddress)
newCosmwasmChecker(
ctx,
cronDuration,
asset.Threshold,
asset.WarningThreshold,
network,
asset.Denom,
rpc,
asset.ContractAddress,
asset.RelayerAddress,
asset.ReportMedian,
asset.ReportDeviation,
logger,
)

logger.Info().Str("network", network).
Str("relayer address", asset.RelayerAddress).
Str("contract address", asset.ContractAddress).
Msg("monitoring")
}

go func() {
for err := range errchan {
logger.Err(err).Msg("Error in monitoring")
}
}()

go func() {
for attachment := range slackchan {
for attachment := range slackChan {
_, timestamp, err := client.PostMessage(accessToken.SlackChannel, slack.MsgOptionAttachments(attachment))
if err != nil {
errchan <- err
logger.Err(err).Msg("error posting slack message")
}

logger.Info().Str("Posted at timestamp", timestamp).Msg("slack message posted")
Expand All @@ -114,11 +120,10 @@ func cwRelayerCmdHandler(cmd *cobra.Command, args []string) error {
for {
select {
case <-ctx.Done():
logger.Info().Msg("closing monitor, waiting for all routines to exit")
logger.Info().Msg("closing monitor, waiting for all monitors to exit")

wg.Wait() // waiting for all goroutines to exit
close(errchan)
close(slackchan)
wg.Wait()
close(slackChan)
return nil
}
}
Expand Down
Loading