Skip to content

Commit

Permalink
Updated the way funds are sent
Browse files Browse the repository at this point in the history
  • Loading branch information
kpachhai committed Jul 25, 2024
1 parent d2f6dbb commit c83ccf2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ PORT=10591 # Optional: Default is 10591
PRIVATE_KEY_BYTES="Mjsdj07tXw2p2pMHGwNPLc6dLSJpLBcvPLJSpk3fr9AbBX3jICl8Ka0MH1ieohaGnPGTjYjJ+9cNZ0gyPb8vpw==" # Optional: Will use "nuklai1qrzvk4zlwj9zsacqgtufx7zvapd3quufqpxk5rsdd4633m4wz2fdjss0gwx" to sign transactions

# Nuklai RPC URL
NUKLAI_RPC="http://api-devnet.nuklaivm-dev.net:9650/ext/bc/zepWp9PbeU9HLHebQ8gXkvxBYH5Bz4v8SoWXE6kyjjwNaMJfC" # Required: Nuklai RPC endpoint
NUKLAI_RPC="https://api-devnet.nuklaivm-dev.net:9650/ext/bc/DPqCib879gKLxtL7Wao6WTh5hNUYFFBZSL9otsLAZ6wKPJuXb" # Required: Nuklai RPC endpoint

# Faucet configuration
AMOUNT=100000000
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func LoadConfigFromEnv() (*Config, error) {
return nil, err
}

startDifficulty, err := strconv.ParseUint(GetEnv("START_DIFFICULTY", "25"), 10, 16)
startDifficulty, err := strconv.ParseUint(GetEnv("START_DIFFICULTY", "1"), 10, 16)
if err != nil {
return nil, err
}
Expand Down
64 changes: 53 additions & 11 deletions manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/ava-labs/avalanchego/utils/timer"
"github.com/ava-labs/hypersdk/chain"
"github.com/ava-labs/hypersdk/codec"
"github.com/ava-labs/hypersdk/pubsub"
"github.com/ava-labs/hypersdk/rpc"
"github.com/ava-labs/hypersdk/utils"
fconfig "github.com/nuklai/nuklai-faucet/config"
Expand All @@ -35,6 +36,7 @@ type Manager struct {
config *fconfig.Config

cli *rpc.JSONRPCClient
scli *rpc.WebSocketClient
ncli *nrpc.JSONRPCClient

factory *auth.ED25519Factory
Expand All @@ -59,14 +61,24 @@ func New(logger logging.Logger, config *fconfig.Config, db *sql.DB) (*Manager, e
return nil, err
}

scli, err := rpc.NewWebSocketClient(
config.NuklaiRPC,
rpc.DefaultHandshakeTimeout,
pubsub.MaxPendingMessages,
pubsub.MaxReadMessageSize,
)
if err != nil {
return nil, err
}

ncli := nrpc.NewJSONRPCClient(config.NuklaiRPC, networkID, chainID)

dbInstance, err := database.NewDB(db)
if err != nil {
cancel()
return nil, err
}
m := &Manager{log: logger, config: config, cli: cli, ncli: ncli, factory: auth.NewED25519Factory(config.PrivateKey()), cancelFunc: cancel, db: dbInstance}
m := &Manager{log: logger, config: config, cli: cli, scli: scli, ncli: ncli, factory: auth.NewED25519Factory(config.PrivateKey()), cancelFunc: cancel, db: dbInstance}
m.lastRotation = time.Now().Unix()
m.difficulty = m.config.StartDifficulty
m.solutions = set.NewSet[ids.ID](m.config.SolutionsPerSalt)
Expand Down Expand Up @@ -138,7 +150,7 @@ func (m *Manager) sendFunds(ctx context.Context, destination codec.Address, amou
m.log.Error("Failed to create parser", zap.Error(err))
return ids.Empty, 0, err
}
submit, tx, maxFee, err := m.cli.GenerateTransaction(ctx, parser, []chain.Action{&actions.Transfer{
_, tx, maxFee, err := m.cli.GenerateTransaction(ctx, parser, []chain.Action{&actions.Transfer{
To: destination,
Asset: ids.Empty,
Value: amount,
Expand All @@ -161,16 +173,33 @@ func (m *Manager) sendFunds(ctx context.Context, destination codec.Address, amou
return ids.Empty, 0, errors.New("insufficient balance")
}

err = submit(ctx)
if err == nil {
destinationAddr, err := codec.AddressBech32(nconsts.HRP, destination)
if err = m.scli.RegisterTx(tx); err != nil {
m.log.Error("Failed to register transaction", zap.Error(err))
return ids.Empty, 0, err
}
for {
txID, dErr, _, err := m.scli.ListenTx(ctx)
if dErr != nil {
return ids.Empty, 0, dErr
}
if err != nil {
m.log.Error("Failed to convert address to bech32", zap.Error(err))
return ids.Empty, 0, err
}
_ = m.db.SaveTransaction(tx.ID().String(), destinationAddr, amount)
m.log.Info("Transaction saved", zap.String("txID", tx.ID().String()), zap.String("destination", destinationAddr), zap.Uint64("amount", amount))
if txID == tx.ID() {
break
}
// TODO: don't drop these results (may be needed by a different connection)
m.log.Warn("skipping unexpected transaction", zap.String("txID", tx.ID().String()))
}

destinationAddr, err := codec.AddressBech32(nconsts.HRP, destination)
if err != nil {
m.log.Error("Failed to convert address to bech32", zap.Error(err))
return ids.Empty, 0, err
}
_ = m.db.SaveTransaction(tx.ID().String(), destinationAddr, amount)
m.log.Info("Transaction saved", zap.String("txID", tx.ID().String()), zap.String("destination", destinationAddr), zap.Uint64("amount", amount))

return tx.ID(), maxFee, err
}

Expand Down Expand Up @@ -206,8 +235,8 @@ func (m *Manager) SolveChallenge(ctx context.Context, solver codec.Address, salt
m.solutions.Add(solutionID)

if m.solutions.Len() >= m.config.SolutionsPerSalt {
m.difficulty++
m.log.Info("Increasing faucet difficulty", zap.Uint16("new difficulty", m.difficulty))
// m.difficulty++
// m.log.Info("Increasing faucet difficulty", zap.Uint16("new difficulty", m.difficulty))
m.lastRotation = time.Now().Unix()
m.salt, err = challenge.New()
if err != nil {
Expand All @@ -217,7 +246,8 @@ func (m *Manager) SolveChallenge(ctx context.Context, solver codec.Address, salt
m.solutions.Clear()
m.t.Cancel()
m.t.SetTimeoutIn(time.Duration(m.config.TargetDurationPerSalt) * time.Second)
m.log.Info("Salt and difficulty updated due to hitting expected solutions", zap.Uint16("new difficulty", m.difficulty))
m.log.Info("Salt updated", zap.Uint16("new difficulty", m.difficulty))
// m.log.Info("Salt and difficulty updated due to hitting expected solutions", zap.Uint16("new difficulty", m.difficulty))
}
return txID, m.config.Amount, nil
}
Expand All @@ -238,6 +268,18 @@ func (m *Manager) UpdateNuklaiRPC(ctx context.Context, newNuklaiRPCUrl string) e
}
m.log.Info("Fetched network details", zap.Uint32("network ID", networkID), zap.String("chain ID", chainID.String()))

scli, err := rpc.NewWebSocketClient(
newNuklaiRPCUrl,
rpc.DefaultHandshakeTimeout,
pubsub.MaxPendingMessages,
pubsub.MaxReadMessageSize,
)
if err != nil {
m.log.Error("Failed to create WebSocket client", zap.Error(err))
return fmt.Errorf("failed to create WebSocket client: %w", err)
}
m.scli = scli

m.cli = cli
m.ncli = nrpc.NewJSONRPCClient(newNuklaiRPCUrl, networkID, chainID)

Expand Down

0 comments on commit c83ccf2

Please sign in to comment.