Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
feat(BUX-120): initializes repository with architecure (#1)
Browse files Browse the repository at this point in the history
* feat: initializes repository with architecure

* Update .gitignore

Co-authored-by: Damian Orzepowski <damian.orzepowski@softwarelab.com.pl>

* fix: removes composite broadcaster wrapper for a single client

---------

Co-authored-by: Damian Orzepowski <damian.orzepowski@softwarelab.com.pl>
  • Loading branch information
wregulski and dorzepowski authored Jul 20, 2023
1 parent 85ee7a3 commit 8f2ad7b
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work

## Jetbrains
.idea/
*.iml

#asdf
.tool-versions
22 changes: 22 additions & 0 deletions broadcast/arc/arc_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package arc

import (
"context"

"github.com/bitcoin-sv/go-broadcast-client/broadcast"
"github.com/bitcoin-sv/go-broadcast-client/config"
)

type ArcClient struct {
apiURL string
token string
}

func NewArcClient(config config.ArcClientConfig) broadcast.Broadcaster {
return &ArcClient{apiURL: config.APIUrl, token: config.Token}
}

func (a *ArcClient) BestQuote(ctx context.Context, feeCategory, feeType string) error {
// implementacja
return nil
}
43 changes: 43 additions & 0 deletions broadcast/broadcast.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package broadcast

import (
"context"
)

type BroadcastFactory func() Broadcaster

type Broadcaster interface {
BestQuoter
// FastestQuoter
// FeeQuoter
// PolicyQuoter
// TransactionQuerier
// TransactionSubmitter
// TransactionsSubmitter
}

type compositeBroadcaster struct {
broadcasters []Broadcaster
strategy Strategy
}

func NewCompositeBroadcaster(strategy Strategy, factories ...BroadcastFactory) Broadcaster {
var broadcasters []Broadcaster
for _, factory := range factories {
broadcasters = append(broadcasters, factory())
}
return &compositeBroadcaster{
broadcasters: broadcasters,
strategy: strategy,
}
}

func (c *compositeBroadcaster) BestQuote(ctx context.Context, feeCategory, feeType string) error {
executionFuncs := make([]func(context.Context) error, len(c.broadcasters))
for i, broadcaster := range c.broadcasters {
executionFuncs[i] = func(ctx context.Context) error {
return broadcaster.BestQuote(ctx, feeCategory, feeType)
}
}
return c.strategy.Execute(ctx, executionFuncs)
}
40 changes: 40 additions & 0 deletions broadcast/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package broadcast

import (
"context"
)

type BestQuoter interface {
// BestQuote(ctx context.Context, feeCategory, feeType string) (*FeeQuoteResponse, error)
BestQuote(ctx context.Context, feeCategory, feeType string) error
}

// type FastestQuoter interface {
// // FastestQuote(ctx context.Context, timeout time.Duration) (*FeeQuoteResponse, error)
// FastestQuote(ctx context.Context, timeout time.Duration) error
// }

// type FeeQuoter interface {
// // FeeQuote(ctx context.Context, miner *Miner) (*FeeQuoteResponse, error)
// FeeQuote(ctx context.Context) error
// }

// type PolicyQuoter interface {
// // PolicyQuote(ctx context.Context, miner *Miner) (*PolicyQuoteResponse, error)
// PolicyQuote(ctx context.Context) error
// }

// type TransactionQuerier interface {
// // // QueryTransaction(ctx context.Context, miner *Miner, txID string, opts ...QueryTransactionOptFunc) (*QueryTransactionResponse, error)
// QueryTransaction(ctx context.Context, txID string) error
// }

// type TransactionSubmitter interface {
// // SubmitTransaction(ctx context.Context, miner *Miner, tx *Transaction) (*SubmitTransactionResponse, error)
// SubmitTransaction(ctx context.Context) error
// }

// type TransactionsSubmitter interface {
// // SubmitTransactions(ctx context.Context, miner *Miner, txs []Transaction) (*SubmitTransactionsResponse, error)
// SubmitTransactions(ctx context.Context) error
// }
41 changes: 41 additions & 0 deletions broadcast/strategy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package broadcast

import (
"context"
"fmt"
)

type StrategyName string

const (
OneByOneStrategy StrategyName = "OneByOneStrategy"
)

type ExecutionFunc func(context.Context, []func(context.Context) error) error

type Strategy struct {
name StrategyName
executionFunc ExecutionFunc
}

func New(name StrategyName, executionFunc ExecutionFunc) *Strategy {
return &Strategy{name: name, executionFunc: executionFunc}
}

func (s *Strategy) Execute(ctx context.Context, executionFuncs []func(context.Context) error) error {
return s.executionFunc(ctx, executionFuncs)
}

var (
OneByOne = New(OneByOneStrategy, func(ctx context.Context, executionFuncs []func(context.Context) error) error {
for _, executionFunc := range executionFuncs {
err := executionFunc(ctx)
if err != nil {
continue
}
return nil
}
// return factory.ErrAllBroadcastersFailed
return fmt.Errorf("all broadcasters failed")
})
)
6 changes: 6 additions & 0 deletions config/arc_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package config

type ArcClientConfig struct {
APIUrl string
Token string
}
7 changes: 7 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package config

import "github.com/bitcoin-sv/go-broadcast-client/broadcast"

type Config interface{}

var DefaultStrategy broadcast.Strategy = *broadcast.OneByOne
21 changes: 21 additions & 0 deletions factory/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package factory

import (
"github.com/bitcoin-sv/go-broadcast-client/broadcast"
"github.com/bitcoin-sv/go-broadcast-client/broadcast/arc"
"github.com/bitcoin-sv/go-broadcast-client/config"
)

func NewBroadcastClient(factories ...broadcast.BroadcastFactory) broadcast.Broadcaster {
if len(factories) == 1 {
return factories[0]()
}

return broadcast.NewCompositeBroadcaster(config.DefaultStrategy, factories...)
}

func WithArc(config config.ArcClientConfig) broadcast.BroadcastFactory {
return func() broadcast.Broadcaster {
return arc.NewArcClient(config)
}
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/bitcoin-sv/go-broadcast-client

go 1.20

0 comments on commit 8f2ad7b

Please sign in to comment.