Skip to content
Open
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
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SEED_PHRASE="{\"seed_phrase\":\"forward service profit benefit punch catch fan chief jealous steel harvest column spell rude warm home melody hat broccoli pulse say garlic you firm\",\"derivation_path\":\"m/44'/118'/0'/0/0\"}"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ data
/.idea/vcs.xml
/.idea/modules.xml
/coverage.txt
.DS_Store
31 changes: 31 additions & 0 deletions Docker_notes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Get started
Docker
- Replace with your seed phrase in .env
- then run `docker compose up`

Setting up Grafana
- Go to http://localhost:3000
- u:admin p:admin
- Connections > Add new connections
- Search 'Prometheus' > Select it
- Click 'Add new data source' (blue button)
- Connection | Prometheus server URL *: `http://prometheus:9090`
- Click 'Save & test'

Setting up Dashboard
- Dashboards > New (dropdown) > Import
- Select `example_sequoia_dashboard.json`
- Select the Data Source you added earlier
- Click Import







## Running sequoia docker image (for testing docker img)

export SEED_PHRASE="{\"seed_phrase\":\"forward service profit benefit punch catch fan chief jealous steel harvest column spell rude warm home melody hat broccoli pulse say garlic you firm\",\"derivation_path\":\"m/44'/118'/0'/0/0\"}"

docker run -p 3334:3334 --env SEED_PHRASE sequoia:latest
19 changes: 19 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM golang:1.21

RUN apt update
RUN apt install yq -y

ADD . sequoia

WORKDIR sequoia

COPY go.mod go.sum ./
RUN go mod download

COPY *.go ./

RUN CGO_ENABLED=0 GOOS=linux go build -o /sequoia

EXPOSE 3334

CMD ["sh", "scripts/sequoia.sh"]
6 changes: 6 additions & 0 deletions api/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (

func IndexHandler(address string) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")

v := types.IndexResponse{
Status: "online",
Address: address,
Expand All @@ -33,6 +36,9 @@ func VersionHandler(wallet *wallet.Wallet) func(http.ResponseWriter, *http.Reque
return
}

w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")

v := types.VersionResponse{
Version: config.Version(),
Commit: config.Commit(),
Expand Down
16 changes: 14 additions & 2 deletions api/server.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package api

import (
"embed"
"errors"
"fmt"
"io/fs"
"net/http"
"time"

Expand All @@ -15,11 +17,15 @@ import (

"github.com/desmos-labs/cosmos-go-wallet/wallet"
"github.com/gorilla/mux"

jsoniter "github.com/json-iterator/go"
)
import jsoniter "github.com/json-iterator/go"

var json = jsoniter.ConfigCompatibleWithStandardLibrary

//go:embed static
var assets embed.FS

type API struct {
port int64
srv *http.Server
Expand Down Expand Up @@ -53,6 +59,12 @@ func (a *API) Serve(f *file_system.FileSystem, p *proofs.Prover, wallet *wallet.

r.Handle("/metrics", promhttp.Handler())

r.HandleFunc("/withdraw", WithdrawHandler(wallet, p)).Methods("POST")

html, _ := fs.Sub(assets, "static")
fs := http.FileServer(http.FS(html))
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", fs))

a.srv = &http.Server{
Handler: r,
Addr: fmt.Sprintf("0.0.0.0:%d", a.port),
Expand All @@ -61,7 +73,7 @@ func (a *API) Serve(f *file_system.FileSystem, p *proofs.Prover, wallet *wallet.
ReadTimeout: 15 * time.Second,
}

log.Logger.Info().Msg(fmt.Sprintf("Sequoia API now listening on %s", a.srv.Addr))
log.Logger.Info().Msg(fmt.Sprintf("Sequoia API now listening on %s...", a.srv.Addr))
err := a.srv.ListenAndServe()
if err != nil {
if !errors.Is(err, http.ErrServerClosed) {
Expand Down
50 changes: 50 additions & 0 deletions api/static/chart-stats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const ctx1 = document.querySelector("#myChart").getContext("2d");
var myChart = new Chart(ctx1, {
type: "line",
plugins: [ChartDatasourcePrometheusPlugin],
options: {
plugins: {
"datasource-prometheus": {
prometheus: {
endpoint: "http://localhost:9092",
},
query:
"sequoia_current_proofs_processing or sequoia_file_count offset 10m",
timeRange: {
type: "relative",
// from 24 hours ago to now
start: -24 * 60 * 60 * 1000,
end: 0,
},
},
},
},
});

const ctx2 = document.querySelector("#networkChart").getContext("2d");
var networkChart = new Chart(ctx2, {
type: "line",
plugins: [ChartDatasourcePrometheusPlugin],
options: {
plugins: {
"datasource-prometheus": {
prometheus: {
endpoint: "http://localhost:9092",
},
query: "sequoia_block_height",
timeRange: {
type: "relative",
// from 2 hours ago to now
start: -2 * 60 * 60 * 1000,
end: 0,
},
},
},
},
});

const doSomething = () => {
console.log("hello...");
};

window.addEventListener("onload", doSomething(), false);
50 changes: 50 additions & 0 deletions api/static/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Provider Dashboard</title>
<link rel="stylesheet" href="./style.css" />
<link rel="icon" href="./favicon.ico" type="image/x-icon" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script
src="https://cdn.jsdelivr.net/npm/chart.js@4/dist/chart.umd.min.js"
crossorigin="anonymous"
></script>
<script
src="https://cdn.jsdelivr.net/npm/chartjs-adapter-date-fns@3/dist/chartjs-adapter-date-fns.bundle.min.js"
crossorigin="anonymous"
></script>
<script
src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datasource-prometheus@2/dist/chartjs-plugin-datasource-prometheus.umd.min.js"
crossorigin="anonymous"
></script>
<script src="index.js"></script>
</head>
<body>
<h1>Provider Dashboard</h1>

<div id="status-bar"></div>
<div>
<h2>Account</h2>
Withdraw to:
<form>
<input type="text" name="to_address" placeholder="jkl1....." />
<input type="number" name="amount" placeholder="Amount" />
</form>
<button id="withdraw-btn">Withdraw</button>
</div>

<div id="stats">
<h2>Stats</h2>
<canvas id="myChart" style="width: 100%; max-height: 500px"></canvas>
</div>

<div>
<h2>Network</h2>
<canvas id="networkChart" style="width: 100%; max-height: 500px"></canvas>
</div>
</body>
<script src="chart-stats.js"></script>
</html>
46 changes: 46 additions & 0 deletions api/static/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
$(document).ready(function () {
const URL = "http://localhost:3334";

const getStatus = async () => {
let data = await fetch(URL).then((res) => res.json());
let version = await fetch(`${URL}/version`).then((res) => res.json());

if (data) {
let status = `<p>Status: ${data.status}</p>`;
let address = `<p>Wallet: ${data.address}</p>`;
$("#status-bar").append(status, address);
}
};

const withdrawRequest = async () => {
let input = getFormData();
console.log(input);
const data = new URLSearchParams();
data.append = ("to_address", input.to_address);
data.append = ("amount", input.amount);

fetch(`${URL}/withdraw/`, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: data,
})
.then((res) => res.json())
.then((data) => console.log(data))
.catch((error) => console.error(error));
};

const statusBar = document.createElement("p");

const getFormData = () => {
const form = document.querySelector("form");
return (data = Object.fromEntries(new FormData(form).entries()));
};
$("#withdraw-btn").click(function () {
console.log("withdraw click...");
withdrawRequest();
});

getStatus();
});
11 changes: 11 additions & 0 deletions api/static/prometheus-notes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
- Download Prometheus and Set up prometheus.yml file

- Run command to start prometheus server

`./prometheus --config.file="prometheus.yml" --web.listen-address ':9092'`

- Download Grafana

- Run cmd

`./bin/grafana server`
Empty file added api/static/style.css
Empty file.
99 changes: 99 additions & 0 deletions api/wallet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package api

import (
"fmt"
"net/http"

bankTypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/rs/zerolog/log"

"github.com/JackalLabs/sequoia/proofs"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/desmos-labs/cosmos-go-wallet/wallet"
)

type WithdrawRequest struct {
ToAddress string `json:"to_address"`
Amount string `json:"amount"`
}

type WithdrawResponse struct {
Response string `json:"response"`
}

func WithdrawHandler(wallet *wallet.Wallet, prover *proofs.Prover) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
w.Header().Set("Access-Control-Allow-Methods", "POST")
w.Header().Set("Content-Type", "application/json")

fmt.Printf("WITHDRAWING... \n")

var withdraw WithdrawRequest

err := json.NewDecoder(req.Body).Decode(&withdraw)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

c, err := sdk.ParseCoinNormalized(withdraw.Amount)
if err != nil {
log.Error()
return
}

m := bankTypes.MsgSend{
FromAddress: wallet.AccAddress(),
ToAddress: withdraw.ToAddress,
Amount: sdk.NewCoins(c),
}

fmt.Fprintf(w, "MsgSend: %+v \n", m)

// data := walletTypes.NewTransactionData(
// &m,
// ).WithGasAuto().WithFeeAuto()

msg, wg := prover.GetQueue().Add(&m)

fmt.Fprintf(w, "Add Queue: %+v %+v \n", msg, wg)

// res, err := wallet.BroadcastTxCommit(data)
// if err != nil {
// http.Error(w, err.Error(), http.StatusBadRequest)
// log.Error().Err(err)
// return
// }

// fmt.Fprintf(w, "RES: %+v \n", res)

// if res.Code == 0 {
// fmt.Fprintf(w, "Withdraw successful!")
// } else {
// fmt.Fprintf(w, "Something went wrong, please try again.")
// }
}
}

func WithdrawHandlerTest() func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
w.Header().Set("Access-Control-Allow-Methods", "POST")
w.Header().Set("Content-Type", "application/json")

fmt.Printf("WITHDRAWING... \n")

var withdraw WithdrawRequest

err := json.NewDecoder(req.Body).Decode(&withdraw)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

fmt.Fprintf(w, "Withdraw: %+v", withdraw)
}
}
Loading