Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

init release #1

Merged
merged 3 commits into from
Nov 21, 2022
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
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
misc/
*.sw[pon]
cmd/foo.go
unsorted.*
.DS_Store
data/*
proto/*
testdir
pkgs/sdk/vm/_testdata
build/*
*.tx
*.log.*
*.log
*.gno.gen.go
*.gno.gen_test.go
.vscode
.idea
*.pb.go
pbbindings.go
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@


.PHONY: build
all: build

build:
@echo "build"
go build -o build/gnobot ./cmd/gnodiscord

build_linux:
@echo "build"
GOOS=linux GOARCH=amd64 go build -o build/gnobot_linux ./cmd/gnodiscord
124 changes: 124 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,127 @@
the community bot(s) interacting with r/faucet

See https://github.com/gnolang/gno/issues/364.

## GNO Discord Faucet

Community members can request testing tokens from the discord channel.

## Problem to solve

A centralized faucet endpoint could easily be an abusive target.
In addition, it adds operational complexity and cost to operating an endpoint.

### Details

There are three issues we try to solve in the web-based faucet.

1) It is centralized and can be an abuse target.

2) First-time users need to understand the fee structure to register and interact with the board and require multiple requests to get in fees

3) There need to be more tokens allocated to the faucet wallet to support many user registrations and board creation.



## Solution

A decentralized discord bot can be a faucet to any discord server and channels.

Discord provides sophisticated verification to prevent abuse. We can leverage it to distribute test tokens in the community group.

It is friendly to community members. The moderator can easily manage and prevent people from abusing the faucet.

### Details

A discord faucet bot can solve 1, 2, and partially 3

1) A discord bot can run on any computer and be configured to use any wallet to support the faucet. There is no direct attacking point as a service. Instead, the testing token request is funneled through the discord app, which the admin moderates. Multiple bots can stand by and respond to the same channel as back to each other.

2) In the gno land test net, users must request 200gnot to register and cost 100gnot to interact with the board contract if not registered.

How do we allow people quickly get started and prevent people keep accumulating testing tokens?

We can set a limit for each, say 400gnot per account. We give max 400gnot for first-time users. So they can get started the right way and have enough tokens to try everything. For non-first-time users, we provide a regular amount of 1gnot to cover most of the gas fee usage.

3) A faucet can drain out with regular usage. We can run a backup bot with another faucet wallet monitor on a different channel as a backup.

## Other aspects

4) We can also recycle the tokens from the user and broad contract back to the faucet manually or automatically.

5) 200 gnot user registration fee is to prevent people from spamming the user board. We can add a limited number of users can register per day and lower the fee. It slows down the pace that the faucets drain out.

## Features

- Each account is limited to 350gnot max.
- Instead of incrementally issuing a small amount to users, we give the first-time user the max amount. It costs 200gnot to register and 100gnot to create a board in the current test net. No need to request multiple times to be able to register users and create boards.

## Instruction

#### 0) Install the gnokey

git clone https://github.com/gnolang/gno
cd gno
make install_gnokey

make sure you include $GOPATH/bin in $PATH

#### 0.1) create admin and controller accounts

gnokey add admin

gnokey add controller1

gnokey add controller2


#### 1) build gnobot

make

#### 2) Deploy faucet contract and assign controller the faucet

please modify the the address in the script.

./provison.sh

#### 3) start the bot

check out

./startbot.sh

The following flags are required when you run the gnobot. We do not recommend storing the discord token on your local machine, not even in the env file.

--chain-id

--token

--channel

--bot-name

--guild

--limit // default 350000000ugnot

--send // default 5000000ugnot

DISCORDBOT_TOKEN:

Your discord application bot token. (NOT OAuth2 token )

DISCORD_CHANNELID:

The id of a channel that you add the bot to

DISCORD_GUILD:

The id of the discord server that you add the bot to

DISCORD_BOTNAME:

The name of your bot


./build/gnodiscord faucet test1 --chain-id test3 --token DISCORDBOT_TOKEN --channel DISCORD_CHANNELID -bot-name DISCORD_BOTNAME --guild $DISCORD_GUILD --remote test3.gno.land:36657
230 changes: 230 additions & 0 deletions cmd/gnodiscord/discord.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
package main

import (
"fmt"
"strings"
"unicode"

"github.com/bwmarrin/discordgo"

"github.com/gnolang/gno/pkgs/amino"

"github.com/gnolang/gno/pkgs/crypto"

"github.com/gnolang/gno/pkgs/crypto/keys/client"
"github.com/gnolang/gno/pkgs/errors"
"github.com/gnolang/gno/pkgs/std"
)

// Start a discord session
func (df *DiscordFaucet) Start() error {
err := df.discordFaucet()
if err != nil {
return err
}

// Open a websocket connection to Discord and begin listening.

return df.session.Open()
}

// Close discord session smoothly
func (df *DiscordFaucet) Close() {
df.session.Close()
}

// it takes discord server API token and rpc client.
// We use rpc client to validate addresses and check abuses.
// and we cap the balance holding to 400 GNOT before issue new tokens from faucet

func (df *DiscordFaucet) discordFaucet() error {
// Create a new Discord session using the provided bot token.
dg, err := discordgo.New("Bot " + df.opts.BotToken)
if err != nil {
fmt.Println("failed to create discord bot session.", err)
return err
}

df.session = dg

// we only care about receiving message events.
dg.Identify.Intents = discordgo.IntentsGuildMessages

// Register the messageCreate func as a callback for MessageCreate events.
dg.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore all messages created by the bot itself
// This is a good practice.
if m.Author.ID == s.State.User.ID {
return
}
// ignore message from other channels
if m.ChannelID != df.opts.Channel {
return
}

// ignore message from other discord guild/server

if m.GuildID != df.opts.Guild {
return
}

// ignore message does not mention anyone
if len(m.Mentions) == 0 {
return
}

user := m.Mentions[0]

// ingore messages that doe not mentiont this bot

if user.Bot != true || user.Username != df.opts.BotName {
return
}

var res string
// retrive toAddress from received disocrd message
toAddr, bal, err := df.process(m)
if err != nil {

res = fmt.Sprintf("%s", err)
dg.ChannelMessageSend(m.ChannelID, "<@"+m.Author.ID+"> "+res)

fmt.Printf("channel %s:<@%s> %s", m.ChannelID, m.Author.Username, res)

return

}
var send std.Coins
// if the account has no balance we give it upto the full.
if bal.IsZero() {
send = std.NewCoins(perAccountLimit)
} else {
send, _ = std.ParseCoins(df.opts.Send)
}

err = df.sendAmountTo(toAddr, send)
if err != nil {

dg.ChannelMessageSend(m.ChannelID, "<@"+m.Author.ID+"> "+"faucet failed")

fmt.Printf("channel %s:<@%s>%v", m.ChannelID, m.Author.Username, err)

return

}

var amount string
for _, v := range send {
amount += v.String() + " "
}

res = fmt.Sprintf("Cha-Ching! %s +%s", toAddr, amount)

dg.ChannelMessageSend(m.ChannelID, "<@"+m.Author.ID+"> "+res)
})

return nil
}

// This function will be called every time a new
// message is created on any channel that the authenticated bot has access to.
// It returns true and valid response if the message from discord is valid.
// Other we returns false with an empty string,which we should ingore.
func (df *DiscordFaucet) process(m *discordgo.MessageCreate) (string, std.Coin, error) {
validAddr, err := retrieveAddr(m.Content)

zero := std.Coin{}

if err != nil {
return "", zero, errors.New("No valid addresse: %s", err)
}

bal, err := df.checkBalance(validAddr)
if err != nil {
return "", zero, errors.New("It seems our faucet is not working properly %s", err)
}

if bal.IsGTE(perAccountLimit) {
return "", zero, errors.New("Your account %s still has %d%s, no need to accumulate more testing tokens", validAddr, bal.Amount, bal.Denom)
}

return validAddr, bal, nil
}

// retrive the first string with valid addresse length
func retrieveAddr(message string) (string, error) {
var addr string
words := strings.Fields(message)

for _, w := range words {

s := strings.TrimFunc(w, func(r rune) bool {
return !unicode.IsLetter(r) && !unicode.IsNumber(r)
})

if len(s) == addressStringLength {
addr = s
break
}
}

ok, err := isValid(addr)

if !ok {
return "", err
}

return addr, nil
}

// A valid address format

func isValid(addr string) (bool, error) {
// validate prefix

if strings.HasPrefix(addr, crypto.Bech32AddrPrefix) == false {
return false, errors.New("The address does not have correct prefix %s", crypto.Bech32AddrPrefix)
}

if addressStringLength != len(addr) {
return false, errors.New("The address does not have correct length %d", addressStringLength)
}

_, err := crypto.AddressFromBech32(addr)
if err != nil {
return false, errors.Wrap(err, "parsing address")
}
return true, nil
}

// return true if the account balance is within limit
// return amount of token from
func (df *DiscordFaucet) checkBalance(addr string) (std.Coin, error) {
qopts := client.QueryOptions{
Path: fmt.Sprintf("auth/accounts/%s", addr),
}
qopts.Remote = df.opts.Remote
qres, err := client.QueryHandler(qopts)
if err != nil {
return std.Coin{}, errors.Wrap(err, "query account")
}
var acc struct{ BaseAccount std.BaseAccount }
err = amino.UnmarshalJSON(qres.Response.Data, &acc)
if err != nil {
return std.Coin{}, err
}

balances := acc.BaseAccount.GetCoins()

bal := std.Coin{Denom: "ugnot", Amount: 0}

if len(balances) > 0 {
for i, v := range balances {
if v.Denom == "ugnot" {
bal = balances[i]
}
}
}

return bal, nil
}
Loading