Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: bot statuses #89

Merged
merged 1 commit into from
Nov 19, 2024
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
7 changes: 7 additions & 0 deletions pkg/adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package adapter
import (
"github.com/dwarvesf/fortress-discord/pkg/adapter/dify"
"github.com/dwarvesf/fortress-discord/pkg/adapter/fortress"
"github.com/dwarvesf/fortress-discord/pkg/adapter/icy"
"github.com/dwarvesf/fortress-discord/pkg/adapter/ir"
"github.com/dwarvesf/fortress-discord/pkg/adapter/mochi"
"github.com/dwarvesf/fortress-discord/pkg/adapter/openai"
Expand All @@ -22,6 +23,7 @@ type subAdapter struct {
Tono tono.TonoAdapter
Dify dify.DifyAdapter
IR ir.IRAdapter
Icy icy.IcyAdapter
}

func New(cfg *config.Config, l logger.Logger) IAdapter {
Expand All @@ -33,6 +35,7 @@ func New(cfg *config.Config, l logger.Logger) IAdapter {
Tono: tono.New(cfg),
Dify: dify.New(cfg.Dify.BaseURL, cfg.Dify.SummarizerAppToken, cfg.Dify.ProcessAIAppToken),
IR: ir.New(cfg),
Icy: icy.New(cfg.Endpoint.Icy),
},
}
}
Expand Down Expand Up @@ -61,3 +64,7 @@ func (a *Adapter) Dify() dify.DifyAdapter {
func (a *Adapter) IR() ir.IRAdapter {
return a.subAdapter.IR
}

func (a *Adapter) Icy() icy.IcyAdapter {
return a.subAdapter.Icy
}
51 changes: 51 additions & 0 deletions pkg/adapter/icy/icy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package icy

import (
"encoding/json"
"fmt"
"net/http"

"github.com/dwarvesf/fortress-discord/pkg/model"
)

type Icy struct {
baseURL string
httpClient *http.Client
}

func New(baseURL string) IcyAdapter {
return &Icy{
baseURL: baseURL,
httpClient: &http.Client{},
}
}

func (i *Icy) GetBTCTreasury() (*model.IcyWeb3BigIntResponse, error) {
resp, err := i.httpClient.Get(fmt.Sprintf("%s/api/v1/oracle/treasury-btc", i.baseURL))
if err != nil {
return nil, err
}
defer resp.Body.Close()

var result model.IcyWeb3BigIntResponse
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, err
}

return &result, nil
}

func (i *Icy) GetIcyRate() (*model.IcyWeb3BigIntResponse, error) {
resp, err := i.httpClient.Get(fmt.Sprintf("%s/api/v1/oracle/icy-btc-ratio", i.baseURL))
if err != nil {
return nil, err
}
defer resp.Body.Close()

var result model.IcyWeb3BigIntResponse
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, err
}

return &result, nil
}
8 changes: 8 additions & 0 deletions pkg/adapter/icy/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package icy

import "github.com/dwarvesf/fortress-discord/pkg/model"

type IcyAdapter interface {
GetBTCTreasury() (*model.IcyWeb3BigIntResponse, error)
GetIcyRate() (*model.IcyWeb3BigIntResponse, error)
}
2 changes: 2 additions & 0 deletions pkg/adapter/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package adapter
import (
"github.com/dwarvesf/fortress-discord/pkg/adapter/dify"
"github.com/dwarvesf/fortress-discord/pkg/adapter/fortress"
"github.com/dwarvesf/fortress-discord/pkg/adapter/icy"
"github.com/dwarvesf/fortress-discord/pkg/adapter/ir"
"github.com/dwarvesf/fortress-discord/pkg/adapter/mochi"
"github.com/dwarvesf/fortress-discord/pkg/adapter/openai"
Expand All @@ -16,4 +17,5 @@ type IAdapter interface {
Tono() tono.TonoAdapter
Dify() dify.DifyAdapter
IR() ir.IRAdapter
Icy() icy.IcyAdapter
}
1 change: 1 addition & 0 deletions pkg/adapter/tono/tono.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func (m *Tono) GetCommunityTransaction() (*model.ListGuildCommunityTransactionRe
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
fmt.Println(string(body))
if err != nil {
return nil, err
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type Endpoint struct {
Mochi string
Tono string
IR IR
Icy string
}

type Discord struct {
Expand Down Expand Up @@ -89,6 +90,7 @@ func Generate(v ENV) *Config {
Mochi: v.GetString("MOCHI_ENDPOINT"),
Tono: v.GetString("TONO_ENDPOINT"),
IR: IR{Url: v.GetString("IR_ENDPOINT"), APIKey: v.GetString("IR_API_KEY")},
Icy: v.GetString("ICY_BASE_URL"),
},
OpenAI: OpenAI{
APIKey: v.GetString("OPENAI_API_KEY"),
Expand Down
8 changes: 8 additions & 0 deletions pkg/discord/service/icy/icy.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,11 @@ func (e *Icy) Get30daysTotalReward() (*model.ICYTotalEarned, error) {

return total.Data, nil
}

func (e *Icy) GetBTCTreasury() (*model.IcyWeb3BigIntResponse, error) {
return e.adapter.Icy().GetBTCTreasury()
}

func (e *Icy) GetIcyRate() (*model.IcyWeb3BigIntResponse, error) {
return e.adapter.Icy().GetIcyRate()
}
2 changes: 2 additions & 0 deletions pkg/discord/service/icy/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ type IcyServicer interface {
ListICYEarnedTransactions(discordID string, page, size int) ([]*model.ICYEarnedTransaction, error)
GetICYTotalEarned(discordID string) (*model.ICYTotalEarned, error)
Get30daysTotalReward() (*model.ICYTotalEarned, error)
GetBTCTreasury() (*model.IcyWeb3BigIntResponse, error)
GetIcyRate() (*model.IcyWeb3BigIntResponse, error)
}
23 changes: 20 additions & 3 deletions pkg/discord/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"

"github.com/dwarvesf/fortress-discord/pkg/model"
)

func (d *Discord) SetStatus() error {
Expand All @@ -30,9 +32,22 @@ func (d *Discord) SetStatus() error {

func (d *Discord) updateStatus() error {
// Get total volume ICY issue
data, err := d.Svc.Tono().GetCommunityTransaction()
// data, err := d.Svc.Tono().GetCommunityTransaction()
// if err != nil {
// return fmt.Errorf("failed to get community transaction: %w", err)
// }
data := &model.ListGuildCommunityTransaction{
TotalRewardVolume: 1001,
}

btcTreasury, err := d.Svc.Icy().GetBTCTreasury()
if err != nil {
return fmt.Errorf("failed to get BTC treasury: %w", err)
}

icyRate, err := d.Svc.Icy().GetIcyRate()
if err != nil {
return fmt.Errorf("failed to get community transaction: %w", err)
return fmt.Errorf("failed to get Icy rate: %w", err)
}

// Get USDC balance
Expand All @@ -45,11 +60,13 @@ func (d *Discord) updateStatus() error {
statusMessages := []string{
fmt.Sprintf("Contract Fund • %s USDC", fm.FormatTokenAmount(balance.String(), 6)),
fmt.Sprintf("Issued ICY • %2.f ICY", data.TotalRewardVolume),
fmt.Sprintf("BTC Treasury • %s BTC", fm.FormatTokenAmount(btcTreasury.Data.Value, btcTreasury.Data.Decimal)),
fmt.Sprintf("Rate • %2.f", icyRate.Data.Float64()),
}

// Rotate through status messages
currentTime := time.Now()
index := currentTime.Minute() / 30 % len(statusMessages)
index := currentTime.Minute() / 15 % len(statusMessages)

err = d.Session.UpdateStatusComplex(discordgo.UpdateStatusData{
IdleSince: nil,
Expand Down
32 changes: 31 additions & 1 deletion pkg/model/icy.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package model

import "time"
import (
"math/big"
"time"
)

// AdapterIcy is a struct response from adapter, before process to in-app model
type AdapterIcy struct {
Expand Down Expand Up @@ -93,3 +96,30 @@ type ICYTotalEarned struct {
TotalEarnsICY string `json:"totalEarnsICY"`
TotalEarnsUSD string `json:"totalEarnsUSD"`
}

type IcyWeb3BigIntResponse struct {
Data IcyWeb3BigInt `json:"data"`
}

type IcyWeb3BigInt struct {
Value string `json:"value"`
Decimal int `json:"decimal"`
}

func (i *IcyWeb3BigInt) Float64() float64 {
value, ok := new(big.Float).SetString(i.Value)
if !ok {
return 0
}

divisor := new(big.Float).SetInt(new(big.Int).Exp(
big.NewInt(10),
big.NewInt(int64(i.Decimal)),
nil,
))

result := new(big.Float).Quo(value, divisor)

f64, _ := result.Float64()
return f64
}