-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: basic faucet service implementation (#163)
* feat: basic faucet service implementation * fix: space in makefile * chore: delete protobuf file Co-authored-by: alvarius <89248902+alvrs@users.noreply.github.com>
- Loading branch information
Showing
14 changed files
with
2,658 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"crypto/ecdsa" | ||
"flag" | ||
"os" | ||
"time" | ||
|
||
"latticexyz/mud/packages/services/pkg/eth" | ||
"latticexyz/mud/packages/services/pkg/faucet" | ||
"latticexyz/mud/packages/services/pkg/grpc" | ||
"latticexyz/mud/packages/services/pkg/logger" | ||
|
||
"github.com/dghubble/go-twitter/twitter" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
"go.uber.org/zap" | ||
"golang.org/x/oauth2/clientcredentials" | ||
) | ||
|
||
var ( | ||
wsUrl = flag.String("ws-url", "ws://localhost:8545", "Websocket Url") | ||
port = flag.Int("port", 50081, "gRPC Server Port") | ||
faucetPrivateKey = flag.String("faucet-private-key", "0x", "Private key to use for faucet") | ||
dripAmount = flag.Int64("drip-amount", 10000000000000000, "Drip amount in wei. Default to 0.01 ETH") | ||
dripFrequency = flag.Float64("drip-frequency", 60, "Drip frequency per account in minutes. Default to 60 minutes") | ||
dripLimit = flag.Uint64("drip-limit", 1000000000000000000, "Drip limit in wei per drip frequency interval. Default to 1 ETH") | ||
) | ||
|
||
func main() { | ||
// Parse command line flags. | ||
flag.Parse() | ||
|
||
// Setup logging. | ||
logger.InitLogger() | ||
logger := logger.GetLogger() | ||
defer logger.Sync() | ||
|
||
// Create a drip config. | ||
dripConfig := &faucet.DripConfig{ | ||
DripAmount: *dripAmount, | ||
DripFrequency: *dripFrequency, | ||
DripLimit: *dripLimit, | ||
} | ||
logger.Info("using a drip configuration", | ||
zap.Int64("amount", dripConfig.DripAmount), | ||
zap.Float64("frequency", dripConfig.DripFrequency), | ||
zap.Uint64("limit", dripConfig.DripLimit), | ||
) | ||
|
||
// Ensure that a twitter <-> address store is setup. | ||
faucet.SetupStore() | ||
|
||
// Oauth2 configures a client that uses app credentials to keep a fresh token. | ||
config := &clientcredentials.Config{ | ||
ClientID: os.Getenv("CLIENT_ID"), | ||
ClientSecret: os.Getenv("CLIENT_SECRET"), | ||
TokenURL: "https://api.twitter.com/oauth2/token", | ||
} | ||
|
||
// Get a connection to Twitter API with a client. | ||
twitterClient := twitter.NewClient(config.Client(context.Background())) | ||
|
||
// Get an instance of ethereum client. | ||
ethClient := eth.GetEthereumClient(*wsUrl, logger) | ||
|
||
// Create a private key ECDSA object. | ||
privateKey, err := crypto.HexToECDSA(*faucetPrivateKey) | ||
if err != nil { | ||
logger.Fatal("error creating ECDSA object from private key string", zap.String("privateKey", *faucetPrivateKey)) | ||
} | ||
|
||
publicKey, ok := privateKey.Public().(*ecdsa.PublicKey) | ||
if !ok { | ||
logger.Fatal("error casting public key to ECDSA") | ||
} | ||
|
||
// Kick off a worked that will reset the faucet limit at the specified interval. | ||
// Note: the duration here matches whatever time units are used in 'drip-frequency'. | ||
go faucet.ReplenishFaucetWorker(time.NewTicker(time.Duration(*dripFrequency)*time.Minute), make(chan struct{})) | ||
|
||
// Start the faucet gRPC server. | ||
grpc.StartFaucetServer(*port, twitterClient, ethClient, privateKey, publicKey, dripConfig, logger) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package faucet | ||
|
||
const MatchingHashtag = "#mud" | ||
|
||
const FaucetStoreFilename = "./FaucetStore" |
Oops, something went wrong.